From afd940d9bd975bf134ee3b16877743b1377ba740 Mon Sep 17 00:00:00 2001 From: isaac hershenson Date: Fri, 19 Jul 2024 12:38:55 -0700 Subject: [PATCH 1/5] first draft --- docs/_scripts/copy_notebooks.py | 1 + docs/docs/how-tos/index.md | 1 + docs/mkdocs.yml | 1 + examples/node-retries.ipynb | 139 ++++++++++++++++++++++++ libs/langgraph/langgraph/graph/state.py | 1 + 5 files changed, 143 insertions(+) create mode 100644 examples/node-retries.ipynb diff --git a/docs/_scripts/copy_notebooks.py b/docs/_scripts/copy_notebooks.py index 5954e7ef5..703fd5dcd 100644 --- a/docs/_scripts/copy_notebooks.py +++ b/docs/_scripts/copy_notebooks.py @@ -56,6 +56,7 @@ _MANUAL = { "human_in_the_loop/time-travel.ipynb", "human_in_the_loop/edit-graph-state.ipynb", "human_in_the_loop/wait-user-input.ipynb", + "node-retries.ipynb", ], "tutorials": [ "introduction.ipynb", diff --git a/docs/docs/how-tos/index.md b/docs/docs/how-tos/index.md index d98b9e255..b1783e492 100644 --- a/docs/docs/how-tos/index.md +++ b/docs/docs/how-tos/index.md @@ -68,6 +68,7 @@ These guides show how to use different streaming modes. - [How to add runtime configuration to your graph](configuration.ipynb) - [How to use a Pydantic model as your state](state-model.ipynb) - [How to use a context object in state](state-context-key.ipynb) +- [How to add node retries](node-retries.ipynb) ## Prebuilt ReAct Agent diff --git a/docs/mkdocs.yml b/docs/mkdocs.yml index daf74c894..e7e1f71e5 100644 --- a/docs/mkdocs.yml +++ b/docs/mkdocs.yml @@ -163,6 +163,7 @@ nav: - Add runtime configuration: how-tos/configuration.ipynb - Use Pydantic model as state: how-tos/state-model.ipynb - Use a context object in state: how-tos/state-context-key.ipynb + - Add node retries: how-tos/node-retries.ipynb - Prebuilt ReAct Agent: - Create a ReAct agent: how-tos/create-react-agent.ipynb - Add memory to a ReAct agent: how-tos/create-react-agent-memory.ipynb diff --git a/examples/node-retries.ipynb b/examples/node-retries.ipynb new file mode 100644 index 000000000..29f59e7dd --- /dev/null +++ b/examples/node-retries.ipynb @@ -0,0 +1,139 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# How to add node retry policies\n", + "\n", + "There are many use cases where you may wish for your node to have a custom retry policy, for example if you are making HTTP requests, calling an API, etc. \n", + "\n", + "## Defining your retry policy\n", + "\n", + "In order to configure the retry policty, you have to pass the `retry` parameter to the `add_node` function. The `retry` parameter takes in a `RetryPolicy` named tuple object. Below we instantiate a `RetryPolicy` object with the default parameters." + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "from langgraph.pregel.types import default_retry_on\n", + "from langgraph.pregel import RetryPolicy\n", + "\n", + "# Note: This is equivalent to writing retry_policy=RetryPolicy() since these are all default values\n", + "retry_policy = RetryPolicy(\n", + " initial_interval = 0.5,\n", + " backoff_factor = 2.0,\n", + " max_interval = 128.0,\n", + " max_attempts = 3,\n", + " jitter = True,\n", + " retry_on = default_retry_on\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You can then change the default values to work for your custom use case:" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "RetryPolicy(initial_interval=0.5, backoff_factor=2.0, max_interval=128.0, max_attempts=5, jitter=True, retry_on=)" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "retry_policy = retry_policy._replace(max_attempts = 5)\n", + "retry_policy" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If you want more information on what each of the parameters does, be sure to read the source code.\n", + "\n", + "## Passing your retry policy to a node\n", + "\n", + "Lastly, we can pass `retry_policy` to the `add_node` function:" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [], + "source": [ + "import operator\n", + "from typing import Annotated, Sequence, TypedDict\n", + "\n", + "from langchain_anthropic import ChatAnthropic\n", + "from langchain_core.messages import BaseMessage\n", + "\n", + "from langgraph.graph import END, StateGraph, START\n", + "\n", + "\n", + "model = ChatAnthropic(model_name=\"claude-2.1\")\n", + "\n", + "\n", + "class AgentState(TypedDict):\n", + " messages: Annotated[Sequence[BaseMessage], operator.add]\n", + "\n", + "\n", + "def _call_model(state):\n", + " response = model.invoke(state[\"messages\"])\n", + " return {\"messages\": [response]}\n", + "\n", + "# Define a new graph\n", + "workflow = StateGraph(AgentState)\n", + "workflow.add_node(\"model\", _call_model,retry=retry_policy)\n", + "workflow.add_edge(START, \"model\")\n", + "workflow.add_edge(\"model\", END)\n", + "\n", + "app = workflow.compile()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now the `\"model\"` node will be configured to use the custom retry policy that was defined above." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "env", + "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 +} diff --git a/libs/langgraph/langgraph/graph/state.py b/libs/langgraph/langgraph/graph/state.py index abde9d0e9..96ad8e513 100644 --- a/libs/langgraph/langgraph/graph/state.py +++ b/libs/langgraph/langgraph/graph/state.py @@ -254,6 +254,7 @@ class StateGraph(Graph): action (Optional[RunnableLike]): The action associated with the node. (default: None) metadata (Optional[dict[str, Any]]): The metadata associated with the node. (default: None) input (Optional[Type[Any]]): The input schema for the node. (default: the graph's input schema) + retry (Optional[RetryPolicy]): The policy for retrying the node. (default: None) Raises: ValueError: If the key is already being used as a state key. From d7dc16310fc598c92e16d5b31adebce7a6105920 Mon Sep 17 00:00:00 2001 From: isaac hershenson Date: Fri, 19 Jul 2024 13:05:28 -0700 Subject: [PATCH 2/5] nuno comments --- examples/node-retries.ipynb | 76 ++++++++++++------------------------- 1 file changed, 25 insertions(+), 51 deletions(-) diff --git a/examples/node-retries.ipynb b/examples/node-retries.ipynb index 29f59e7dd..93749e065 100644 --- a/examples/node-retries.ipynb +++ b/examples/node-retries.ipynb @@ -6,113 +6,87 @@ "source": [ "# How to add node retry policies\n", "\n", - "There are many use cases where you may wish for your node to have a custom retry policy, for example if you are making HTTP requests, calling an API, etc. \n", + "There are many use cases where you may wish for your node to have a custom retry policy, for example if you are calling an API, querying a databse, or calling an LLM, etc. \n", "\n", - "## Defining your retry policy\n", - "\n", - "In order to configure the retry policty, you have to pass the `retry` parameter to the `add_node` function. The `retry` parameter takes in a `RetryPolicy` named tuple object. Below we instantiate a `RetryPolicy` object with the default parameters." + "In order to configure the retry policty, you have to pass the `retry` parameter to the `add_node` function. The `retry` parameter takes in a `RetryPolicy` named tuple object. Below we instantiate a `RetryPolicy` object with the default parameters:" ] }, { "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [], - "source": [ - "from langgraph.pregel.types import default_retry_on\n", - "from langgraph.pregel import RetryPolicy\n", - "\n", - "# Note: This is equivalent to writing retry_policy=RetryPolicy() since these are all default values\n", - "retry_policy = RetryPolicy(\n", - " initial_interval = 0.5,\n", - " backoff_factor = 2.0,\n", - " max_interval = 128.0,\n", - " max_attempts = 3,\n", - " jitter = True,\n", - " retry_on = default_retry_on\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "You can then change the default values to work for your custom use case:" - ] - }, - { - "cell_type": "code", - "execution_count": 9, + "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "RetryPolicy(initial_interval=0.5, backoff_factor=2.0, max_interval=128.0, max_attempts=5, jitter=True, retry_on=)" + "RetryPolicy(initial_interval=0.5, backoff_factor=2.0, max_interval=128.0, max_attempts=3, jitter=True, retry_on=)" ] }, - "execution_count": 9, + "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "retry_policy = retry_policy._replace(max_attempts = 5)\n", - "retry_policy" + "from langgraph.pregel import RetryPolicy\n", + "\n", + "RetryPolicy()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "If you want more information on what each of the parameters does, be sure to read the source code.\n", + "If you want more information on what each of the parameters does, be sure to read the [reference](https://langchain-ai.github.io/langgraph/reference/graphs/#langgraph.graph.StateGraph.add_node).\n", "\n", - "## Passing your retry policy to a node\n", + "## Passing a retry policy to a node\n", "\n", - "Lastly, we can pass `retry_policy` to the `add_node` function:" + "Lastly, we can pass `RetryPolicy` objects when we call the `add_node` function. In the example below we pass two different retry policies to each of our nodes:" ] }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "import operator\n", + "import requests\n", + "import sqlite3\n", "from typing import Annotated, Sequence, TypedDict\n", "\n", "from langchain_anthropic import ChatAnthropic\n", "from langchain_core.messages import BaseMessage\n", "\n", "from langgraph.graph import END, StateGraph, START\n", + "from langchain_community.utilities import SQLDatabase\n", + "from langchain_core.messages import AIMessage\n", "\n", + "db = SQLDatabase.from_uri(\"sqlite:///:memory:\")\n", "\n", "model = ChatAnthropic(model_name=\"claude-2.1\")\n", "\n", - "\n", "class AgentState(TypedDict):\n", " messages: Annotated[Sequence[BaseMessage], operator.add]\n", "\n", + "def query_database(state):\n", + " query_result = db.run(\"SELECT * FROM Artist LIMIT 10;\")\n", + " return {\"messages\": [AIMessage(content=query_result)]}\n", "\n", - "def _call_model(state):\n", + "def call_model(state):\n", " response = model.invoke(state[\"messages\"])\n", " return {\"messages\": [response]}\n", "\n", "# Define a new graph\n", "workflow = StateGraph(AgentState)\n", - "workflow.add_node(\"model\", _call_model,retry=retry_policy)\n", + "workflow.add_node(\"query_database\",query_database, retry=RetryPolicy(retry_on=sqlite3.OperationalError))\n", + "workflow.add_node(\"model\", call_model, retry=RetryPolicy(max_attempts=5))\n", "workflow.add_edge(START, \"model\")\n", - "workflow.add_edge(\"model\", END)\n", + "workflow.add_edge(\"model\",\"query_database\")\n", + "workflow.add_edge(\"query_database\", END)\n", "\n", "app = workflow.compile()" ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now the `\"model\"` node will be configured to use the custom retry policy that was defined above." - ] } ], "metadata": { From 2a7ae83bf83f583c06d6fc73bf31097ec5256d36 Mon Sep 17 00:00:00 2001 From: isaac hershenson Date: Fri, 19 Jul 2024 13:07:26 -0700 Subject: [PATCH 3/5] spelling --- examples/node-retries.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/node-retries.ipynb b/examples/node-retries.ipynb index 93749e065..6798852f3 100644 --- a/examples/node-retries.ipynb +++ b/examples/node-retries.ipynb @@ -6,7 +6,7 @@ "source": [ "# How to add node retry policies\n", "\n", - "There are many use cases where you may wish for your node to have a custom retry policy, for example if you are calling an API, querying a databse, or calling an LLM, etc. \n", + "There are many use cases where you may wish for your node to have a custom retry policy, for example if you are calling an API, querying a database, or calling an LLM, etc. \n", "\n", "In order to configure the retry policty, you have to pass the `retry` parameter to the `add_node` function. The `retry` parameter takes in a `RetryPolicy` named tuple object. Below we instantiate a `RetryPolicy` object with the default parameters:" ] From 55c78bbf90ad6951faca81490df44269791c52a6 Mon Sep 17 00:00:00 2001 From: isaac hershenson Date: Fri, 19 Jul 2024 13:13:02 -0700 Subject: [PATCH 4/5] remove unneccesary imports --- examples/node-retries.ipynb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/node-retries.ipynb b/examples/node-retries.ipynb index 6798852f3..498acd1ab 100644 --- a/examples/node-retries.ipynb +++ b/examples/node-retries.ipynb @@ -46,12 +46,11 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "import operator\n", - "import requests\n", "import sqlite3\n", "from typing import Annotated, Sequence, TypedDict\n", "\n", From eaa23ef4e51c5730b3761db6894f353725194285 Mon Sep 17 00:00:00 2001 From: isaac hershenson Date: Fri, 19 Jul 2024 14:56:14 -0700 Subject: [PATCH 5/5] added retrypolicy reference --- docs/docs/reference/graphs.md | 6 +++++- examples/node-retries.ipynb | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/docs/docs/reference/graphs.md b/docs/docs/reference/graphs.md index 6a2dfd613..497e16d14 100644 --- a/docs/docs/reference/graphs.md +++ b/docs/docs/reference/graphs.md @@ -65,4 +65,8 @@ builder.add_conditional_edges("my_node", my_condition) ## Send -::: langgraph.constants.Send \ No newline at end of file +::: langgraph.constants.Send + +## RetryPolicy + +::: langgraph.pregel.types.RetryPolicy \ No newline at end of file diff --git a/examples/node-retries.ipynb b/examples/node-retries.ipynb index 498acd1ab..c50230ded 100644 --- a/examples/node-retries.ipynb +++ b/examples/node-retries.ipynb @@ -37,7 +37,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "If you want more information on what each of the parameters does, be sure to read the [reference](https://langchain-ai.github.io/langgraph/reference/graphs/#langgraph.graph.StateGraph.add_node).\n", + "If you want more information on what each of the parameters does, be sure to read the [reference](https://langchain-ai.github.io/langgraph/reference/graphs/#retrypolicy).\n", "\n", "## Passing a retry policy to a node\n", "\n",