Spaces:
Sleeping
Sleeping
Update graph_builder.py
Browse files- graph_builder.py +22 -5
graph_builder.py
CHANGED
|
@@ -1,21 +1,38 @@
|
|
| 1 |
from langgraph.graph import StateGraph
|
|
|
|
| 2 |
from llm_node import llm_node
|
| 3 |
from formatter_node import formatter_node
|
| 4 |
|
| 5 |
class AgentState(dict):
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
def build_graph():
|
| 9 |
graph = StateGraph(AgentState)
|
| 10 |
|
| 11 |
-
def llm_step(state):
|
| 12 |
-
question = state
|
|
|
|
|
|
|
| 13 |
llm_output = llm_node(question)
|
| 14 |
state["llm_output"] = llm_output
|
| 15 |
return state
|
| 16 |
|
| 17 |
-
def formatter_step(state):
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
| 19 |
state["final_answer"] = final_answer
|
| 20 |
return state
|
| 21 |
|
|
|
|
| 1 |
from langgraph.graph import StateGraph
|
| 2 |
+
from langchain_core.runnables import RunnableConfig
|
| 3 |
from llm_node import llm_node
|
| 4 |
from formatter_node import formatter_node
|
| 5 |
|
| 6 |
class AgentState(dict):
|
| 7 |
+
"""
|
| 8 |
+
Holds the agent state passed between graph nodes.
|
| 9 |
+
Always includes:
|
| 10 |
+
- question: str
|
| 11 |
+
- llm_output: str (added after LLM node)
|
| 12 |
+
- final_answer: str (added after formatter)
|
| 13 |
+
"""
|
| 14 |
+
def __init__(self, initial_data):
|
| 15 |
+
super().__init__()
|
| 16 |
+
if "question" not in initial_data:
|
| 17 |
+
raise ValueError("AgentState requires a 'question' key at init")
|
| 18 |
+
self.update(initial_data)
|
| 19 |
|
| 20 |
def build_graph():
|
| 21 |
graph = StateGraph(AgentState)
|
| 22 |
|
| 23 |
+
def llm_step(state, config: RunnableConfig = None):
|
| 24 |
+
question = state.get("question")
|
| 25 |
+
if not question:
|
| 26 |
+
raise ValueError("Missing 'question' in state")
|
| 27 |
llm_output = llm_node(question)
|
| 28 |
state["llm_output"] = llm_output
|
| 29 |
return state
|
| 30 |
|
| 31 |
+
def formatter_step(state, config: RunnableConfig = None):
|
| 32 |
+
llm_output = state.get("llm_output")
|
| 33 |
+
if not llm_output:
|
| 34 |
+
raise ValueError("Missing 'llm_output' in state")
|
| 35 |
+
final_answer = formatter_node(llm_output)
|
| 36 |
state["final_answer"] = final_answer
|
| 37 |
return state
|
| 38 |
|