langchain/docs/modules/memory/examples/conversational_customization.ipynb

388 lines
11 KiB
Plaintext
Raw Normal View History

2022-12-27 14:17:01 +00:00
{
"cells": [
{
"cell_type": "markdown",
"id": "69e35d6f",
"metadata": {},
"source": [
"# Conversational Memory Customization\n",
"\n",
"This notebook walks through a few ways to customize conversational memory."
2022-12-27 14:17:01 +00:00
]
},
{
"cell_type": "code",
"execution_count": 1,
2022-12-27 14:17:01 +00:00
"id": "0f964494",
"metadata": {},
"outputs": [],
"source": [
"from langchain.llms import OpenAI\n",
"from langchain.chains import ConversationChain\n",
"from langchain.chains.conversation.memory import ConversationBufferMemory\n",
"\n",
"\n",
"llm = OpenAI(temperature=0)"
]
},
{
"cell_type": "markdown",
"id": "fe3cd3e9",
"metadata": {},
"source": [
"## AI Prefix\n",
"\n",
"The first way to do so is by changing the AI prefix in the conversation summary. By default, this is set to \"AI\", but you can set this to be anything you want. Note that if you change this, you should also change the prompt used in the chain to reflect this naming change. Let's walk through an example of that in the example below."
]
},
2022-12-27 14:17:01 +00:00
{
"cell_type": "code",
"execution_count": 2,
2022-12-27 14:17:01 +00:00
"id": "d0e66d87",
"metadata": {},
"outputs": [],
"source": [
"# Here it is by default set to \"AI\"\n",
"conversation = ConversationChain(\n",
" llm=llm, \n",
" verbose=True, \n",
" memory=ConversationBufferMemory()\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 3,
2022-12-27 14:17:01 +00:00
"id": "f8fa6999",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\u001b[1m> Entering new ConversationChain chain...\u001b[0m\n",
"Prompt after formatting:\n",
"\u001b[32;1m\u001b[1;3mThe following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.\n",
"\n",
"Current conversation:\n",
"\n",
"Human: Hi there!\n",
"AI:\u001b[0m\n",
"\n",
"\u001b[1m> Finished ConversationChain chain.\u001b[0m\n"
]
},
{
"data": {
"text/plain": [
"\" Hi there! It's nice to meet you. How can I help you today?\""
]
},
"execution_count": 3,
2022-12-27 14:17:01 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"conversation.predict(input=\"Hi there!\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
2022-12-27 14:17:01 +00:00
"id": "de213386",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\u001b[1m> Entering new ConversationChain chain...\u001b[0m\n",
"Prompt after formatting:\n",
"\u001b[32;1m\u001b[1;3mThe following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.\n",
"\n",
"Current conversation:\n",
"\n",
"Human: Hi there!\n",
"AI: Hi there! It's nice to meet you. How can I help you today?\n",
"Human: What's the weather?\n",
"AI:\u001b[0m\n",
"\n",
"\u001b[1m> Finished ConversationChain chain.\u001b[0m\n"
]
},
{
"data": {
"text/plain": [
"' The current weather is sunny and warm with a temperature of 75 degrees Fahrenheit. The forecast for the next few days is sunny with temperatures in the mid-70s.'"
]
},
"execution_count": 4,
2022-12-27 14:17:01 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"conversation.predict(input=\"What's the weather?\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
2022-12-27 14:17:01 +00:00
"id": "585949eb",
"metadata": {},
"outputs": [],
"source": [
"# Now we can override it and set it to \"AI Assistant\"\n",
"from langchain.prompts.prompt import PromptTemplate\n",
"\n",
"template = \"\"\"The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.\n",
"\n",
"Current conversation:\n",
"{history}\n",
"Human: {input}\n",
"AI Assistant:\"\"\"\n",
"PROMPT = PromptTemplate(\n",
" input_variables=[\"history\", \"input\"], template=template\n",
")\n",
"conversation = ConversationChain(\n",
" prompt=PROMPT,\n",
" llm=llm, \n",
" verbose=True, \n",
" memory=ConversationBufferMemory(ai_prefix=\"AI Assistant\")\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 6,
2022-12-27 14:17:01 +00:00
"id": "1bb9bc53",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\u001b[1m> Entering new ConversationChain chain...\u001b[0m\n",
"Prompt after formatting:\n",
"\u001b[32;1m\u001b[1;3mThe following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.\n",
"\n",
"Current conversation:\n",
"\n",
"Human: Hi there!\n",
"AI Assistant:\u001b[0m\n",
"\n",
"\u001b[1m> Finished ConversationChain chain.\u001b[0m\n"
]
},
{
"data": {
"text/plain": [
"\" Hi there! It's nice to meet you. How can I help you today?\""
]
},
"execution_count": 6,
2022-12-27 14:17:01 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"conversation.predict(input=\"Hi there!\")"
]
},
{
"cell_type": "code",
"execution_count": 7,
2022-12-27 14:17:01 +00:00
"id": "d9241923",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\u001b[1m> Entering new ConversationChain chain...\u001b[0m\n",
"Prompt after formatting:\n",
"\u001b[32;1m\u001b[1;3mThe following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.\n",
"\n",
"Current conversation:\n",
"\n",
"Human: Hi there!\n",
"AI Assistant: Hi there! It's nice to meet you. How can I help you today?\n",
"Human: What's the weather?\n",
"AI Assistant:\u001b[0m\n",
"\n",
"\u001b[1m> Finished ConversationChain chain.\u001b[0m\n"
]
},
{
"data": {
"text/plain": [
"' The current weather is sunny and warm with a temperature of 75 degrees Fahrenheit. The forecast for the rest of the day is sunny with a high of 78 degrees and a low of 65 degrees.'"
]
},
"execution_count": 7,
2022-12-27 14:17:01 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"conversation.predict(input=\"What's the weather?\")"
]
},
{
"cell_type": "markdown",
"id": "0517ccf8",
"metadata": {},
"source": [
"## Human Prefix\n",
"\n",
"The next way to do so is by changing the Human prefix in the conversation summary. By default, this is set to \"Human\", but you can set this to be anything you want. Note that if you change this, you should also change the prompt used in the chain to reflect this naming change. Let's walk through an example of that in the example below."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "6357a461",
"metadata": {},
"outputs": [],
"source": [
"# Now we can override it and set it to \"Friend\"\n",
"from langchain.prompts.prompt import PromptTemplate\n",
"\n",
"template = \"\"\"The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.\n",
"\n",
"Current conversation:\n",
"{history}\n",
"Friend: {input}\n",
"AI:\"\"\"\n",
"PROMPT = PromptTemplate(\n",
" input_variables=[\"history\", \"input\"], template=template\n",
")\n",
"conversation = ConversationChain(\n",
" prompt=PROMPT,\n",
" llm=llm, \n",
" verbose=True, \n",
" memory=ConversationBufferMemory(human_prefix=\"Friend\")\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "969b6f54",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\u001b[1m> Entering new ConversationChain chain...\u001b[0m\n",
"Prompt after formatting:\n",
"\u001b[32;1m\u001b[1;3mThe following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.\n",
"\n",
"Current conversation:\n",
"\n",
"Friend: Hi there!\n",
"AI:\u001b[0m\n",
"\n",
"\u001b[1m> Finished ConversationChain chain.\u001b[0m\n"
]
},
{
"data": {
"text/plain": [
"\" Hi there! It's nice to meet you. How can I help you today?\""
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"conversation.predict(input=\"Hi there!\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "d5ea82bb",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\u001b[1m> Entering new ConversationChain chain...\u001b[0m\n",
"Prompt after formatting:\n",
"\u001b[32;1m\u001b[1;3mThe following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.\n",
"\n",
"Current conversation:\n",
"\n",
"Friend: Hi there!\n",
"AI: Hi there! It's nice to meet you. How can I help you today?\n",
"Friend: What's the weather?\n",
"AI:\u001b[0m\n",
"\n",
"\u001b[1m> Finished ConversationChain chain.\u001b[0m\n"
]
},
{
"data": {
"text/plain": [
"' The weather right now is sunny and warm with a temperature of 75 degrees Fahrenheit. The forecast for the rest of the day is mostly sunny with a high of 82 degrees.'"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"conversation.predict(input=\"What's the weather?\")"
]
},
2022-12-27 14:17:01 +00:00
{
"cell_type": "code",
"execution_count": null,
"id": "ce7f79ab",
2022-12-27 14:17:01 +00:00
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.9"
2022-12-27 14:17:01 +00:00
}
},
"nbformat": 4,
"nbformat_minor": 5
}