/ chat_workflow / tools / search.py
search.py
 1  import os
 2  from dotenv import load_dotenv
 3  # from langchain_community.document_loaders import BraveSearchLoader
 4  from typing import List, Callable
 5  from langchain_community.tools.tavily_search import TavilySearchResults
 6  
 7  load_dotenv()
 8  
 9  
10  def is_search_tool_available() -> bool:
11      """
12      Check if the search tool is available.
13      """
14      # Check Tavily API key is set
15      return os.getenv("TAVILY_API_KEY") is not None
16  
17  
18  async def tavily_search(query: str) -> str:
19      """
20      Search the web to fact checking, getting latest or real-time information.
21  
22      Args:
23          query: The query to search for
24      """
25      search_engine = TavilySearchResults(max_results=5)
26      pages = await search_engine.ainvoke(query)
27      result = "\n".join([f" - {page['content']}" for page in pages])
28      return result
29  
30  
31  # A function that return a list of async search functions if the search tool is available
32  def get_search_tools() -> List[Callable]:
33      if is_search_tool_available():
34          return [tavily_search]
35      else:
36          return []