akrstova's picture
Add Arxiv search tool
32aa30b
"""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}