Rahul-8799 commited on
Commit
77d2314
Β·
verified Β·
1 Parent(s): e98599b

Update utils/langgraph_pipeline.py

Browse files
Files changed (1) hide show
  1. 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) State types
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
- # Create a StructuredTool (with explicit description) for the bridge
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("Bridge", bridge_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", dev_node)
94
- graph.add_node("QualityAssurance", qa_node)
95
-
96
- graph.set_entry_point("Bridge")
97
- graph.add_edge("Bridge", "ProductManager")
98
- graph.add_edge("ProductManager", "ProjectManager")
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
- # 5) Pipeline entrypoint
108
- # β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
109
  def run_pipeline_and_save(prompt: str):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
  initial_state = {
111
- "messages": [HumanMessage(content=prompt)],
112
- "chat_log": [],
113
  }
114
- # this invoke will now see:
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"]