Spaces:
Sleeping
Sleeping
Commit ·
ee70c71
1
Parent(s): 4ea88b5
add tavily search
Browse files
agent.py
CHANGED
|
@@ -31,11 +31,16 @@ import pytesseract
|
|
| 31 |
import fitz # PyMuPDF
|
| 32 |
from dotenv import load_dotenv
|
| 33 |
from contextlib import redirect_stdout
|
|
|
|
|
|
|
| 34 |
|
| 35 |
# Load environment variables from .env file
|
| 36 |
# in HF Spaces, the .env file is saved in Variables and secrets in settings
|
| 37 |
load_dotenv()
|
| 38 |
|
|
|
|
|
|
|
|
|
|
| 39 |
# === System Prompt ===
|
| 40 |
SYSTEM_PROMPT = """
|
| 41 |
You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template:
|
|
@@ -102,6 +107,21 @@ def wikipedia_search(query: str) -> str:
|
|
| 102 |
return WikipediaAPIWrapper().run(query)
|
| 103 |
except Exception as e:
|
| 104 |
return f"Error searching Wikipedia: {e}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
@tool
|
| 107 |
def image_recognition(image_path: str) -> str:
|
|
@@ -253,12 +273,13 @@ def download_file(url_or_path: str, save_dir: str = "./downloads") -> str:
|
|
| 253 |
except Exception as e:
|
| 254 |
return f"Error downloading/copying file: {e}"
|
| 255 |
|
|
|
|
| 256 |
# Update tools list
|
| 257 |
tools: List[StructuredTool] = [
|
| 258 |
calculate, web_search, wikipedia_search, image_recognition,
|
| 259 |
read_pdf, read_csv, read_spreadsheet, transcribe_audio,
|
| 260 |
youtube_transcript_tool, youtube_transcript_api, read_jsonl,
|
| 261 |
-
python_interpreter, download_file # Add
|
| 262 |
]
|
| 263 |
|
| 264 |
class AgentState(TypedDict):
|
|
@@ -462,3 +483,4 @@ class MyAgent:
|
|
| 462 |
|
| 463 |
|
| 464 |
|
|
|
|
|
|
| 31 |
import fitz # PyMuPDF
|
| 32 |
from dotenv import load_dotenv
|
| 33 |
from contextlib import redirect_stdout
|
| 34 |
+
from langchain_community.tools import TavilySearchResults
|
| 35 |
+
from tavily import TavilyClient
|
| 36 |
|
| 37 |
# Load environment variables from .env file
|
| 38 |
# in HF Spaces, the .env file is saved in Variables and secrets in settings
|
| 39 |
load_dotenv()
|
| 40 |
|
| 41 |
+
# Initialize Tavily client (after loading environment variables)
|
| 42 |
+
tavily_client = TavilyClient(api_key=os.environ.get("TAVILY_API_KEY"))
|
| 43 |
+
|
| 44 |
# === System Prompt ===
|
| 45 |
SYSTEM_PROMPT = """
|
| 46 |
You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template:
|
|
|
|
| 107 |
return WikipediaAPIWrapper().run(query)
|
| 108 |
except Exception as e:
|
| 109 |
return f"Error searching Wikipedia: {e}"
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
@tool
|
| 113 |
+
def tavily_search(query: str) -> str:
|
| 114 |
+
"""Search the web using Tavily for comprehensive results."""
|
| 115 |
+
try:
|
| 116 |
+
results = tavily_client.search(query)
|
| 117 |
+
# You can format the results as needed; here we just return the summary or first result
|
| 118 |
+
if isinstance(results, dict) and "results" in results:
|
| 119 |
+
return results["results"][0].get("content", "No content found.")
|
| 120 |
+
elif isinstance(results, list) and results:
|
| 121 |
+
return results[0].get("content", "No content found.")
|
| 122 |
+
return str(results)
|
| 123 |
+
except Exception as e:
|
| 124 |
+
return f"Error performing Tavily search: {e}"
|
| 125 |
|
| 126 |
@tool
|
| 127 |
def image_recognition(image_path: str) -> str:
|
|
|
|
| 273 |
except Exception as e:
|
| 274 |
return f"Error downloading/copying file: {e}"
|
| 275 |
|
| 276 |
+
|
| 277 |
# Update tools list
|
| 278 |
tools: List[StructuredTool] = [
|
| 279 |
calculate, web_search, wikipedia_search, image_recognition,
|
| 280 |
read_pdf, read_csv, read_spreadsheet, transcribe_audio,
|
| 281 |
youtube_transcript_tool, youtube_transcript_api, read_jsonl,
|
| 282 |
+
python_interpreter, download_file, tavily_search # Add tavily_search here
|
| 283 |
]
|
| 284 |
|
| 285 |
class AgentState(TypedDict):
|
|
|
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
+
|