Skip to content

Commit

Permalink
update persistence notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
bracesproul committed Aug 19, 2024
1 parent fc1a483 commit fa54104
Showing 1 changed file with 28 additions and 95 deletions.
123 changes: 28 additions & 95 deletions examples/how-tos/persistence.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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<IState>[\"channels\"] = {\n",
" messages: {\n",
" value: (x: BaseMessage[], y: BaseMessage[]) => x.concat(y),\n",
" default: () => [],\n",
" },\n",
"};"
"const GraphState = Annotation.Root({\n",
" messages: Annotation<BaseMessage[]>({\n",
" reducer: (x, y) => x.concat(y),\n",
" }),\n",
"});"
]
},
{
Expand Down Expand Up @@ -185,7 +179,7 @@
"source": [
"import { ToolNode } from \"@langchain/langgraph/prebuilt\";\n",
"\n",
"const toolNode = new ToolNode<{ messages: BaseMessage[] }>(tools);"
"const toolNode = new ToolNode<typeof GraphState.State>(tools);"
]
},
{
Expand Down Expand Up @@ -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",
Expand All @@ -281,17 +275,15 @@
"};\n",
"\n",
"const callModel = async (\n",
" state: IState,\n",
" state: typeof GraphState.State,\n",
" config?: RunnableConfig,\n",
") => {\n",
" const { messages } = state;\n",
" const response = await boundModel.invoke(messages, config);\n",
" return { messages: [response] };\n",
"};\n",
"\n",
"const workflow = new StateGraph<IState>({\n",
" channels: graphState,\n",
"})\n",
"const workflow = new StateGraph(GraphState)\n",
" .addNode(\"agent\", callModel)\n",
" .addNode(\"tools\", toolNode)\n",
" .addEdge(START, \"agent\")\n",
Expand All @@ -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"
]
Expand Down Expand Up @@ -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"
]
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
]
Expand Down Expand Up @@ -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"
]
}
],
Expand All @@ -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"
]
Expand Down

0 comments on commit fa54104

Please sign in to comment.