Quản lí trạng thái (tiếp tục)
Hãy hiểu cái đó với một ví dụ đơn giản. Trước tiên, chúng ta cần định nghĩa một kế hoạch của trạng thái:
from typing_extensions import TypedDict
class JobApplicationState(TypedDict):job_description: str
is_suitable: bool
application: str
Một TypeDict là một constructor dạng Python cái cho phép định nghĩa các dictionaries với một tập định
nghĩa trước các keys và mỗi key có thể có dạng của bản thân nó (đối nghịch với một xây dựng Dict[str, str])
Kế hoạch của trạng thái LangGraph không nên nhất thiết được định nghĩa như một TypeDict; bạn cũng có thể
sử dụng các clases dữ liệu hay Pydantic models.
Sau khi chúng ta đã định nghĩa một kế hoạch cho một trạng thái, chúng ta có thê định nghĩa dòng làm việc
đơn giản đầu tiên của chúng ta:
from langgraph.graph import StateGraph, START, END, Graph
def analyze_job_description(state):print(“…Analyzing a provided job description …”)
return {“is_suitable”: len(state[“job_description”]) > 100}def generate_application(state):
print(“…generating application…”)
return {“application”: “some_fake_application”}builder = StateGraph(JobApplicationState)
builder.add_node(“analyze_job_description”, analyze_job_description)
builder.add_node(“generate_application”, generate_application)
builder.add_edge(START, “analyze_job_description”)
builder.add_edge(“analyze_job_description”, “generate_application”)
builder.add_edge(“generate_application”, END)
graph = builder.compile()
