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