Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from langgraph.graph import StateGraph, MessagesState, START, END
|
3 |
+
from langgraph.types import Command
|
4 |
+
from langchain_core.messages import BaseMessage, HumanMessage
|
5 |
+
from langgraph.prebuilt import create_react_agent
|
6 |
+
from langchain_anthropic import ChatAnthropic
|
7 |
+
|
8 |
+
# Define your LangGraph agents and nodes
|
9 |
+
llm = ChatAnthropic(model="claude-3-5-sonnet-latest")
|
10 |
+
|
11 |
+
def make_system_prompt(suffix: str) -> str:
|
12 |
+
return (
|
13 |
+
"You are a helpful AI assistant, collaborating with other assistants."
|
14 |
+
" Use the provided tools to progress towards answering the question."
|
15 |
+
" If you are unable to fully answer, that's OK, another assistant with different tools "
|
16 |
+
" will help where you left off. Execute what you can to make progress."
|
17 |
+
" If you or any of the other assistants have the final answer or deliverable,"
|
18 |
+
" prefix your response with FINAL ANSWER so the team knows to stop."
|
19 |
+
f"\n{suffix}"
|
20 |
+
)
|
21 |
+
|
22 |
+
# Research agent and node
|
23 |
+
def research_node(state: MessagesState) -> Command[str]:
|
24 |
+
agent = create_react_agent(
|
25 |
+
llm,
|
26 |
+
tools=[], # Define your tools if needed
|
27 |
+
state_modifier=make_system_prompt("You can only do research.")
|
28 |
+
)
|
29 |
+
result = agent.invoke(state)
|
30 |
+
goto = END if "FINAL ANSWER" in result["messages"][-1].content else "chart_generator"
|
31 |
+
result["messages"][-1] = HumanMessage(
|
32 |
+
content=result["messages"][-1].content, name="researcher"
|
33 |
+
)
|
34 |
+
return Command(update={"messages": result["messages"]}, goto=goto)
|
35 |
+
|
36 |
+
# Chart generator agent and node
|
37 |
+
def chart_node(state: MessagesState) -> Command[str]:
|
38 |
+
agent = create_react_agent(
|
39 |
+
llm,
|
40 |
+
tools=[], # Define your tools if needed
|
41 |
+
state_modifier=make_system_prompt("You can only generate charts.")
|
42 |
+
)
|
43 |
+
result = agent.invoke(state)
|
44 |
+
goto = END if "FINAL ANSWER" in result["messages"][-1].content else "researcher"
|
45 |
+
result["messages"][-1] = HumanMessage(
|
46 |
+
content=result["messages"][-1].content, name="chart_generator"
|
47 |
+
)
|
48 |
+
return Command(update={"messages": result["messages"]}, goto=goto)
|
49 |
+
|
50 |
+
# Initialize the LangGraph workflow
|
51 |
+
workflow = StateGraph(MessagesState)
|
52 |
+
workflow.add_node("researcher", research_node)
|
53 |
+
workflow.add_node("chart_generator", chart_node)
|
54 |
+
workflow.add_edge(START, "researcher")
|
55 |
+
workflow.add_edge("researcher", "chart_generator")
|
56 |
+
workflow.add_edge("chart_generator", END)
|
57 |
+
graph = workflow.compile()
|
58 |
+
|
59 |
+
# Define the function to run the graph
|
60 |
+
def run_langgraph(user_input):
|
61 |
+
# Start with user input
|
62 |
+
events = graph.stream(
|
63 |
+
{"messages": [("user", user_input)]},
|
64 |
+
{"recursion_limit": 150}
|
65 |
+
)
|
66 |
+
# Collect outputs from the workflow
|
67 |
+
output = []
|
68 |
+
for event in events:
|
69 |
+
output.append(event)
|
70 |
+
# Return the final output
|
71 |
+
return output[-1]["messages"][-1].content if output else "No output generated"
|
72 |
+
|
73 |
+
# Create Gradio interface
|
74 |
+
def process_input(user_input):
|
75 |
+
result = run_langgraph(user_input)
|
76 |
+
return result
|
77 |
+
|
78 |
+
interface = gr.Interface(
|
79 |
+
fn=process_input,
|
80 |
+
inputs="text",
|
81 |
+
outputs="text",
|
82 |
+
title="LangGraph Research Automation",
|
83 |
+
description="Enter your research task (e.g., 'Get GDP data for the USA over the past 5 years and create a chart.')."
|
84 |
+
)
|
85 |
+
|
86 |
+
# Launch the Gradio interface
|
87 |
+
if __name__ == "__main__":
|
88 |
+
interface.launch()
|