Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,90 +1,89 @@
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
import matplotlib.pyplot as plt
|
4 |
-
|
5 |
-
from langgraph.
|
6 |
-
from langgraph.
|
|
|
|
|
|
|
|
|
7 |
from langchain_core.messages import HumanMessage
|
8 |
from langchain_anthropic import ChatAnthropic
|
9 |
|
10 |
-
#
|
11 |
os.environ["ANTHROPIC_API_KEY"] = os.getenv("ANTHROPIC_API_KEY")
|
12 |
|
13 |
-
#
|
14 |
llm = ChatAnthropic(model="claude-3-5-sonnet-latest")
|
15 |
|
16 |
-
# System prompt
|
17 |
def make_system_prompt(suffix: str) -> str:
|
18 |
return (
|
19 |
-
"You are a helpful AI assistant, collaborating with other assistants.
|
20 |
-
"Use the provided tools to progress
|
21 |
-
"If you
|
22 |
-
"
|
23 |
-
"If you or any of the other assistants have the final answer or deliverable, "
|
24 |
-
"prefix your response with FINAL ANSWER so the team knows to stop.\n"
|
25 |
f"{suffix}"
|
26 |
)
|
27 |
|
28 |
-
# Research
|
29 |
-
def research_node(state:
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
)
|
35 |
-
|
36 |
-
|
37 |
-
result["messages"][-1].name = "researcher"
|
38 |
-
return Command(update={"messages": result["messages"]}, goto=goto)
|
39 |
|
40 |
-
# Chart
|
41 |
-
def chart_node(state:
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
)
|
47 |
-
|
48 |
-
result["messages"][-1].name = "chart_generator"
|
49 |
-
return Command(update={"messages": result["messages"]}, goto="__end__")
|
50 |
|
51 |
-
#
|
52 |
-
workflow = StateGraph(
|
53 |
workflow.add_node("researcher", research_node)
|
54 |
workflow.add_node("chart_generator", chart_node)
|
55 |
workflow.set_entry_point("researcher")
|
56 |
-
workflow.set_finish_point(
|
57 |
-
workflow.
|
|
|
|
|
58 |
graph = workflow.compile()
|
59 |
|
60 |
-
#
|
61 |
-
def run_langgraph(
|
62 |
try:
|
63 |
-
events = graph.stream({"messages": [HumanMessage(content=
|
64 |
-
|
65 |
-
|
66 |
|
67 |
-
if "FINAL ANSWER" in
|
68 |
-
#
|
69 |
years = [2020, 2021, 2022, 2023, 2024]
|
70 |
gdp = [21.4, 22.0, 23.1, 24.8, 26.2]
|
71 |
-
|
72 |
plt.figure()
|
73 |
-
plt.plot(years, gdp, marker=
|
74 |
plt.title("USA GDP Over Last 5 Years")
|
75 |
plt.xlabel("Year")
|
76 |
-
plt.ylabel("GDP in Trillions
|
77 |
plt.grid(True)
|
78 |
plt.tight_layout()
|
79 |
plt.savefig("gdp_chart.png")
|
80 |
-
|
81 |
-
return "Chart generated based on FINAL ANSWER.", "gdp_chart.png"
|
82 |
else:
|
83 |
-
return
|
|
|
84 |
except Exception as e:
|
85 |
return f"Error: {str(e)}", None
|
86 |
|
87 |
-
# Gradio
|
88 |
def process_input(user_input):
|
89 |
return run_langgraph(user_input)
|
90 |
|
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
import matplotlib.pyplot as plt
|
4 |
+
import pandas as pd
|
5 |
+
from langgraph.graph import StateGraph, END
|
6 |
+
from langgraph.prebuilt.tool_executor import ToolExecutor
|
7 |
+
from langgraph.prebuilt.react import create_react_plan_and_execute
|
8 |
+
from langgraph.checkpoint.sqlite import SqliteSaver
|
9 |
+
from langgraph.graph.message import add_messages
|
10 |
+
|
11 |
from langchain_core.messages import HumanMessage
|
12 |
from langchain_anthropic import ChatAnthropic
|
13 |
|
14 |
+
# Load API Key securely
|
15 |
os.environ["ANTHROPIC_API_KEY"] = os.getenv("ANTHROPIC_API_KEY")
|
16 |
|
17 |
+
# Define the LLM (Claude 3.5 Sonnet)
|
18 |
llm = ChatAnthropic(model="claude-3-5-sonnet-latest")
|
19 |
|
20 |
+
# System prompt modifier
|
21 |
def make_system_prompt(suffix: str) -> str:
|
22 |
return (
|
23 |
+
"You are a helpful AI assistant, collaborating with other assistants."
|
24 |
+
" Use the provided tools to progress toward answering the question."
|
25 |
+
" If you cannot fully answer, another assistant will continue where you left off."
|
26 |
+
" If you or the team has a complete answer, prefix it with FINAL ANSWER.\n"
|
|
|
|
|
27 |
f"{suffix}"
|
28 |
)
|
29 |
|
30 |
+
# Node 1: Research assistant logic
|
31 |
+
def research_node(state: dict) -> dict:
|
32 |
+
messages = state.get("messages", [])
|
33 |
+
prompt = make_system_prompt("You can only do research.")
|
34 |
+
executor = create_react_plan_and_execute(llm=llm, tools=[], system_prompt=prompt)
|
35 |
+
response = executor.invoke(messages)
|
36 |
+
messages.append(HumanMessage(content=response.content, name="researcher"))
|
37 |
+
next_node = "chart_generator" if "FINAL ANSWER" not in response.content else END
|
38 |
+
return {"messages": messages, "next": next_node}
|
|
|
|
|
39 |
|
40 |
+
# Node 2: Chart assistant logic
|
41 |
+
def chart_node(state: dict) -> dict:
|
42 |
+
messages = state.get("messages", [])
|
43 |
+
prompt = make_system_prompt("You can only generate charts.")
|
44 |
+
executor = create_react_plan_and_execute(llm=llm, tools=[], system_prompt=prompt)
|
45 |
+
response = executor.invoke(messages)
|
46 |
+
messages.append(HumanMessage(content=response.content, name="chart_generator"))
|
47 |
+
return {"messages": messages, "next": END}
|
|
|
|
|
48 |
|
49 |
+
# Define LangGraph flow
|
50 |
+
workflow = StateGraph(dict)
|
51 |
workflow.add_node("researcher", research_node)
|
52 |
workflow.add_node("chart_generator", chart_node)
|
53 |
workflow.set_entry_point("researcher")
|
54 |
+
workflow.set_finish_point(END)
|
55 |
+
workflow.add_conditional_edges("researcher", lambda x: x["next"])
|
56 |
+
workflow.add_edge("chart_generator", END)
|
57 |
+
|
58 |
graph = workflow.compile()
|
59 |
|
60 |
+
# Function to run the graph and optionally return chart
|
61 |
+
def run_langgraph(input_text):
|
62 |
try:
|
63 |
+
events = graph.stream({"messages": [HumanMessage(content=input_text)]})
|
64 |
+
output = list(events)[-1]
|
65 |
+
final_content = output["messages"][-1].content
|
66 |
|
67 |
+
if "FINAL ANSWER" in final_content:
|
68 |
+
# Example static chart
|
69 |
years = [2020, 2021, 2022, 2023, 2024]
|
70 |
gdp = [21.4, 22.0, 23.1, 24.8, 26.2]
|
|
|
71 |
plt.figure()
|
72 |
+
plt.plot(years, gdp, marker="o")
|
73 |
plt.title("USA GDP Over Last 5 Years")
|
74 |
plt.xlabel("Year")
|
75 |
+
plt.ylabel("GDP in Trillions")
|
76 |
plt.grid(True)
|
77 |
plt.tight_layout()
|
78 |
plt.savefig("gdp_chart.png")
|
79 |
+
return "Chart generated based on FINAL ANSWER", "gdp_chart.png"
|
|
|
80 |
else:
|
81 |
+
return final_content, None
|
82 |
+
|
83 |
except Exception as e:
|
84 |
return f"Error: {str(e)}", None
|
85 |
|
86 |
+
# Gradio Interface
|
87 |
def process_input(user_input):
|
88 |
return run_langgraph(user_input)
|
89 |
|