Skip to content

Commit

Permalink
docs[patch]: Fix Code in Managing Conversation History (#6252)
Browse files Browse the repository at this point in the history
* [Docs] Fix Code in Managing Conversation History

* Apply suggestions from code review

* Update docs/core_docs/docs/tutorials/chatbot.ipynb

* Fix type errors

---------

Co-authored-by: Brace Sproul <braceasproul@gmail.com>
  • Loading branch information
sarangan12 and bracesproul authored Jul 29, 2024
1 parent 023a581 commit 8718e3b
Showing 1 changed file with 34 additions and 31 deletions.
65 changes: 34 additions & 31 deletions docs/core_docs/docs/tutorials/chatbot.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -447,17 +447,17 @@
}
],
"source": [
"const config = {\n",
"const config2 = {\n",
" configurable: {\n",
" sessionId: \"abc3\"\n",
" }\n",
"};\n",
"\n",
"const response = await withMessageHistory.invoke({\n",
"const response2 = await withMessageHistory.invoke({\n",
" input: \"What's my name?\",\n",
"}, config);\n",
"}, config2);\n",
"\n",
"response.content"
"response2.content"
]
},
{
Expand All @@ -484,17 +484,17 @@
}
],
"source": [
"const config = {\n",
"const config3 = {\n",
" configurable: {\n",
" sessionId: \"abc2\"\n",
" }\n",
"};\n",
"\n",
"const response = await withMessageHistory.invoke({\n",
"const response3 = await withMessageHistory.invoke({\n",
" input: \"What's my name?\",\n",
"}, config);\n",
"}, config3);\n",
"\n",
"response.content"
"response3.content"
]
},
{
Expand Down Expand Up @@ -526,11 +526,14 @@
"import type { BaseMessage } from \"@langchain/core/messages\";\n",
"import { RunnablePassthrough, RunnableSequence } from \"@langchain/core/runnables\";\n",
"\n",
"const filterMessages = ({ chat_history }: { chat_history: BaseMessage[]; }) => {\n",
" return chat_history.slice(-10);\n",
"};\n",
"type ChainInput = {\n",
" chat_history: BaseMessage[];\n",
" input: string;\n",
"}\n",
"\n",
"const chain = RunnableSequence.from([\n",
"const filterMessages = (input: ChainInput) => input.chat_history.slice(-10);\n",
"\n",
"const chain2 = RunnableSequence.from<ChainInput>([\n",
" RunnablePassthrough.assign({\n",
" chat_history: filterMessages\n",
" }),\n",
Expand Down Expand Up @@ -585,13 +588,13 @@
}
],
"source": [
"const response = await chain.invoke(\n",
"const response4 = await chain2.invoke(\n",
" {\n",
" chat_history: messages,\n",
" input: \"what's my name?\"\n",
" }\n",
")\n",
"response.content"
"response4.content"
]
},
{
Expand All @@ -618,13 +621,13 @@
}
],
"source": [
"const response = await chain.invoke(\n",
"const response5 = await chain2.invoke(\n",
" {\n",
" chat_history: messages,\n",
" input: \"what's my fav ice cream\"\n",
" }\n",
")\n",
"response.content"
"response5.content"
]
},
{
Expand All @@ -651,36 +654,36 @@
}
],
"source": [
"const messageHistories: Record<string, InMemoryChatMessageHistory> = {};\n",
"const messageHistories2: Record<string, InMemoryChatMessageHistory> = {};\n",
"\n",
"const withMessageHistory = new RunnableWithMessageHistory({\n",
"const withMessageHistory2 = new RunnableWithMessageHistory({\n",
" runnable: chain,\n",
" getMessageHistory: async (sessionId) => {\n",
" if (messageHistories[sessionId] === undefined) {\n",
" if (messageHistories2[sessionId] === undefined) {\n",
" const messageHistory = new InMemoryChatMessageHistory();\n",
" await messageHistory.addMessages(messages);\n",
" messageHistories[sessionId] = messageHistory;\n",
" messageHistories2[sessionId] = messageHistory;\n",
" }\n",
" return messageHistories[sessionId];\n",
" return messageHistories2[sessionId];\n",
" },\n",
" inputMessagesKey: \"input\",\n",
" historyMessagesKey: \"chat_history\",\n",
"})\n",
"\n",
"const config = {\n",
"const config4 = {\n",
" configurable: {\n",
" sessionId: \"abc4\"\n",
" }\n",
"};\n",
"\n",
"const response = await withMessageHistory.invoke(\n",
"const response7 = await withMessageHistory2.invoke(\n",
" {\n",
" input: \"whats my name?\",\n",
" },\n",
" config,\n",
" config4,\n",
")\n",
"\n",
"response.content"
"response7.content"
]
},
{
Expand All @@ -707,11 +710,11 @@
}
],
"source": [
"const response = await withMessageHistory.invoke({\n",
"const response8 = await withMessageHistory2.invoke({\n",
" input: \"whats my favorite ice cream?\"\n",
"}, config);\n",
"}, config4);\n",
"\n",
"response.content"
"response8.content"
]
},
{
Expand Down Expand Up @@ -780,15 +783,15 @@
}
],
"source": [
"const config = {\n",
"const config5 = {\n",
" configurable: {\n",
" sessionId: \"abc6\"\n",
" }\n",
"};\n",
"\n",
"const stream = await withMessageHistory.stream({\n",
"const stream = await withMessageHistory2.stream({\n",
" input: \"hi! I'm todd. tell me a joke\",\n",
"}, config);\n",
"}, config5);\n",
"\n",
"for await (const chunk of stream) {\n",
" console.log(\"|\", chunk.content);\n",
Expand Down

0 comments on commit 8718e3b

Please sign in to comment.