Add multiple LangGraph examples including basic agents, chatbots, conditional edges, checkpointers, tool calls, and human-in-the-loop approval processes. Update .gitignore to include local environment files.
This commit is contained in:
51
07_command.py
Normal file
51
07_command.py
Normal file
@@ -0,0 +1,51 @@
|
||||
from typing import Literal, TypedDict
|
||||
|
||||
from langgraph.graph import END, START, StateGraph
|
||||
from langgraph.types import Command
|
||||
|
||||
|
||||
class State(TypedDict):
|
||||
number: int
|
||||
result: str
|
||||
|
||||
|
||||
def check_number(
|
||||
state: State,
|
||||
) -> Command[Literal["positive", "negative"]]:
|
||||
if state["number"] >= 0:
|
||||
return Command(
|
||||
update={"result": "这是非负数"},
|
||||
goto="positive",
|
||||
)
|
||||
|
||||
return Command(
|
||||
update={"result": "这是负数"},
|
||||
goto="negative",
|
||||
)
|
||||
|
||||
|
||||
def positive(state: State):
|
||||
print(state["result"])
|
||||
return {}
|
||||
|
||||
|
||||
def negative(state: State):
|
||||
print(state["result"])
|
||||
return {}
|
||||
|
||||
|
||||
builder = StateGraph(State)
|
||||
builder.add_node("check", check_number)
|
||||
builder.add_node("positive", positive)
|
||||
builder.add_node("negative", negative)
|
||||
|
||||
builder.add_edge(START, "check")
|
||||
builder.add_edge("positive", END)
|
||||
builder.add_edge("negative", END)
|
||||
|
||||
graph = builder.compile()
|
||||
|
||||
graph.invoke({
|
||||
"number": -5,
|
||||
"result": "",
|
||||
})
|
||||
Reference in New Issue
Block a user