diff --git a/docs/docs/cloud/how-tos/index.md b/docs/docs/cloud/how-tos/index.md index be24adf34..d64d541df 100644 --- a/docs/docs/cloud/how-tos/index.md +++ b/docs/docs/cloud/how-tos/index.md @@ -46,10 +46,25 @@ When creating complex graphs, leaving every decision up to the LLM can be danger ## LangGraph Studio -- [Test Cloud Deployment](https://langchain-ai.github.io/langgraph/cloud/how-tos/test_deployment/) -- [Invoke graph in LangGraph Studio](https://langchain-ai.github.io/langgraph/cloud/how-tos/invoke_studio/) +LangGraph Studio is a built-in UI for visualizing, testing, and debugging your agents. + +- [How to enter LangGraph Studio](https://langchain-ai.github.io/langgraph/cloud/how-tos/test_deployment/) +- [How to test your graph in LangGraph Studio](https://langchain-ai.github.io/langgraph/cloud/how-tos/invoke_studio/) - [Interact with threads in LangGraph Studio](https://langchain-ai.github.io/langgraph/cloud/how-tos/threads_studio/) -## And more! +## Different Types of Runs: -The four sections above don't cover everything that is possible with LangGraph cloud - make sure to check out our other how-to guides to learn even more! \ No newline at end of file +LangGraph Cloud supports multiple types of runs besides streaming runs. + +- [How to run an agent in the background](cloud_examples/background_run.ipynb) +- [How to run multiple agents in the same thread](cloud_examples/same-thread.ipynb) +- [How to create cron jobs](cloud_examples/cron_jobs.ipynb) +- [How to create stateless runs](cloud_examples/stateless_runs.ipynb) + +## Other + +Other guides that may prove helpful! + +- [How to configure agents](cloud_examples/configuration_cloud.ipynb) +- [How to convert LangGraph calls to LangGraph cloud calls](cloud_examples/langgraph_to_langgraph_cloud.ipynb) +- [How to integrate webhooks](cloud_examples/webhooks.ipynb) diff --git a/docs/docs/cloud/quick_start.md b/docs/docs/cloud/quick_start.md index 7807e1381..5cbd5245a 100644 --- a/docs/docs/cloud/quick_start.md +++ b/docs/docs/cloud/quick_start.md @@ -23,15 +23,15 @@ This tutorial will use: 2. The `agent.py` file should contain Python code for defining your graph. The following code is a simple example, the important thing is that at some point in your file you compile your graph and assign the compiled graph to a variable (in this case the `graph` variable). This example code uses `create_react_agent`, a prebuilt agent, read more about it [here](..//concepts/agentic_concepts.md#react-agent). ```python - from langchain_anthropic import ChatAnthropic - from langchain_community.tools.tavily_search import TavilySearchResults - from langgraph.prebuilt import create_react_agent + from langchain_anthropic import ChatAnthropic + from langchain_community.tools.tavily_search import TavilySearchResults + from langgraph.prebuilt import create_react_agent - model = ChatAnthropic(model="claude-3-5-sonnet-20240620") + model = ChatAnthropic(model="claude-3-5-sonnet-20240620") - tools = [TavilySearchResults(max_results=2)] + tools = [TavilySearchResults(max_results=2)] - graph = create_react_agent(model, tools) + graph = create_react_agent(model, tools) ``` 3. The `requirements.txt` file should contain any dependencies for your graph(s). In this case we only require four packages for our graph to run: @@ -231,8 +231,6 @@ async for chunk in client.runs.stream( {'agent': {'messages': [{'content': "Hi Bagatur! It's nice to meet you. How can I assist you today?", 'additional_kwargs': {}, 'response_metadata': {'finish_reason': 'stop', 'model_name': 'gpt-4o-2024-05-13', 'system_fingerprint': 'fp_9cb5d38cf7'}, 'type': 'ai', 'name': None, 'id': 'run-c89118b7-1b1e-42b9-a85d-c43fe99881cd', 'example': False, 'tool_calls': [], 'invalid_tool_calls': [], 'usage_metadata': None}]}} -You can learn more about the Python SDK in [this how-to guide](./sdk/python_sdk.ipynb), and read up on the Javascript SDK in [this how-to guide](./sdk/js_sdk.ipynb) - ## What's Next Congratulations! If you've worked your way through this tutorial you are well on your way to becoming a LangGraph Cloud expert. Here are some other resources to check out to help you out on the path to expertise: diff --git a/docs/docs/concepts/index.md b/docs/docs/concepts/index.md index 671b53f0f..3acace645 100644 --- a/docs/docs/concepts/index.md +++ b/docs/docs/concepts/index.md @@ -14,28 +14,28 @@ LangGraph for Agentic Applications Low Level Concepts - [Graphs](low_level.md#graphs) - - [StateGraph](low_level.md#stategraph) - - [MessageGraph](low_level.md#messagegraph) - - [Compiling Your Graph](low_level.md#compiling-your-graph) + - [StateGraph](low_level.md#stategraph) + - [MessageGraph](low_level.md#messagegraph) + - [Compiling Your Graph](low_level.md#compiling-your-graph) - [State](low_level.md#state) - - [Schema](low_level.md#schema) - - [Reducers](low_level.md#reducers) - - [MessageState](low_level.md#messagestate) + - [Schema](low_level.md#schema) + - [Reducers](low_level.md#reducers) + - [MessageState](low_level.md#messagestate) - [Nodes](low_level.md#nodes) - - [`START` node](low_level.md#start-node) - - [`END` node](low_level.md#end-node) + - [`START` node](low_level.md#start-node) + - [`END` node](low_level.md#end-node) - [Edges](low_level.md#edges) - - [Normal Edges](low_level.md#normal-edges) - - [Conditional Edges](low_level.md#conditional-edges) - - [Entry Point](low_level.md#entry-point) - - [Conditional Entry Point](low_level.md#conditional-entry-point) + - [Normal Edges](low_level.md#normal-edges) + - [Conditional Edges](low_level.md#conditional-edges) + - [Entry Point](low_level.md#entry-point) + - [Conditional Entry Point](low_level.md#conditional-entry-point) - [Send](low_level.md#send) - [Checkpointer](low_level.md#checkpointer) - [Threads](low_level.md#threads) - [Checkpointer states](low_level.md#checkpointer-state) - - [Get state](low_level.md#get-state) - - [Get state history](low_level.md#get-state-history) - - [Update state](low_level.md#update-state) + - [Get state](low_level.md#get-state) + - [Get state history](low_level.md#get-state-history) + - [Update state](low_level.md#update-state) - [Configuration](low_level.md#configuration) - [Visualization](low_level.md#visualization) - [Streaming](low_level.md#streaming) @@ -46,10 +46,10 @@ Common Agentic Patterns - [Tool calling](agentic_concepts.md#tool-calling) - [Memory](agentic_concepts.md#memory) - [Human in the loop](agentic_concepts.md#human-in-the-loop) - - [Approval](agentic_concepts.md#approval) - - [Wait for input](agentic_concepts.md#wait-for-input) - - [Edit agent actions](agentic_concepts.md#edit-agent-actions) - - [Time travel](agentic_concepts.md#time-travel) + - [Approval](agentic_concepts.md#approval) + - [Wait for input](agentic_concepts.md#wait-for-input) + - [Edit agent actions](agentic_concepts.md#edit-agent-actions) + - [Time travel](agentic_concepts.md#time-travel) - [Map-Reduce](agentic_concepts.md#map-reduce) - [Multi-agent](agentic_concepts.md#multi-agent) - [Planning](agentic_concepts.md#planning) diff --git a/docs/docs/tutorials/index.md b/docs/docs/tutorials/index.md index a5aa052dc..3e414d592 100644 --- a/docs/docs/tutorials/index.md +++ b/docs/docs/tutorials/index.md @@ -7,11 +7,11 @@ hide: Welcome to the LangGraph Tutorials! These notebooks introduce LangGraph through building various language agents and applications. -## Introduction to LangGraph +## Quick Start -Learn the basics of LangGraph through the onboarding tutorials. +Learn the basics of LangGraph through a comprehensive quick start in which you will build an agent from scratch. -- [Introduction to LangGraph](introduction.ipynb) +- [Quick Start](introduction.ipynb) ## Use cases diff --git a/docs/mkdocs.yml b/docs/mkdocs.yml index 6af3a2296..7c4926427 100644 --- a/docs/mkdocs.yml +++ b/docs/mkdocs.yml @@ -85,7 +85,7 @@ nav: - "index.md" - Tutorials: - "tutorials/index.md" - - Introduction: tutorials/introduction.ipynb + - Quick Start: tutorials/introduction.ipynb - Chatbots: - Customer Support: tutorials/customer-support/customer-support.ipynb - Prompt Generation from User Requirements: tutorials/chatbots/information-gather-prompting.ipynb @@ -193,20 +193,19 @@ nav: - Wait for User Input: "cloud/how-tos/cloud_examples/human_in_the_loop_user_input.ipynb" - Edit Graph State: "cloud/how-tos/cloud_examples/human_in_the_loop_edit_state.ipynb" - Replay and Branch from Prior States: "cloud/how-tos/cloud_examples/human_in_the_loop_time_travel.ipynb" - - Test with LangGraph Studio: + - LangGraph Studio: - Test Cloud Deployment: "cloud/how-tos/test_deployment.md" - Invoke graph in LangGraph Studio: "cloud/how-tos/invoke_studio.md" - Interact with threads in LangGraph Studio: "cloud/how-tos/threads_studio.md" - - Run Agent in Background: "cloud/how-tos/cloud_examples/background_run.ipynb" - - Run Multiple Agents in Thread: "cloud/how-tos/cloud_examples/same-thread.ipynb" - - Create Agents with Configuration: "cloud/how-tos/cloud_examples/configuration_cloud.ipynb" - - Convert LangGraph calls to LangGraph Cloud calls: "cloud/how-tos/cloud_examples/langgraph_to_langgraph_cloud.ipynb" - - Create Cron Jobs: "cloud/how-tos/cloud_examples/cron_jobs.ipynb" - - Create Stateless Runs: "cloud/how-tos/cloud_examples/stateless_runs.ipynb" - - Integrate Webhooks: 'cloud/how-tos/cloud_examples/webhooks.ipynb' - - SDK: - - Python: "cloud/sdk/python_sdk.ipynb" - - JS/TS: "cloud/sdk/js_sdk.ipynb" + - Different Types of Runs: + - Run an Agent in the Background: "cloud/how-tos/cloud_examples/background_run.ipynb" + - Run Multiple Agents in Same Thread: "cloud/how-tos/cloud_examples/same-thread.ipynb" + - Create Cron Jobs: "cloud/how-tos/cloud_examples/cron_jobs.ipynb" + - Create Stateless Runs: "cloud/how-tos/cloud_examples/stateless_runs.ipynb" + - Other: + - Configure Agents: "cloud/how-tos/cloud_examples/configuration_cloud.ipynb" + - Convert LangGraph calls to LangGraph Cloud calls: "cloud/how-tos/cloud_examples/langgraph_to_langgraph_cloud.ipynb" + - Integrate Webhooks: 'cloud/how-tos/cloud_examples/webhooks.ipynb' - Conceptual Guides: "cloud/concepts/index.md" - Reference: - API: "cloud/reference/api/api_ref.md" diff --git a/examples/introduction.ipynb b/examples/introduction.ipynb index 7d2343512..3d20c0656 100644 --- a/examples/introduction.ipynb +++ b/examples/introduction.ipynb @@ -5,9 +5,9 @@ "id": "4a1aae78-88a6-4133-b905-7e46c8e3772f", "metadata": {}, "source": [ - "# Introduction to LangGraph\n", + "# Quick Start\n", "\n", - "In this tutorial, we will build a support chatbot in LangGraph that can:\n", + "In this comprehensive quick start, we will build a support chatbot in LangGraph that can:\n", "\n", "- Answer common questions by searching the web\n", "- Maintain conversation state across calls\n", @@ -343,16 +343,13 @@ "\n", "However, you may have noticed that the bot's knowledge is limited to what's in its training data. In the next part, we'll add a web search tool to expand the bot's knowledge and make it more capable.\n", "\n", - "Below is the full code for this section for your reference:" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "id": "e014741f-cfad-4045-b6fa-ebd74ae93a5a", - "metadata": {}, - "outputs": [], - "source": [ + "Below is the full code for this section for your reference:\n", + "\n", + "
\n", + "Full Code\n", + "
\n",
+    "        \n",
+    "```python\n",
     "from typing import Annotated\n",
     "\n",
     "from langchain_anthropic import ChatAnthropic\n",
