Spaces:
Runtime error
Runtime error
Update utils/langgraph_pipeline.py
Browse files- utils/langgraph_pipeline.py +34 -71
utils/langgraph_pipeline.py
CHANGED
@@ -2,13 +2,12 @@
|
|
2 |
|
3 |
from typing import TypedDict, List
|
4 |
|
5 |
-
from langchain_core.messages import HumanMessage, AIMessage
|
6 |
-
from langchain_core.messages.base import BaseMessage
|
7 |
-
from langchain_core.tools.structured import StructuredTool
|
8 |
-
|
9 |
from langgraph.graph import StateGraph, END
|
10 |
from langgraph.prebuilt import ToolNode
|
11 |
|
|
|
|
|
|
|
12 |
from agents import (
|
13 |
product_manager_agent,
|
14 |
project_manager_agent,
|
@@ -17,9 +16,8 @@ from agents import (
|
|
17 |
quality_assurance_agent,
|
18 |
)
|
19 |
|
20 |
-
|
21 |
-
# 1)
|
22 |
-
# ββββββββββββββ
|
23 |
class InputState(TypedDict):
|
24 |
messages: List[BaseMessage]
|
25 |
chat_log: list
|
@@ -32,86 +30,51 @@ class OutputState(TypedDict):
|
|
32 |
qa_output: str
|
33 |
chat_log: list
|
34 |
|
35 |
-
# ββββββββββββββ
|
36 |
-
# 2) Bridge β ProductManager
|
37 |
-
# ββββββββββββββ
|
38 |
-
def bridge_to_product_manager(state: dict) -> dict:
|
39 |
-
"""
|
40 |
-
Convert the last HumanMessage into a structured system AIMessage
|
41 |
-
that the Product Manager agent can consume.
|
42 |
-
"""
|
43 |
-
msgs = state.get("messages")
|
44 |
-
logs = state.get("chat_log", [])
|
45 |
-
if not isinstance(msgs, list) or not msgs or not isinstance(msgs[-1], HumanMessage):
|
46 |
-
raise ValueError("Expected state['messages'] to be a non-empty list ending in a HumanMessage")
|
47 |
-
|
48 |
-
user_text = msgs[-1].content
|
49 |
-
spec = f"""# Stakeholder Prompt
|
50 |
-
|
51 |
-
A new product request has been submitted:
|
52 |
-
|
53 |
-
"{user_text}"
|
54 |
-
|
55 |
-
Please convert this into a structured product specification including:
|
56 |
-
- Goals
|
57 |
-
- Key Features
|
58 |
-
- User Stories
|
59 |
-
- Success Metrics
|
60 |
-
"""
|
61 |
-
ai = AIMessage(content=spec)
|
62 |
-
return {
|
63 |
-
"messages": msgs + [ai],
|
64 |
-
"chat_log": logs + [{"role": "System", "content": spec}],
|
65 |
-
}
|
66 |
|
67 |
-
#
|
68 |
-
bridge_tool = StructuredTool.from_function(
|
69 |
-
func=bridge_to_product_manager,
|
70 |
-
name="bridge_to_product_manager",
|
71 |
-
description="Generate a structured AIMessage from a HumanMessage for the Product Manager agent."
|
72 |
-
)
|
73 |
-
bridge_node = ToolNode([bridge_tool])
|
74 |
-
|
75 |
-
# ββββββββββββββ
|
76 |
-
# 3) Wrap your LLM agents
|
77 |
-
# ββββββββββββββ
|
78 |
-
pm_node = ToolNode([product_manager_agent.run])
|
79 |
proj_node = ToolNode([project_manager_agent.run])
|
80 |
arch_node = ToolNode([software_architect_agent.run])
|
81 |
dev_node = ToolNode([software_engineer_agent.run])
|
82 |
qa_node = ToolNode([quality_assurance_agent.run])
|
83 |
|
84 |
-
#
|
85 |
-
# 4) Build & compile the graph
|
86 |
-
# ββββββββββββββ
|
87 |
graph = StateGraph(input=InputState, output=OutputState)
|
88 |
|
89 |
-
graph.add_node("
|
90 |
-
graph.add_node("ProductManager", pm_node)
|
91 |
-
graph.add_node("ProjectManager", proj_node)
|
92 |
graph.add_node("SoftwareArchitect", arch_node)
|
93 |
-
graph.add_node("SoftwareEngineer",
|
94 |
-
graph.add_node("QualityAssurance",
|
95 |
-
|
96 |
-
graph.set_entry_point("
|
97 |
-
graph.add_edge("
|
98 |
-
graph.add_edge("
|
99 |
-
graph.add_edge("ProjectManager", "SoftwareArchitect")
|
100 |
-
graph.add_edge("SoftwareArchitect", "SoftwareEngineer")
|
101 |
graph.add_edge("SoftwareEngineer", "QualityAssurance")
|
102 |
graph.add_edge("QualityAssurance", END)
|
103 |
|
104 |
compiled_graph = graph.compile()
|
105 |
|
106 |
-
|
107 |
-
#
|
108 |
-
# ββββββββββββββ
|
109 |
def run_pipeline_and_save(prompt: str):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
initial_state = {
|
111 |
-
"messages": [
|
112 |
-
"chat_log": [],
|
113 |
}
|
114 |
-
|
115 |
-
# Bridge β PM β ProjectManager β Architect β Engineer β QA β END
|
116 |
final_state = compiled_graph.invoke(initial_state)
|
|
|
117 |
return final_state["chat_log"], final_state["qa_output"]
|
|
|
2 |
|
3 |
from typing import TypedDict, List
|
4 |
|
|
|
|
|
|
|
|
|
5 |
from langgraph.graph import StateGraph, END
|
6 |
from langgraph.prebuilt import ToolNode
|
7 |
|
8 |
+
from langchain_core.messages import HumanMessage, AIMessage
|
9 |
+
from langchain_core.messages.base import BaseMessage
|
10 |
+
|
11 |
from agents import (
|
12 |
product_manager_agent,
|
13 |
project_manager_agent,
|
|
|
16 |
quality_assurance_agent,
|
17 |
)
|
18 |
|
19 |
+
|
20 |
+
# 1) Define your state shapes
|
|
|
21 |
class InputState(TypedDict):
|
22 |
messages: List[BaseMessage]
|
23 |
chat_log: list
|
|
|
30 |
qa_output: str
|
31 |
chat_log: list
|
32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
+
# 2) Wrap ONLY the downstream agents in ToolNodes
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
proj_node = ToolNode([project_manager_agent.run])
|
36 |
arch_node = ToolNode([software_architect_agent.run])
|
37 |
dev_node = ToolNode([software_engineer_agent.run])
|
38 |
qa_node = ToolNode([quality_assurance_agent.run])
|
39 |
|
40 |
+
# 3) Build the LangGraph starting at ProjectManager
|
|
|
|
|
41 |
graph = StateGraph(input=InputState, output=OutputState)
|
42 |
|
43 |
+
graph.add_node("ProjectManager", proj_node)
|
|
|
|
|
44 |
graph.add_node("SoftwareArchitect", arch_node)
|
45 |
+
graph.add_node("SoftwareEngineer", dev_node)
|
46 |
+
graph.add_node("QualityAssurance", qa_node)
|
47 |
+
|
48 |
+
graph.set_entry_point("ProjectManager")
|
49 |
+
graph.add_edge("ProjectManager", "SoftwareArchitect")
|
50 |
+
graph.add_edge("SoftwareArchitect","SoftwareEngineer")
|
|
|
|
|
51 |
graph.add_edge("SoftwareEngineer", "QualityAssurance")
|
52 |
graph.add_edge("QualityAssurance", END)
|
53 |
|
54 |
compiled_graph = graph.compile()
|
55 |
|
56 |
+
|
57 |
+
# 4) The oneβandβonly run function
|
|
|
58 |
def run_pipeline_and_save(prompt: str):
|
59 |
+
# a) Start with the raw HumanMessage
|
60 |
+
messages = [HumanMessage(content=prompt)]
|
61 |
+
chat_log = []
|
62 |
+
|
63 |
+
# b) Run your ProductManager agent manually
|
64 |
+
pm_state = product_manager_agent.run({
|
65 |
+
"messages": messages,
|
66 |
+
"chat_log": chat_log,
|
67 |
+
})
|
68 |
+
|
69 |
+
# Sanity check: ensure we got back an AIMessage
|
70 |
+
assert isinstance(pm_state["messages"][-1], AIMessage), "ProductManager must produce an AIMessage"
|
71 |
+
|
72 |
+
# c) Feed that into the graph
|
73 |
initial_state = {
|
74 |
+
"messages": pm_state["messages"],
|
75 |
+
"chat_log": pm_state["chat_log"],
|
76 |
}
|
77 |
+
|
|
|
78 |
final_state = compiled_graph.invoke(initial_state)
|
79 |
+
|
80 |
return final_state["chat_log"], final_state["qa_output"]
|