Harrison/summary memory history (#4649)

Co-authored-by: engkheng <60956360+outday29@users.noreply.github.com>
This commit is contained in:
Harrison Chase
2023-05-13 21:46:11 -07:00
committed by GitHub
parent 44ae673388
commit c09bb00959
2 changed files with 73 additions and 1 deletions

View File

@@ -18,7 +18,7 @@
"metadata": {},
"outputs": [],
"source": [
"from langchain.memory import ConversationSummaryMemory\n",
"from langchain.memory import ConversationSummaryMemory, ChatMessageHistory\n",
"from langchain.llms import OpenAI"
]
},
@@ -125,6 +125,59 @@
"memory.predict_new_summary(messages, previous_summary)"
]
},
{
"cell_type": "markdown",
"id": "fa3ad83f",
"metadata": {},
"source": [
"## Initializing with messages\n",
"\n",
"If you have messages outside this class, you can easily initialize the class with ChatMessageHistory. During loading, a summary will be calculated."
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "80fd072b",
"metadata": {},
"outputs": [],
"source": [
"history = ChatMessageHistory()\n",
"history.add_user_message(\"hi\")\n",
"history.add_ai_message(\"hi there!\")"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "ee9c74ad",
"metadata": {},
"outputs": [],
"source": [
"memory = ConversationSummaryMemory.from_messages(llm=OpenAI(temperature=0), chat_memory=history, return_messages=True)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "0ce6924d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'\\nThe human greets the AI, to which the AI responds with a friendly greeting.'"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"memory.buffer"
]
},
{
"cell_type": "markdown",
"id": "4fad9448",