Spaces:
Sleeping
Sleeping
Create websearch_agent.py
Browse files- websearch_agent.py +58 -0
websearch_agent.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_groq import ChatGroq
|
| 2 |
+
from langchain_tavily import TavilySearch
|
| 3 |
+
from langgraph.checkpoint.memory import InMemorySaver
|
| 4 |
+
from langgraph.prebuilt import create_react_agent
|
| 5 |
+
from loguru import logger
|
| 6 |
+
import os
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
+
load_dotenv()
|
| 9 |
+
|
| 10 |
+
# =======================
|
| 11 |
+
# 1. LLM MODEL (Groq) - Optimized for Low Latency
|
| 12 |
+
# =======================
|
| 13 |
+
model = ChatGroq(
|
| 14 |
+
model="openai/gpt-oss-20b", # Faster than gpt-oss-20b
|
| 15 |
+
max_tokens=256, # Reduced from 512 for faster responses
|
| 16 |
+
api_key=os.getenv("GROQ_API_KEY"),
|
| 17 |
+
temperature=0.7,
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
# =======================
|
| 21 |
+
# 2. TAVILY SEARCH TOOL - Optimized for Speed
|
| 22 |
+
# =======================
|
| 23 |
+
tavily_tool = TavilySearch(
|
| 24 |
+
max_results=2, # Reduced from 5 for faster responses
|
| 25 |
+
topic="general",
|
| 26 |
+
api_key=os.getenv("TAVILY_API_KEY")
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
tools = [tavily_tool] # 🔥 Replace math tools with Tavily
|
| 30 |
+
|
| 31 |
+
# =======================
|
| 32 |
+
# 3. SYSTEM PROMPT - Optimized for Speed
|
| 33 |
+
# =======================
|
| 34 |
+
system_prompt = """
|
| 35 |
+
You are Samantha, a helpful assistant. Use Tavily for factual or current information.
|
| 36 |
+
Keep responses brief and conversational for audio playback.
|
| 37 |
+
"""
|
| 38 |
+
|
| 39 |
+
# =======================
|
| 40 |
+
# 4. MEMORY
|
| 41 |
+
# =======================
|
| 42 |
+
memory = InMemorySaver()
|
| 43 |
+
|
| 44 |
+
# =======================
|
| 45 |
+
# 5. BUILD THE AGENT
|
| 46 |
+
# =======================
|
| 47 |
+
agent = create_react_agent(
|
| 48 |
+
model=model,
|
| 49 |
+
tools=tools,
|
| 50 |
+
prompt=system_prompt,
|
| 51 |
+
checkpointer=memory,
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
agent_config = {
|
| 55 |
+
"configurable": {
|
| 56 |
+
"thread_id": "default_user"
|
| 57 |
+
}
|
| 58 |
+
}
|