[Docs] use END instead of set_finish_point (#903)

This commit is contained in:
William FH
2024-07-01 21:56:10 -07:00
committed by GitHub
parent 727e63c01e
commit 320a87e1b9
31 changed files with 4189 additions and 389 deletions
+15 -5
View File
@@ -20,9 +20,10 @@
"outputs": [],
"source": [
"from langgraph_sdk import get_client\n",
"\n",
"client = get_client()\n",
"assistants = await client.assistants.search()\n",
"assistants = [a for a in assistants if not a['config']]\n",
"assistants = [a for a in assistants if not a[\"config\"]]\n",
"assistant = assistants[0]\n",
"thread = await client.threads.create()"
]
@@ -43,7 +44,12 @@
"outputs": [],
"source": [
"# This schedules a job to run at 15:27 (3:27PM) every day\n",
"cron_1 = await client.crons.create_for_thread(thread['thread_id'],assistant['assistant_id'],schedule=\"27 15 * * *\",input={'messages':[{\"role\":\"user\",\"content\":\"What time is it?\"}]})"
"cron_1 = await client.crons.create_for_thread(\n",
" thread[\"thread_id\"],\n",
" assistant[\"assistant_id\"],\n",
" schedule=\"27 15 * * *\",\n",
" input={\"messages\": [{\"role\": \"user\", \"content\": \"What time is it?\"}]},\n",
")"
]
},
{
@@ -59,7 +65,7 @@
"metadata": {},
"outputs": [],
"source": [
"await client.crons.delete(cron_1['cron_id'])"
"await client.crons.delete(cron_1[\"cron_id\"])"
]
},
{
@@ -78,7 +84,11 @@
"outputs": [],
"source": [
"# This schedules a job to run at 15:27 (3:27PM) every day\n",
"cron_2 = await client.crons.create(assistant['assistant_id'],schedule=\"27 15 * * *\",input={'messages':[{\"role\":\"user\",\"content\":\"What time is it?\"}]})"
"cron_2 = await client.crons.create(\n",
" assistant[\"assistant_id\"],\n",
" schedule=\"27 15 * * *\",\n",
" input={\"messages\": [{\"role\": \"user\", \"content\": \"What time is it?\"}]},\n",
")"
]
},
{
@@ -94,7 +104,7 @@
"metadata": {},
"outputs": [],
"source": [
"await client.crons.delete(cron_2['cron_id'])"
"await client.crons.delete(cron_2[\"cron_id\"])"
]
}
],
@@ -35,11 +35,12 @@
"outputs": [],
"source": [
"from langgraph_sdk import get_client\n",
"\n",
"client = get_client()\n",
"assistants = await client.assistants.search()\n",
"assistants = [a for a in assistants if not a['config']]\n",
"assistants = [a for a in assistants if not a[\"config\"]]\n",
"assistant = assistants[0]\n",
"assistant_id = assistant['assistant_id']\n",
"assistant_id = assistant[\"assistant_id\"]\n",
"thread = await client.threads.create()"
]
},
@@ -26,9 +26,10 @@
"outputs": [],
"source": [
"from langgraph_sdk import get_client\n",
"\n",
"client = get_client()\n",
"assistants = await client.assistants.search()\n",
"assistants = [a for a in assistants if not a['config']]\n",
"assistants = [a for a in assistants if not a[\"config\"]]\n",
"assistant = assistants[0]\n",
"thread = await client.threads.create()"
]
@@ -58,11 +59,11 @@
}
],
"source": [
"input = {'messages':[{\"role\":\"user\",\"content\":\"search for weather in SF\"}]}\n",
"input = {\"messages\": [{\"role\": \"user\", \"content\": \"search for weather in SF\"}]}\n",
"\n",
"async for chunk in client.runs.stream(\n",
" thread[\"thread_id\"],\n",
" assistant[\"assistant_id\"], # graph_id\n",
" assistant[\"assistant_id\"], # graph_id\n",
" input=input,\n",
" stream_mode=\"updates\",\n",
" interrupt_before=[\"action\"],\n",
@@ -99,14 +100,14 @@
],
"source": [
"# First, lets get the current state\n",
"current_state = await client.threads.get_state(thread['thread_id'])\n",
"current_state = await client.threads.get_state(thread[\"thread_id\"])\n",
"\n",
"# Let's now get the last message in the state\n",
"# This is the one with the tool calls that we want to update\n",
"last_message = current_state['values']['messages'][-1]\n",
"last_message = current_state[\"values\"][\"messages\"][-1]\n",
"\n",
"# Let's now update the args for that tool call\n",
"last_message['tool_calls'][0]['args'] = {'query': 'current weather in Sidi Frej'}\n",
"last_message[\"tool_calls\"][0][\"args\"] = {\"query\": \"current weather in Sidi Frej\"}\n",
"\n",
"# Let's now call `update_state` to pass in this message in the `messages` key\n",
"# This will get treated as any other update to the state\n",
@@ -114,7 +115,7 @@
"# That reducer function will use the ID of the message to update it\n",
"# It's important that it has the right ID! Otherwise it would get appended\n",
"# as a new message\n",
"await client.threads.update_state(thread['thread_id'], {\"messages\": last_message})"
"await client.threads.update_state(thread[\"thread_id\"], {\"messages\": last_message})"
]
},
{
@@ -145,7 +146,7 @@
"source": [
"async for chunk in client.runs.stream(\n",
" thread[\"thread_id\"],\n",
" assistant[\"assistant_id\"], # graph_id\n",
" assistant[\"assistant_id\"], # graph_id\n",
" input=None,\n",
" stream_mode=\"updates\",\n",
"):\n",
@@ -24,9 +24,10 @@
"outputs": [],
"source": [
"from langgraph_sdk import get_client\n",
"\n",
"client = get_client()\n",
"assistants = await client.assistants.search()\n",
"assistants = [a for a in assistants if not a['config']]\n",
"assistants = [a for a in assistants if not a[\"config\"]]\n",
"assistant = assistants[0]\n",
"thread = await client.threads.create()"
]
@@ -58,11 +59,11 @@
}
],
"source": [
"input = {'messages':[{\"role\":\"user\",\"content\":\"Please search the weather in SF\"}]}\n",
"input = {\"messages\": [{\"role\": \"user\", \"content\": \"Please search the weather in SF\"}]}\n",
"\n",
"async for chunk in client.runs.stream(\n",
" thread[\"thread_id\"],\n",
" assistant[\"assistant_id\"], # graph_id\n",
" assistant[\"assistant_id\"], # graph_id\n",
" input=input,\n",
" stream_mode=\"updates\",\n",
"):\n",
@@ -83,7 +84,7 @@
"metadata": {},
"outputs": [],
"source": [
"states = await client.threads.get_history(thread['thread_id'])"
"states = await client.threads.get_history(thread[\"thread_id\"])"
]
},
{
@@ -105,7 +106,7 @@
"source": [
"# We can confirm that this state is correct by checking the 'next' attribute and seeing that it is the tool call node\n",
"state_to_replay = states[2]\n",
"state_to_replay['next']"
"state_to_replay[\"next\"]"
]
},
{
@@ -132,10 +133,10 @@
"source": [
"async for chunk in client.runs.stream(\n",
" thread[\"thread_id\"],\n",
" assistant[\"assistant_id\"], # graph_id\n",
" assistant[\"assistant_id\"], # graph_id\n",
" input=None,\n",
" stream_mode=\"updates\",\n",
" config={\"configurable\":{\"thread_ts\":state_to_replay['checkpoint_id']}}\n",
" config={\"configurable\": {\"thread_ts\": state_to_replay[\"checkpoint_id\"]}},\n",
"):\n",
" if chunk.data and \"run_id\" not in chunk.data:\n",
" print(chunk.data)"
@@ -162,12 +163,16 @@
"source": [
"# Let's now get the last message in the state\n",
"# This is the one with the tool calls that we want to update\n",
"last_message = state_to_replay['values']['messages'][-1]\n",
"last_message = state_to_replay[\"values\"][\"messages\"][-1]\n",
"\n",
"# Let's now update the args for that tool call\n",
"last_message['tool_calls'][0]['args'] = {'query': 'current weather in SF'}\n",
"last_message[\"tool_calls\"][0][\"args\"] = {\"query\": \"current weather in SF\"}\n",
"\n",
"new_state = await client.threads.update_state(thread['thread_id'],{\"messages\":[last_message]},checkpoint_id=state_to_replay['checkpoint_id'])"
"new_state = await client.threads.update_state(\n",
" thread[\"thread_id\"],\n",
" {\"messages\": [last_message]},\n",
" checkpoint_id=state_to_replay[\"checkpoint_id\"],\n",
")"
]
},
{
@@ -194,10 +199,10 @@
"source": [
"async for chunk in client.runs.stream(\n",
" thread[\"thread_id\"],\n",
" assistant[\"assistant_id\"], # graph_id\n",
" assistant[\"assistant_id\"], # graph_id\n",
" input=None,\n",
" stream_mode=\"updates\",\n",
" config={\"configurable\":{\"thread_ts\":new_state['configurable']['thread_ts']}}\n",
" config={\"configurable\": {\"thread_ts\": new_state[\"configurable\"][\"thread_ts\"]}},\n",
"):\n",
" if chunk.data and \"run_id\" not in chunk.data:\n",
" print(chunk.data)"
@@ -44,9 +44,10 @@
"outputs": [],
"source": [
"from langgraph_sdk import get_client\n",
"\n",
"client = get_client()\n",
"assistants = await client.assistants.search()\n",
"assistants = [a for a in assistants if not a['config']]\n",
"assistants = [a for a in assistants if not a[\"config\"]]\n",
"assistant = assistants[0]\n",
"thread = await client.threads.create()"
]
@@ -76,11 +77,18 @@
}
],
"source": [
"input = {'messages':[{\"role\":\"user\",\"content\":\"Use the search tool to ask the user where they are, then look up the weather there\"}]}\n",
"input = {\n",
" \"messages\": [\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": \"Use the search tool to ask the user where they are, then look up the weather there\",\n",
" }\n",
" ]\n",
"}\n",
"\n",
"async for chunk in client.runs.stream(\n",
" thread[\"thread_id\"],\n",
" assistant[\"assistant_id\"], # graph_id\n",
" assistant[\"assistant_id\"], # graph_id\n",
" input=input,\n",
" stream_mode=\"updates\",\n",
" interrupt_before=[\"ask_human\"],\n",
@@ -118,13 +126,17 @@
}
],
"source": [
"state = await client.threads.get_state(thread['thread_id'])\n",
"tool_call_id = state['values']['messages'][-1]['tool_calls'][0]['id']\n",
"state = await client.threads.get_state(thread[\"thread_id\"])\n",
"tool_call_id = state[\"values\"][\"messages\"][-1][\"tool_calls\"][0][\"id\"]\n",
"\n",
"# We now create the tool call with the id and the response we want\n",
"tool_message = [{\"tool_call_id\": tool_call_id, \"type\": \"tool\", \"content\": \"san francisco\"}]\n",
"tool_message = [\n",
" {\"tool_call_id\": tool_call_id, \"type\": \"tool\", \"content\": \"san francisco\"}\n",
"]\n",
"\n",
"await client.threads.update_state(thread['thread_id'], {\"messages\": tool_message}, as_node=\"ask_human\")\n"
"await client.threads.update_state(\n",
" thread[\"thread_id\"], {\"messages\": tool_message}, as_node=\"ask_human\"\n",
")"
]
},
{
@@ -154,7 +166,7 @@
"source": [
"async for chunk in client.runs.stream(\n",
" thread[\"thread_id\"],\n",
" assistant[\"assistant_id\"], # graph_id\n",
" assistant[\"assistant_id\"], # graph_id\n",
" input=None,\n",
" stream_mode=\"updates\",\n",
"):\n",
@@ -119,6 +119,7 @@
"outputs": [],
"source": [
"from langgraph_sdk import get_client\n",
"\n",
"client = get_client()"
]
},
@@ -354,7 +355,9 @@
],
"source": [
"inputs = {\"messages\": [(\"human\", \"what's the weather in sf\")]}\n",
"async for chunk in client.runs.stream(None, \"agent\", input=inputs, stream_mode=\"values\"):\n",
"async for chunk in client.runs.stream(\n",
" None, \"agent\", input=inputs, stream_mode=\"values\"\n",
"):\n",
" if chunk.event == \"values\":\n",
" messages = convert_to_messages(chunk.data[\"messages\"])\n",
" messages[-1].pretty_print()"
@@ -423,7 +426,9 @@
],
"source": [
"inputs = {\"messages\": [(\"human\", \"what's the weather in nyc\")]}\n",
"invoke_output = await graph_with_memory.ainvoke(inputs, config={\"configurable\": {\"thread_id\": \"1\"}})\n",
"invoke_output = await graph_with_memory.ainvoke(\n",
" inputs, config={\"configurable\": {\"thread_id\": \"1\"}}\n",
")\n",
"invoke_output[\"messages\"][-1].pretty_print()"
]
},
@@ -458,7 +463,9 @@
],
"source": [
"inputs = {\"messages\": [(\"human\", \"what's it known for?\")]}\n",
"invoke_output = await graph_with_memory.ainvoke(inputs, config={\"configurable\": {\"thread_id\": \"1\"}})\n",
"invoke_output = await graph_with_memory.ainvoke(\n",
" inputs, config={\"configurable\": {\"thread_id\": \"1\"}}\n",
")\n",
"invoke_output[\"messages\"][-1].pretty_print()"
]
},
@@ -480,7 +487,9 @@
],
"source": [
"inputs = {\"messages\": [(\"human\", \"what's it known for?\")]}\n",
"invoke_output = await graph_with_memory.ainvoke(inputs, config={\"configurable\": {\"thread_id\": \"2\"}})\n",
"invoke_output = await graph_with_memory.ainvoke(\n",
" inputs, config={\"configurable\": {\"thread_id\": \"2\"}}\n",
")\n",
"invoke_output[\"messages\"][-1].pretty_print()"
]
},
@@ -724,7 +733,12 @@
],
"source": [
"inputs = {\"messages\": [(\"human\", \"what's the weather in sf\")]}\n",
"async for chunk in graph_with_memory.astream(inputs, stream_mode=\"values\", interrupt_before=[\"tools\"], config={\"configurable\": {\"thread_id\": \"3\"}}):\n",
"async for chunk in graph_with_memory.astream(\n",
" inputs,\n",
" stream_mode=\"values\",\n",
" interrupt_before=[\"tools\"],\n",
" config={\"configurable\": {\"thread_id\": \"3\"}},\n",
"):\n",
" chunk[\"messages\"][-1].pretty_print()"
]
},
@@ -749,7 +763,12 @@
}
],
"source": [
"async for chunk in graph_with_memory.astream(None, stream_mode=\"values\", interrupt_before=[\"tools\"], config={\"configurable\": {\"thread_id\": \"3\"}}):\n",
"async for chunk in graph_with_memory.astream(\n",
" None,\n",
" stream_mode=\"values\",\n",
" interrupt_before=[\"tools\"],\n",
" config={\"configurable\": {\"thread_id\": \"3\"}},\n",
"):\n",
" chunk[\"messages\"][-1].pretty_print()"
]
},
@@ -794,7 +813,13 @@
"source": [
"thread = await client.threads.create()\n",
"\n",
"async for chunk in client.runs.stream(thread[\"thread_id\"], \"agent\", input=inputs, stream_mode=\"values\", interrupt_before=[\"tools\"]):\n",
"async for chunk in client.runs.stream(\n",
" thread[\"thread_id\"],\n",
" \"agent\",\n",
" input=inputs,\n",
" stream_mode=\"values\",\n",
" interrupt_before=[\"tools\"],\n",
"):\n",
" if chunk.event == \"values\":\n",
" messages = convert_to_messages(chunk.data[\"messages\"])\n",
" messages[-1].pretty_print()"
@@ -821,7 +846,13 @@
}
],
"source": [
"async for chunk in client.runs.stream(thread[\"thread_id\"], \"agent\", input=None, stream_mode=\"values\", interrupt_before=[\"tools\"]):\n",
"async for chunk in client.runs.stream(\n",
" thread[\"thread_id\"],\n",
" \"agent\",\n",
" input=None,\n",
" stream_mode=\"values\",\n",
" interrupt_before=[\"tools\"],\n",
"):\n",
" if chunk.event == \"values\":\n",
" messages = convert_to_messages(chunk.data[\"messages\"])\n",
" messages[-1].pretty_print()"
@@ -1000,7 +1031,9 @@
],
"source": [
"inputs = {\"messages\": [(\"human\", \"what's the weather in sf\")]}\n",
"async for chunk in client.runs.stream(None, \"agent\", input=inputs, stream_mode=\"events\"):\n",
"async for chunk in client.runs.stream(\n",
" None, \"agent\", input=inputs, stream_mode=\"events\"\n",
"):\n",
" if chunk.event == \"events\" and chunk.data[\"event\"] == \"on_chat_model_stream\":\n",
" print(chunk.data[\"data\"][\"chunk\"])"
]
+9 -4
View File
@@ -20,9 +20,10 @@
"outputs": [],
"source": [
"from langgraph_sdk import get_client\n",
"\n",
"client = get_client()\n",
"assistants = await client.assistants.search()\n",
"assistants = [a for a in assistants if not a['config']]\n",
"assistants = [a for a in assistants if not a[\"config\"]]\n",
"assistant = assistants[0]\n",
"thread = await client.threads.create()"
]
@@ -50,13 +51,17 @@
}
],
"source": [
"input = {\"messages\":[{\"role\": \"user\", \"content\": \"Hello! My name is Bagatur and I am 26 years old.\"}]}\n",
"input = {\n",
" \"messages\": [\n",
" {\"role\": \"user\", \"content\": \"Hello! My name is Bagatur and I am 26 years old.\"}\n",
" ]\n",
"}\n",
"\n",
"\n",
"async for chunk in client.runs.stream(\n",
" # Don't pass in a thread_id and the stream will be stateless\n",
" None,\n",
" assistant[\"assistant_id\"], # graph_id\n",
" assistant[\"assistant_id\"], # graph_id\n",
" input=input,\n",
" stream_mode=\"updates\",\n",
"):\n",
@@ -81,7 +86,7 @@
"source": [
"stateless_run_result = await client.runs.wait(\n",
" None,\n",
" assistant[\"assistant_id\"], # graph_id\n",
" assistant[\"assistant_id\"], # graph_id\n",
" input=input,\n",
")"
]