Back to Llama Index

Setup OpenAI Agent

llama-index-integrations/tools/llama-index-tools-wikipedia/examples/wikipedia.ipynb

0.14.211.0 KB
Original Source
python
# Setup OpenAI Agent
import os

os.environ["OPENAI_API_KEY"] = "sk-your-key"
python
# Import and initialize our tool spec
from llama_index.tools.wikipedia.base import WikipediaToolSpec
from llama_index.tools.tool_spec.load_and_search.base import LoadAndSearchToolSpec

wiki_spec = WikipediaToolSpec()
# Get the search wikipedia tool
tool = wiki_spec.to_tool_list()[1]
python
# Create the Agent with our tools
from llama_index.core.agent.workflow import FunctionAgent
from llama_index.llms.openai import OpenAI

agent = FunctionAgent(
    tools=LoadAndSearchToolSpec.from_defaults(tool).to_tool_list(),
    llm=OpenAI(model="gpt-4.1"),
)

# Create a context for the agent
from llama_index.core.workflow import Context

ctx = Context(agent)
python
print(await agent.run("what is the capital of poland", ctx=ctx))
python
print(await agent.run("how long has poland existed", ctx=ctx))
python
print(await agent.run("using information already loaded, how big is poland?", ctx=ctx))