Spaces:
Sleeping
Sleeping
Update graph_builder.py
Browse files- graph_builder.py +8 -24
graph_builder.py
CHANGED
|
@@ -1,44 +1,28 @@
|
|
| 1 |
from langgraph.graph import StateGraph
|
| 2 |
|
| 3 |
-
class AgentState(dict):
|
| 4 |
-
"""
|
| 5 |
-
Holds the agent state passed between graph nodes.
|
| 6 |
-
Always includes:
|
| 7 |
-
- question: str
|
| 8 |
-
- llm_output: str (added after LLM node)
|
| 9 |
-
- final_answer: str (added after formatter)
|
| 10 |
-
"""
|
| 11 |
-
def __init__(self, initial_data):
|
| 12 |
-
super().__init__()
|
| 13 |
-
if "question" not in initial_data:
|
| 14 |
-
raise ValueError("AgentState requires a 'question' key at init")
|
| 15 |
-
self.update(initial_data)
|
| 16 |
-
|
| 17 |
-
from llm_node import llm_node
|
| 18 |
-
from formatter_node import formatter_node
|
| 19 |
-
|
| 20 |
def build_graph():
|
| 21 |
-
graph = StateGraph(
|
| 22 |
|
| 23 |
def llm_step(state):
|
| 24 |
question = state.get("question")
|
| 25 |
if not question:
|
| 26 |
-
raise ValueError("
|
| 27 |
-
|
|
|
|
| 28 |
state["llm_output"] = llm_output
|
| 29 |
return state
|
| 30 |
|
| 31 |
def formatter_step(state):
|
| 32 |
llm_output = state.get("llm_output")
|
| 33 |
if not llm_output:
|
| 34 |
-
raise ValueError("
|
| 35 |
-
|
| 36 |
-
state["final_answer"] =
|
| 37 |
return state
|
| 38 |
|
| 39 |
graph.add_node("llm", llm_step)
|
| 40 |
graph.add_node("formatter", formatter_step)
|
| 41 |
-
graph.set_entry_point("llm")
|
| 42 |
graph.add_edge("llm", "formatter")
|
| 43 |
graph.set_finish_point("formatter")
|
| 44 |
|
|
|
|
| 1 |
from langgraph.graph import StateGraph
|
| 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
def build_graph():
|
| 4 |
+
graph = StateGraph(dict)
|
| 5 |
|
| 6 |
def llm_step(state):
|
| 7 |
question = state.get("question")
|
| 8 |
if not question:
|
| 9 |
+
raise ValueError("State is missing 'question'")
|
| 10 |
+
# This is where you integrate your LLM call.
|
| 11 |
+
llm_output = f"Dummy answer to: {question}"
|
| 12 |
state["llm_output"] = llm_output
|
| 13 |
return state
|
| 14 |
|
| 15 |
def formatter_step(state):
|
| 16 |
llm_output = state.get("llm_output")
|
| 17 |
if not llm_output:
|
| 18 |
+
raise ValueError("State is missing 'llm_output'")
|
| 19 |
+
# For now, we just pass it through.
|
| 20 |
+
state["final_answer"] = llm_output.strip()
|
| 21 |
return state
|
| 22 |
|
| 23 |
graph.add_node("llm", llm_step)
|
| 24 |
graph.add_node("formatter", formatter_step)
|
| 25 |
+
graph.set_entry_point("llm")
|
| 26 |
graph.add_edge("llm", "formatter")
|
| 27 |
graph.set_finish_point("formatter")
|
| 28 |
|