Spaces:
Runtime error
Runtime error
File size: 9,707 Bytes
1e43666 | 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 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 | {
"cells": [
{
"cell_type": "markdown",
"id": "ae5bcee9-6588-4d29-bbb9-6fb351ef6630",
"metadata": {},
"source": [
"# L1 Language Models, the Chat Format and Tokens"
]
},
{
"cell_type": "markdown",
"id": "0c797991-8486-4d79-8c1d-5dc0c1289c2f",
"metadata": {},
"source": [
"## Setup\n",
"#### Load the API key and relevant Python libaries.\n",
"In this course, we've provided some code that loads the OpenAI API key for you."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "19cd4e96",
"metadata": {
"height": 132
},
"outputs": [],
"source": [
"import os\n",
"import openai\n",
"import tiktoken\n",
"from dotenv import load_dotenv, find_dotenv\n",
"_ = load_dotenv(find_dotenv()) # read local .env file\n",
"\n",
"openai.api_key = os.environ['OPENAI_API_KEY']"
]
},
{
"cell_type": "markdown",
"id": "47ba0938-7ca5-46c4-a9d1-b55708d4dc7c",
"metadata": {},
"source": [
"#### helper function\n",
"This may look familiar if you took the earlier course \"ChatGPT Prompt Engineering for Developers\" Course"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1ed96988",
"metadata": {
"height": 149
},
"outputs": [],
"source": [
"def get_completion(prompt, model=\"gpt-3.5-turbo\"):\n",
" messages = [{\"role\": \"user\", \"content\": prompt}]\n",
" response = openai.ChatCompletion.create(\n",
" model=model,\n",
" messages=messages,\n",
" temperature=0,\n",
" )\n",
" return response.choices[0].message[\"content\"]"
]
},
{
"cell_type": "markdown",
"id": "fe10a390-2461-447d-bf8b-8498db404c44",
"metadata": {},
"source": [
"## Prompt the model and get a completion"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e1cc57b2",
"metadata": {
"height": 45
},
"outputs": [],
"source": [
"response = get_completion(\"What is the capital of France?\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "76774108",
"metadata": {
"height": 30
},
"outputs": [],
"source": [
"print(response)"
]
},
{
"cell_type": "markdown",
"id": "b83d4e38-3e3c-4c5a-a949-040a27f29d63",
"metadata": {},
"source": [
"## Tokens"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cc2d9e40",
"metadata": {
"height": 64
},
"outputs": [],
"source": [
"response = get_completion(\"Take the letters in lollipop \\\n",
"and reverse them\")\n",
"print(response)"
]
},
{
"cell_type": "markdown",
"id": "9d2b14d0-749d-4a79-9812-7b00ace9ae6f",
"metadata": {},
"source": [
"\"lollipop\" in reverse should be \"popillol\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "37cab84f",
"metadata": {
"height": 47
},
"outputs": [],
"source": [
"response = get_completion(\"\"\"Take the letters in \\\n",
"l-o-l-l-i-p-o-p and reverse them\"\"\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1577c561",
"metadata": {
"height": 30
},
"outputs": [],
"source": [
"response"
]
},
{
"cell_type": "markdown",
"id": "c8b88940-d3ab-4c00-b5c0-31531deaacbd",
"metadata": {},
"source": [
"## Helper function (chat format)\n",
"Here's the helper function we'll use in this course."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8f89efad",
"metadata": {
"height": 215
},
"outputs": [],
"source": [
"def get_completion_from_messages(messages, \n",
" model=\"gpt-3.5-turbo\", \n",
" temperature=0, \n",
" max_tokens=500):\n",
" response = openai.ChatCompletion.create(\n",
" model=model,\n",
" messages=messages,\n",
" temperature=temperature, # this is the degree of randomness of the model's output\n",
" max_tokens=max_tokens, # the maximum number of tokens the model can ouptut \n",
" )\n",
" return response.choices[0].message[\"content\"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b28c3424",
"metadata": {
"height": 198
},
"outputs": [],
"source": [
"messages = [ \n",
"{'role':'system', \n",
" 'content':\"\"\"You are an assistant who\\\n",
" responds in the style of Dr Seuss.\"\"\"}, \n",
"{'role':'user', \n",
" 'content':\"\"\"write me a very short poem\\\n",
" about a happy carrot\"\"\"}, \n",
"] \n",
"response = get_completion_from_messages(messages, temperature=1)\n",
"print(response)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "56c6978d",
"metadata": {
"height": 198
},
"outputs": [],
"source": [
"# length\n",
"messages = [ \n",
"{'role':'system',\n",
" 'content':'All your responses must be \\\n",
"one sentence long.'}, \n",
"{'role':'user',\n",
" 'content':'write me a story about a happy carrot'}, \n",
"] \n",
"response = get_completion_from_messages(messages, temperature =1)\n",
"print(response)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "14fd6331",
"metadata": {
"height": 217
},
"outputs": [],
"source": [
"# combined\n",
"messages = [ \n",
"{'role':'system',\n",
" 'content':\"\"\"You are an assistant who \\\n",
"responds in the style of Dr Seuss. \\\n",
"All your responses must be one sentence long.\"\"\"}, \n",
"{'role':'user',\n",
" 'content':\"\"\"write me a story about a happy carrot\"\"\"},\n",
"] \n",
"response = get_completion_from_messages(messages, \n",
" temperature =1)\n",
"print(response)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "89a70c79",
"metadata": {
"height": 385
},
"outputs": [],
"source": [
"def get_completion_and_token_count(messages, \n",
" model=\"gpt-3.5-turbo\", \n",
" temperature=0, \n",
" max_tokens=500):\n",
" \n",
" response = openai.ChatCompletion.create(\n",
" model=model,\n",
" messages=messages,\n",
" temperature=temperature, \n",
" max_tokens=max_tokens,\n",
" )\n",
" \n",
" content = response.choices[0].message[\"content\"]\n",
" \n",
" token_dict = {\n",
"'prompt_tokens':response['usage']['prompt_tokens'],\n",
"'completion_tokens':response['usage']['completion_tokens'],\n",
"'total_tokens':response['usage']['total_tokens'],\n",
" }\n",
"\n",
" return content, token_dict"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a64cf3c6",
"metadata": {
"height": 181
},
"outputs": [],
"source": [
"messages = [\n",
"{'role':'system', \n",
" 'content':\"\"\"You are an assistant who responds\\\n",
" in the style of Dr Seuss.\"\"\"}, \n",
"{'role':'user',\n",
" 'content':\"\"\"write me a very short poem \\ \n",
" about a happy carrot\"\"\"}, \n",
"] \n",
"response, token_dict = get_completion_and_token_count(messages)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cfd8fbd4",
"metadata": {
"height": 30
},
"outputs": [],
"source": [
"print(response)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "352ad320",
"metadata": {
"height": 30
},
"outputs": [],
"source": [
"print(token_dict)"
]
},
{
"cell_type": "markdown",
"id": "65372cdd-d869-4768-947a-0173e7f96335",
"metadata": {},
"source": [
"#### Notes on using the OpenAI API outside of this classroom\n",
"\n",
"To install the OpenAI Python library:\n",
"```\n",
"!pip install openai\n",
"```\n",
"\n",
"The library needs to be configured with your account's secret key, which is available on the [website](https://platform.openai.com/account/api-keys). \n",
"\n",
"You can either set it as the `OPENAI_API_KEY` environment variable before using the library:\n",
" ```\n",
" !export OPENAI_API_KEY='sk-...'\n",
" ```\n",
"\n",
"Or, set `openai.api_key` to its value:\n",
"\n",
"```\n",
"import openai\n",
"openai.api_key = \"sk-...\"\n",
"```"
]
},
{
"cell_type": "markdown",
"id": "d8f889c1-f2e4-40a5-bd27-164facb54402",
"metadata": {},
"source": [
"#### A note about the backslash\n",
"- In the course, we are using a backslash `\\` to make the text fit on the screen without inserting newline '\\n' characters.\n",
"- GPT-3 isn't really affected whether you insert newline characters or not. But when working with LLMs in general, you may consider whether newline characters in your prompt may affect the model's performance."
]
}
],
"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.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|