docs: update chain migration guide (#24501)

- Update `ConversationChain` example to show use without session IDs;
- Fix a minor bug (specify history_messages_key).
This commit is contained in:
ccurme 2024-07-24 10:45:00 -04:00 committed by GitHub
parent 22175738ac
commit f337f3ed36
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -284,17 +284,17 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 8, "execution_count": 1,
"id": "173e1a9c-2a18-4669-b0de-136f39197786", "id": "173e1a9c-2a18-4669-b0de-136f39197786",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"\"Arr, matey! I be sailin' the high seas with me crew, searchin' for buried treasure and adventure! How be ye doin' on this fine day?\"" "\"Arrr, I be doin' well, me heartie! Just sailin' the high seas in search of treasure and adventure. How be ye?\""
] ]
}, },
"execution_count": 8, "execution_count": 1,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -316,14 +316,20 @@
"\n", "\n",
"history = InMemoryChatMessageHistory()\n", "history = InMemoryChatMessageHistory()\n",
"\n", "\n",
"\n",
"def get_history():\n",
" return history\n",
"\n",
"\n",
"chain = prompt | ChatOpenAI() | StrOutputParser()\n", "chain = prompt | ChatOpenAI() | StrOutputParser()\n",
"\n", "\n",
"wrapped_chain = RunnableWithMessageHistory(chain, lambda x: history)\n", "wrapped_chain = RunnableWithMessageHistory(\n",
" chain,\n",
" get_history,\n",
" history_messages_key=\"chat_history\",\n",
")\n",
"\n", "\n",
"wrapped_chain.invoke(\n", "wrapped_chain.invoke({\"input\": \"how are you?\"})"
" {\"input\": \"how are you?\"},\n",
" config={\"configurable\": {\"session_id\": \"42\"}},\n",
")"
] ]
}, },
{ {
@ -340,17 +346,17 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 7, "execution_count": 2,
"id": "4e05994f-1fbc-4699-bf2e-62cb0e4deeb8", "id": "4e05994f-1fbc-4699-bf2e-62cb0e4deeb8",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"AIMessage(content=\"Ahoy there! What be ye wantin' from this old pirate?\", response_metadata={'token_usage': {'completion_tokens': 15, 'prompt_tokens': 29, 'total_tokens': 44}, 'model_name': 'gpt-3.5-turbo-0125', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-1846d5f5-0dda-43b6-bb49-864e541f9c29-0', usage_metadata={'input_tokens': 29, 'output_tokens': 15, 'total_tokens': 44})" "'Ahoy matey! What can this old pirate do for ye today?'"
] ]
}, },
"execution_count": 7, "execution_count": 2,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -370,9 +376,16 @@
"\n", "\n",
"chain = prompt | ChatOpenAI() | StrOutputParser()\n", "chain = prompt | ChatOpenAI() | StrOutputParser()\n",
"\n", "\n",
"wrapped_chain = RunnableWithMessageHistory(chain, get_session_history)\n", "wrapped_chain = RunnableWithMessageHistory(\n",
" chain,\n",
" get_session_history,\n",
" history_messages_key=\"chat_history\",\n",
")\n",
"\n", "\n",
"wrapped_chain.invoke(\"Hello!\", config={\"configurable\": {\"session_id\": \"abc123\"}})" "wrapped_chain.invoke(\n",
" {\"input\": \"Hello!\"},\n",
" config={\"configurable\": {\"session_id\": \"abc123\"}},\n",
")"
] ]
}, },
{ {
@ -790,7 +803,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.10.5" "version": "3.10.4"
} }
}, },
"nbformat": 4, "nbformat": 4,