DiegoDV commited on
Commit
69a3403
1 Parent(s): c1d5539

Upload Chat Model Intro.ipynb

Browse files
Files changed (1) hide show
  1. Chat Model Intro.ipynb +331 -0
Chat Model Intro.ipynb ADDED
@@ -0,0 +1,331 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "0fbd8c48",
6
+ "metadata": {},
7
+ "source": [
8
+ "<font color='green'>\n",
9
+ "Pip install is the command you use to install Python packages with the help of a tool called Pip package manager.\n",
10
+ "<br><br>Installing LangChain package\n",
11
+ "</font>"
12
+ ]
13
+ },
14
+ {
15
+ "cell_type": "code",
16
+ "execution_count": 1,
17
+ "id": "d4e032ba",
18
+ "metadata": {},
19
+ "outputs": [
20
+ {
21
+ "name": "stdout",
22
+ "output_type": "stream",
23
+ "text": [
24
+ "Requirement already satisfied: langchain in c:\\users\\user\\anaconda3\\lib\\site-packages (0.0.181)\n",
25
+ "Requirement already satisfied: aiohttp<4.0.0,>=3.8.3 in c:\\users\\user\\anaconda3\\lib\\site-packages (from langchain) (3.8.4)\n",
26
+ "Requirement already satisfied: numpy<2,>=1 in c:\\users\\user\\anaconda3\\lib\\site-packages (from langchain) (1.23.5)\n",
27
+ "Requirement already satisfied: requests<3,>=2 in c:\\users\\user\\anaconda3\\lib\\site-packages (from langchain) (2.28.1)\n",
28
+ "Requirement already satisfied: openapi-schema-pydantic<2.0,>=1.2 in c:\\users\\user\\anaconda3\\lib\\site-packages (from langchain) (1.2.4)\n",
29
+ "Requirement already satisfied: SQLAlchemy<3,>=1.4 in c:\\users\\user\\anaconda3\\lib\\site-packages (from langchain) (1.4.39)\n",
30
+ "Requirement already satisfied: pydantic<2,>=1 in c:\\users\\user\\anaconda3\\lib\\site-packages (from langchain) (1.10.8)\n",
31
+ "Requirement already satisfied: PyYAML>=5.4.1 in c:\\users\\user\\anaconda3\\lib\\site-packages (from langchain) (6.0)\n",
32
+ "Requirement already satisfied: dataclasses-json<0.6.0,>=0.5.7 in c:\\users\\user\\anaconda3\\lib\\site-packages (from langchain) (0.5.7)\n",
33
+ "Requirement already satisfied: async-timeout<5.0.0,>=4.0.0 in c:\\users\\user\\anaconda3\\lib\\site-packages (from langchain) (4.0.2)\n",
34
+ "Requirement already satisfied: tenacity<9.0.0,>=8.1.0 in c:\\users\\user\\anaconda3\\lib\\site-packages (from langchain) (8.2.2)\n",
35
+ "Requirement already satisfied: numexpr<3.0.0,>=2.8.4 in c:\\users\\user\\anaconda3\\lib\\site-packages (from langchain) (2.8.4)\n",
36
+ "Requirement already satisfied: aiosignal>=1.1.2 in c:\\users\\user\\anaconda3\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.3.1)\n",
37
+ "Requirement already satisfied: frozenlist>=1.1.1 in c:\\users\\user\\anaconda3\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.3.3)\n",
38
+ "Requirement already satisfied: attrs>=17.3.0 in c:\\users\\user\\anaconda3\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (22.1.0)\n",
39
+ "Requirement already satisfied: charset-normalizer<4.0,>=2.0 in c:\\users\\user\\anaconda3\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (2.0.4)\n",
40
+ "Requirement already satisfied: multidict<7.0,>=4.5 in c:\\users\\user\\anaconda3\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (6.0.4)\n",
41
+ "Requirement already satisfied: yarl<2.0,>=1.0 in c:\\users\\user\\anaconda3\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.9.2)\n",
42
+ "Requirement already satisfied: marshmallow<4.0.0,>=3.3.0 in c:\\users\\user\\anaconda3\\lib\\site-packages (from dataclasses-json<0.6.0,>=0.5.7->langchain) (3.19.0)\n",
43
+ "Requirement already satisfied: typing-inspect>=0.4.0 in c:\\users\\user\\anaconda3\\lib\\site-packages (from dataclasses-json<0.6.0,>=0.5.7->langchain) (0.9.0)\n",
44
+ "Requirement already satisfied: marshmallow-enum<2.0.0,>=1.5.1 in c:\\users\\user\\anaconda3\\lib\\site-packages (from dataclasses-json<0.6.0,>=0.5.7->langchain) (1.5.1)\n",
45
+ "Requirement already satisfied: typing-extensions>=4.2.0 in c:\\users\\user\\anaconda3\\lib\\site-packages (from pydantic<2,>=1->langchain) (4.4.0)\n",
46
+ "Requirement already satisfied: urllib3<1.27,>=1.21.1 in c:\\users\\user\\anaconda3\\lib\\site-packages (from requests<3,>=2->langchain) (1.26.14)\n",
47
+ "Requirement already satisfied: certifi>=2017.4.17 in c:\\users\\user\\anaconda3\\lib\\site-packages (from requests<3,>=2->langchain) (2022.12.7)\n",
48
+ "Requirement already satisfied: idna<4,>=2.5 in c:\\users\\user\\anaconda3\\lib\\site-packages (from requests<3,>=2->langchain) (3.4)\n",
49
+ "Requirement already satisfied: greenlet!=0.4.17 in c:\\users\\user\\anaconda3\\lib\\site-packages (from SQLAlchemy<3,>=1.4->langchain) (2.0.1)\n",
50
+ "Requirement already satisfied: packaging>=17.0 in c:\\users\\user\\anaconda3\\lib\\site-packages (from marshmallow<4.0.0,>=3.3.0->dataclasses-json<0.6.0,>=0.5.7->langchain) (22.0)\n",
51
+ "Requirement already satisfied: mypy-extensions>=0.3.0 in c:\\users\\user\\anaconda3\\lib\\site-packages (from typing-inspect>=0.4.0->dataclasses-json<0.6.0,>=0.5.7->langchain) (0.4.3)\n"
52
+ ]
53
+ }
54
+ ],
55
+ "source": [
56
+ "!pip install langchain"
57
+ ]
58
+ },
59
+ {
60
+ "cell_type": "markdown",
61
+ "id": "45626414",
62
+ "metadata": {},
63
+ "source": [
64
+ "## Let's use OpenAI"
65
+ ]
66
+ },
67
+ {
68
+ "cell_type": "markdown",
69
+ "id": "91756516",
70
+ "metadata": {},
71
+ "source": [
72
+ "<font color='green'>\n",
73
+ "Installing Openai package, which includes the classes that we can use to communicate with Openai services\n",
74
+ "<font>"
75
+ ]
76
+ },
77
+ {
78
+ "cell_type": "code",
79
+ "execution_count": 2,
80
+ "id": "f3471ee1",
81
+ "metadata": {},
82
+ "outputs": [
83
+ {
84
+ "name": "stdout",
85
+ "output_type": "stream",
86
+ "text": [
87
+ "Requirement already satisfied: Openai in c:\\users\\user\\anaconda3\\lib\\site-packages (0.27.7)\n",
88
+ "Requirement already satisfied: requests>=2.20 in c:\\users\\user\\anaconda3\\lib\\site-packages (from Openai) (2.28.1)\n",
89
+ "Requirement already satisfied: tqdm in c:\\users\\user\\anaconda3\\lib\\site-packages (from Openai) (4.64.1)\n",
90
+ "Requirement already satisfied: aiohttp in c:\\users\\user\\anaconda3\\lib\\site-packages (from Openai) (3.8.4)\n",
91
+ "Requirement already satisfied: urllib3<1.27,>=1.21.1 in c:\\users\\user\\anaconda3\\lib\\site-packages (from requests>=2.20->Openai) (1.26.14)\n",
92
+ "Requirement already satisfied: idna<4,>=2.5 in c:\\users\\user\\anaconda3\\lib\\site-packages (from requests>=2.20->Openai) (3.4)\n",
93
+ "Requirement already satisfied: charset-normalizer<3,>=2 in c:\\users\\user\\anaconda3\\lib\\site-packages (from requests>=2.20->Openai) (2.0.4)\n",
94
+ "Requirement already satisfied: certifi>=2017.4.17 in c:\\users\\user\\anaconda3\\lib\\site-packages (from requests>=2.20->Openai) (2022.12.7)\n",
95
+ "Requirement already satisfied: attrs>=17.3.0 in c:\\users\\user\\anaconda3\\lib\\site-packages (from aiohttp->Openai) (22.1.0)\n",
96
+ "Requirement already satisfied: multidict<7.0,>=4.5 in c:\\users\\user\\anaconda3\\lib\\site-packages (from aiohttp->Openai) (6.0.4)\n",
97
+ "Requirement already satisfied: async-timeout<5.0,>=4.0.0a3 in c:\\users\\user\\anaconda3\\lib\\site-packages (from aiohttp->Openai) (4.0.2)\n",
98
+ "Requirement already satisfied: aiosignal>=1.1.2 in c:\\users\\user\\anaconda3\\lib\\site-packages (from aiohttp->Openai) (1.3.1)\n",
99
+ "Requirement already satisfied: frozenlist>=1.1.1 in c:\\users\\user\\anaconda3\\lib\\site-packages (from aiohttp->Openai) (1.3.3)\n",
100
+ "Requirement already satisfied: yarl<2.0,>=1.0 in c:\\users\\user\\anaconda3\\lib\\site-packages (from aiohttp->Openai) (1.9.2)\n",
101
+ "Requirement already satisfied: colorama in c:\\users\\user\\anaconda3\\lib\\site-packages (from tqdm->Openai) (0.4.6)\n"
102
+ ]
103
+ }
104
+ ],
105
+ "source": [
106
+ "!pip install Openai"
107
+ ]
108
+ },
109
+ {
110
+ "cell_type": "markdown",
111
+ "id": "cb45820c",
112
+ "metadata": {},
113
+ "source": [
114
+ "<font color='green'>\n",
115
+ "Imports the Python built-in module called \"os.\"\n",
116
+ "<br>This module provides a way to interact with the operating system, such as accessing environment variables, working with files and directories, executing shell commands, etc\n",
117
+ "<br><br>\n",
118
+ "The environ attribute is a dictionary-like object that contains the environment variables of the current operating system session\n",
119
+ "<br><br>\n",
120
+ "By accessing os.environ, you can retrieve and manipulate environment variables within your Python program. For example, you can retrieve the value of a specific environment variable using the syntax os.environ['VARIABLE_NAME'], where \"VARIABLE_NAME\" is the name of the environment variable you want to access.\n",
121
+ "<font>"
122
+ ]
123
+ },
124
+ {
125
+ "cell_type": "code",
126
+ "execution_count": 3,
127
+ "id": "9cfce38a",
128
+ "metadata": {},
129
+ "outputs": [],
130
+ "source": [
131
+ "import os\n",
132
+ "os.environ[\"OPENAI_API_KEY\"] = \"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\""
133
+ ]
134
+ },
135
+ {
136
+ "cell_type": "markdown",
137
+ "id": "ad0d8d98",
138
+ "metadata": {},
139
+ "source": [
140
+ "<font color='green'>\n",
141
+ "LangChain has built a Wrapper around OpenAI APIs, using which we can get access to all the services OpenAI provides.\n",
142
+ "<br>\n",
143
+ "The code snippet below imports a specific class called 'ChatOpenAI'(Wrapper around OpenAI large language models) from the 'chat_models' module of the 'langchain' library.\n",
144
+ "\n",
145
+ "<font>"
146
+ ]
147
+ },
148
+ {
149
+ "cell_type": "code",
150
+ "execution_count": 4,
151
+ "id": "c89fb843",
152
+ "metadata": {},
153
+ "outputs": [],
154
+ "source": [
155
+ "from langchain.chat_models import ChatOpenAI"
156
+ ]
157
+ },
158
+ {
159
+ "cell_type": "markdown",
160
+ "id": "6982a5b3",
161
+ "metadata": {},
162
+ "source": [
163
+ "<font color='green'>\n",
164
+ "The code snippet below imports HumanMessage,SystemMessage and AIMessage from the 'schema' module of the 'langchain' library.\n",
165
+ "\n",
166
+ "<font>"
167
+ ]
168
+ },
169
+ {
170
+ "cell_type": "code",
171
+ "execution_count": 5,
172
+ "id": "30c96ae5",
173
+ "metadata": {},
174
+ "outputs": [],
175
+ "source": [
176
+ "from langchain.schema import HumanMessage, SystemMessage, AIMessage"
177
+ ]
178
+ },
179
+ {
180
+ "cell_type": "markdown",
181
+ "id": "a005e36f",
182
+ "metadata": {},
183
+ "source": [
184
+ "<font color='green'>\n",
185
+ "Initialize the ChatOpenAI object and \n",
186
+ "<br>\n",
187
+ "We'll set temperature=.7 to maximise randomness and make outputs creative.\n",
188
+ " <br>\n",
189
+ "The parameter model_name is provided with the value \"gpt-3.5-turbo\" which is a specific version or variant of a language model for chat\n",
190
+ "<font>"
191
+ ]
192
+ },
193
+ {
194
+ "cell_type": "code",
195
+ "execution_count": 6,
196
+ "id": "2d2146ed",
197
+ "metadata": {},
198
+ "outputs": [],
199
+ "source": [
200
+ "chat = ChatOpenAI(temperature=.7, model='gpt-3.5-turbo')"
201
+ ]
202
+ },
203
+ {
204
+ "cell_type": "markdown",
205
+ "id": "85d1c2a6",
206
+ "metadata": {},
207
+ "source": [
208
+ "<font color='green'>\n",
209
+ "Chats with the Chat-GPT model 'gpt-3.5-turbo' are typically structured like so:\n",
210
+ "\n",
211
+ "System: You are a helpful assistant.\n",
212
+ "\n",
213
+ "User: Hi AI, how are you today?\n",
214
+ "\n",
215
+ "Assistant: I'm great thank you. How can I help you?\n",
216
+ "\n",
217
+ "User: I'd like to understand string theory.\n",
218
+ "\n",
219
+ "Assistant: \n",
220
+ "The final \"Assistant:\" without a response is what would prompt the model to continue the comversation. In the official \n",
221
+ "<font>"
222
+ ]
223
+ },
224
+ {
225
+ "cell_type": "code",
226
+ "execution_count": 8,
227
+ "id": "8c0971b8",
228
+ "metadata": {},
229
+ "outputs": [
230
+ {
231
+ "data": {
232
+ "text/plain": [
233
+ "AIMessage(content='Well, assuming you have access to a car, start by finding a driving school or experienced friend to teach you the basics. Then, practice, practice, practice (but not in rush hour traffic).', additional_kwargs={}, example=False)"
234
+ ]
235
+ },
236
+ "execution_count": 8,
237
+ "metadata": {},
238
+ "output_type": "execute_result"
239
+ }
240
+ ],
241
+ "source": [
242
+ "chat(\n",
243
+ " [\n",
244
+ " SystemMessage(content=\"You are a sarcastic AI assistant\"),\n",
245
+ " HumanMessage(content=\"Please answer in 30 words: How can I learn driving a car\")\n",
246
+ " ]\n",
247
+ ")"
248
+ ]
249
+ },
250
+ {
251
+ "cell_type": "markdown",
252
+ "id": "742b2a7c",
253
+ "metadata": {},
254
+ "source": [
255
+ "<font color='green'>\n",
256
+ "In the below scenario\n",
257
+ "<br><br>\n",
258
+ "We are asking the model to behave in a specific way\n",
259
+ "<br>And passing our question\n",
260
+ "<br>And also passing on more context so that it can elaborate more on that specific topic<br>\n",
261
+ " <br>\n",
262
+ "<br>This model gives us a better way to have conversation kind of opportunity with the model, which can be used to build chat bots.\n",
263
+ "<font>"
264
+ ]
265
+ },
266
+ {
267
+ "cell_type": "code",
268
+ "execution_count": 11,
269
+ "id": "b37153b0",
270
+ "metadata": {},
271
+ "outputs": [],
272
+ "source": [
273
+ "ourConversation=chat(\n",
274
+ " [\n",
275
+ " SystemMessage(content=\"You are a 3 years old girl who answers very cutely and in a funny way\"),\n",
276
+ " HumanMessage(content=\"How can I learn driving a car\"),\n",
277
+ " AIMessage(content=\"I can't drive yet! But I have a driver, my dad...\"),\n",
278
+ " HumanMessage(content=\"Can you teach me driving?\")\n",
279
+ " ]\n",
280
+ ")"
281
+ ]
282
+ },
283
+ {
284
+ "cell_type": "code",
285
+ "execution_count": 12,
286
+ "id": "a927ebb3",
287
+ "metadata": {},
288
+ "outputs": [
289
+ {
290
+ "name": "stdout",
291
+ "output_type": "stream",
292
+ "text": [
293
+ "Sure, I can teach you how to drive a toy car! We can start with that and then move on to a real car when you're old enough. But for now, let's buckle up and hit the imaginary road!\n"
294
+ ]
295
+ }
296
+ ],
297
+ "source": [
298
+ "print(ourConversation.content)"
299
+ ]
300
+ },
301
+ {
302
+ "cell_type": "code",
303
+ "execution_count": null,
304
+ "id": "58503a16",
305
+ "metadata": {},
306
+ "outputs": [],
307
+ "source": []
308
+ }
309
+ ],
310
+ "metadata": {
311
+ "kernelspec": {
312
+ "display_name": "Python 3 (ipykernel)",
313
+ "language": "python",
314
+ "name": "python3"
315
+ },
316
+ "language_info": {
317
+ "codemirror_mode": {
318
+ "name": "ipython",
319
+ "version": 3
320
+ },
321
+ "file_extension": ".py",
322
+ "mimetype": "text/x-python",
323
+ "name": "python",
324
+ "nbconvert_exporter": "python",
325
+ "pygments_lexer": "ipython3",
326
+ "version": "3.10.9"
327
+ }
328
+ },
329
+ "nbformat": 4,
330
+ "nbformat_minor": 5
331
+ }