Skip to content

Commit 76efb13

Browse files
committed
Code Update
1 parent 7a0766e commit 76efb13

File tree

1 file changed

+29
-35
lines changed
  • Module 9 - GenAI (LLMs and Prompt Engineering)/6. Building Apps Powered by GenAI using LangChain/1. Introduction to LangChain/6. Memory

1 file changed

+29
-35
lines changed
Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"id": "3f5c86a2-76b6-4cf7-8f94-051684e7784d",
66
"metadata": {},
77
"source": [
8-
"# **LangChain Memory**\n",
8+
"# **LangChain Memory [Beta]**\n",
99
"\n",
1010
"LangChain provides a few different options for storing a conversation memory:\n",
1111
"- Storing all messages\n",
@@ -95,58 +95,52 @@
9595
},
9696
{
9797
"cell_type": "code",
98-
"execution_count": 5,
99-
"id": "68d0c98f-d4c6-4a53-89f1-11c67009d285",
98+
"execution_count": 1,
99+
"id": "37f451bf-b4c1-4f49-afb5-125a09115327",
100100
"metadata": {},
101101
"outputs": [],
102102
"source": [
103-
"from langchain_openai import ChatOpenAI\n",
104-
"from langchain.chains import ConversationChain\n",
105-
"from langchain.memory import ConversationBufferMemory"
103+
"from langchain.memory import ConversationBufferMemory\n",
104+
"\n",
105+
"# Set up an instance of Conversation buffer memory\n",
106+
"memory = ConversationBufferMemory()"
106107
]
107108
},
108109
{
109110
"cell_type": "code",
110-
"execution_count": 6,
111-
"id": "7b998268-325b-4d47-8e09-2a3fa720ca20",
111+
"execution_count": 2,
112+
"id": "68d0c98f-d4c6-4a53-89f1-11c67009d285",
112113
"metadata": {},
113114
"outputs": [],
114115
"source": [
116+
"from langchain_openai import ChatOpenAI\n",
117+
"\n",
115118
"# Setup API Key\n",
116119
"\n",
117120
"f = open('keys/.openai_api_key.txt')\n",
118121
"\n",
119-
"OPENAI_API_KEY = f.read()"
122+
"OPENAI_API_KEY = f.read()\n",
123+
"\n",
124+
"# Set the OpenAI Key and initialize a ChatModel\n",
125+
"chat_model = ChatOpenAI(openai_api_key=OPENAI_API_KEY)"
120126
]
121127
},
122128
{
123129
"cell_type": "code",
124-
"execution_count": 7,
130+
"execution_count": 3,
125131
"id": "b0a0a98b-29f1-4019-b876-e603e533fb53",
126132
"metadata": {},
127133
"outputs": [],
128134
"source": [
129-
"# Set the OpenAI Key and initialize a ChatModel\n",
130-
"chat_model = ChatOpenAI(openai_api_key=OPENAI_API_KEY)\n",
135+
"from langchain.chains import ConversationChain\n",
131136
"\n",
132-
"# Set up an instance of Conversation buffer memory\n",
133-
"memory = ConversationBufferMemory()"
134-
]
135-
},
136-
{
137-
"cell_type": "code",
138-
"execution_count": 8,
139-
"id": "1352f945-88cc-467a-b2d2-142605eba5fe",
140-
"metadata": {},
141-
"outputs": [],
142-
"source": [
143137
"# Set up an assistant\n",
144138
"conversation = ConversationChain(llm=chat_model, memory=memory, verbose=True)"
145139
]
146140
},
147141
{
148142
"cell_type": "code",
149-
"execution_count": 9,
143+
"execution_count": 4,
150144
"id": "2bc0dd73-b250-4d87-a3d9-8e98b0508989",
151145
"metadata": {},
152146
"outputs": [
@@ -171,10 +165,10 @@
171165
{
172166
"data": {
173167
"text/plain": [
174-
"\"Hello! It's nice to meet you too. I am an AI programmed to assist you with any questions or tasks you may have. How can I help you today?\""
168+
"\"Hello! It's great to meet you too. I'm an AI designed to assist with any questions or information you may need. How can I help you today?\""
175169
]
176170
},
177-
"execution_count": 9,
171+
"execution_count": 4,
178172
"metadata": {},
179173
"output_type": "execute_result"
180174
}
@@ -185,7 +179,7 @@
185179
},
186180
{
187181
"cell_type": "code",
188-
"execution_count": 10,
182+
"execution_count": 5,
189183
"id": "001d762a-d15f-4706-b64c-0039f8a36fda",
190184
"metadata": {},
191185
"outputs": [
@@ -201,7 +195,7 @@
201195
"\n",
202196
"Current conversation:\n",
203197
"Human: Hello, Nice to meet you.\n",
204-
"AI: Hello! It's nice to meet you too. I am an AI programmed to assist you with any questions or tasks you may have. How can I help you today?\n",
198+
"AI: Hello! It's great to meet you too. I'm an AI designed to assist with any questions or information you may need. How can I help you today?\n",
205199
"Human: Tell me a nice machine learning fact.\n",
206200
"AI:\u001b[0m\n",
207201
"\n",
@@ -211,10 +205,10 @@
211205
{
212206
"data": {
213207
"text/plain": [
214-
"\"Sure! Did you know that machine learning algorithms can analyze large amounts of data to find patterns and make predictions without being explicitly programmed to do so? It's pretty fascinating how they can learn and improve over time based on the data they are given.\""
208+
"\"Sure! Did you know that machine learning is a type of artificial intelligence that allows computers to learn and improve from experience without being explicitly programmed? It's a fascinating field that has seen rapid growth and development in recent years.\""
215209
]
216210
},
217-
"execution_count": 10,
211+
"execution_count": 5,
218212
"metadata": {},
219213
"output_type": "execute_result"
220214
}
@@ -225,7 +219,7 @@
225219
},
226220
{
227221
"cell_type": "code",
228-
"execution_count": 11,
222+
"execution_count": 6,
229223
"id": "99676123-ff03-4393-b6fb-8820a0c7f5ee",
230224
"metadata": {},
231225
"outputs": [
@@ -234,9 +228,9 @@
234228
"output_type": "stream",
235229
"text": [
236230
"Human: Hello, Nice to meet you.\n",
237-
"AI: Hello! It's nice to meet you too. I am an AI programmed to assist you with any questions or tasks you may have. How can I help you today?\n",
231+
"AI: Hello! It's great to meet you too. I'm an AI designed to assist with any questions or information you may need. How can I help you today?\n",
238232
"Human: Tell me a nice machine learning fact.\n",
239-
"AI: Sure! Did you know that machine learning algorithms can analyze large amounts of data to find patterns and make predictions without being explicitly programmed to do so? It's pretty fascinating how they can learn and improve over time based on the data they are given.\n"
233+
"AI: Sure! Did you know that machine learning is a type of artificial intelligence that allows computers to learn and improve from experience without being explicitly programmed? It's a fascinating field that has seen rapid growth and development in recent years.\n"
240234
]
241235
}
242236
],
@@ -246,15 +240,15 @@
246240
},
247241
{
248242
"cell_type": "code",
249-
"execution_count": 14,
243+
"execution_count": 7,
250244
"id": "43cc9006-32d3-4523-8c5d-a812b55c11f9",
251245
"metadata": {},
252246
"outputs": [
253247
{
254248
"name": "stdout",
255249
"output_type": "stream",
256250
"text": [
257-
"chat_memory=ChatMessageHistory(messages=[HumanMessage(content='Hello, Nice to meet you.'), AIMessage(content=\"Hello! It's nice to meet you too. I am an AI programmed to assist you with any questions or tasks you may have. How can I help you today?\"), HumanMessage(content='Tell me a nice machine learning fact.'), AIMessage(content=\"Sure! Did you know that machine learning algorithms can analyze large amounts of data to find patterns and make predictions without being explicitly programmed to do so? It's pretty fascinating how they can learn and improve over time based on the data they are given.\")])\n"
251+
"chat_memory=ChatMessageHistory(messages=[HumanMessage(content='Hello, Nice to meet you.'), AIMessage(content=\"Hello! It's great to meet you too. I'm an AI designed to assist with any questions or information you may need. How can I help you today?\"), HumanMessage(content='Tell me a nice machine learning fact.'), AIMessage(content=\"Sure! Did you know that machine learning is a type of artificial intelligence that allows computers to learn and improve from experience without being explicitly programmed? It's a fascinating field that has seen rapid growth and development in recent years.\")])\n"
258252
]
259253
}
260254
],

0 commit comments

Comments
 (0)