Spaces:
Sleeping
Sleeping
Update agent.py
Browse files
agent.py
CHANGED
|
@@ -156,14 +156,11 @@ from langchain_core.tools import tool
|
|
| 156 |
# from langchain_community.chat_models import ChatOpenAI
|
| 157 |
# from langchain.chat_models import ChatOpenAI
|
| 158 |
# from langchain_core.pydantic_v1 import BaseModel
|
| 159 |
-
|
| 160 |
-
from langchain_community.chat_models import ChatOpenAI
|
| 161 |
# Or, if you’ve installed langchain-openai:
|
| 162 |
# from langchain_openai import ChatOpenAI
|
| 163 |
from pydantic import BaseModel
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
from langgraph.graph import StateGraph, START, END
|
| 168 |
from langchain_experimental.utilities import PythonREPL
|
| 169 |
from langchain_core.messages import SystemMessage, HumanMessage
|
|
@@ -176,10 +173,6 @@ GPT_4o_API_KEY = os.environ["GPT_4o_API_KEY"]
|
|
| 176 |
TAVILY_API_KEY = os.environ["TAVILY_API_KEY"]
|
| 177 |
Pixtral12B_API = os.environ["Pixtral12B_API"]
|
| 178 |
|
| 179 |
-
print(8888888888888888888888888)
|
| 180 |
-
print(GPT_4o_API_KEY)
|
| 181 |
-
print(TAVILY_API_KEY)
|
| 182 |
-
print(Pixtral12B_API)
|
| 183 |
|
| 184 |
API_URL = "https://openrouter.ai/api/v1/chat/completions"
|
| 185 |
|
|
@@ -430,30 +423,56 @@ def synthesizer(state: GaiaAgentState) -> dict:
|
|
| 430 |
|
| 431 |
|
| 432 |
# === 4. BUILD & COMPILE GRAPH ===
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 433 |
|
| 434 |
-
builder = StateGraph(GaiaAgentState)
|
| 435 |
-
|
| 436 |
-
builder.add_node("planner", planner_node)
|
| 437 |
-
builder.add_node("execute", tool_executor)
|
| 438 |
-
builder.add_node("synthesize", synthesizer)
|
| 439 |
-
|
| 440 |
-
builder.add_edge(START, "planner")
|
| 441 |
-
builder.add_edge("planner", "execute")
|
| 442 |
-
builder.add_edge("execute", "synthesize")
|
| 443 |
-
builder.add_edge("synthesize", END)
|
| 444 |
|
| 445 |
-
graph = builder.compile()
|
| 446 |
|
| 447 |
-
# === 5. RUN EXAMPLE ===
|
| 448 |
-
# Example dev payload (in practice, fetch from /random-question)
|
| 449 |
|
| 450 |
-
# test
|
| 451 |
-
if __name__ == "__main__":
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
|
|
|
|
| 156 |
# from langchain_community.chat_models import ChatOpenAI
|
| 157 |
# from langchain.chat_models import ChatOpenAI
|
| 158 |
# from langchain_core.pydantic_v1 import BaseModel
|
| 159 |
+
# from langchain_community.chat_models import ChatOpenAI
|
|
|
|
| 160 |
# Or, if you’ve installed langchain-openai:
|
| 161 |
# from langchain_openai import ChatOpenAI
|
| 162 |
from pydantic import BaseModel
|
| 163 |
+
from langchain_openai import ChatOpenAI
|
|
|
|
|
|
|
| 164 |
from langgraph.graph import StateGraph, START, END
|
| 165 |
from langchain_experimental.utilities import PythonREPL
|
| 166 |
from langchain_core.messages import SystemMessage, HumanMessage
|
|
|
|
| 173 |
TAVILY_API_KEY = os.environ["TAVILY_API_KEY"]
|
| 174 |
Pixtral12B_API = os.environ["Pixtral12B_API"]
|
| 175 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
API_URL = "https://openrouter.ai/api/v1/chat/completions"
|
| 178 |
|
|
|
|
| 423 |
|
| 424 |
|
| 425 |
# === 4. BUILD & COMPILE GRAPH ===
|
| 426 |
+
# agent.py
|
| 427 |
+
class EsiAgent:
|
| 428 |
+
def __init__(self):
|
| 429 |
+
# 1. Build & compile your graph
|
| 430 |
+
builder = StateGraph(GaiaAgentState)
|
| 431 |
+
|
| 432 |
+
builder.add_node("planner", planner_node)
|
| 433 |
+
builder.add_node("execute", tool_executor)
|
| 434 |
+
builder.add_node("synthesize", synthesizer)
|
| 435 |
+
|
| 436 |
+
builder.add_edge(START, "planner")
|
| 437 |
+
builder.add_edge("planner", "execute")
|
| 438 |
+
builder.add_edge("execute", "synthesize")
|
| 439 |
+
builder.add_edge("synthesize", END)
|
| 440 |
+
|
| 441 |
+
self.graph = builder.compile()
|
| 442 |
+
|
| 443 |
+
def run(self, question: str) -> str:
|
| 444 |
+
"""
|
| 445 |
+
Runs the agent end‑to‑end on a single question string and returns
|
| 446 |
+
just the answer text (stripped of any prefix).
|
| 447 |
+
"""
|
| 448 |
+
# Wrap in the same state shape your graph expects
|
| 449 |
+
initial_state = {"messages": [HumanMessage(content=question)]}
|
| 450 |
+
# Invoke the graph
|
| 451 |
+
final_state = self.graph.invoke(initial_state)
|
| 452 |
+
# Extract the last message
|
| 453 |
+
msgs = final_state["messages"]
|
| 454 |
+
last = msgs[-1].content
|
| 455 |
+
# If you prefix with "FINAL ANSWER: " remove that
|
| 456 |
+
if last.startswith("FINAL ANSWER:"):
|
| 457 |
+
return last[len("FINAL ANSWER:"):].strip()
|
| 458 |
+
return last.strip()
|
| 459 |
+
|
| 460 |
+
# Optional: allow running agent.py directly for quick tests
|
| 461 |
+
if __name__ == "__main__":
|
| 462 |
+
agent = EsiAgent()
|
| 463 |
+
q = "When was a picture of St. Thomas Aquinas first added to the Wikipedia page on the Principle of double effect?"
|
| 464 |
+
print("→", agent.run(q))
|
| 465 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 466 |
|
|
|
|
| 467 |
|
|
|
|
|
|
|
| 468 |
|
| 469 |
+
# # test
|
| 470 |
+
# if __name__ == "__main__":
|
| 471 |
+
# question = "When was a picture of St. Thomas Aquinas first added to the Wikipedia page on the Principle of double effect?"
|
| 472 |
+
# # Build the graph
|
| 473 |
+
# # Run the graph
|
| 474 |
+
# messages = [HumanMessage(content=question)]
|
| 475 |
+
# messages = graph.invoke({"messages": messages})
|
| 476 |
+
# for m in messages["messages"]:
|
| 477 |
+
# m.pretty_print()
|
| 478 |
|