Skip to content

WikipediaQueryRunTool

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

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

Bases: Tool

Tool for querying Wikipedia and returning summarized page results.

This tool uses the wikipediaapi package to perform searches against a specified language edition of Wikipedia and returns up to top_k page summaries.

Public methods

query_run(query: str) -> list[str] | str

ATTRIBUTE DESCRIPTION
language

Language code for the Wikipedia edition (e.g., 'en', 'es').

TYPE: str

top_k

Max number of page summaries returned (≤ MAX_PAGE_RETRIEVE).

TYPE: int

verbose

If True, enables debug logging to stdout.

TYPE: bool

wiki_cli

Internal client for Wikipedia API calls.

TYPE: WikipediaClient

Initialize the WikipediaQueryRunTool.

PARAMETER DESCRIPTION
language

ISO code of the Wikipedia edition to query.

TYPE: str DEFAULT: 'en'

top_k

Desired number of summaries (capped by MAX_PAGE_RETRIEVE).

TYPE: int DEFAULT: 3

verbose

If True, print debug information during searches.

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 WikipediaQueryRunTool.

    Args:
        language (str): ISO code of the Wikipedia edition to query.
        top_k (int): Desired number of summaries (capped by MAX_PAGE_RETRIEVE).
        verbose (bool): If True, print debug information during searches.
    """
    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="Run a Wikipedia query and return page summaries.",
        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)

Search Wikipedia and return formatted page summaries.

    Truncates `query` to MAX_QUERY_LENGTH before searching.

    Args:
        query (str): Search term(s) to look up in Wikipedia.

    Returns:
        list[str]: Each element is "Page: <title>

Summary: ". str: Error message if no results are found or on exception.

    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]:
    """Search Wikipedia and return formatted page summaries.

    Truncates `query` to MAX_QUERY_LENGTH before searching.

    Args:
        query (str): Search term(s) to look up in Wikipedia.

    Returns:
        list[str]: Each element is "Page: <title>\nSummary: <text>".
        str: Error message if no results are found or on exception.

    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)