@@ -382,7 +379,11 @@
     "graph_builder.add_node(\"chatbot\", chatbot)\n",
     "graph_builder.set_entry_point(\"chatbot\")\n",
     "graph_builder.set_finish_point(\"chatbot\")\n",
-    "graph = graph_builder.compile()"
+    "graph = graph_builder.compile()\n",
+    "```\n",
+    "\n",
+    "
\n", + "
" ] }, { @@ -766,16 +767,13 @@ "Our chatbot still can't remember past interactions on its own, limiting its ability to have coherent, multi-turn conversations. In the next part, we'll add **memory** to address this.\n", "\n", "\n", - "The full code for the graph we've created in this section is reproduced below, replacing our `BasicToolNode` for the prebuilt [ToolNode](https://langchain-ai.github.io/langgraph/reference/prebuilt/#toolnode), and our `route_tools` condition with the prebuilt [tools_condition](https://langchain-ai.github.io/langgraph/reference/prebuilt/#tools_condition)" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "id": "8755d551-160e-4f8f-afac-0e4e07ca79ff", - "metadata": {}, - "outputs": [], - "source": [ + "The full code for the graph we've created in this section is reproduced below, replacing our `BasicToolNode` for the prebuilt [ToolNode](https://langchain-ai.github.io/langgraph/reference/prebuilt/#toolnode), and our `route_tools` condition with the prebuilt [tools_condition](https://langchain-ai.github.io/langgraph/reference/prebuilt/#tools_condition)\n", + "\n", + "
\n", + "Full Code\n", + "
\n",
+    "\n",
+    "```python\n",
     "from typing import Annotated\n",
     "\n",
     "from langchain_anthropic import ChatAnthropic\n",
@@ -817,7 +815,11 @@
     "# Any time a tool is called, we return to the chatbot to decide the next step\n",
     "graph_builder.add_edge(\"tools\", \"chatbot\")\n",
     "graph_builder.set_entry_point(\"chatbot\")\n",
-    "graph = graph_builder.compile()"
+    "graph = graph_builder.compile()\n",
+    "```\n",
+    "\n",
+    "
\n", + "
" ] }, { @@ -1170,16 +1172,13 @@ "\n", "In the next part, we'll introduce human oversight to our bot to handle situations where it may need guidance or verification before proceeding.\n", " \n", - "Check out the code snippet below to review our graph from this section." - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "id": "9c50a794-3ae5-484c-8edd-50e0d54da982", - "metadata": {}, - "outputs": [], - "source": [ + "Check out the code snippet below to review our graph from this section.\n", + "\n", + "
\n", + "Full Code\n", + "
\n",
+    "\n",
+    "```python\n",
     "from typing import Annotated\n",
     "\n",
     "from langchain_anthropic import ChatAnthropic\n",
@@ -1221,7 +1220,10 @@
     ")\n",
     "graph_builder.add_edge(\"tools\", \"chatbot\")\n",
     "graph_builder.set_entry_point(\"chatbot\")\n",
-    "graph = graph_builder.compile(checkpointer=memory)"
+    "graph = graph_builder.compile(checkpointer=memory)\n",
+    "```\n",
+    "
\n", + "details" ] }, { @@ -1478,16 +1480,13 @@ "\n", "Next, we'll explore how to further customize the bot's behavior using custom state updates.\n", "\n", - "Below is a copy of the code you used in this section. The only difference between this and the previous parts is the addition of the `interrupt_before` argument." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "id": "a7228caf-a5aa-4f68-b775-81ea5402aca8", - "metadata": {}, - "outputs": [], - "source": [ + "Below is a copy of the code you used in this section. The only difference between this and the previous parts is the addition of the `interrupt_before` argument.\n", + "\n", + "
\n", + "Full Code\n", + "
\n",
+    "\n",
+    "```python\n",
     "from typing import Annotated\n",
     "\n",
     "from langchain_anthropic import ChatAnthropic\n",
@@ -1537,7 +1536,10 @@
     "    interrupt_before=[\"tools\"],\n",
     "    # Note: can also interrupt __after__ actions, if desired.\n",
     "    # interrupt_after=[\"tools\"]\n",
-    ")"
+    ")\n",
+    "```\n",
+    "
\n", + "
" ] }, { @@ -2508,16 +2510,13 @@ "\n", "We're almost done with the tutorial, but there is one more concept we'd like to review before finishing that connects `checkpointing` and `state updates`. \n", "\n", - "This section's code is reproduced below for your reference." - ] - }, - { - "cell_type": "code", - "execution_count": 26, - "id": "6516baf8-bbb6-4400-b867-0add1a087342", - "metadata": {}, - "outputs": [], - "source": [ + "This section's code is reproduced below for your reference.\n", + "\n", + "
\n", + "Full Code\n", + "
\n",
+    "\n",
+    "```python\n",
     "from typing import Annotated\n",
     "\n",
     "from langchain_anthropic import ChatAnthropic\n",
@@ -2617,7 +2616,10 @@
     "graph = graph_builder.compile(\n",
     "    checkpointer=memory,\n",
     "    interrupt_before=[\"human\"],\n",
-    ")"
+    ")\n",
+    "```\n",
+    "
\n", + "
" ] }, { @@ -3066,7 +3068,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.2" + "version": "3.11.1" } }, "nbformat": 4, diff --git a/examples/sdk/img/graph_diagram.png b/examples/sdk/img/graph_diagram.png deleted file mode 100644 index 2105cde69..000000000 Binary files a/examples/sdk/img/graph_diagram.png and /dev/null differ diff --git a/examples/sdk/img/thread_diagram.png b/examples/sdk/img/thread_diagram.png deleted file mode 100644 index ec938ac6f..000000000 Binary files a/examples/sdk/img/thread_diagram.png and /dev/null differ diff --git a/examples/sdk/js_sdk.ipynb b/examples/sdk/js_sdk.ipynb deleted file mode 100644 index 9331b89a6..000000000 --- a/examples/sdk/js_sdk.ipynb +++ /dev/null @@ -1,800 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "This how-to takes you through the steps of using the JS/TS SDK for interacting with deployed Langgraph Cloud APIs." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Initialization\n", - "\n", - "### Initializing client\n", - "\n", - "To get started we need to initialize our client. The process for initializing our client is almost identical for both the local deployment and cloud deployment using Langsmith." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "vscode": { - "languageId": "javascript" - } - }, - "outputs": [], - "source": [ - "import { Client } from \"@langchain/langgraph-sdk\";\n", - "\n", - "\n", - "\n", - "// If you deployed using Langsmith use this option\n", - "// Find this url on your Langsmith deployment page\n", - "const example_deployed_url = \"https://ht-unhealthy-buffalo25-39d00f953458585aa9f7b5a4fa-g3ps4aazkq-uc.a.run.app\";\n", - "\n", - "// If you deployed locally using langgraph up -c langgraph.json use this option\n", - "// This is the default URL, and you can just call get_client() to use it\n", - "const example_local_url = \"http://localhost:8123\";\n", - "\n", - "const client = new Client({apiUrl:\"whatever-your-url-is\"});" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Selecting an Assistant\n", - "\n", - "To select an assistant we can search the assistants that are hosted on our client, and then select the one we want:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "vscode": { - "languageId": "javascript" - } - }, - "outputs": [], - "source": [ - "// List all assistants\n", - "const assistants = await client.assistants.search({\n", - " metadata: null,\n", - " offset: 0,\n", - " limit: 10,\n", - "});\n", - "\n", - "// We auto-create an assistant for each graph you register in config.\n", - "const agent = assistants[0];" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In our example we are only hosting a single assistant, but you have the option to host many, in which case you will most likely want to do more filtering than just selecting the first one. Each assistant is a JSON object with the following format, allowing you to select based on a variety of parameters." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "vscode": { - "languageId": "javascript" - } - }, - "outputs": [], - "source": [ - "console.log(agent);" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "vscode": { - "languageId": "javascript" - } - }, - "outputs": [], - "source": [ - "{\n", - " assistant_id: 'fe096781-5601-53d2-b2f6-0d3403f7e9ca',\n", - " graph_id: 'agent',\n", - " created_at: '2024-06-11T20:12:45.862108+00:00',\n", - " updated_at: '2024-06-11T20:12:45.862108+00:00',\n", - " config: {},\n", - " metadata: { created_by: 'system' }\n", - "}" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Configuring an assistant\n", - "\n", - "One important thing to know is that graph can be defined to be configurable, meaning that not every instance of the graph needs to be the same (read up on [this guide](https://langchain-ai.github.io/langgraph/how-tos/configuration/) to learn more about how to create your own configurable graphs). Let's briefly show how we can configure an assistant. The first step to do is find the assistant we want to configure. In our simple example we are only hosting a single graph, so we must choose it as the graph to configure." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "vscode": { - "languageId": "javascript" - } - }, - "outputs": [], - "source": [ - "const base_assistant = assistants[0];" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "This assistant has one configurable argument called `model`, which can take on two values: `openai` or `anthropic`. In this case, I want to create a graph that originates from this assistant that uses the `openai` option, which we can do as follows:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "vscode": { - "languageId": "javascript" - } - }, - "outputs": [], - "source": [ - "const config_graph = await client.assistants.create({graphId: base_assistant['graph_id'],config: {'configurable':{'model':'openai'}}});\n", - "console.log(config_graph);" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "vscode": { - "languageId": "javascript" - } - }, - "outputs": [], - "source": [ - "{\n", - " assistant_id: '414a1ddd-4453-4500-ab0b-7bc4f5bb41b1',\n", - " graph_id: 'agent',\n", - " created_at: '2024-06-21T23:42:16.388341+00:00',\n", - " updated_at: '2024-06-21T23:42:16.388341+00:00',\n", - " config: { configurable: { model: 'openai' } },\n", - " metadata: {}\n", - "}" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Being able to create configurable assistants allows you to create different graphs all based on the same underlying structure. This can be very powerful for testing different configurations of a graph or allowing users to customize their graph." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Creating a thread\n", - "\n", - "Threads are what we will actually use to run our graphs (assistants). Each thread will update the same state for the graph, meaning we can run the graph multiple times while the state will persist. We can also look back at our thread history, add meta data to different steps of our thread, and update the thread state manually if we wish. We will dive into all of those topics later in this article, but for now let’s just see how to start a thread:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "vscode": { - "languageId": "javascript" - } - }, - "outputs": [], - "source": [ - "// Start a new thread\n", - "const thread = await client.threads.create();" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We can examine the structure of our thread, which similar to the assistants object provides us with some information about the thread itself, including its id, timestamps, and metadata:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "vscode": { - "languageId": "javascript" - } - }, - "outputs": [], - "source": [ - "console.log(thread);" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "vscode": { - "languageId": "javascript" - } - }, - "outputs": [], - "source": [ - "{\n", - " thread_id: '12a112dc-d175-42ce-8b06-85697733cccc',\n", - " created_at: '2024-06-20T22:01:18.715497+00:00',\n", - " updated_at: '2024-06-20T22:01:18.715497+00:00',\n", - " metadata: {}\n", - "}" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now we are ready to actually use our graph!\n", - "\n", - "## Invoking the graph\n", - "\n", - "The graph used in this example is a simple example of a StateGraph, but it allows us to show most of the API functionality. The state of our graph is defined as follows (Langgraph Cloud is coming in the future for JS!):" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "from typing import Annotated, TypedDict\n", - "\n", - "from langchain_core.messages import AnyMessage\n", - "\n", - "from langgraph.graph import add_messages\n", - "\n", - "\n", - "def update_user_info(old_info, new_info):\n", - " if \"name\" not in new_info or new_info[\"age\"] == -1:\n", - " return old_info\n", - " return new_info\n", - "\n", - "\n", - "class UserInformation(TypedDict):\n", - " age: int\n", - " name: str\n", - "\n", - "\n", - "class AgentState(TypedDict):\n", - " messages: Annotated[list[AnyMessage], add_messages]\n", - " user_info: Annotated[UserInformation, update_user_info]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "It is important to note that our member variables can be updated by using the `Annotated` class. This is especially important when we make API calls that will update our variables. This is also a good example that your graph can hold much more information than just messages. In our example we use a very simple `UserInformation` class, but you can imagine holding much richer information in your state.\n", - "\n", - "Our graph looks like follows:\n", - "\n", - "
\n", - " \n", - "
\n", - "\n", - "The workflow is as follows: first the user inputs some message, our llm decides how to configure the call to our tool `get_user_info` , and after getting the results of the tool call we respond to our user using another LLM.\n", - "\n", - "### Simple Invocation\n", - "\n", - "Ok, now that we have set up our client, assistant, and thread we can actually invoke the graph above. Let’s first define the function we will use to invoke the graph, since we don’t want to have to rewrite this code every single run." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "vscode": { - "languageId": "javascript" - } - }, - "outputs": [], - "source": [ - "async function processStreamedMessages(client, thread, agent, messages, metadata = {}) {\n", - " var answer = [];\n", - " try {\n", - " const streamResponse = client.runs.stream(\n", - " thread[\"thread_id\"],\n", - " agent[\"assistant_id\"],\n", - " {\n", - " input: { messages },\n", - " config: {\"configurable\": metadata}\n", - " }\n", - " );\n", - "\n", - " for await (const chunk of streamResponse) {\n", - " answer.concat(chunk);\n", - " // Process each chunk of streamed data here\n", - " }\n", - " return answer;\n", - " } catch (error) {\n", - " console.error('Error processing streamed messages:', error);\n", - " }\n", - "}" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let’s now see what happens to our graph when we run it with a simple sentence:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "vscode": { - "languageId": "javascript" - } - }, - "outputs": [], - "source": [ - "var messages = [{ role: \"human\", content: \"My name is Bagatur and I am 26 years old.\" }];\n", - "\n", - "await processStreamedMessages(client, thread, agent, messages);" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In order to see what happened to our graph, let's examine the state which was updated after the run we just sent through." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "vscode": { - "languageId": "javascript" - } - }, - "outputs": [], - "source": [ - "var state = await client.threads.getState(thread['thread_id']);\n", - "console.log(state);" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "vscode": { - "languageId": "javascript" - } - }, - "outputs": [], - "source": [ - "{\n", - " values: {\n", - " messages: [ [Object], [Object], [Object] ],\n", - " user_info: { age: 26, name: 'Bagatur' }\n", - " },\n", - " next: [],\n", - " config: {\n", - " configurable: {\n", - " thread_id: '15292d98-6aca-4df8-b2a1-f4508e11abb1',\n", - " thread_ts: '1ef2f516-ebd2-68f4-8003-2a1251f89da5'\n", - " }\n", - " },\n", - " metadata: {\n", - " step: 3,\n", - " run_id: '1ef2f516-e211-6e60-8d19-6a1309008ece',\n", - " source: 'loop',\n", - " writes: { respond_to_user: [Object] },\n", - " user_id: '',\n", - " graph_id: 'agent',\n", - " thread_id: '15292d98-6aca-4df8-b2a1-f4508e11abb1',\n", - " created_by: 'system',\n", - " assistant_id: 'fe096781-5601-53d2-b2f6-0d3403f7e9ca'\n", - " },\n", - " created_at: '2024-06-20T22:07:06.852149+00:00',\n", - " parent_config: {\n", - " configurable: {\n", - " thread_id: '15292d98-6aca-4df8-b2a1-f4508e11abb1',\n", - " thread_ts: '1ef2f516-e6c7-6698-8002-80a6bedfa731'\n", - " }\n", - " }\n", - " }" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Our state variable contains a variety of important information. Here is a quick summary of the keys and what they represent:\n", - "\n", - "- `values` contains the actual state values, so in our case you could call `state['values']['messages']` or `state['values']['user_info']` and get the actual values of each of the state variables.\n", - "- `next` tells us what action in the graph is next at the current state. Since we just finished running our graph and reached the end node, it is currently empty because there is no next action to take. However, if you go through the state at each point of the run you will see that the `next` value goes from `__start__` → `llm` → `get_user_info` →`respond_to_user` .\n", - "- `config` tells us what the configuration of the state is. This is important for when we want to run a query starting at a previous state instead of the one we are at. An example of this is shown in the Invoking from a previous checkpoint section\n", - "- `metadata` stores the metadata associated with our state. This is data that is outside of the agent state, but is important to keep track of across multiple runs. An example of this is shown in the next section.\n", - "- `created_at` is information on the date and time the state was created at.\n", - "- `parent_config` is config for the previous step of the graph. Note that the previous step is not the previous run, but rather the previous node that the graph was at.\n", - "\n", - "\n", - "### Invoking with Metadata\n", - "\n", - "Let’s create a new thread to reset our state and start fresh." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "vscode": { - "languageId": "javascript" - } - }, - "outputs": [], - "source": [ - "const thread_2 = await client.threads.create();" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now let’s add some metadata to our request. In this example we are going to treat each run of our assistant as a separate “node”. For each run, we will pass in a “node_id” as well as a “parent_node” in the metadata. This way we can easily go “back in time” and rerun our graph from a previous checkpoint. \n", - "\n", - "> NOTE: The reason we add this metadata instead of using the `parent_config` attribute is because `parent_config` tracks every individual step of a run, not the entire run itself.\n", - ">" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "vscode": { - "languageId": "javascript" - } - }, - "outputs": [], - "source": [ - "var messages = [{ role: \"human\", content: \"My name is Bagatur and I am 26 years old.\" }];\n", - "\n", - "await processStreamedMessages(client, thread_2, agent, messages, {\"node_id\": 1, \"parent_node\": null});" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Using our `run_input` function makes it easy to pass in metadata and you can inspect the function as well as the API docs to see exactly how metadata gets passed.\n", - "\n", - "We can continue our thread by creating a second node as follows:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "vscode": { - "languageId": "javascript" - } - }, - "outputs": [], - "source": [ - "messages = [{ role: \"human\", content: \"What is my name?\" }];\n", - "\n", - "await processStreamedMessages(client, thread_2, agent, messages, {\"node_id\": 2, \"parent_node\": 1});" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let's check our state to make sure the graph remembered our name on the second run:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "vscode": { - "languageId": "javascript" - } - }, - "outputs": [], - "source": [ - "var state = await client.threads.getState(thread_2['thread_id']);\n", - "var graph_messages = state.values['messages'].map((message) => message.content);\n", - "console.log(graph_messages);" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "vscode": { - "languageId": "javascript" - } - }, - "outputs": [], - "source": [ - "[\n", - " 'My name is Bagatur and I am 26 years old.',\n", - " '',\n", - " 'Hello Bagatur! How can I assist you today?',\n", - " 'What is my name?',\n", - " '',\n", - " 'Your name is Bagatur. How can I assist you today, Bagatur?'\n", - "]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Perfect! The state persisted across separate runs, and the LLM remembers the name of our user. In a future we will explore non-sequential runs, i.e. not having each run just follow the last one but choosing which checkpoint we start our run from.\n", - "\n", - "## Querying and Updating the thread\n", - "\n", - "### Getting checkpoints by metadata\n", - "\n", - "Let’s say we want to start a new run from a previous state (not the current state). This state lives somewhere in our history, so we can utilize the `get_history` function to try and find it." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "vscode": { - "languageId": "javascript" - } - }, - "outputs": [], - "source": [ - "var all_history = await client.threads.getHistory(thread_2[\"thread_id\"])" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "This is helpful for inspecting the specifics of our current thread, but remember that the history contains all the intermediate steps a graph takes. In our case, where the graph has 5 nodes (remember that Start and End both count as nodes), our history array grows quickly. Luckily, there is a way to query by using metadata. For example if we wanted to start a run from Node 1(from the example from above) we need to find the state from the end of run with metadata node_id:1 , which we can do like so:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "vscode": { - "languageId": "javascript" - } - }, - "outputs": [], - "source": [ - "var node_1_history = await client.threads.getHistory(thread_2[\"thread_id\"],{metadata: {'node_id':1}})\n", - "\n", - "var node_1_end_of_run = history.filter(node => node.next.length === 0)[0];" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now let’s explore how we could use this information to create a new branch in our thread.\n", - "\n", - "### Invoking from a previous checkpoint\n", - "\n", - "The following diagram describes what we would like to happen:\n", - "\n", - "
\n", - " \n", - "
\n", - "Basically, we want to have 3 runs of our graph, but instead of having them sequentially - we want both the second and third run to originate from the same state. We can do this by utilizing the code we used above, and passing additional metadata to our run." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "vscode": { - "languageId": "javascript" - } - }, - "outputs": [], - "source": [ - "messages = [{ role: \"human\", content: \"What is my age?\" }];\n", - "\n", - "await processStreamedMessages(client, thread_2, agent, messages, {\"node_id\": 3, \"parent_node\": 1, \"thread_ts\":node_1_end_of_run.checkpoint_id});" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "To check that everything actually worked as planned, let’s check our current state and check that message history to ensure that the message we passed to Node 2 is nowhere to be found." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "vscode": { - "languageId": "javascript" - } - }, - "outputs": [], - "source": [ - "var state = await client.threads.getState(thread_2['thread_id']);\n", - "var graph_messages = state.values['messages'].map((message) => message.content);\n", - "console.log(graph_messages);" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "vscode": { - "languageId": "javascript" - } - }, - "outputs": [], - "source": [ - "[\n", - " 'My name is Bagatur and I am 26 years old.',\n", - " '',\n", - " 'Hello Bagatur! How can I assist you today?',\n", - " 'What is my age?',\n", - " '',\n", - " 'You are 26 years old, Bagatur. How can I assist you today?'\n", - "]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Great! This has worked as expected. Being able to go back to previous states and execute new graph runs from those checkpoints is a great way to develop flexible applications that don’t require reloading or restarting everything when an error is detected or a user changes their mind.\n", - "\n", - "### Updating/Patching the thread state\n", - "\n", - "Lastly, let’s discuss the ability to manually change both the thread state as well as the metadata for a given state. Let’s say we incorrectly inputted data to the LLM and we want to rectify it. \n", - "\n", - "Continuing our previous example, let’s say the user mistyped their age and we want to let the graph know that without actually running it. In this case we can rectify this by using `update_state`" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "vscode": { - "languageId": "javascript" - } - }, - "outputs": [], - "source": [ - "await client.threads.updateState(thread_2['thread_id'], { values: { \"user_info\": { \"name\": \"Bagatur\", \"age\": 35 } } });" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let’s make sure that the state did in fact update by checking the current state:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "vscode": { - "languageId": "javascript" - } - }, - "outputs": [], - "source": [ - "var state = await client.threads.getState(thread_2['thread_id']);\n", - "console.log(state.values['user_info']);\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "vscode": { - "languageId": "javascript" - } - }, - "outputs": [], - "source": [ - "{ name: 'Bagatur', age: 35 }" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Voila! The LLM knows our users age updated without us having to prompt it at all.\n", - "\n", - "The last thing we will talk about is patching the thread, which is used when we want to update the metadata of a state. For example, say we actually wanted to update our last state to have `node_id:4` instead of `node_id:3`. To do this, we can call:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "vscode": { - "languageId": "javascript" - } - }, - "outputs": [], - "source": [ - "await client.threads.patchState(thread_2['thread_id'],{\"node_id\": 4});" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We can check that this worked by checking the metadata of our state" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "vscode": { - "languageId": "javascript" - } - }, - "outputs": [], - "source": [ - "var state = await client.threads.getState(thread_2['thread_id']);\n", - "console.log(state.metadata['node_id']);" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "vscode": { - "languageId": "javascript" - } - }, - "outputs": [], - "source": [ - "4" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Perfect! The patch worked as expected." - ] - } - ], - "metadata": { - "language_info": { - "name": "python" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/examples/sdk/python_sdk.ipynb b/examples/sdk/python_sdk.ipynb deleted file mode 100644 index e778aa7e8..000000000 --- a/examples/sdk/python_sdk.ipynb +++ /dev/null @@ -1,728 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "\n", - "This how-to takes you through the steps of using the Python SDK for interacting with deployed Langgraph Cloud APIs.\n", - "\n", - "## Initialization\n", - "\n", - "### Initializing client\n", - "\n", - "To get started we need to initialize our client. The process for initializing our client is almost identical for both the local deployment and cloud deployment using Langsmith." - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [], - "source": [ - "from langgraph_sdk import get_client\n", - "\n", - "# If you deployed using Langsmith use this option\n", - "# Find this url on your Langsmith deployment page\n", - "example_deployed_url = (\n", - " \"https://ht-unhealthy-buffalo25-39d00f953458585aa9f7b5a4fa-g3ps4aazkq-uc.a.run.app\"\n", - ")\n", - "\n", - "# If you deployed locally using langgraph up -c langgraph.json use this option\n", - "# This is the default URL, and you can just call get_client() to use it\n", - "example_local_url = \"http://localhost:8123\"\n", - "\n", - "client = get_client(url=\"whatever-your-url-is\")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Selecting an Assistant\n", - "\n", - "To select an assistant we can search the assistants that are hosted on our client, and then select the one we want:" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [], - "source": [ - "assistants = await client.assistants.search()\n", - "assistants = [a for a in assistants if not a[\"config\"]]\n", - "assistant = assistants[0]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In our example we are only hosting a single assistant, but you have the option to host many, in which case you will most likely want to do more filtering than just selecting the first one. Each assistant is a JSON object with the following format, allowing you to select based on a variety of parameters." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'assistant_id': 'fe096781-5601-53d2-b2f6-0d3403f7e9ca',\n", - " 'graph_id': 'agent',\n", - " 'created_at': '2024-06-11T20:12:45.862108+00:00',\n", - " 'updated_at': '2024-06-11T20:12:45.862108+00:00',\n", - " 'config': {},\n", - " 'metadata': {'created_by': 'system'}}" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "assistant" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Configuring an assistant\n", - "\n", - "One important thing to know is that graph can be defined to be configurable, meaning that not every instance of the graph needs to be the same (read up on [this guide](https://langchain-ai.github.io/langgraph/how-tos/configuration/) to learn more about how to create your own configurable graphs). Let's briefly show how we can configure an assistant. The first step to do is find the assistant we want to configure. In our simple example we are only hosting a single graph, so we must choose it as the graph to configure." - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [], - "source": [ - "base_assistant = assistants[0]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "This assistant has one configurable argument called `model`, which can take on two values: `openai` or `anthropic`. In this case, I want to create a graph that originates from this assistant that uses the `openai` option, which we can do as follows:" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [], - "source": [ - "config_graph = await client.assistants.create(base_assistant['graph_id'],config = {'configurable':{'model':'openai'}})" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now if we inspect our new graph, we can see that it has been configured and whenever we run it, it will select the `openai` model." - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'assistant_id': '4d91047c-585c-4d1b-9ade-5b0f4d292528',\n", - " 'graph_id': 'agent',\n", - " 'created_at': '2024-06-21T23:06:08.203776+00:00',\n", - " 'updated_at': '2024-06-21T23:06:08.203776+00:00',\n", - " 'config': {'configurable': {'model': 'openai'}},\n", - " 'metadata': {}}" - ] - }, - "execution_count": 8, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "config_graph" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Being able to create configurable assistants allows you to create different graphs all based on the same underlying structure. This can be very powerful for testing different configurations of a graph or allowing users to customize their graph." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Creating a thread\n", - "\n", - "Threads are what we will actually use to run our graphs (assistants). Each thread will update the same state for the graph, meaning we can run the graph multiple times while the state will persist. We can also look back at our thread history, add meta data to different steps of our thread, and update the thread state manually if we wish. We will dive into all of those topics later in this article, but for now let’s just see how to start a thread:" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [], - "source": [ - "thread = await client.threads.create()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We can examine the structure of our thread, which similar to the assistants object provides us with some information about the thread itself, including its id, timestamps, and metadata:" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'thread_id': '6c2e8002-5712-4388-bed6-0747e9a86e31',\n", - " 'created_at': '2024-06-19T15:58:59.243657+00:00',\n", - " 'updated_at': '2024-06-19T15:58:59.243657+00:00',\n", - " 'metadata': {}}" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "thread" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now we are ready to actually use our graph!\n", - "\n", - "## Invoking the graph\n", - "\n", - "The graph used in this example is a simple example of a StateGraph, but it allows us to show most of the API functionality. The state of our graph is defined as follows:" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [], - "source": [ - "from typing import Annotated, TypedDict\n", - "\n", - "from langchain_core.messages import AnyMessage\n", - "\n", - "from langgraph.graph import add_messages\n", - "\n", - "\n", - "def update_user_info(old_info, new_info):\n", - " if \"name\" not in new_info or new_info[\"age\"] == -1:\n", - " return old_info\n", - " return new_info\n", - "\n", - "\n", - "class UserInformation(TypedDict):\n", - " age: int\n", - " name: str\n", - "\n", - "\n", - "class AgentState(TypedDict):\n", - " messages: Annotated[list[AnyMessage], add_messages]\n", - " user_info: Annotated[UserInformation, update_user_info]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "It is important to note that our member variables can be updated by using the `Annotated` class. This is especially important when we make API calls that will update our variables. This is also a good example that your graph can hold much more information than just messages. In our example we use a very simple `UserInformation` class, but you can imagine holding much richer information in your state.\n", - "\n", - "Our graph looks like follows:\n", - "\n", - "
\n", - " \n", - "
\n", - "\n", - "The workflow is as follows: first the user inputs some message, our llm decides how to configure the call to our tool `get_user_info` , and after getting the results of the tool call we respond to our user using another LLM.\n", - "\n", - "### Simple Invocation\n", - "\n", - "Ok, now that we have set up our client, assistant, and thread we can actually invoke the graph above. Let’s first define the function we will use to invoke the graph, since we don’t want to have to rewrite this code every single run." - ] - }, - { - "cell_type": "code", - "execution_count": 48, - "metadata": {}, - "outputs": [], - "source": [ - "async def run_input(client, thread, assistant, input, metadata={}):\n", - " # client.runs.stream will stream the results of running our graph\n", - " async for chunk in client.runs.stream(\n", - " thread[\"thread_id\"],\n", - " assistant[\"assistant_id\"],\n", - " input=input,\n", - " config={\"configurable\": metadata},\n", - " stream_mode=\"updates\",\n", - " ):\n", - " if chunk.data and \"run_id\" not in chunk.data:\n", - " print(chunk.data)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let’s now see what happens to our graph when we run it with a simple sentence:" - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'llm': {'messages': [{'content': '', 'additional_kwargs': {'tool_calls': [{'index': 0, 'id': 'call_ioeUDw39bXQ2nap5f593xZMW', 'function': {'arguments': '{\"age\":26,\"name\":\"Bagatur\"}', 'name': 'PersonalInfo'}, 'type': 'function'}]}, 'response_metadata': {'finish_reason': 'stop'}, 'type': 'ai', 'name': None, 'id': 'run-75a0d9db-df99-4b0c-b356-f24e76f5ca5a', 'example': False, 'tool_calls': [{'name': 'PersonalInfo', 'args': {'age': 26, 'name': 'Bagatur'}, 'id': 'call_ioeUDw39bXQ2nap5f593xZMW'}], 'invalid_tool_calls': [], 'usage_metadata': None}]}}\n", - "{'get_user_info': {'messages': [{'content': 'Hello! My name is Bagatur and I am 26 years old.', 'additional_kwargs': {}, 'response_metadata': {}, 'type': 'human', 'name': None, 'id': 'abafa365-1280-439d-b2ac-d4c547284ff9', 'example': False}, {'content': '', 'additional_kwargs': {'tool_calls': [{'index': 0, 'id': 'call_E6NK18NXOHFkp8CeIuF7iI3K', 'function': {'arguments': '{\"age\":26,\"name\":\"Bagatur\"}', 'name': 'PersonalInfo'}, 'type': 'function'}]}, 'response_metadata': {'finish_reason': 'stop'}, 'type': 'ai', 'name': None, 'id': 'run-c018c6a2-b93a-4787-aae3-46df0ef24c5b', 'example': False, 'tool_calls': [{'name': 'PersonalInfo', 'args': {'age': 26, 'name': 'Bagatur'}, 'id': 'call_E6NK18NXOHFkp8CeIuF7iI3K'}], 'invalid_tool_calls': [], 'usage_metadata': None}, {'content': 'Hello Bagatur! How can I assist you today?', 'additional_kwargs': {}, 'response_metadata': {'finish_reason': 'stop'}, 'type': 'ai', 'name': None, 'id': 'run-fb1ac0ca-b1f8-454b-ba3f-849c266dcc05', 'example': False, 'tool_calls': [], 'invalid_tool_calls': [], 'usage_metadata': None}, {'content': 'Hello! What is my name?', 'additional_kwargs': {}, 'response_metadata': {}, 'type': 'human', 'name': None, 'id': '8bf0637b-8214-4c85-85ff-873ec193a7b8', 'example': False}, {'content': '', 'additional_kwargs': {'tool_calls': [{'index': 0, 'id': 'call_6EFvGDGWHuoOKm4p7dq3qazN', 'function': {'arguments': '{\"age\":-1,\"name\":\"John Doe\"}', 'name': 'PersonalInfo'}, 'type': 'function'}]}, 'response_metadata': {'finish_reason': 'stop'}, 'type': 'ai', 'name': None, 'id': 'run-30a91dfa-4526-4bb7-b4a2-017e5c530ed2', 'example': False, 'tool_calls': [{'name': 'PersonalInfo', 'args': {'age': -1, 'name': 'John Doe'}, 'id': 'call_6EFvGDGWHuoOKm4p7dq3qazN'}], 'invalid_tool_calls': [], 'usage_metadata': None}, {'content': 'Your name is Bagatur. How can I assist you today?', 'additional_kwargs': {}, 'response_metadata': {'finish_reason': 'stop'}, 'type': 'ai', 'name': None, 'id': 'run-f3dcd3ad-b438-4011-9353-83978758ee81', 'example': False, 'tool_calls': [], 'invalid_tool_calls': [], 'usage_metadata': None}, {'content': 'Hello! My name is Bagatur and I am 26 years old.', 'additional_kwargs': {}, 'response_metadata': {}, 'type': 'human', 'name': None, 'id': '88e7bb12-552d-4607-8bc5-ce2b342f0604', 'example': False}, {'content': '', 'additional_kwargs': {'tool_calls': [{'index': 0, 'id': 'call_ioeUDw39bXQ2nap5f593xZMW', 'function': {'arguments': '{\"age\":26,\"name\":\"Bagatur\"}', 'name': 'PersonalInfo'}, 'type': 'function'}]}, 'response_metadata': {'finish_reason': 'stop'}, 'type': 'ai', 'name': None, 'id': 'run-75a0d9db-df99-4b0c-b356-f24e76f5ca5a', 'example': False, 'tool_calls': [{'name': 'PersonalInfo', 'args': {'age': 26, 'name': 'Bagatur'}, 'id': 'call_ioeUDw39bXQ2nap5f593xZMW'}], 'invalid_tool_calls': [], 'usage_metadata': None}], 'user_info': {'age': 26, 'name': 'Bagatur'}}}\n", - "{'respond_to_user': {'messages': [{'content': 'Hello Bagatur! How can I assist you today?', 'additional_kwargs': {}, 'response_metadata': {'finish_reason': 'stop'}, 'type': 'ai', 'name': None, 'id': 'run-49176b69-aae5-41ac-8c1e-7bab373b2b9b', 'example': False, 'tool_calls': [], 'invalid_tool_calls': [], 'usage_metadata': None}]}}\n" - ] - } - ], - "source": [ - "input = {\n", - " \"messages\": [\n", - " {\"role\": \"user\", \"content\": \"Hello! My name is Bagatur and I am 26 years old.\"}\n", - " ]\n", - "}\n", - "\n", - "await run_input(client, thread, assistant, input)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We can see our graph ran as expected, by calling our three nodes sequentially. Let’s now examine the state to take a further look under the hood. We can get the state by using the following command: " - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "dict_keys(['values', 'next', 'config', 'metadata', 'created_at', 'parent_config'])" - ] - }, - "execution_count": 17, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "state = await client.threads.get_state(thread_id=thread[\"thread_id\"])\n", - "state.keys()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Our state variable contains a variety of important information. Here is a quick summary of the keys and what they represent:\n", - "\n", - "- `values` contains the actual state values, so in our case you could call `state['values']['messages']` or `state['values']['user_info']` and get the actual values of each of the state variables.\n", - "- `next` tells us what action in the graph is next at the current state. Since we just finished running our graph and reached the end node, it is currently empty because there is no next action to take. However, if you go through the state at each point of the run you will see that the `next` value goes from `__start__` → `llm` → `get_user_info` →`respond_to_user` .\n", - "- `metadata` stores the metadata associated with our state. This is data that is outside of the agent state, but is important to keep track of across multiple runs. An example of this is shown in the next section.\n", - "- `config` tells us what the configuration of the state is. This is important for when we want to run a query starting at a previous state instead of the one we are at. An example of this is shown in the Invoking from a previous checkpoint section\n", - "\n", - "### Invoking with Metadata\n", - "\n", - "Let’s create a new thread to reset our state and start fresh." - ] - }, - { - "cell_type": "code", - "execution_count": 49, - "metadata": {}, - "outputs": [], - "source": [ - "thread = await client.threads.create()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now let’s add some metadata to our request. In this example we are going to treat each run of our assistant as a separate “node”. For each run, we will pass in a “node_id” as well as a “parent_node” in the metadata. This way we can easily go “back in time” and rerun our graph from a previous checkpoint. \n", - "\n", - "> NOTE: The reason we add this metadata instead of using the `parent_config` attribute is because `parent_config` tracks every individual step of a run, not the entire run itself.\n", - ">" - ] - }, - { - "cell_type": "code", - "execution_count": 50, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'llm': {'messages': [{'content': '', 'additional_kwargs': {'tool_calls': [{'index': 0, 'id': 'call_VrA7UKg2w99BvIpsqrwjoawk', 'function': {'arguments': '{\"age\":26,\"name\":\"Bagatur\"}', 'name': 'PersonalInfo'}, 'type': 'function'}]}, 'response_metadata': {'finish_reason': 'stop'}, 'type': 'ai', 'name': None, 'id': 'run-067609c1-4f1a-4d6d-bc1c-224a29153d37', 'example': False, 'tool_calls': [{'name': 'PersonalInfo', 'args': {'age': 26, 'name': 'Bagatur'}, 'id': 'call_VrA7UKg2w99BvIpsqrwjoawk'}], 'invalid_tool_calls': [], 'usage_metadata': None}]}}\n", - "{'get_user_info': {'messages': [{'content': 'Hello! My name is Bagatur and I am 26 years old.', 'additional_kwargs': {}, 'response_metadata': {}, 'type': 'human', 'name': None, 'id': '05faf88c-9f85-462e-84ee-4667de35625d', 'example': False}, {'content': '', 'additional_kwargs': {'tool_calls': [{'index': 0, 'id': 'call_VrA7UKg2w99BvIpsqrwjoawk', 'function': {'arguments': '{\"age\":26,\"name\":\"Bagatur\"}', 'name': 'PersonalInfo'}, 'type': 'function'}]}, 'response_metadata': {'finish_reason': 'stop'}, 'type': 'ai', 'name': None, 'id': 'run-067609c1-4f1a-4d6d-bc1c-224a29153d37', 'example': False, 'tool_calls': [{'name': 'PersonalInfo', 'args': {'age': 26, 'name': 'Bagatur'}, 'id': 'call_VrA7UKg2w99BvIpsqrwjoawk'}], 'invalid_tool_calls': [], 'usage_metadata': None}], 'user_info': {'age': 26, 'name': 'Bagatur'}}}\n", - "{'respond_to_user': {'messages': [{'content': 'Hello Bagatur! How can I assist you today?', 'additional_kwargs': {}, 'response_metadata': {'finish_reason': 'stop'}, 'type': 'ai', 'name': None, 'id': 'run-48041247-fe04-419f-9f12-511e28c5f8aa', 'example': False, 'tool_calls': [], 'invalid_tool_calls': [], 'usage_metadata': None}]}}\n" - ] - } - ], - "source": [ - "input = {\n", - " \"messages\": [\n", - " {\"role\": \"user\", \"content\": \"Hello! My name is Bagatur and I am 26 years old.\"}\n", - " ]\n", - "}\n", - "\n", - "metadata = {\"node_id\": 1, \"parent_node\": None}\n", - "\n", - "await run_input(client, thread, assistant, input, metadata)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Using our `run_input` function makes it easy to pass in metadata and you can inspect the function as well as the API docs to see exactly how metadata gets passed.\n", - "\n", - "We can continue our thread by creating a second node as follows:" - ] - }, - { - "cell_type": "code", - "execution_count": 51, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'llm': {'messages': [{'content': '', 'additional_kwargs': {'tool_calls': [{'index': 0, 'id': 'call_b2T2e0doVrh6uB1aHLIQglYR', 'function': {'arguments': '{\"age\":-1,\"name\":\"John Doe\"}', 'name': 'PersonalInfo'}, 'type': 'function'}]}, 'response_metadata': {'finish_reason': 'stop'}, 'type': 'ai', 'name': None, 'id': 'run-808dca3a-0188-4c6b-9fc7-0037949fd820', 'example': False, 'tool_calls': [{'name': 'PersonalInfo', 'args': {'age': -1, 'name': 'John Doe'}, 'id': 'call_b2T2e0doVrh6uB1aHLIQglYR'}], 'invalid_tool_calls': [], 'usage_metadata': None}]}}\n", - "{'get_user_info': {'messages': [{'content': 'Hello! My name is Bagatur and I am 26 years old.', 'additional_kwargs': {}, 'response_metadata': {}, 'type': 'human', 'name': None, 'id': '05faf88c-9f85-462e-84ee-4667de35625d', 'example': False}, {'content': '', 'additional_kwargs': {'tool_calls': [{'index': 0, 'id': 'call_VrA7UKg2w99BvIpsqrwjoawk', 'function': {'arguments': '{\"age\":26,\"name\":\"Bagatur\"}', 'name': 'PersonalInfo'}, 'type': 'function'}]}, 'response_metadata': {'finish_reason': 'stop'}, 'type': 'ai', 'name': None, 'id': 'run-067609c1-4f1a-4d6d-bc1c-224a29153d37', 'example': False, 'tool_calls': [{'name': 'PersonalInfo', 'args': {'age': 26, 'name': 'Bagatur'}, 'id': 'call_VrA7UKg2w99BvIpsqrwjoawk'}], 'invalid_tool_calls': [], 'usage_metadata': None}, {'content': 'Hello Bagatur! How can I assist you today?', 'additional_kwargs': {}, 'response_metadata': {'finish_reason': 'stop'}, 'type': 'ai', 'name': None, 'id': 'run-48041247-fe04-419f-9f12-511e28c5f8aa', 'example': False, 'tool_calls': [], 'invalid_tool_calls': [], 'usage_metadata': None}, {'content': 'Hello! What is my name?', 'additional_kwargs': {}, 'response_metadata': {}, 'type': 'human', 'name': None, 'id': '0970d6dd-e071-49bf-bf71-5345185b8291', 'example': False}, {'content': '', 'additional_kwargs': {'tool_calls': [{'index': 0, 'id': 'call_b2T2e0doVrh6uB1aHLIQglYR', 'function': {'arguments': '{\"age\":-1,\"name\":\"John Doe\"}', 'name': 'PersonalInfo'}, 'type': 'function'}]}, 'response_metadata': {'finish_reason': 'stop'}, 'type': 'ai', 'name': None, 'id': 'run-808dca3a-0188-4c6b-9fc7-0037949fd820', 'example': False, 'tool_calls': [{'name': 'PersonalInfo', 'args': {'age': -1, 'name': 'John Doe'}, 'id': 'call_b2T2e0doVrh6uB1aHLIQglYR'}], 'invalid_tool_calls': [], 'usage_metadata': None}], 'user_info': {'age': -1, 'name': 'John Doe'}}}\n", - "{'respond_to_user': {'messages': [{'content': 'Your name is Bagatur. How can I assist you today, Bagatur?', 'additional_kwargs': {}, 'response_metadata': {'finish_reason': 'stop'}, 'type': 'ai', 'name': None, 'id': 'run-9ada26f0-951e-4dd5-a2c5-4357bdeeac9d', 'example': False, 'tool_calls': [], 'invalid_tool_calls': [], 'usage_metadata': None}]}}\n" - ] - } - ], - "source": [ - "input = {\"messages\": [{\"role\": \"user\", \"content\": \"Hello! What is my name?\"}]}\n", - "metadata = {\"node_id\": 2, \"parent_node\": 1}\n", - "\n", - "await run_input(client, thread, assistant, input, metadata)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Perfect! The state persisted across separate runs, and the LLM remembers the name of our user. In a future we will explore non-sequential runs, i.e. not having each run just follow the last one but choosing which checkpoint we start our run from.\n", - "\n", - "### Invoking on a schedule\n", - "\n", - "Running your graph on a schedule can be valuable in situations where you want to run tasks periodically. The Langgraph Cloud API allows you to do this through the `CronAssistant` class. To schedule a graph job, you need to pass a [cron expression](https://crontab.cronhub.io/) to inform the client when you want to run the graph. `Cron` jobs are run in the background and do not interfere with normal invocations of the graph." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# This schedules a job to run at 15:27 (3:27PM) every day\n", - "cron_1 = await client.crons.create(assistant['assistant_id'],schedule=\"27 15 * * *\",input={'messages':[{\"role\":\"user\",\"content\":\"What time is it?\"}]})" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Note that it is **very** important to delete `Cron` jobs that are no longer useful. Otherwise you could rack up unwanted API charges to the LLM! You can delete a `Cron` job using the following code:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "await client.crons.delete(cron_1['cron_id'])" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Querying and Updating the thread\n", - "\n", - "### Getting checkpoints by metadata\n", - "\n", - "Let’s say we want to start a new run from a previous state (not the current state). This state lives somewhere in our history, so we can utilize the `get_history` function to try and find it." - ] - }, - { - "cell_type": "code", - "execution_count": 52, - "metadata": {}, - "outputs": [], - "source": [ - "history = await client.threads.get_history(thread_id=thread[\"thread_id\"])" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "This is helpful for inspecting the specifics of our current thread, but remember that the history contains all the intermediate steps a graph takes. In our case, where the graph has 5 nodes (remember that Start and End both count as nodes), our history array grows quickly. Luckily, there is a way to query by using metadata. For example if we wanted to start a run from Node 1(from the example from above) we need to find the state from the end of run with metadata node_id:1 , which we can do like so:" - ] - }, - { - "cell_type": "code", - "execution_count": 53, - "metadata": {}, - "outputs": [], - "source": [ - "node_1_history = await client.threads.get_history(\n", - " thread_id=thread[\"thread_id\"], metadata={\"node_id\": 1}\n", - ")\n", - "# At the end of the run there will be no 'next' for the graph to execute\n", - "node_1_end_of_run = [h for h in node_1_history if h[\"next\"] == []][0]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now let’s explore how we could use this information to create a new branch in our thread.\n", - "\n", - "### Invoking from a previous checkpoint\n", - "\n", - "The following diagram describes what we would like to happen:\n", - "\n", - "
\n", - " \n", - "
\n", - "Basically, we want to have 3 runs of our graph, but instead of having them sequentially - we want both the second and third run to originate from the same state. We can do this by utilizing the code we used above, and passing additional metadata to our run." - ] - }, - { - "cell_type": "code", - "execution_count": 54, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'llm': {'messages': [{'content': '', 'additional_kwargs': {'tool_calls': [{'index': 0, 'id': 'call_yC7HUAQfcLzheepMD8SnAojR', 'function': {'arguments': '{\"age\":-1}', 'name': 'PersonalInfo'}, 'type': 'function'}]}, 'response_metadata': {'finish_reason': 'stop'}, 'type': 'ai', 'name': None, 'id': 'run-b6a653e2-11ea-4f82-aa6a-ef321deed9ce', 'example': False, 'tool_calls': [{'name': 'PersonalInfo', 'args': {'age': -1}, 'id': 'call_yC7HUAQfcLzheepMD8SnAojR'}], 'invalid_tool_calls': [], 'usage_metadata': None}]}}\n", - "{'get_user_info': {'messages': [{'content': 'Hello! My name is Bagatur and I am 26 years old.', 'additional_kwargs': {}, 'response_metadata': {}, 'type': 'human', 'name': None, 'id': '05faf88c-9f85-462e-84ee-4667de35625d', 'example': False}, {'content': '', 'additional_kwargs': {'tool_calls': [{'index': 0, 'id': 'call_VrA7UKg2w99BvIpsqrwjoawk', 'function': {'arguments': '{\"age\":26,\"name\":\"Bagatur\"}', 'name': 'PersonalInfo'}, 'type': 'function'}]}, 'response_metadata': {'finish_reason': 'stop'}, 'type': 'ai', 'name': None, 'id': 'run-067609c1-4f1a-4d6d-bc1c-224a29153d37', 'example': False, 'tool_calls': [{'name': 'PersonalInfo', 'args': {'age': 26, 'name': 'Bagatur'}, 'id': 'call_VrA7UKg2w99BvIpsqrwjoawk'}], 'invalid_tool_calls': [], 'usage_metadata': None}, {'content': 'Hello Bagatur! How can I assist you today?', 'additional_kwargs': {}, 'response_metadata': {'finish_reason': 'stop'}, 'type': 'ai', 'name': None, 'id': 'run-48041247-fe04-419f-9f12-511e28c5f8aa', 'example': False, 'tool_calls': [], 'invalid_tool_calls': [], 'usage_metadata': None}, {'content': 'Hello! What is my age?', 'additional_kwargs': {}, 'response_metadata': {}, 'type': 'human', 'name': None, 'id': 'e94fa535-f821-4bfb-9d96-68c414adad8d', 'example': False}, {'content': '', 'additional_kwargs': {'tool_calls': [{'index': 0, 'id': 'call_yC7HUAQfcLzheepMD8SnAojR', 'function': {'arguments': '{\"age\":-1}', 'name': 'PersonalInfo'}, 'type': 'function'}]}, 'response_metadata': {'finish_reason': 'stop'}, 'type': 'ai', 'name': None, 'id': 'run-b6a653e2-11ea-4f82-aa6a-ef321deed9ce', 'example': False, 'tool_calls': [{'name': 'PersonalInfo', 'args': {'age': -1}, 'id': 'call_yC7HUAQfcLzheepMD8SnAojR'}], 'invalid_tool_calls': [], 'usage_metadata': None}], 'user_info': {'age': -1}}}\n", - "{'respond_to_user': {'messages': [{'content': 'You are 26 years old, Bagatur! How can I assist you today?', 'additional_kwargs': {}, 'response_metadata': {'finish_reason': 'stop'}, 'type': 'ai', 'name': None, 'id': 'run-e3b6e627-b4b1-4249-a1bc-a6359ad7b961', 'example': False, 'tool_calls': [], 'invalid_tool_calls': [], 'usage_metadata': None}]}}\n" - ] - } - ], - "source": [ - "input = {\"messages\": [{\"role\": \"user\", \"content\": \"Hello! What is my age?\"}]}\n", - "metadata = {\n", - " **{\"node_id\": 3, \"parent_node\": 1},\n", - " **{\n", - " \"thread_ts\": node_1_end_of_run[\"checkpoint_id\"],\n", - " \"thread_id\": thread[\"thread_id\"],\n", - " },\n", - "}\n", - "\n", - "await run_input(client, thread, assistant, input, metadata)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "To check that everything actually worked as planned, let’s check our current state and check that message history to ensure that the message we passed to Node 2 is nowhere to be found." - ] - }, - { - "cell_type": "code", - "execution_count": 55, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 55, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "\"Hello! What is my name?\" in [\n", - " message[\"content\"] for message in state[\"values\"][\"messages\"]\n", - "]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Great! This has worked as expected. Being able to go back to previous states and execute new graph runs from those checkpoints is a great way to develop flexible applications that don’t require reloading or restarting everything when an error is detected or a user changes their mind.\n", - "\n", - "### Updating/Patching the thread state\n", - "\n", - "Lastly, let’s discuss the ability to manually change both the thread state as well as the metadata for a given state. Let’s say we incorrectly inputted data to the LLM and we want to rectify it. \n", - "\n", - "Continuing our previous example, let’s say the user mistyped their age and we want to let the graph know that without actually running it. In this case we can rectify this by using `update_state`" - ] - }, - { - "cell_type": "code", - "execution_count": 56, - "metadata": {}, - "outputs": [], - "source": [ - "new_state = await client.threads.update_state(\n", - " thread_id=thread[\"thread_id\"], values={\"user_info\": {\"name\": \"Bagatur\", \"age\": 35}}\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let’s make sure that the state did in fact update and ask our LLM again how old we are by invoking the graph again:" - ] - }, - { - "cell_type": "code", - "execution_count": 57, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'llm': {'messages': [{'content': '', 'additional_kwargs': {'tool_calls': [{'index': 0, 'id': 'call_Ph3tYdt2UNdwqAF3kVL198Bg', 'function': {'arguments': '{\"age\":-1}', 'name': 'PersonalInfo'}, 'type': 'function'}]}, 'response_metadata': {'finish_reason': 'stop'}, 'type': 'ai', 'name': None, 'id': 'run-373f3e22-8549-4f77-9ff2-05133099bb09', 'example': False, 'tool_calls': [{'name': 'PersonalInfo', 'args': {'age': -1}, 'id': 'call_Ph3tYdt2UNdwqAF3kVL198Bg'}], 'invalid_tool_calls': [], 'usage_metadata': None}]}}\n", - "{'get_user_info': {'messages': [{'content': 'Hello! My name is Bagatur and I am 26 years old.', 'additional_kwargs': {}, 'response_metadata': {}, 'type': 'human', 'name': None, 'id': '05faf88c-9f85-462e-84ee-4667de35625d', 'example': False}, {'content': '', 'additional_kwargs': {'tool_calls': [{'index': 0, 'id': 'call_VrA7UKg2w99BvIpsqrwjoawk', 'function': {'arguments': '{\"age\":26,\"name\":\"Bagatur\"}', 'name': 'PersonalInfo'}, 'type': 'function'}]}, 'response_metadata': {'finish_reason': 'stop'}, 'type': 'ai', 'name': None, 'id': 'run-067609c1-4f1a-4d6d-bc1c-224a29153d37', 'example': False, 'tool_calls': [{'name': 'PersonalInfo', 'args': {'age': 26, 'name': 'Bagatur'}, 'id': 'call_VrA7UKg2w99BvIpsqrwjoawk'}], 'invalid_tool_calls': [], 'usage_metadata': None}, {'content': 'Hello Bagatur! How can I assist you today?', 'additional_kwargs': {}, 'response_metadata': {'finish_reason': 'stop'}, 'type': 'ai', 'name': None, 'id': 'run-48041247-fe04-419f-9f12-511e28c5f8aa', 'example': False, 'tool_calls': [], 'invalid_tool_calls': [], 'usage_metadata': None}, {'content': 'Hello! What is my age?', 'additional_kwargs': {}, 'response_metadata': {}, 'type': 'human', 'name': None, 'id': 'e94fa535-f821-4bfb-9d96-68c414adad8d', 'example': False}, {'content': '', 'additional_kwargs': {'tool_calls': [{'index': 0, 'id': 'call_yC7HUAQfcLzheepMD8SnAojR', 'function': {'arguments': '{\"age\":-1}', 'name': 'PersonalInfo'}, 'type': 'function'}]}, 'response_metadata': {'finish_reason': 'stop'}, 'type': 'ai', 'name': None, 'id': 'run-b6a653e2-11ea-4f82-aa6a-ef321deed9ce', 'example': False, 'tool_calls': [{'name': 'PersonalInfo', 'args': {'age': -1}, 'id': 'call_yC7HUAQfcLzheepMD8SnAojR'}], 'invalid_tool_calls': [], 'usage_metadata': None}, {'content': 'You are 26 years old, Bagatur! How can I assist you today?', 'additional_kwargs': {}, 'response_metadata': {'finish_reason': 'stop'}, 'type': 'ai', 'name': None, 'id': 'run-e3b6e627-b4b1-4249-a1bc-a6359ad7b961', 'example': False, 'tool_calls': [], 'invalid_tool_calls': [], 'usage_metadata': None}, {'content': 'Hello! What is my age?', 'additional_kwargs': {}, 'response_metadata': {}, 'type': 'human', 'name': None, 'id': '24ac02be-7b08-4e91-9d20-90507ed25391', 'example': False}, {'content': '', 'additional_kwargs': {'tool_calls': [{'index': 0, 'id': 'call_Ph3tYdt2UNdwqAF3kVL198Bg', 'function': {'arguments': '{\"age\":-1}', 'name': 'PersonalInfo'}, 'type': 'function'}]}, 'response_metadata': {'finish_reason': 'stop'}, 'type': 'ai', 'name': None, 'id': 'run-373f3e22-8549-4f77-9ff2-05133099bb09', 'example': False, 'tool_calls': [{'name': 'PersonalInfo', 'args': {'age': -1}, 'id': 'call_Ph3tYdt2UNdwqAF3kVL198Bg'}], 'invalid_tool_calls': [], 'usage_metadata': None}], 'user_info': {'age': -1}}}\n", - "{'respond_to_user': {'messages': [{'content': 'My apologies for the confusion earlier. You are 35 years old, Bagatur! How can I assist you today?', 'additional_kwargs': {}, 'response_metadata': {'finish_reason': 'stop'}, 'type': 'ai', 'name': None, 'id': 'run-298306df-34a5-459a-a8c3-a0f16440c429', 'example': False, 'tool_calls': [], 'invalid_tool_calls': [], 'usage_metadata': None}]}}\n" - ] - } - ], - "source": [ - "input = {\"messages\": [{\"role\": \"user\", \"content\": \"Hello! What is my age?\"}]}\n", - "\n", - "await run_input(client, thread, assistant, input)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Voila! The LLM knows our users age updated without us having to prompt it at all.\n", - "\n", - "The last thing we will talk about is patching the thread, which is used when we want to update the metadata of a state. For example, say we actually wanted to update our last state to have `node_id:4` instead of `node_id:3`. To do this, we can call:" - ] - }, - { - "cell_type": "code", - "execution_count": 58, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'configurable': {'thread_id': '4f044e5a-6f6e-4663-923e-6333c052ce9f',\n", - " 'thread_ts': '1ef2e5d6-e39f-6d26-800e-22245f8220a7'}}" - ] - }, - "execution_count": 58, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "await client.threads.patch_state(thread_id=thread[\"thread_id\"], metadata={\"node_id\": 4})" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We can check that this worked by checking the metadata of our state" - ] - }, - { - "cell_type": "code", - "execution_count": 59, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Current node id is 4\n" - ] - } - ], - "source": [ - "state = await client.threads.get_state(thread_id=thread[\"thread_id\"])\n", - "print(f\"Current node id is {state['metadata']['node_id']}\")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Perfect! The patch worked as expected." - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.11.9" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -}