File size: 1,116 Bytes
e4c7240
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import wikipediaapi
from smolagents import tool


@tool
def wiki(query: str) -> str:
    """
    Search and retrieve information from Wikipedia using the Wikipedia-API library.

    Args:
        query: The search query to look up on Wikipedia

    Returns:
        A string containing the Wikipedia page summary and relevant sections
    """
    # Initialize Wikipedia API with a user agent
    wiki_wiki = wikipediaapi.Wikipedia(
        user_agent="HF-Agents-Course (https://huggingface.co/courses/agents-course)",
        language="en",
    )

    # Search for the page
    page = wiki_wiki.page(query)

    if not page.exists():
        return f"No Wikipedia page found for query: {query}"

    # Get the page summary
    result = f"Title: {page.title}\n\n"
    result += f"Summary: {page.summary}\n\n"

    # Add the first few sections if they exist
    if page.sections:
        result += "Sections:\n"
        for section in page.sections[
            :3
        ]:  # Limit to first 3 sections to avoid too much text
            result += f"\n{section.title}:\n{section.text[:500]}...\n"

    return result