Update app.py
Browse files
app.py
CHANGED
@@ -49,8 +49,28 @@ def create_agent(llm: ChatOpenAI, tools: list, system_prompt: str):
|
|
49 |
return AgentExecutor(agent=agent, tools=tools)
|
50 |
|
51 |
def agent_node(state, agent, name):
|
|
|
52 |
result = agent.invoke(state)
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
@tool
|
56 |
def RAG(state):
|
|
|
49 |
return AgentExecutor(agent=agent, tools=tools)
|
50 |
|
51 |
def agent_node(state, agent, name):
|
52 |
+
# Run the agent and get its output
|
53 |
result = agent.invoke(state)
|
54 |
+
output_content = result["output"]
|
55 |
+
|
56 |
+
# Check if the output contains Python code that generates a graph
|
57 |
+
if "matplotlib" in output_content or "plt." in output_content:
|
58 |
+
exec_locals = {}
|
59 |
+
try:
|
60 |
+
exec(output_content, {}, exec_locals) # Safely execute the code
|
61 |
+
fig = plt.gcf() # Get the current matplotlib figure
|
62 |
+
|
63 |
+
# Save the figure to a buffer
|
64 |
+
buf = io.BytesIO()
|
65 |
+
fig.savefig(buf, format="png")
|
66 |
+
buf.seek(0)
|
67 |
+
|
68 |
+
# Add image to session state for display
|
69 |
+
st.session_state.graph_image = buf
|
70 |
+
except Exception as e:
|
71 |
+
output_content += f"\nError: {str(e)}"
|
72 |
+
|
73 |
+
return {"messages": [HumanMessage(content=output_content, name=name)]}
|
74 |
|
75 |
@tool
|
76 |
def RAG(state):
|