File size: 1,974 Bytes
2a6057d
 
 
32aa30b
2a6057d
 
 
 
0538019
2a6057d
0538019
 
2a6057d
0538019
2a6057d
0538019
 
 
 
 
 
 
 
2a6057d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0538019
2a6057d
 
0538019
32aa30b
 
 
 
 
 
 
 
 
 
 
 
 
 
2a6057d
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""This module contains tools for searching external sources."""
from langchain_core.tools import tool
from langchain_community.tools.tavily_search import TavilySearchResults
from langchain_community.document_loaders import WikipediaLoader, ArxivLoader


@tool
def web_search(query: str) -> str:
    """Search Tavily for a query and return maximum 3 results.
    Args:
        query: The search query.
        
    Returns:
        str: The result of the search query
    """
    search_tool = TavilySearchResults(max_results=3)
    search_docs = search_tool.invoke({"query": query})
    # Convert search results to a single string
    results_text = ""
    for doc in search_docs:
        results_text += f"{doc['title']}: {doc['content']}\n"
    return results_text


@tool
def search_wikipedia(query: str) -> str:
    """
    Searches Wikipedia and returns content from the topic.

    Args:
        query (str): The search query/topic to look up on Wikipedia
        
    Returns:
        str: Content from the Wikipedia article for the query
        
    Raises:
        ValueError: If no matching page is found
    """
    try:
        loader = WikipediaLoader(query=query, load_max_docs=1)
        docs = loader.load()
        if not docs:
            raise ValueError(f"No Wikipedia page found for query: {query}")
        return docs[0].page_content
    except Exception as e:
        raise ValueError(f"Error searching Wikipedia: {str(e)}")
    
@tool
def arxiv_search(query: str) -> str:
    """Search Arxiv for a query and return maximum 3 result.
    
    Args:
        query: The search query."""
    search_docs = ArxivLoader(query=query, load_max_docs=3).load()
    formatted_search_docs = "\n\n---\n\n".join(
        [
            f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content[:1000]}\n</Document>'
            for doc in search_docs
        ])
    return {"arvix_results": formatted_search_docs}