Download src/tools/websearch.py from bpratik/Chatbot: direct link, hf CLI and curl.
- Browser
- Download file 1.5 kB
-
https://huggingface.co/spaces/bpratik/Chatbot/resolve/main/src/tools/websearch.py
- Command line
-
hf download hf://spaces/bpratik/Chatbot/src/tools/websearch.py
-
curl -L -o websearch.py https://huggingface.co/spaces/bpratik/Chatbot/resolve/main/src/tools/websearch.py
1.5 kB
| from langchain_community.tools.tavily_search import TavilySearchResults | |
| from langchain_core.tools import tool | |
| import os | |
| class WebSearchTool: | |
| def __init__(self, api_key: str): | |
| self.api_key = api_key | |
| if not api_key: | |
| raise ValueError("Tavily API key is required for web search functionality") | |
| # Set the API key as environment variable for TavilySearchResults | |
| os.environ["TAVILY_API_KEY"] = api_key | |
| self.search_tool = TavilySearchResults( | |
| max_results=5 | |
| ) | |
| def search_web(self, query: str) -> str: | |
| """Search the web for current information about the given query.""" | |
| try: | |
| results = self.search_tool.invoke(query) | |
| if not results: | |
| return "No search results found." | |
| formatted_results = [] | |
| for result in results: | |
| if isinstance(result, dict): | |
| title = result.get('title', 'N/A') | |
| content = result.get('content', 'N/A') | |
| url = result.get('url', 'N/A') | |
| formatted_results.append(f"Title: {title}\nContent: {content}\nURL: {url}\n") | |
| else: | |
| formatted_results.append(str(result)) | |
| return "\n".join(formatted_results) | |
| except Exception as e: | |
| return f"Error searching the web: {str(e)}" | |
| def get_tool(self): | |
| return self.search_tool | |