mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-23 11:30:37 +00:00
@@ -7,7 +7,7 @@
|
||||
"source": [
|
||||
"# Adding Memory To an LLMChain\n",
|
||||
"\n",
|
||||
"This notebook goes over how to use the Memory class with an arbitrary chain. For the purposes of this walkthrough, we will add `ConversationBufferMemory` to a `LLMChain`."
|
||||
"This notebook goes over how to use the Memory class with an LLMChain. For the purposes of this walkthrough, we will add the `ConversationBufferMemory` class, although this can be any memory class."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -46,7 +46,7 @@
|
||||
" input_variables=[\"chat_history\", \"human_input\"], \n",
|
||||
" template=template\n",
|
||||
")\n",
|
||||
"memory = ConversationBufferMemory(dynamic_key=\"chat_history\")"
|
||||
"memory = ConversationBufferMemory(memory_key=\"chat_history\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -90,7 +90,7 @@
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"'\\n\\nHi there my friend! Thank you for talking with me.'"
|
||||
"' Hi there!'"
|
||||
]
|
||||
},
|
||||
"execution_count": 4,
|
||||
@@ -120,9 +120,7 @@
|
||||
"\n",
|
||||
"\n",
|
||||
"Human: Hi there my friend\n",
|
||||
"AI: \n",
|
||||
"\n",
|
||||
"Hi there my friend! Thank you for talking with me.\n",
|
||||
"AI: Hi there!\n",
|
||||
"Human: Not to bad - how are you?\n",
|
||||
"Chatbot:\u001b[0m\n",
|
||||
"\n",
|
||||
@@ -132,7 +130,7 @@
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"\"\\n\\nI'm doing well, thank you for asking. How about you?\""
|
||||
"\"\\n\\nI'm doing well, thanks for asking. How about you?\""
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
|
@@ -66,7 +66,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"execution_count": 8,
|
||||
"id": "1d45d429",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@@ -77,21 +77,21 @@
|
||||
" # Define dictionary to store information about entities.\n",
|
||||
" entities: dict = {}\n",
|
||||
" # Define key to pass information about entities into prompt.\n",
|
||||
" dynamic_key: str = \"entities\"\n",
|
||||
" memory_key: str = \"entities\"\n",
|
||||
"\n",
|
||||
" @property\n",
|
||||
" def dynamic_keys(self) -> List[str]:\n",
|
||||
" \"\"\"Define the keys we are providing to the prompt.\"\"\"\n",
|
||||
" return [self.dynamic_key]\n",
|
||||
" def memory_variables(self) -> List[str]:\n",
|
||||
" \"\"\"Define the variables we are providing to the prompt.\"\"\"\n",
|
||||
" return [self.memory_key]\n",
|
||||
"\n",
|
||||
" def load_dynamic_keys(self, inputs: Dict[str, Any]) -> Dict[str, str]:\n",
|
||||
" \"\"\"Load the dynamic keys, in this case the entity key.\"\"\"\n",
|
||||
" def load_memory_variables(self, inputs: Dict[str, Any]) -> Dict[str, str]:\n",
|
||||
" \"\"\"Load the memory variables, in this case the entity key.\"\"\"\n",
|
||||
" # Get the input text and run through spacy\n",
|
||||
" doc = nlp(inputs[list(inputs.keys())[0]])\n",
|
||||
" # Extract known information about entities, if they exist.\n",
|
||||
" entities = [self.entities[str(ent)] for ent in doc.ents if str(ent) in self.entities]\n",
|
||||
" # Return combined information about entities to put into context.\n",
|
||||
" return {self.dynamic_key: \"\\n\".join(entities)}\n",
|
||||
" return {self.memory_key: \"\\n\".join(entities)}\n",
|
||||
"\n",
|
||||
" def save_context(self, inputs: Dict[str, Any], outputs: Dict[str, str]) -> None:\n",
|
||||
" \"\"\"Save context from this conversation to buffer.\"\"\"\n",
|
||||
@@ -117,7 +117,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"execution_count": 9,
|
||||
"id": "c05159b6",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@@ -147,7 +147,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"execution_count": 10,
|
||||
"id": "f08dc8ed",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@@ -166,7 +166,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"execution_count": 11,
|
||||
"id": "5b96e836",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@@ -196,7 +196,7 @@
|
||||
"\"\\n\\nThat's really interesting! I'm sure he has a lot of fun with it.\""
|
||||
]
|
||||
},
|
||||
"execution_count": 7,
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -215,7 +215,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"execution_count": 12,
|
||||
"id": "4bca7070",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@@ -245,7 +245,7 @@
|
||||
"\" Harrison's favorite subject in college was machine learning.\""
|
||||
]
|
||||
},
|
||||
"execution_count": 8,
|
||||
"execution_count": 12,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
|
Reference in New Issue
Block a user