Files
langgraph/examples/cloud_examples/rollback_concurrent.ipynb
T

160 lines
5.3 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Rollback\n",
"\n",
"There are several strategies for handling concurrent runs in your graph. This notebook covers how to use the `rollback` option - please see the other how-to guides in the \"Double Texting\" directory to learn about the other methods.\n",
"\n",
"First, let's import our required packages and instantiate our client, assistant, and thread."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import asyncio\n",
"\n",
"import httpx\n",
"from langchain_core.messages import convert_to_messages\n",
"from langgraph_sdk import get_client"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"client = get_client()\n",
"assistant_id = \"agent\"\n",
"thread = await client.threads.create()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# the first run will be interrupted\n",
"rolled_back_run = await client.runs.create(\n",
" thread[\"thread_id\"],\n",
" assistant_id,\n",
" input={\"messages\": [{\"role\": \"human\", \"content\": \"what's the weather in sf?\"}]},\n",
")\n",
"await asyncio.sleep(2)\n",
"run = await client.runs.create(\n",
" thread[\"thread_id\"],\n",
" assistant_id,\n",
" input={\"messages\": [{\"role\": \"human\", \"content\": \"what's the weather in nyc?\"}]},\n",
" multitask_strategy=\"rollback\",\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# wait until the second run completes\n",
"await client.runs.join(thread[\"thread_id\"], run[\"run_id\"])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can see that the thread has data only from the second run"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"state = await client.threads.get_state(thread[\"thread_id\"])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"================================\u001b[1m Human Message \u001b[0m=================================\n",
"\n",
"what's the weather in nyc?\n",
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
"\n",
"[{'id': 'toolu_01JzPqefao1gxwajHQ3Yh3JD', 'input': {'query': 'weather in nyc'}, 'name': 'tavily_search_results_json', 'type': 'tool_use'}]\n",
"Tool Calls:\n",
" tavily_search_results_json (toolu_01JzPqefao1gxwajHQ3Yh3JD)\n",
" Call ID: toolu_01JzPqefao1gxwajHQ3Yh3JD\n",
" Args:\n",
" query: weather in nyc\n",
"=================================\u001b[1m Tool Message \u001b[0m=================================\n",
"Name: tavily_search_results_json\n",
"\n",
"[{\"url\": \"https://www.weatherapi.com/\", \"content\": \"{'location': {'name': 'New York', 'region': 'New York', 'country': 'United States of America', 'lat': 40.71, 'lon': -74.01, 'tz_id': 'America/New_York', 'localtime_epoch': 1718734479, 'localtime': '2024-06-18 14:14'}, 'current': {'last_updated_epoch': 1718733600, 'last_updated': '2024-06-18 14:00', 'temp_c': 29.4, 'temp_f': 84.9, 'is_day': 1, 'condition': {'text': 'Sunny', 'icon': '//cdn.weatherapi.com/weather/64x64/day/113.png', 'code': 1000}, 'wind_mph': 2.2, 'wind_kph': 3.6, 'wind_degree': 158, 'wind_dir': 'SSE', 'pressure_mb': 1025.0, 'pressure_in': 30.26, 'precip_mm': 0.0, 'precip_in': 0.0, 'humidity': 63, 'cloud': 0, 'feelslike_c': 31.3, 'feelslike_f': 88.3, 'windchill_c': 28.3, 'windchill_f': 82.9, 'heatindex_c': 29.6, 'heatindex_f': 85.3, 'dewpoint_c': 18.4, 'dewpoint_f': 65.2, 'vis_km': 16.0, 'vis_miles': 9.0, 'uv': 7.0, 'gust_mph': 16.5, 'gust_kph': 26.5}}\"}]\n",
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
"\n",
"The weather API results show that the current weather in New York City is sunny with a temperature of around 85°F (29°C). The wind is light at around 2-3 mph from the south-southeast. Overall it looks like a nice sunny summer day in NYC.\n"
]
}
],
"source": [
"for m in convert_to_messages(state[\"values\"][\"messages\"]):\n",
" m.pretty_print()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Verify that the original, rolled back run was deleted"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Original run was correctly deleted\n"
]
}
],
"source": [
"try:\n",
" await client.runs.get(thread[\"thread_id\"], rolled_back_run[\"run_id\"])\n",
"except httpx.HTTPStatusError as _:\n",
" print(\"Original run was correctly deleted\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "",
"name": ""
},
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 4
}