Open In Colab Open on GitHub In the Wikipedia Search Tools notebook, we demonstrated how to create Agents with basic Wikipedia search capabilities. Now, we’re taking it a step further with WikipediaAgent—a powerful agent equipped with built-in wiki search tools right out of the box!

Package Installation

To get started with the Wikipedia search integration in AG2, follow these steps: Install AG2 with "wikipedia and openai since we use OpenAI’s LLMs in our example:
pip install -U "ag2[wikipedia, openai]"
Note: If you have been using autogen or ag2, all you need to do is upgrade it using:
pip install -U "autogen[wikipedia, openai]"
or
pip install -U "ag2[wikipedia, openai]"
as autogen, and ag2 are aliases for the same PyPI package.
You’re all set! Now you can start using Wikipedia Search with AG2.

Demonstration

The WikipediaAgent comes packaged with two Wikipedia tools, WikipediaQueryRunTool for running title/keyword searches and WikipediaPageLoadTool for fetching full page content. Together with the fact that it has a pre-configured system message (which you can override), you only need to create the agent and add it to a chat to get going.

Imports

from autogen import LLMConfig
from autogen.agents.experimental import WikipediaAgent

Agent Configuration

Configure WikipediaAgent and run it. Using the agent’s run command will automatically
# LLM configuration
config_list = LLMConfig(api_type="openai", model="gpt-4o-mini")

# Create the agent
wiki_agent = WikipediaAgent(name="wiki-agent", llm_config=config_list)

# If you are adding this agent into a chat that requires separate tool execution, register the tools with the executing agent:
# for tool in wiki_agent.tools:
# tool.register_for_execution(the_executing_agent)

Start the Conversation

With the setup complete, you can now use the conversation.
response = wiki_agent.run(
    message="What's the population of Australia?",
    max_turns=2,
    # Instruct the chat to associate the WikipediaAgent's tools with the internal tool executor
    tools=wiki_agent.tools,
)

# Run the chat
response.process()