68 lines
1.9 KiB
Python
68 lines
1.9 KiB
Python
"""
|
|
A simple self-correcting agent example using LangGraph.
|
|
|
|
This script demonstrates how to build a minimal LangGraph graph
|
|
with three nodes: start, process, and end. The graph concatenates
|
|
a greeting message and prints it at the end. The example ensures
|
|
that imports from `langgraph.graph` work correctly.
|
|
"""
|
|
|
|
from langgraph.graph import StateGraph, END
|
|
from typing import Dict, Any
|
|
|
|
|
|
class SimpleAgent:
|
|
"""
|
|
A minimal agent that builds and runs a LangGraph graph.
|
|
"""
|
|
|
|
def __init__(self) -> None:
|
|
# Create a new StateGraph instance
|
|
self.graph = StateGraph()
|
|
|
|
# Add nodes to the graph
|
|
self.graph.add_node("start", self.start_node)
|
|
self.graph.add_node("process", self.process_node)
|
|
self.graph.add_node("end", self.end_node)
|
|
|
|
# Define the entry point and edges
|
|
self.graph.set_entry_point("start")
|
|
self.graph.add_edge("start", "process")
|
|
self.graph.add_edge("process", "end")
|
|
self.graph.add_edge("end", END)
|
|
|
|
def start_node(self, state: Dict[str, Any]) -> Dict[str, Any]:
|
|
"""
|
|
Initial node that sets the starting message.
|
|
"""
|
|
state["message"] = "Hello"
|
|
return state
|
|
|
|
def process_node(self, state: Dict[str, Any]) -> Dict[str, Any]:
|
|
"""
|
|
Process node that appends to the message.
|
|
"""
|
|
state["message"] += " World"
|
|
return state
|
|
|
|
def end_node(self, state: Dict[str, Any]) -> Dict[str, Any]:
|
|
"""
|
|
End node that prints the final message.
|
|
"""
|
|
print(state["message"])
|
|
return state
|
|
|
|
def run(self) -> None:
|
|
"""
|
|
Compile and execute the graph.
|
|
"""
|
|
# Compile the graph into a runnable function
|
|
runnable = self.graph.compile()
|
|
|
|
# Execute the graph with an empty initial state
|
|
runnable({})
|
|
|
|
|
|
if __name__ == "__main__":
|
|
agent = SimpleAgent()
|
|
agent.run() |