Skip to content

WikipediaQueryRunTool

autogen.tools.experimental.wikipedia.wikipedia.WikipediaQueryRunTool #

WikipediaQueryRunTool(language='en', top_k=3, verbose=False)

Bases: Tool

A tool for executing Wikipedia queries and returning summarized page results.

This tool requires the optional wikipediaapi package to be installed. Provides controlled access to Wikipedia content with configurable result limits.

ATTRIBUTE DESCRIPTION
language

Wikipedia language edition to use (e.g., 'en', 'es')

TYPE: str

top_k

Maximum number of page summaries to return (capped at MAX_PAGE_RETRIEVE)

TYPE: int

verbose

Flag to enable debug logging

TYPE: bool

wiki_cli

Internal client for Wikipedia API interactions

TYPE: WikipediaClient

Initialize the Wikipedia query tool.

PARAMETER DESCRIPTION
language

Language code for Wikipedia edition. Defaults to 'en'.

TYPE: str DEFAULT: 'en'

top_k

Maximum number of page summaries to return. Will be capped at MAX_PAGE_RETRIEVE constant. Defaults to 3.

TYPE: int DEFAULT: 3

verbose

Enable operational logging. Defaults to False.

TYPE: bool DEFAULT: False

Source code in autogen/tools/experimental/wikipedia/wikipedia.py
def __init__(self, language: str = "en", top_k: int = 3, verbose: bool = False) -> None:
    """
    Initialize the Wikipedia query tool.

    Args:
        language (str, optional): Language code for Wikipedia edition. Defaults to 'en'.
        top_k (int, optional): Maximum number of page summaries to return. Will be capped
            at MAX_PAGE_RETRIEVE constant. Defaults to 3.
        verbose (bool, optional): Enable operational logging. Defaults to False.
    """
    self.language = language
    self.tool_name = "wikipedia-query-run"
    self.wiki_cli = WikipediaClient(language, self.tool_name)
    self.top_k = min(top_k, MAX_PAGE_RETRIEVE)
    self.verbose = verbose
    super().__init__(
        name=self.tool_name,
        description="Use this tool to run a query in Wikipedia. It returns the summary of the pages found.",
        func_or_tool=self.query_run,
    )

language instance-attribute #

language = language

tool_name instance-attribute #

tool_name = 'wikipedia-query-run'

wiki_cli instance-attribute #

wiki_cli = WikipediaClient(language, tool_name)

top_k instance-attribute #

top_k = min(top_k, MAX_PAGE_RETRIEVE)

verbose instance-attribute #

verbose = verbose

name property #

name

description property #

description

func property #

func

tool_schema property #

tool_schema

Get the schema for the tool.

This is the preferred way of handling function calls with OpeaAI and compatible frameworks.

function_schema property #

function_schema

Get the schema for the function.

This is the old way of handling function calls with OpenAI and compatible frameworks. It is provided for backward compatibility.

realtime_tool_schema property #

realtime_tool_schema

Get the schema for the tool.

This is the preferred way of handling function calls with OpeaAI and compatible frameworks.

query_run #

query_run(query)
    Execute a Wikipedia search and return processed page summaries.

    Args:
        query (str): Search term(s) to look up in Wikipedia. Will be truncated to
            MAX_QUERY_LENGTH characters if too long.

    Returns:
        Union[list[str], str]:
            - List of formatted page summaries ("Page: <title>

Summary: ") if successful - Error message string if no results found or exception occurs

    Note:
        Automatically handles API exceptions and returns error strings for robust operation
Source code in autogen/tools/experimental/wikipedia/wikipedia.py
def query_run(self, query: str) -> Union[list[str], str]:
    """
    Execute a Wikipedia search and return processed page summaries.

    Args:
        query (str): Search term(s) to look up in Wikipedia. Will be truncated to
            MAX_QUERY_LENGTH characters if too long.

    Returns:
        Union[list[str], str]:
            - List of formatted page summaries ("Page: <title>\nSummary: <content>") if successful
            - Error message string if no results found or exception occurs

    Note:
        Automatically handles API exceptions and returns error strings for robust operation
    """
    try:
        if self.verbose:
            print(f"INFO\t [{self.tool_name}] search query='{query[:MAX_QUERY_LENGTH]}' top_k={self.top_k}")
        search_results = self.wiki_cli.search(query[:MAX_QUERY_LENGTH], limit=self.top_k)
        summaries: list[str] = []
        for item in search_results:
            title = item["title"]
            page = self.wiki_cli.get_page(title)
            # Only format the summary if the page exists and has a summary.
            if page is not None and page.summary:
                summary = f"Page: {title}\nSummary: {page.summary}"
                summaries.append(summary)
        if not summaries:
            return "No good Wikipedia Search Result was found"
        return summaries
    except Exception as e:
        return f"wikipedia search failed: {str(e)}"

register_for_llm #

register_for_llm(agent)

Registers the tool for use with a ConversableAgent's language model (LLM).

This method registers the tool so that it can be invoked by the agent during interactions with the language model.

PARAMETER DESCRIPTION
agent

The agent to which the tool will be registered.

TYPE: ConversableAgent

Source code in autogen/tools/tool.py
def register_for_llm(self, agent: "ConversableAgent") -> None:
    """Registers the tool for use with a ConversableAgent's language model (LLM).

    This method registers the tool so that it can be invoked by the agent during
    interactions with the language model.

    Args:
        agent (ConversableAgent): The agent to which the tool will be registered.
    """
    if self._func_schema:
        agent.update_tool_signature(self._func_schema, is_remove=False)
    else:
        agent.register_for_llm()(self)

register_for_execution #

register_for_execution(agent)

Registers the tool for direct execution by a ConversableAgent.

This method registers the tool so that it can be executed by the agent, typically outside of the context of an LLM interaction.

PARAMETER DESCRIPTION
agent

The agent to which the tool will be registered.

TYPE: ConversableAgent

Source code in autogen/tools/tool.py
def register_for_execution(self, agent: "ConversableAgent") -> None:
    """Registers the tool for direct execution by a ConversableAgent.

    This method registers the tool so that it can be executed by the agent,
    typically outside of the context of an LLM interaction.

    Args:
        agent (ConversableAgent): The agent to which the tool will be registered.
    """
    agent.register_for_execution()(self)

register_tool #

register_tool(agent)

Register a tool to be both proposed and executed by an agent.

Equivalent to calling both register_for_llm and register_for_execution with the same agent.

Note: This will not make the agent recommend and execute the call in the one step. If the agent recommends the tool, it will need to be the next agent to speak in order to execute the tool.

PARAMETER DESCRIPTION
agent

The agent to which the tool will be registered.

TYPE: ConversableAgent

Source code in autogen/tools/tool.py
def register_tool(self, agent: "ConversableAgent") -> None:
    """Register a tool to be both proposed and executed by an agent.

    Equivalent to calling both `register_for_llm` and `register_for_execution` with the same agent.

    Note: This will not make the agent recommend and execute the call in the one step. If the agent
    recommends the tool, it will need to be the next agent to speak in order to execute the tool.

    Args:
        agent (ConversableAgent): The agent to which the tool will be registered.
    """
    self.register_for_llm(agent)
    self.register_for_execution(agent)