Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -82,21 +82,14 @@ def research_node(state):
|
|
82 |
last_message = result["messages"][-1]
|
83 |
content = last_message.content if hasattr(last_message, "content") else last_message
|
84 |
|
85 |
-
# Determine next step based on content
|
86 |
-
if "FINAL ANSWER" in content:
|
87 |
-
# If there's a final answer, we can end the workflow
|
88 |
-
goto = "end"
|
89 |
-
else:
|
90 |
-
# Otherwise, continue to the chart generator
|
91 |
-
goto = "chart_generator"
|
92 |
-
|
93 |
# Create an AIMessage with the researcher name
|
94 |
if not isinstance(last_message, dict):
|
95 |
result["messages"][-1] = AIMessage(content=content, name="researcher")
|
96 |
else:
|
97 |
result["messages"][-1]["name"] = "researcher"
|
98 |
|
99 |
-
|
|
|
100 |
|
101 |
# Chart generation phase
|
102 |
def chart_node(state):
|
@@ -120,22 +113,25 @@ def chart_node(state):
|
|
120 |
else:
|
121 |
result["messages"][-1]["name"] = "chart_generator"
|
122 |
|
123 |
-
#
|
124 |
-
return
|
|
|
|
|
|
|
|
|
|
|
125 |
|
126 |
-
#
|
127 |
def end_node(state):
|
128 |
return state
|
129 |
|
130 |
-
# Build
|
131 |
-
workflow = StateGraph(
|
132 |
workflow.add_node("researcher", research_node)
|
133 |
workflow.add_node("chart_generator", chart_node)
|
134 |
workflow.add_node("end", end_node)
|
135 |
workflow.set_entry_point("researcher")
|
136 |
-
|
137 |
-
workflow.add_edge("researcher", "chart_generator")
|
138 |
-
workflow.add_edge("researcher", "end")
|
139 |
workflow.add_edge("chart_generator", "end")
|
140 |
graph = workflow.compile()
|
141 |
|
@@ -145,12 +141,18 @@ def run_langgraph(user_input):
|
|
145 |
# Create a human message
|
146 |
human_message = HumanMessage(content=user_input)
|
147 |
|
148 |
-
#
|
149 |
-
events = graph.stream({"messages": [human_message]})
|
150 |
outputs = list(events)
|
151 |
|
152 |
# Get the final message
|
153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
final_content = final_message.content if hasattr(final_message, "content") else final_message
|
155 |
|
156 |
if isinstance(final_content, str) and "FINAL ANSWER" in final_content:
|
|
|
82 |
last_message = result["messages"][-1]
|
83 |
content = last_message.content if hasattr(last_message, "content") else last_message
|
84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
# Create an AIMessage with the researcher name
|
86 |
if not isinstance(last_message, dict):
|
87 |
result["messages"][-1] = AIMessage(content=content, name="researcher")
|
88 |
else:
|
89 |
result["messages"][-1]["name"] = "researcher"
|
90 |
|
91 |
+
# In langgraph 0.0.41 style, return the updated state
|
92 |
+
return {"messages": result["messages"], "has_final_answer": "FINAL ANSWER" in content}
|
93 |
|
94 |
# Chart generation phase
|
95 |
def chart_node(state):
|
|
|
113 |
else:
|
114 |
result["messages"][-1]["name"] = "chart_generator"
|
115 |
|
116 |
+
# Return the updated state
|
117 |
+
return {"messages": result["messages"], "has_final_answer": True}
|
118 |
+
|
119 |
+
# Define the router function for the graph
|
120 |
+
def router(state):
|
121 |
+
# Check if we have a final answer
|
122 |
+
return "chart_generator" if not state.get("has_final_answer", False) else "end"
|
123 |
|
124 |
+
# Simplified end node that doesn't modify the state
|
125 |
def end_node(state):
|
126 |
return state
|
127 |
|
128 |
+
# Build a proper LangGraph for v0.0.41
|
129 |
+
workflow = StateGraph({"messages": [], "has_final_answer": False})
|
130 |
workflow.add_node("researcher", research_node)
|
131 |
workflow.add_node("chart_generator", chart_node)
|
132 |
workflow.add_node("end", end_node)
|
133 |
workflow.set_entry_point("researcher")
|
134 |
+
workflow.add_conditional_edges("researcher", router)
|
|
|
|
|
135 |
workflow.add_edge("chart_generator", "end")
|
136 |
graph = workflow.compile()
|
137 |
|
|
|
141 |
# Create a human message
|
142 |
human_message = HumanMessage(content=user_input)
|
143 |
|
144 |
+
# Run the workflow with proper initialization
|
145 |
+
events = graph.stream({"messages": [human_message], "has_final_answer": False})
|
146 |
outputs = list(events)
|
147 |
|
148 |
# Get the final message
|
149 |
+
final_state = outputs[-1]
|
150 |
+
final_messages = final_state.get("messages", [])
|
151 |
+
|
152 |
+
if not final_messages:
|
153 |
+
return "Error: No messages in the final state.", None
|
154 |
+
|
155 |
+
final_message = final_messages[-1]
|
156 |
final_content = final_message.content if hasattr(final_message, "content") else final_message
|
157 |
|
158 |
if isinstance(final_content, str) and "FINAL ANSWER" in final_content:
|