Harrison/add human prefix (#520)

Co-authored-by: Andrew Huang <jhuang16888@gmail.com>
This commit is contained in:
Harrison Chase
2023-01-03 08:03:50 -08:00
committed by GitHub
parent 03f185bcd5
commit 0db05b6725
3 changed files with 144 additions and 8 deletions

View File

@@ -7,9 +7,7 @@
"source": [
"# Conversational Memory Customization\n",
"\n",
"This notebook walks through a few ways to customize conversational memory.\n",
"\n",
"The main 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."
"This notebook walks through a few ways to customize conversational memory."
]
},
{
@@ -27,6 +25,16 @@
"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."
]
},
{
"cell_type": "code",
"execution_count": 2,
@@ -229,10 +237,127 @@
"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?\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1023b6ef",
"id": "ce7f79ab",
"metadata": {},
"outputs": [],
"source": []