"By default, state in a graph is scoped to that thread.\n",
"LangGraph also allows you to specify a \"scope\" for a given key/value pair that exists between threads. This can be useful for storing information that is shared between threads. For instance, you may want to store information about a user's preferences expressed in one thread, and then use that information in another thread.\n",
"By default, state is scoped to a single thread. LangGraph also lets you customize the scope for a given key-value pair. You can use this to share information between threads.\n",
"\n",
"In this notebook we will go through an example of how to construct and use such a graph.\n",
"For instance, you can persist each user’s preferences to shared state and reuse them in new conversational threads.\n",
"\n",
"In this notebook, we will show how to construct and use such a graph.\n",
"\n",
"## Setup\n",
"\n",
@@ -19,7 +20,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"id": "3457aadf",
"metadata": {},
"outputs": [],
@@ -30,7 +31,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "aa2c64a7",
"metadata": {},
"outputs": [],
@@ -73,44 +74,44 @@
"<div class=\"admonition note\">\n",
" <p class=\"admonition-title\">Typing shared state keys</p>\n",
" <p style=\"margin-top: 5px;\">\n",
" Shared state channels (keys) MUST be dictionaries (see <code>info</code> channel in the AgentState example below)\n",
" Shared state channels (keys) MUST be dictionaries (see <code>info</code> channel in the State example below)\n",
"# Let's continue the conversation (by passing the same config) and tell the AI we like pepperoni pizza\n",
"for update in graph.stream({\"messages\": [{\"role\": \"user\", \"content\": \"i like pepperoni pizza\"}]}, config, stream_mode=\"updates\"):\n",
"for update in graph.stream(\n",
" {\"messages\": [{\"role\": \"user\", \"content\": \"i like pepperoni pizza\"}]},\n",
" config,\n",
" stream_mode=\"updates\",\n",
"):\n",
" print(update)\n",
"\n",
"# Let's continue the conversation even further (by passing the same config) and tell the AI we live in SF\n",
"for update in graph.stream({\"messages\": [{\"role\": \"user\", \"content\": \"i also just moved to SF\"}]}, config, stream_mode=\"updates\"):\n",
"for update in graph.stream(\n",
" {\"messages\": [{\"role\": \"user\", \"content\": \"i also just moved to SF\"}]},\n",
" config,\n",
" stream_mode=\"updates\",\n",
"):\n",
" print(update)"
]
},
@@ -247,7 +266,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 7,
"id": "e240f025-ff8b-4d17-beb7-2420c0575dd9",
"metadata": {},
"outputs": [
@@ -255,14 +274,25 @@
"name": "stdout",
"output_type": "stream",
"text": [
"{'call_model': {'messages': [AIMessage(content=\"Sure! Since you just moved to San Francisco, how about trying some popular local spots? Here are a few restaurant recommendations in SF:\\n\\n1. Tony's Pizza Napoletana - Known for their delicious pepperoni pizza!\\n2. The Slanted Door - A popular Vietnamese restaurant in the city.\\n3. Zuni Cafe - A classic American restaurant with a great ambiance.\\n4. Tartine Bakery - Perfect for a casual dinner with amazing baked goods.\\n5. State Bird Provisions - A unique dining experience with small plates and a lively atmosphere.\\n\\nFeel free to explore these options and enjoy your dinner! If you need more recommendations or information about a specific cuisine, let me know!\", additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 138, 'prompt_tokens': 197, 'total_tokens': 335}, 'model_name': 'gpt-3.5-turbo-0125', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-de8ad08c-0810-4bb5-b2e8-d3dc89522f8e-0', usage_metadata={'input_tokens': 197, 'output_tokens': 138, 'total_tokens': 335})]}}\n"
"{'call_model': {'messages': [AIMessage(content=\"I can help with that! Since you just moved to San Francisco, how about trying some local favorites? Here are a few restaurants you might enjoy:\\n\\n1. Tony's Pizza Napoletana - Known for their delicious pepperoni pizza.\\n2. The House - Offers a mix of Asian fusion dishes.\\n3. Tadich Grill - A historic seafood restaurant with a cozy atmosphere.\\n4. Zuni Cafe - Famous for its roast chicken and innovative cuisine.\\n5. La Taqueria - A popular spot for authentic Mexican tacos.\\n\\nFeel free to explore these options and let me know if you'd like more recommendations or information about any specific cuisine!\", additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 131, 'prompt_tokens': 206, 'total_tokens': 337, 'completion_tokens_details': {'reasoning_tokens': 0}}, 'model_name': 'gpt-3.5-turbo-0125', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-8530de74-bc07-4b31-b58d-4f4c064918b6-0', usage_metadata={'input_tokens': 206, 'output_tokens': 131, 'total_tokens': 337})]}}\n"
"for update in graph.stream({\"messages\": [{\"role\": \"user\", \"content\": \"where and what should i eat for dinner? Can you list some restaurants?\"}]}, config, stream_mode=\"updates\"):\n",
"for update in graph.stream(\n",
" {\n",
" \"messages\": [\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": \"where and what should i eat for dinner? Can you list some restaurants?\",\n",
" }\n",
" ]\n",
" },\n",
" config,\n",
" stream_mode=\"updates\",\n",
"):\n",
" print(update)"
]
},
@@ -280,7 +310,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 8,
"id": "f9bf2c15",
"metadata": {},
"outputs": [
@@ -288,14 +318,25 @@
"name": "stdout",
"output_type": "stream",
"text": [
"{'call_model': {'messages': [AIMessage(content='I can definitely help you with that! To provide you with personalized restaurant recommendations, could you please let me know your location or any specific preferences you have for dinner?', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 34, 'prompt_tokens': 185, 'total_tokens': 219}, 'model_name': 'gpt-3.5-turbo-0125', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-5a483acf-1289-4d7f-b707-97760a8c3620-0', usage_metadata={'input_tokens': 185, 'output_tokens': 34, 'total_tokens': 219})]}}\n"
"{'call_model': {'messages': [AIMessage(content='I can help you with that! Could you please provide me with your location or a preferred cuisine for dinner?', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 23, 'prompt_tokens': 195, 'total_tokens': 218, 'completion_tokens_details': {'reasoning_tokens': 0}}, 'model_name': 'gpt-3.5-turbo-0125', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-3f7eaa92-d3f0-4cca-ab13-0ecd7b922b9a-0', usage_metadata={'input_tokens': 195, 'output_tokens': 23, 'total_tokens': 218})]}}\n"
"for update in graph.stream({\"messages\": [{\"role\": \"user\", \"content\": \"where and what should i eat for dinner? Can you list some restaurants?\"}]}, config, stream_mode=\"updates\"):\n",
"for update in graph.stream(\n",
" {\n",
" \"messages\": [\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": \"where and what should i eat for dinner? Can you list some restaurants?\",\n",
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.