mirror of
https://github.com/langchain-ai/langgraph.git
synced 2026-08-29 03:09:45 +02:00
docs: __start__/__end__ consistency (#1874)
* changes * skip mermaid * retries fix * viz error * changes
This commit is contained in:
@@ -464,7 +464,7 @@
|
||||
"from langchain_anthropic import ChatAnthropic\n",
|
||||
"from typing_extensions import TypedDict\n",
|
||||
"\n",
|
||||
"from langgraph.graph import StateGraph, START\n",
|
||||
"from langgraph.graph import StateGraph, START, END\n",
|
||||
"from langgraph.graph.message import add_messages\n",
|
||||
"\n",
|
||||
"\n",
|
||||
@@ -552,7 +552,7 @@
|
||||
"\n",
|
||||
"Below, call define a router function called `route_tools`, that checks for tool_calls in the chatbot's output. Provide this function to the graph by calling `add_conditional_edges`, which tells the graph that whenever the `chatbot` node completes to check this function to see where to go next. \n",
|
||||
"\n",
|
||||
"The condition will route to `tools` if tool calls are present and \"`__end__`\" if not.\n",
|
||||
"The condition will route to `tools` if tool calls are present and `END` if not.\n",
|
||||
"\n",
|
||||
"Later, we will replace this with the prebuilt [tools_condition](https://langchain-ai.github.io/langgraph/reference/prebuilt/#tools_condition) to be more concise, but implementing it ourselves first makes things more clear. "
|
||||
]
|
||||
@@ -569,7 +569,7 @@
|
||||
"\n",
|
||||
"def route_tools(\n",
|
||||
" state: State,\n",
|
||||
") -> Literal[\"tools\", \"__end__\"]:\n",
|
||||
"):\n",
|
||||
" \"\"\"\n",
|
||||
" Use in the conditional_edge to route to the ToolNode if the last message\n",
|
||||
" has tool calls. Otherwise, route to the end.\n",
|
||||
@@ -582,10 +582,10 @@
|
||||
" raise ValueError(f\"No messages found in input state to tool_edge: {state}\")\n",
|
||||
" if hasattr(ai_message, \"tool_calls\") and len(ai_message.tool_calls) > 0:\n",
|
||||
" return \"tools\"\n",
|
||||
" return \"__end__\"\n",
|
||||
" return END\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# The `tools_condition` function returns \"tools\" if the chatbot asks to use a tool, and \"__end__\" if\n",
|
||||
"# The `tools_condition` function returns \"tools\" if the chatbot asks to use a tool, and \"END\" if\n",
|
||||
"# it is fine directly responding. This conditional routing defines the main agent loop.\n",
|
||||
"graph_builder.add_conditional_edges(\n",
|
||||
" \"chatbot\",\n",
|
||||
@@ -595,7 +595,7 @@
|
||||
" # want to use a node named something else apart from \"tools\",\n",
|
||||
" # You can update the value of the dictionary to something else\n",
|
||||
" # e.g., \"tools\": \"my_tools\"\n",
|
||||
" {\"tools\": \"tools\", \"__end__\": \"__end__\"},\n",
|
||||
" {\"tools\": \"tools\", END: END},\n",
|
||||
")\n",
|
||||
"# Any time a tool is called, we return to the chatbot to decide the next step\n",
|
||||
"graph_builder.add_edge(\"tools\", \"chatbot\")\n",
|
||||
@@ -610,7 +610,7 @@
|
||||
"source": [
|
||||
"**Notice** that conditional edges start from a single node. This tells the graph \"any time the '`chatbot`' node runs, either go to 'tools' if it calls a tool, or end the loop if it responds directly. \n",
|
||||
"\n",
|
||||
"Like the prebuilt `tools_condition`, our function returns the \"`__end__`\" string if no tool calls are made. When the graph transitions to `__end__`, it has no more tasks to complete and ceases execution. Because the condition can return `__end__`, we don't need to explicitly set a `finish_point` this time. Our graph already has a way to finish!\n",
|
||||
"Like the prebuilt `tools_condition`, our function returns the `END` string if no tool calls are made. When the graph transitions to `END`, it has no more tasks to complete and ceases execution. Because the condition can return `END`, we don't need to explicitly set a `finish_point` this time. Our graph already has a way to finish!\n",
|
||||
"\n",
|
||||
"Let's visualize the graph we've built. The following function has some additional dependencies to run that are unimportant for this tutorial."
|
||||
]
|
||||
@@ -1115,7 +1115,7 @@
|
||||
"id": "627f4998-6780-4cce-8f3c-9a5580888e3a",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"The snapshot above contains the current state values, corresponding config, and the `next` node to process. In our case, the graph has reached an `__end__` state, so `next` is empty.\n",
|
||||
"The snapshot above contains the current state values, corresponding config, and the `next` node to process. In our case, the graph has reached an `END` state, so `next` is empty.\n",
|
||||
"\n",
|
||||
"**Congratulations!** Your chatbot can now maintain conversation state across sessions thanks to LangGraph's checkpointing system. This opens up exciting possibilities for more natural, contextual interactions. LangGraph's checkpointing even handles **arbitrarily complex graph states**, which is much more expressive and powerful than simple chat memory.\n",
|
||||
"\n",
|
||||
@@ -2243,7 +2243,7 @@
|
||||
"graph_builder.add_conditional_edges(\n",
|
||||
" \"chatbot\",\n",
|
||||
" select_next_node,\n",
|
||||
" {\"human\": \"human\", \"tools\": \"tools\", \"__end__\": \"__end__\"},\n",
|
||||
" {\"human\": \"human\", \"tools\": \"tools\", END: END},\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
@@ -2744,7 +2744,7 @@
|
||||
"graph_builder.add_node(\"human\", human_node)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def select_next_node(state: State) -> Literal[\"human\", \"tools\", \"__end__\"]:\n",
|
||||
"def select_next_node(state: State):\n",
|
||||
" if state[\"ask_human\"]:\n",
|
||||
" return \"human\"\n",
|
||||
" # Otherwise, we can route as before\n",
|
||||
@@ -2754,7 +2754,7 @@
|
||||
"graph_builder.add_conditional_edges(\n",
|
||||
" \"chatbot\",\n",
|
||||
" select_next_node,\n",
|
||||
" {\"human\": \"human\", \"tools\": \"tools\", \"__end__\": \"__end__\"},\n",
|
||||
" {\"human\": \"human\", \"tools\": \"tools\", END: END},\n",
|
||||
")\n",
|
||||
"graph_builder.add_edge(\"tools\", \"chatbot\")\n",
|
||||
"graph_builder.add_edge(\"human\", \"chatbot\")\n",
|
||||
|
||||
Reference in New Issue
Block a user