docs: __start__/__end__ consistency (#1874)

* changes

* skip mermaid

* retries fix

* viz error

* changes
This commit is contained in:
Isaac Francisco
2024-09-30 13:55:53 -07:00
committed by GitHub
parent d29747906d
commit 17dc1108a3
18 changed files with 110 additions and 108 deletions
@@ -107,7 +107,7 @@
"from langchain_core.tools import tool\n",
"\n",
"from langgraph.checkpoint.memory import MemorySaver\n",
"from langgraph.graph import MessagesState, StateGraph, START\n",
"from langgraph.graph import MessagesState, StateGraph, START, END\n",
"from langgraph.prebuilt import ToolNode\n",
"\n",
"memory = MemorySaver()\n",
@@ -127,12 +127,12 @@
"bound_model = model.bind_tools(tools)\n",
"\n",
"\n",
"def should_continue(state: MessagesState) -> Literal[\"action\", \"__end__\"]:\n",
"def should_continue(state: MessagesState):\n",
" \"\"\"Return the next node to execute.\"\"\"\n",
" last_message = state[\"messages\"][-1]\n",
" # If there is no function call, then we finish\n",
" if not last_message.tool_calls:\n",
" return \"__end__\"\n",
" return END\n",
" # Otherwise if there is, we continue\n",
" return \"action\"\n",
"\n",
@@ -162,6 +162,8 @@
" \"agent\",\n",
" # Next, we pass in the function that will determine which node is called next.\n",
" should_continue,\n",
" # Next, we pass in the path map - all the possible nodes this edge could go to\n",
" ['action', END]\n",
")\n",
"\n",
"# We now add a normal edge from `tools` to `agent`.\n",
@@ -98,7 +98,7 @@
"from langchain_core.tools import tool\n",
"\n",
"from langgraph.checkpoint.memory import MemorySaver\n",
"from langgraph.graph import MessagesState, StateGraph, START\n",
"from langgraph.graph import MessagesState, StateGraph, START, END\n",
"from langgraph.prebuilt import ToolNode\n",
"\n",
"memory = MemorySaver()\n",
@@ -118,12 +118,12 @@
"bound_model = model.bind_tools(tools)\n",
"\n",
"\n",
"def should_continue(state: MessagesState) -> Literal[\"action\", \"__end__\"]:\n",
"def should_continue(state: MessagesState):\n",
" \"\"\"Return the next node to execute.\"\"\"\n",
" last_message = state[\"messages\"][-1]\n",
" # If there is no function call, then we finish\n",
" if not last_message.tool_calls:\n",
" return \"__end__\"\n",
" return END\n",
" # Otherwise if there is, we continue\n",
" return \"action\"\n",
"\n",
@@ -153,6 +153,8 @@
" \"agent\",\n",
" # Next, we pass in the function that will determine which node is called next.\n",
" should_continue,\n",
" # Next, we pass in the path map - all the possible nodes this edge could go to\n",
" ['action',END]\n",
")\n",
"\n",
"# We now add a normal edge from `tools` to `agent`.\n",
@@ -247,12 +249,12 @@
"bound_model = model.bind_tools(tools)\n",
"\n",
"\n",
"def should_continue(state: MessagesState) -> Literal[\"action\", \"__end__\"]:\n",
"def should_continue(state: MessagesState):\n",
" \"\"\"Return the next node to execute.\"\"\"\n",
" last_message = state[\"messages\"][-1]\n",
" # If there is no function call, then we finish\n",
" if not last_message.tool_calls:\n",
" return \"__end__\"\n",
" return END\n",
" # Otherwise if there is, we continue\n",
" return \"action\"\n",
"\n",
@@ -288,6 +290,8 @@
" \"agent\",\n",
" # Next, we pass in the function that will determine which node is called next.\n",
" should_continue,\n",
" # Next, we pass in the pathmap - all the possible nodes this edge could go to\n",
" ['action', END]\n",
")\n",
"\n",
"# We now add a normal edge from `tools` to `agent`.\n",
+5 -4
View File
@@ -180,15 +180,15 @@
"source": [
"from typing import Literal\n",
"\n",
"from langgraph.graph import StateGraph, MessagesState\n",
"from langgraph.graph import StateGraph, MessagesState, START, END\n",
"\n",
"\n",
"def should_continue(state: MessagesState) -> Literal[\"tools\", \"__end__\"]:\n",
"def should_continue(state: MessagesState):\n",
" messages = state[\"messages\"]\n",
" last_message = messages[-1]\n",
" if last_message.tool_calls:\n",
" return \"tools\"\n",
" return \"__end__\"\n",
" return END\n",
"\n",
"\n",
"def call_model(state: MessagesState):\n",
@@ -203,10 +203,11 @@
"workflow.add_node(\"agent\", call_model)\n",
"workflow.add_node(\"tools\", tool_node)\n",
"\n",
"workflow.add_edge(\"__start__\", \"agent\")\n",
"workflow.add_edge(START, \"agent\")\n",
"workflow.add_conditional_edges(\n",
" \"agent\",\n",
" should_continue,\n",
" [\"tools\",END]\n",
")\n",
"workflow.add_edge(\"tools\", \"agent\")\n",
"\n",
+4 -3
View File
@@ -283,14 +283,14 @@
"source": [
"# Define the function that determines whether to continue or not\n",
"from typing import Literal\n",
"from langgraph.graph import END\n",
"\n",
"\n",
"def should_continue(state: State) -> Literal[\"action\", \"__end__\"]:\n",
"def should_continue(state: State):\n",
" \"\"\"Return the next node to execute.\"\"\"\n",
" last_message = state[\"messages\"][-1]\n",
" # If there is no function call, then we finish\n",
" if not last_message.tool_calls:\n",
" return \"__end__\"\n",
" return END\n",
" # Otherwise if there is, we continue\n",
" return \"action\"\n",
"\n",
@@ -339,6 +339,7 @@
" \"agent\",\n",
" # Next, we pass in the function that will determine which node is called next.\n",
" should_continue,\n",
" [\"action\", END]\n",
")\n",
"\n",
"# We now add a normal edge from `tools` to `agent`.\n",
@@ -65,7 +65,7 @@
"\n",
"def router(state: State):\n",
" if state['value'] == \"end\":\n",
" return \"__end__\"\n",
" return END\n",
" else:\n",
" return \"action\"\n",
"\n",
@@ -80,7 +80,7 @@
"workflow.add_node('decision',decision_node)\n",
"workflow.add_node('action',action_node)\n",
"workflow.add_edge(START,'decision')\n",
"workflow.add_conditional_edges('decision',router)\n",
"workflow.add_conditional_edges('decision',router,['action',END])\n",
"workflow.add_edge('action','decision')\n",
"app = workflow.compile()"
]
@@ -177,9 +177,9 @@
"def router(state: State):\n",
" # Force the agent to end if it is on the last step\n",
" if state['is_last_step']:\n",
" return \"__end__\"\n",
" return END\n",
" if state['value'] == \"end\":\n",
" return \"__end__\"\n",
" return END\n",
" else:\n",
" return \"action\"\n",
"\n",
@@ -194,7 +194,7 @@
"workflow.add_node('decision',decision_node)\n",
"workflow.add_node('action',action_node)\n",
"workflow.add_edge(START,'decision')\n",
"workflow.add_conditional_edges('decision',router)\n",
"workflow.add_conditional_edges('decision',router,['action',END])\n",
"workflow.add_edge('action','decision')\n",
"app = workflow.compile()"
]
+4 -2
View File
@@ -282,7 +282,7 @@
"\n",
"\n",
"# Define the function that determines whether to continue or not\n",
"def should_continue(state: State) -> Literal[\"__end__\", \"tools\"]:\n",
"def should_continue(state: State):\n",
" messages = state[\"messages\"]\n",
" last_message = messages[-1]\n",
" # If there is no function call, then we finish\n",
@@ -338,6 +338,8 @@
" \"agent\",\n",
" # Next, we pass in the function that will determine which node is called next.\n",
" should_continue,\n",
" # Next we pass in the path map - all the nodes this edge could go to\n",
" [\"tools\",END]\n",
")\n",
"\n",
"workflow.add_edge(\"tools\", \"agent\")\n",
@@ -443,7 +445,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
"version": "3.9.6"
}
},
"nbformat": 4,
@@ -772,17 +772,17 @@
" # Dummy logic that will always continue\n",
" return {\"to_continue\": True}\n",
"\n",
"def route_after_prediction(state: GrandfatherState) -> Literal[\"graph\", \"__end__\"]:\n",
"def route_after_prediction(state: GrandfatherState):\n",
" if state['to_continue']:\n",
" return \"graph\"\n",
" else:\n",
" return \"__end__\"\n",
" return END\n",
"\n",
"grandparent_graph = StateGraph(GrandfatherState)\n",
"grandparent_graph.add_node(router_node)\n",
"grandparent_graph.add_node(\"graph\", graph)\n",
"grandparent_graph.add_edge(START, \"router_node\")\n",
"grandparent_graph.add_conditional_edges(\"router_node\", route_after_prediction)\n",
"grandparent_graph.add_conditional_edges(\"router_node\", route_after_prediction, ['graph',END])\n",
"grandparent_graph.add_edge(\"graph\", END)\n",
"grandparent_graph = grandparent_graph.compile(checkpointer=MemorySaver())"
]
+13 -10
View File
@@ -124,7 +124,7 @@
"from typing import Literal\n",
"\n",
"from langchain_anthropic import ChatAnthropic\n",
"from langgraph.graph import StateGraph, MessagesState\n",
"from langgraph.graph import StateGraph, MessagesState, START, END\n",
"from langgraph.prebuilt import ToolNode\n",
"\n",
"tool_node = ToolNode([get_weather])\n",
@@ -134,12 +134,12 @@
").bind_tools([get_weather])\n",
"\n",
"\n",
"def should_continue(state: MessagesState) -> Literal[\"tools\", \"__end__\"]:\n",
"def should_continue(state: MessagesState):\n",
" messages = state[\"messages\"]\n",
" last_message = messages[-1]\n",
" if last_message.tool_calls:\n",
" return \"tools\"\n",
" return \"__end__\"\n",
" return END\n",
"\n",
"\n",
"def call_model(state: MessagesState):\n",
@@ -154,10 +154,11 @@
"workflow.add_node(\"agent\", call_model)\n",
"workflow.add_node(\"tools\", tool_node)\n",
"\n",
"workflow.add_edge(\"__start__\", \"agent\")\n",
"workflow.add_edge(START, \"agent\")\n",
"workflow.add_conditional_edges(\n",
" \"agent\",\n",
" should_continue,\n",
" ['tools',END]\n",
")\n",
"workflow.add_edge(\"tools\", \"agent\")\n",
"\n",
@@ -319,12 +320,12 @@
"model_with_tools = model.bind_tools([master_haiku_generator])\n",
"\n",
"\n",
"def should_continue(state: MessagesState) -> Literal[\"tools\", \"__end__\"]:\n",
"def should_continue(state: MessagesState):\n",
" messages = state[\"messages\"]\n",
" last_message = messages[-1]\n",
" if last_message.tool_calls:\n",
" return \"tools\"\n",
" return \"__end__\"\n",
" return END\n",
"\n",
"\n",
"def call_model(state: MessagesState):\n",
@@ -339,10 +340,11 @@
"workflow.add_node(\"agent\", call_model)\n",
"workflow.add_node(\"tools\", tool_node)\n",
"\n",
"workflow.add_edge(\"__start__\", \"agent\")\n",
"workflow.add_edge(START, \"agent\")\n",
"workflow.add_conditional_edges(\n",
" \"agent\",\n",
" should_continue,\n",
" ['tools',END]\n",
")\n",
"workflow.add_edge(\"tools\", \"agent\")\n",
"\n",
@@ -424,12 +426,12 @@
"better_model_with_tools = better_model.bind_tools([master_haiku_generator])\n",
"\n",
"\n",
"def should_continue(state: MessagesState) -> Literal[\"tools\", \"__end__\"]:\n",
"def should_continue(state: MessagesState):\n",
" messages = state[\"messages\"]\n",
" last_message = messages[-1]\n",
" if last_message.tool_calls:\n",
" return \"tools\"\n",
" return \"__end__\"\n",
" return END\n",
"\n",
"\n",
"def should_fallback(\n",
@@ -480,10 +482,11 @@
"workflow.add_node(\"remove_failed_tool_call_attempt\", remove_failed_tool_call_attempt)\n",
"workflow.add_node(\"fallback_agent\", call_fallback_model)\n",
"\n",
"workflow.add_edge(\"__start__\", \"agent\")\n",
"workflow.add_edge(START, \"agent\")\n",
"workflow.add_conditional_edges(\n",
" \"agent\",\n",
" should_continue,\n",
" ['tools',END]\n",
")\n",
"workflow.add_conditional_edges(\"tools\", should_fallback)\n",
"workflow.add_edge(\"remove_failed_tool_call_attempt\", \"fallback_agent\")\n",
+5 -4
View File
@@ -311,15 +311,15 @@
"source": [
"from typing import Literal\n",
"\n",
"from langgraph.graph import StateGraph, MessagesState\n",
"from langgraph.graph import StateGraph, MessagesState, START, END\n",
"\n",
"\n",
"def should_continue(state: MessagesState) -> Literal[\"tools\", \"__end__\"]:\n",
"def should_continue(state: MessagesState):\n",
" messages = state[\"messages\"]\n",
" last_message = messages[-1]\n",
" if last_message.tool_calls:\n",
" return \"tools\"\n",
" return \"__end__\"\n",
" return END\n",
"\n",
"\n",
"def call_model(state: MessagesState):\n",
@@ -334,10 +334,11 @@
"workflow.add_node(\"agent\", call_model)\n",
"workflow.add_node(\"tools\", tool_node)\n",
"\n",
"workflow.add_edge(\"__start__\", \"agent\")\n",
"workflow.add_edge(START, \"agent\")\n",
"workflow.add_conditional_edges(\n",
" \"agent\",\n",
" should_continue,\n",
" ['tools',END]\n",
")\n",
"workflow.add_edge(\"tools\", \"agent\")\n",
"\n",