Skip to content

Commit

Permalink
Demonstrate use of get_buffer_string (langchain-ai#13013)
Browse files Browse the repository at this point in the history
**Description**

The docs for creating a RAG chain with Memory [currently use a manual
lambda](https://python.langchain.com/docs/expression_language/cookbook/retrieval#with-memory-and-returning-source-documents)
to format chat history messages. [There exists a helper method within
the
codebase](https://github.com/langchain-ai/langchain/blob/master/libs/langchain/langchain/schema/messages.py#L14C15-L14C15)
to perform this task so I've updated the documentation to demonstrate
its usage

Also worth noting that the current documented method of using the
included `_format_chat_history ` function actually results in an error:

```
TypeError: 'HumanMessage' object is not subscriptable
```

---------

Co-authored-by: Harrison Chase <[email protected]>
  • Loading branch information
2 people authored and aymeric-roucher committed Dec 11, 2023
1 parent 2d9bec0 commit d1b639e
Showing 1 changed file with 73 additions and 61 deletions.
134 changes: 73 additions & 61 deletions docs/docs/expression_language/cookbook/retrieval.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 1,
"id": "33be32af",
"metadata": {},
"outputs": [],
Expand All @@ -48,7 +48,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 2,
"id": "bfc47ec1",
"metadata": {},
"outputs": [],
Expand All @@ -70,7 +70,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 3,
"id": "eae31755",
"metadata": {},
"outputs": [],
Expand All @@ -85,7 +85,7 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 4,
"id": "f3040b0c",
"metadata": {},
"outputs": [
Expand All @@ -95,7 +95,7 @@
"'Harrison worked at Kensho.'"
]
},
"execution_count": 5,
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -106,7 +106,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 5,
"id": "e1d20c7c",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -134,7 +134,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 6,
"id": "7ee8b2d4",
"metadata": {},
"outputs": [
Expand All @@ -144,7 +144,7 @@
"'Harrison ha lavorato a Kensho.'"
]
},
"execution_count": 7,
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -165,18 +165,20 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 21,
"id": "3f30c348",
"metadata": {},
"outputs": [],
"source": [
"from langchain.schema import format_document\n",
"from langchain.schema.runnable import RunnableParallel"
"from langchain.schema.messages import get_buffer_string\n",
"from langchain.schema.runnable import RunnableParallel\n",
"from langchain_core.messages import AIMessage, HumanMessage"
]
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 8,
"id": "64ab1dbf",
"metadata": {},
"outputs": [],
Expand All @@ -194,7 +196,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 9,
"id": "7d628c97",
"metadata": {},
"outputs": [],
Expand All @@ -209,7 +211,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 10,
"id": "f60a5d0f",
"metadata": {},
"outputs": [],
Expand All @@ -226,39 +228,14 @@
},
{
"cell_type": "code",
"execution_count": 12,
"id": "7d007db6",
"metadata": {},
"outputs": [],
"source": [
"from typing import List, Tuple\n",
"\n",
"\n",
"def _format_chat_history(chat_history: List[Tuple[str, str]]) -> str:\n",
" # chat history is of format:\n",
" # [\n",
" # (human_message_str, ai_message_str),\n",
" # ...\n",
" # ]\n",
" # see below for an example of how it's invoked\n",
" buffer = \"\"\n",
" for dialogue_turn in chat_history:\n",
" human = \"Human: \" + dialogue_turn[0]\n",
" ai = \"Assistant: \" + dialogue_turn[1]\n",
" buffer += \"\\n\" + \"\\n\".join([human, ai])\n",
" return buffer"
]
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 11,
"id": "5c32cc89",
"metadata": {},
"outputs": [],
"source": [
"_inputs = RunnableParallel(\n",
" standalone_question=RunnablePassthrough.assign(\n",
" chat_history=lambda x: _format_chat_history(x[\"chat_history\"])\n",
" chat_history=lambda x: get_buffer_string(x[\"chat_history\"])\n",
" )\n",
" | CONDENSE_QUESTION_PROMPT\n",
" | ChatOpenAI(temperature=0)\n",
Expand All @@ -273,17 +250,17 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 12,
"id": "135c8205",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"AIMessage(content='Harrison was employed at Kensho.', additional_kwargs={}, example=False)"
"AIMessage(content='Harrison was employed at Kensho.')"
]
},
"execution_count": 14,
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -299,17 +276,17 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 22,
"id": "424e7e7a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"AIMessage(content='Harrison worked at Kensho.', additional_kwargs={}, example=False)"
"AIMessage(content='Harrison worked at Kensho.')"
]
},
"execution_count": 15,
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -318,7 +295,10 @@
"conversational_qa_chain.invoke(\n",
" {\n",
" \"question\": \"where did he work?\",\n",
" \"chat_history\": [(\"Who wrote this notebook?\", \"Harrison\")],\n",
" \"chat_history\": [\n",
" HumanMessage(content=\"Who wrote this notebook?\"),\n",
" AIMessage(content=\"Harrison\"),\n",
" ],\n",
" }\n",
")"
]
Expand All @@ -335,7 +315,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 14,
"id": "e31dd17c",
"metadata": {},
"outputs": [],
Expand All @@ -347,7 +327,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 15,
"id": "d4bffe94",
"metadata": {},
"outputs": [],
Expand All @@ -359,7 +339,7 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 16,
"id": "733be985",
"metadata": {},
"outputs": [],
Expand All @@ -373,7 +353,7 @@
"standalone_question = {\n",
" \"standalone_question\": {\n",
" \"question\": lambda x: x[\"question\"],\n",
" \"chat_history\": lambda x: _format_chat_history(x[\"chat_history\"]),\n",
" \"chat_history\": lambda x: get_buffer_string(x[\"chat_history\"]),\n",
" }\n",
" | CONDENSE_QUESTION_PROMPT\n",
" | ChatOpenAI(temperature=0)\n",
Expand All @@ -400,18 +380,18 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 17,
"id": "806e390c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'answer': AIMessage(content='Harrison was employed at Kensho.', additional_kwargs={}, example=False),\n",
" 'docs': [Document(page_content='harrison worked at kensho', metadata={})]}"
"{'answer': AIMessage(content='Harrison was employed at Kensho.'),\n",
" 'docs': [Document(page_content='harrison worked at kensho')]}"
]
},
"execution_count": 19,
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -424,7 +404,7 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 18,
"id": "977399fd",
"metadata": {},
"outputs": [],
Expand All @@ -437,25 +417,57 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 19,
"id": "f94f7de4",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'history': [HumanMessage(content='where did harrison work?', additional_kwargs={}, example=False),\n",
" AIMessage(content='Harrison was employed at Kensho.', additional_kwargs={}, example=False)]}"
"{'history': [HumanMessage(content='where did harrison work?'),\n",
" AIMessage(content='Harrison was employed at Kensho.')]}"
]
},
"execution_count": 21,
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"memory.load_memory_variables({})"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "88f2b7cd",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'answer': AIMessage(content='Harrison actually worked at Kensho.'),\n",
" 'docs': [Document(page_content='harrison worked at kensho')]}"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"inputs = {\"question\": \"but where did he really work?\"}\n",
"result = final_chain.invoke(inputs)\n",
"result"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "207a2782",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -474,7 +486,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.1"
"version": "3.10.1"
}
},
"nbformat": 4,
Expand Down

0 comments on commit d1b639e

Please sign in to comment.