|
5 | 5 | "id": "3f5c86a2-76b6-4cf7-8f94-051684e7784d", |
6 | 6 | "metadata": {}, |
7 | 7 | "source": [ |
8 | | - "# **LangChain Memory**\n", |
| 8 | + "# **LangChain Memory [Beta]**\n", |
9 | 9 | "\n", |
10 | 10 | "LangChain provides a few different options for storing a conversation memory:\n", |
11 | 11 | "- Storing all messages\n", |
|
95 | 95 | }, |
96 | 96 | { |
97 | 97 | "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", |
100 | 100 | "metadata": {}, |
101 | 101 | "outputs": [], |
102 | 102 | "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()" |
106 | 107 | ] |
107 | 108 | }, |
108 | 109 | { |
109 | 110 | "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", |
112 | 113 | "metadata": {}, |
113 | 114 | "outputs": [], |
114 | 115 | "source": [ |
| 116 | + "from langchain_openai import ChatOpenAI\n", |
| 117 | + "\n", |
115 | 118 | "# Setup API Key\n", |
116 | 119 | "\n", |
117 | 120 | "f = open('keys/.openai_api_key.txt')\n", |
118 | 121 | "\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)" |
120 | 126 | ] |
121 | 127 | }, |
122 | 128 | { |
123 | 129 | "cell_type": "code", |
124 | | - "execution_count": 7, |
| 130 | + "execution_count": 3, |
125 | 131 | "id": "b0a0a98b-29f1-4019-b876-e603e533fb53", |
126 | 132 | "metadata": {}, |
127 | 133 | "outputs": [], |
128 | 134 | "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", |
131 | 136 | "\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": [ |
143 | 137 | "# Set up an assistant\n", |
144 | 138 | "conversation = ConversationChain(llm=chat_model, memory=memory, verbose=True)" |
145 | 139 | ] |
146 | 140 | }, |
147 | 141 | { |
148 | 142 | "cell_type": "code", |
149 | | - "execution_count": 9, |
| 143 | + "execution_count": 4, |
150 | 144 | "id": "2bc0dd73-b250-4d87-a3d9-8e98b0508989", |
151 | 145 | "metadata": {}, |
152 | 146 | "outputs": [ |
|
171 | 165 | { |
172 | 166 | "data": { |
173 | 167 | "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?\"" |
175 | 169 | ] |
176 | 170 | }, |
177 | | - "execution_count": 9, |
| 171 | + "execution_count": 4, |
178 | 172 | "metadata": {}, |
179 | 173 | "output_type": "execute_result" |
180 | 174 | } |
|
185 | 179 | }, |
186 | 180 | { |
187 | 181 | "cell_type": "code", |
188 | | - "execution_count": 10, |
| 182 | + "execution_count": 5, |
189 | 183 | "id": "001d762a-d15f-4706-b64c-0039f8a36fda", |
190 | 184 | "metadata": {}, |
191 | 185 | "outputs": [ |
|
201 | 195 | "\n", |
202 | 196 | "Current conversation:\n", |
203 | 197 | "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", |
205 | 199 | "Human: Tell me a nice machine learning fact.\n", |
206 | 200 | "AI:\u001b[0m\n", |
207 | 201 | "\n", |
|
211 | 205 | { |
212 | 206 | "data": { |
213 | 207 | "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.\"" |
215 | 209 | ] |
216 | 210 | }, |
217 | | - "execution_count": 10, |
| 211 | + "execution_count": 5, |
218 | 212 | "metadata": {}, |
219 | 213 | "output_type": "execute_result" |
220 | 214 | } |
|
225 | 219 | }, |
226 | 220 | { |
227 | 221 | "cell_type": "code", |
228 | | - "execution_count": 11, |
| 222 | + "execution_count": 6, |
229 | 223 | "id": "99676123-ff03-4393-b6fb-8820a0c7f5ee", |
230 | 224 | "metadata": {}, |
231 | 225 | "outputs": [ |
|
234 | 228 | "output_type": "stream", |
235 | 229 | "text": [ |
236 | 230 | "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", |
238 | 232 | "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" |
240 | 234 | ] |
241 | 235 | } |
242 | 236 | ], |
|
246 | 240 | }, |
247 | 241 | { |
248 | 242 | "cell_type": "code", |
249 | | - "execution_count": 14, |
| 243 | + "execution_count": 7, |
250 | 244 | "id": "43cc9006-32d3-4523-8c5d-a812b55c11f9", |
251 | 245 | "metadata": {}, |
252 | 246 | "outputs": [ |
253 | 247 | { |
254 | 248 | "name": "stdout", |
255 | 249 | "output_type": "stream", |
256 | 250 | "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" |
258 | 252 | ] |
259 | 253 | } |
260 | 254 | ], |
|
0 commit comments