From fa54104c6d41463a197a67a227933ab59b675003 Mon Sep 17 00:00:00 2001 From: bracesproul Date: Mon, 19 Aug 2024 16:15:43 -0700 Subject: [PATCH] update persistence notebook --- examples/how-tos/persistence.ipynb | 123 +++++++---------------------- 1 file changed, 28 insertions(+), 95 deletions(-) diff --git a/examples/how-tos/persistence.ipynb b/examples/how-tos/persistence.ipynb index 37557b424..ed58047c1 100644 --- a/examples/how-tos/persistence.ipynb +++ b/examples/how-tos/persistence.ipynb @@ -25,11 +25,11 @@ "Example:\n", "\n", "```javascript\n", - "import { MemorySaver } from \"@langchain/langgraph\";\n", + "import { MemorySaver, Annotation } from \"@langchain/langgraph\";\n", "\n", - "const workflow = new StateGraph({\n", - " channels: graphState,\n", - "});\n", + "const GraphState = Annotation.Root({ ... });\n", + "\n", + "const workflow = new StateGraph(GraphState);\n", "\n", "/// ... Add nodes and edges\n", "// Initialize any compatible CheckPointSaver\n", @@ -103,20 +103,14 @@ }, "outputs": [], "source": [ + "import { Annotation } from \"@langchain/langgraph\";\n", "import { BaseMessage } from \"@langchain/core/messages\";\n", - "import { StateGraphArgs } from \"@langchain/langgraph\";\n", - "\n", - "interface IState {\n", - " messages: BaseMessage[];\n", - "}\n", "\n", - "// This defines the agent state\n", - "const graphState: StateGraphArgs[\"channels\"] = {\n", - " messages: {\n", - " value: (x: BaseMessage[], y: BaseMessage[]) => x.concat(y),\n", - " default: () => [],\n", - " },\n", - "};" + "const GraphState = Annotation.Root({\n", + " messages: Annotation({\n", + " reducer: (x, y) => x.concat(y),\n", + " }),\n", + "});" ] }, { @@ -185,7 +179,7 @@ "source": [ "import { ToolNode } from \"@langchain/langgraph/prebuilt\";\n", "\n", - "const toolNode = new ToolNode<{ messages: BaseMessage[] }>(tools);" + "const toolNode = new ToolNode(tools);" ] }, { @@ -269,7 +263,7 @@ "import { AIMessage } from \"@langchain/core/messages\";\n", "import { RunnableConfig } from \"@langchain/core/runnables\";\n", "\n", - "const routeMessage = (state: IState) => {\n", + "const routeMessage = (state: typeof GraphState.State) => {\n", " const { messages } = state;\n", " const lastMessage = messages[messages.length - 1] as AIMessage;\n", " // If no tools are called, we can finish (respond to the user)\n", @@ -281,7 +275,7 @@ "};\n", "\n", "const callModel = async (\n", - " state: IState,\n", + " state: typeof GraphState.State,\n", " config?: RunnableConfig,\n", ") => {\n", " const { messages } = state;\n", @@ -289,9 +283,7 @@ " return { messages: [response] };\n", "};\n", "\n", - "const workflow = new StateGraph({\n", - " channels: graphState,\n", - "})\n", + "const workflow = new StateGraph(GraphState)\n", " .addNode(\"agent\", callModel)\n", " .addNode(\"tools\", toolNode)\n", " .addEdge(START, \"agent\")\n", @@ -311,23 +303,10 @@ "name": "stdout", "output_type": "stream", "text": [ - "[ \u001b[32m'user'\u001b[39m, \u001b[32m\"Hi I'm Yu, niced to meet you.\"\u001b[39m ]\n", + "[ 'user', \"Hi I'm Yu, niced to meet you.\" ]\n", "-----\n", - "\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Skipping write for channel branch:agent:routeMessage:undefined which has no readers\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Nice to meet you, Yu! How can I assist you today?\n", + "\n", + "Hi Yu, nice to meet you too! How can I assist you today?\n", "-----\n", "\n" ] @@ -362,23 +341,10 @@ "name": "stdout", "output_type": "stream", "text": [ - "[ \u001b[32m'user'\u001b[39m, \u001b[32m'Remember my name?'\u001b[39m ]\n", + "[ 'user', 'Remember my name?' ]\n", "-----\n", - "\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Skipping write for channel branch:agent:routeMessage:undefined which has no readers\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "I cannot remember personalized details, including names, from previous interactions. However, I'd be happy to help you with any inquiries you have! How can I assist you today?\n", + "\n", + "I don't have memory of previous interactions, so I don't remember your name. Can you please tell me again?\n", "-----\n", "\n" ] @@ -441,22 +407,9 @@ "name": "stdout", "output_type": "stream", "text": [ - "[ \u001b[32m'user'\u001b[39m, \u001b[32m\"Hi I'm Jo, niced to meet you.\"\u001b[39m ]\n", + "[ 'user', \"Hi I'm Jo, niced to meet you.\" ]\n", "-----\n", - "\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Skipping write for channel branch:agent:routeMessage:undefined which has no readers\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ + "\n", "Hi Jo, nice to meet you too! How can I assist you today?\n", "-----\n", "\n" @@ -494,23 +447,10 @@ "name": "stdout", "output_type": "stream", "text": [ - "[ \u001b[32m'user'\u001b[39m, \u001b[32m'Remember my name?'\u001b[39m ]\n", + "[ 'user', 'Remember my name?' ]\n", "-----\n", - "\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Skipping write for channel branch:agent:routeMessage:undefined which has no readers\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Yes, your name is Jo. How can I assist you today?\n", + "\n", + "Of course, Jo! How can I help you today?\n", "-----\n", "\n" ] @@ -560,7 +500,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "{ configurable: { thread_id: \u001b[32m'conversation-2'\u001b[39m } }\n" + "{ configurable: { thread_id: 'conversation-2' } }\n" ] } ], @@ -578,23 +518,16 @@ "name": "stdout", "output_type": "stream", "text": [ - "[ \u001b[32m'user'\u001b[39m, \u001b[32m'you forgot?'\u001b[39m ]\n", + "[ 'user', 'you forgot?' ]\n", "-----\n", "\n" ] }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Skipping write for channel branch:agent:routeMessage:undefined which has no readers\n" - ] - }, { "name": "stdout", "output_type": "stream", "text": [ - "Could you please provide more context or clarify what you're referring to? Let me know how I can assist you further!\n", + "I'm sorry, it seems like I missed something. Could you remind me what you're referring to?\n", "-----\n", "\n" ]