feat: solution for 'Экзамен: Самокорректирующийся агент'

This commit is contained in:
2026-07-01 15:06:45 +03:00
parent 0486d5cf52
commit 7e9a879dfd
4 changed files with 125 additions and 18 deletions
+68
View File
@@ -0,0 +1,68 @@
"""
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()