Files
langgraph-learning-examples/01_basic_agent_no_llm.py

38 lines
789 B
Python

from typing import TypedDict
from langgraph.graph import StateGraph, START, END
class State(TypedDict):
text: str
length: int
def normalize_text(state: State):
"""清理文本。"""
normalized = state["text"].strip().lower()
return {"text": normalized}
def calculate_length(state: State):
"""计算文本长度。"""
return {"length": len(state["text"])}
builder = StateGraph(State)
builder.add_node("normalize_text", normalize_text)
builder.add_node("calculate_length", calculate_length)
builder.add_edge(START, "normalize_text")
builder.add_edge("normalize_text", "calculate_length")
builder.add_edge("calculate_length", END)
graph = builder.compile()
result = graph.invoke({
"text": " Hello LangGraph! ",
"length": 0,
})
print(result)