Spaces:
Sleeping
Sleeping
Delete agent.py
Browse files
agent.py
DELETED
|
@@ -1,233 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
from typing import Annotated, List
|
| 3 |
-
from typing_extensions import TypedDict
|
| 4 |
-
|
| 5 |
-
from langchain_core.messages import HumanMessage, SystemMessage
|
| 6 |
-
from langchain_groq import ChatGroq
|
| 7 |
-
from langchain_community.utilities import GoogleSerperAPIWrapper
|
| 8 |
-
|
| 9 |
-
from langgraph.graph import StateGraph, END
|
| 10 |
-
from langgraph.graph.message import add_messages
|
| 11 |
-
|
| 12 |
-
from dotenv import load_dotenv
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
groq_api_key = os.getenv("GROQ_API_KEY")
|
| 16 |
-
|
| 17 |
-
# =========================
|
| 18 |
-
# TOOLS
|
| 19 |
-
# =========================
|
| 20 |
-
|
| 21 |
-
search = GoogleSerperAPIWrapper()
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
def calculator(expr: str):
|
| 25 |
-
try:
|
| 26 |
-
return str(eval(expr))
|
| 27 |
-
except:
|
| 28 |
-
return "0"
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
# =========================
|
| 32 |
-
# MODELS
|
| 33 |
-
# =========================
|
| 34 |
-
|
| 35 |
-
leader_llm = ChatGroq(
|
| 36 |
-
model="llama-3.3-70b-versatile",
|
| 37 |
-
groq_api_key=groq_api_key
|
| 38 |
-
)
|
| 39 |
-
|
| 40 |
-
worker_llm = ChatGroq(
|
| 41 |
-
model="llama-3.3-70b-versatile",
|
| 42 |
-
groq_api_key=groq_api_key
|
| 43 |
-
)
|
| 44 |
-
|
| 45 |
-
# =========================
|
| 46 |
-
# STATE
|
| 47 |
-
# =========================
|
| 48 |
-
|
| 49 |
-
class AgentState(TypedDict):
|
| 50 |
-
messages: Annotated[List, add_messages]
|
| 51 |
-
route: str
|
| 52 |
-
answer: str
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
# =========================
|
| 56 |
-
# LEADER
|
| 57 |
-
# =========================
|
| 58 |
-
|
| 59 |
-
def leader_node(state: AgentState):
|
| 60 |
-
|
| 61 |
-
system = SystemMessage(content="""
|
| 62 |
-
You are a routing agent.
|
| 63 |
-
|
| 64 |
-
Decide which tool to use.
|
| 65 |
-
|
| 66 |
-
math → calculations or numbers
|
| 67 |
-
search → facts, dates, people, places
|
| 68 |
-
llm → reasoning or text tasks
|
| 69 |
-
|
| 70 |
-
Return only:
|
| 71 |
-
|
| 72 |
-
math
|
| 73 |
-
search
|
| 74 |
-
llm
|
| 75 |
-
""")
|
| 76 |
-
|
| 77 |
-
decision = leader_llm.invoke([system] + state["messages"])
|
| 78 |
-
|
| 79 |
-
route = decision.content.lower().strip()
|
| 80 |
-
|
| 81 |
-
if route not in ["math", "search", "llm"]:
|
| 82 |
-
route = "llm"
|
| 83 |
-
|
| 84 |
-
print("ROUTE:", route)
|
| 85 |
-
|
| 86 |
-
return {"route": route}
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
# =========================
|
| 90 |
-
# SEARCH
|
| 91 |
-
# =========================
|
| 92 |
-
|
| 93 |
-
def search_node(state: AgentState):
|
| 94 |
-
|
| 95 |
-
query = state["messages"][-1].content
|
| 96 |
-
|
| 97 |
-
try:
|
| 98 |
-
results = search.results(query, num_results=3)
|
| 99 |
-
|
| 100 |
-
snippets = []
|
| 101 |
-
|
| 102 |
-
if isinstance(results, dict) and "organic" in results:
|
| 103 |
-
for r in results["organic"]:
|
| 104 |
-
if "snippet" in r:
|
| 105 |
-
snippets.append(r["snippet"])
|
| 106 |
-
|
| 107 |
-
if not snippets:
|
| 108 |
-
snippets.append(str(results))
|
| 109 |
-
|
| 110 |
-
context = "\n".join(snippets)
|
| 111 |
-
|
| 112 |
-
except:
|
| 113 |
-
context = search.run(query)
|
| 114 |
-
|
| 115 |
-
response = worker_llm.invoke([
|
| 116 |
-
SystemMessage(content=f"""
|
| 117 |
-
Use the web information below to answer.
|
| 118 |
-
|
| 119 |
-
{context}
|
| 120 |
-
|
| 121 |
-
Give a short answer only.
|
| 122 |
-
"""),
|
| 123 |
-
HumanMessage(content=query)
|
| 124 |
-
])
|
| 125 |
-
|
| 126 |
-
return {"answer": response.content}
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
# =========================
|
| 130 |
-
# MATH
|
| 131 |
-
# =========================
|
| 132 |
-
|
| 133 |
-
def math_node(state: AgentState):
|
| 134 |
-
|
| 135 |
-
query = state["messages"][-1].content
|
| 136 |
-
|
| 137 |
-
expr = worker_llm.invoke([
|
| 138 |
-
SystemMessage(content="""
|
| 139 |
-
Extract only the mathematical expression.
|
| 140 |
-
|
| 141 |
-
Example:
|
| 142 |
-
12*5+7
|
| 143 |
-
"""),
|
| 144 |
-
HumanMessage(content=query)
|
| 145 |
-
])
|
| 146 |
-
|
| 147 |
-
expression = expr.content.strip()
|
| 148 |
-
|
| 149 |
-
result = calculator(expression)
|
| 150 |
-
|
| 151 |
-
return {"answer": result}
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
# =========================
|
| 155 |
-
# LLM
|
| 156 |
-
# =========================
|
| 157 |
-
|
| 158 |
-
def llm_node(state: AgentState):
|
| 159 |
-
|
| 160 |
-
query = state["messages"][-1].content
|
| 161 |
-
|
| 162 |
-
response = worker_llm.invoke([
|
| 163 |
-
SystemMessage(content="""
|
| 164 |
-
Answer the question.
|
| 165 |
-
|
| 166 |
-
Return the shortest possible answer.
|
| 167 |
-
Do not explain.
|
| 168 |
-
"""),
|
| 169 |
-
HumanMessage(content=query)
|
| 170 |
-
])
|
| 171 |
-
|
| 172 |
-
return {"answer": response.content}
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
# =========================
|
| 176 |
-
# FINAL ANSWER CLEANER
|
| 177 |
-
# =========================
|
| 178 |
-
|
| 179 |
-
def final_node(state: AgentState):
|
| 180 |
-
|
| 181 |
-
answer = state["answer"]
|
| 182 |
-
|
| 183 |
-
response = worker_llm.invoke([
|
| 184 |
-
SystemMessage(content="""
|
| 185 |
-
Return ONLY the final answer.
|
| 186 |
-
|
| 187 |
-
Rules:
|
| 188 |
-
- no explanation
|
| 189 |
-
- no punctuation
|
| 190 |
-
- one word or number only
|
| 191 |
-
"""),
|
| 192 |
-
HumanMessage(content=answer)
|
| 193 |
-
])
|
| 194 |
-
|
| 195 |
-
clean = response.content.strip()
|
| 196 |
-
|
| 197 |
-
clean = clean.replace(".", "")
|
| 198 |
-
clean = clean.replace(",", "")
|
| 199 |
-
|
| 200 |
-
return {"answer": clean}
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
# =========================
|
| 204 |
-
# GRAPH
|
| 205 |
-
# =========================
|
| 206 |
-
|
| 207 |
-
builder = StateGraph(AgentState)
|
| 208 |
-
|
| 209 |
-
builder.add_node("leader", leader_node)
|
| 210 |
-
builder.add_node("search", search_node)
|
| 211 |
-
builder.add_node("math", math_node)
|
| 212 |
-
builder.add_node("llm", llm_node)
|
| 213 |
-
builder.add_node("final", final_node)
|
| 214 |
-
|
| 215 |
-
builder.set_entry_point("leader")
|
| 216 |
-
|
| 217 |
-
builder.add_conditional_edges(
|
| 218 |
-
"leader",
|
| 219 |
-
lambda state: state["route"],
|
| 220 |
-
{
|
| 221 |
-
"search": "search",
|
| 222 |
-
"math": "math",
|
| 223 |
-
"llm": "llm"
|
| 224 |
-
}
|
| 225 |
-
)
|
| 226 |
-
|
| 227 |
-
builder.add_edge("search", "final")
|
| 228 |
-
builder.add_edge("math", "final")
|
| 229 |
-
builder.add_edge("llm", "final")
|
| 230 |
-
|
| 231 |
-
builder.add_edge("final", END)
|
| 232 |
-
|
| 233 |
-
graph = builder.compile()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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