wikipedia.py

Overview

The wikipedia.py file provides an integration component for querying and retrieving information from the Wikipedia online encyclopedia. It is designed as a tool within the InfiniFlow system, enabling users or agents to search Wikipedia by specific keywords and obtain summarized content from the most relevant articles.

The component handles search queries, fetches multiple matching Wikipedia pages, extracts their titles, URLs, and summaries, and formats the results for downstream consumption. It also supports configuration parameters such as the number of results to retrieve (top_n) and language selection.

This module leverages the third-party wikipedia Python library for interacting with the Wikipedia API and includes robust error handling and retry mechanisms to ensure reliability within the larger system.


Classes and Methods

WikipediaParam

class WikipediaParam(ToolParamBase):

This class defines the configuration parameters for the Wikipedia tool. It extends the base class ToolParamBase and encapsulates metadata and validation logic.

Attributes

Methods


Wikipedia

class Wikipedia(ToolBase, ABC):

The main tool class that implements Wikipedia search and content retrieval functionality. It inherits from ToolBase and ABC (Abstract Base Class).

Class Attributes

Methods


Important Implementation Details


Interaction with Other System Components


Usage Example

from wikipedia import Wikipedia, WikipediaParam

# Instantiate parameters and set custom values
params = WikipediaParam()
params.top_n = 5
params.language = 'en'

# Instantiate the Wikipedia tool with parameters
wiki_tool = Wikipedia()
wiki_tool._param = params

# Invoke the search with a query
result = wiki_tool._invoke(query="Natural Language Processing")

print(result)  # Outputs concatenated summaries of top 5 Wikipedia articles on the topic

Mermaid Class Diagram

classDiagram
    class WikipediaParam {
        +meta: ToolMeta
        +top_n: int
        +language: str
        +__init__()
        +check()
        +get_input_form() dict
    }

    class Wikipedia {
        +component_name: str
        +_invoke(**kwargs) str
        +thoughts() str
    }

    WikipediaParam <|-- Wikipedia
    Wikipedia ..|> ToolBase
    WikipediaParam ..|> ToolParamBase
    Wikipedia ..|> ABC

Summary

The wikipedia.py file defines a Wikipedia search tool component for the InfiniFlow system. It supports configurable parameters, robust error handling, and language selection for querying Wikipedia articles by keyword. The tool fetches multiple matching pages, extracts key information, and returns summarized content suitable for use by agents or downstream applications. It integrates tightly with the InfiniFlow agent framework and external Wikipedia API via the wikipedia Python package.