Add src/graph.py
This commit is contained in:
@@ -0,0 +1,46 @@
|
|||||||
|
"""
|
||||||
|
LangGraph implementation for the research brief generator.
|
||||||
|
|
||||||
|
The graph orchestrates the following nodes (defined in ``brief.py``):
|
||||||
|
|
||||||
|
* ``outline`` – generates a 4–5 point outline.
|
||||||
|
* ``research_step`` – performs a web search for the current outline point and creates a short note.
|
||||||
|
* ``synthesize`` – combines all notes into a coherent brief.
|
||||||
|
|
||||||
|
The graph loops over ``research_step`` until all outline points are processed.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from langgraph import StateGraph, END
|
||||||
|
from .brief import BriefState, outline_node, research_step_node, synthesize_node
|
||||||
|
|
||||||
|
def build_graph() -> StateGraph:
|
||||||
|
"""Build and return the LangGraph graph.
|
||||||
|
|
||||||
|
The graph starts at ``outline``. After generating the outline it
|
||||||
|
proceeds to ``research_step``. ``research_step`` is repeated until
|
||||||
|
``step_index`` reaches the length of the outline, then the graph
|
||||||
|
moves to ``synthesize`` and ends.
|
||||||
|
"""
|
||||||
|
graph = StateGraph(BriefState)
|
||||||
|
|
||||||
|
# Add nodes
|
||||||
|
graph.add_node("outline", outline_node)
|
||||||
|
graph.add_node("research_step", research_step_node)
|
||||||
|
graph.add_node("synthesize", synthesize_node)
|
||||||
|
|
||||||
|
# Entry point
|
||||||
|
graph.set_entry_point("outline")
|
||||||
|
|
||||||
|
# After outline, always go to research_step
|
||||||
|
graph.add_edge("outline", "research_step")
|
||||||
|
|
||||||
|
# Conditional loop: if more steps remain, stay in research_step
|
||||||
|
def research_cond(state: BriefState):
|
||||||
|
return "research_step" if state["step_index"] < len(state["outline"]) else "synthesize"
|
||||||
|
|
||||||
|
graph.add_conditional_edges("research_step", research_cond)
|
||||||
|
|
||||||
|
# End after synthesis
|
||||||
|
graph.add_edge("synthesize", END)
|
||||||
|
|
||||||
|
return graph
|
||||||
Reference in New Issue
Block a user