Spaces:
Sleeping
Sleeping
Daniel Amendoeira
commited on
Update agent.py
Browse files
agent.py
CHANGED
|
@@ -108,14 +108,30 @@ builder.add_edge("environment", "llm_call") # after running the tools go back t
|
|
| 108 |
|
| 109 |
gaia_agent = builder.compile() # converts my builder into a runnable agent by using gaia_agent.invoke()
|
| 110 |
|
| 111 |
-
# Call the Agent
|
| 112 |
-
def agent_run(question: str):
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
gaia_agent = builder.compile() # converts my builder into a runnable agent by using gaia_agent.invoke()
|
| 110 |
|
| 111 |
+
# Call the Agent
|
| 112 |
+
#def agent_run(question: str):
|
| 113 |
+
input_state = {"messages": [HumanMessage(content=question)]} # prepare the initial user message
|
| 114 |
+
result = gaia_agent.invoke(input_state, {"recursion_limit": 30}) # prevents infinite looping when the LLM keeps calling tools over and over
|
| 115 |
+
final_response = result["messages"][-1].content
|
| 116 |
+
|
| 117 |
+
try:
|
| 118 |
+
return final_response.split("FINAL ANSWER:")[-1].strip() # parse out only what's after "FINAL ANSWER:"
|
| 119 |
+
except Exception:
|
| 120 |
+
print("Could not split on 'FINAL ANSWER:'")
|
| 121 |
+
return final_response
|
| 122 |
+
|
| 123 |
+
class LangGraphAgent:
|
| 124 |
+
def __init__(self):
|
| 125 |
+
print("LangGraphAgent initialized.")
|
| 126 |
+
|
| 127 |
+
def __call__(self, question: str) -> str:
|
| 128 |
+
input_state = {"messages": [HumanMessage(content=question)]} # prepare the initial user message
|
| 129 |
+
print(f"Running LangGraphAgent with input: {question[:50]}...")
|
| 130 |
+
result = gaia_agent.invoke(input_state, {"recursion_limit": 30}) # prevents infinite looping when the LLM keeps calling tools over and over
|
| 131 |
+
final_response = result["messages"][-1].content
|
| 132 |
+
|
| 133 |
+
try:
|
| 134 |
+
return final_response.split("FINAL ANSWER:")[-1].strip() # parse out only what's after "FINAL ANSWER:"
|
| 135 |
+
except Exception:
|
| 136 |
+
print("Could not split on 'FINAL ANSWER:'")
|
| 137 |
+
return final_response
|