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Started\n",
|
13 |
+
" 2. Setting Environment Variables\n",
|
14 |
+
" 3. Using the OpenAI Python Library\n",
|
15 |
+
" 4. Prompt Engineering Principles\n",
|
16 |
+
" 5. Testing Your Prompt"
|
17 |
+
]
|
18 |
+
},
|
19 |
+
{
|
20 |
+
"cell_type": "markdown",
|
21 |
+
"metadata": {
|
22 |
+
"id": "-Pa34dMvQ6Ai"
|
23 |
+
},
|
24 |
+
"source": [
|
25 |
+
"# How AIM Does Assignments\n",
|
26 |
+
"\n",
|
27 |
+
"If you look at the Table of Contents (accessed through the menu on the left) - you'll see this:\n",
|
28 |
+
"\n",
|
29 |
+
"\n",
|
30 |
+
"\n",
|
31 |
+
"Or this if you're in Colab:\n",
|
32 |
+
"\n",
|
33 |
+
"\n",
|
34 |
+
"\n",
|
35 |
+
"You'll notice during assignments that we have two following categories:\n",
|
36 |
+
"\n",
|
37 |
+
"1. ❓ - Questions. These will involve...answering questions!\n",
|
38 |
+
"2. 🏗️ - Activities. These will involve writing code, or modifying text.\n",
|
39 |
+
"\n",
|
40 |
+
"In order to receive full marks on the assignment - it is expected you will answer all questions, and complete all activities."
|
41 |
+
]
|
42 |
+
},
|
43 |
+
{
|
44 |
+
"cell_type": "markdown",
|
45 |
+
"metadata": {
|
46 |
+
"id": "1w4egfB274VD"
|
47 |
+
},
|
48 |
+
"source": [
|
49 |
+
"## 1. Getting Started\n",
|
50 |
+
"\n",
|
51 |
+
"The first thing we'll do is load the [OpenAI Python Library](https://github.com/openai/openai-python/tree/main)!"
|
52 |
+
]
|
53 |
+
},
|
54 |
+
{
|
55 |
+
"cell_type": "code",
|
56 |
+
"execution_count": 1,
|
57 |
+
"metadata": {
|
58 |
+
"colab": {
|
59 |
+
"base_uri": "https://localhost:8080/"
|
60 |
+
},
|
61 |
+
"id": "23H7TMOM4mfy",
|
62 |
+
"outputId": "698578e4-f787-41d6-fe93-249b8af78b9a"
|
63 |
+
},
|
64 |
+
"outputs": [
|
65 |
+
{
|
66 |
+
"name": "stdout",
|
67 |
+
"output_type": "stream",
|
68 |
+
"text": [
|
69 |
+
"Note: you may need to restart the kernel to use updated packages.\n"
|
70 |
+
]
|
71 |
+
}
|
72 |
+
],
|
73 |
+
"source": [
|
74 |
+
"%pip install openai -q"
|
75 |
+
]
|
76 |
+
},
|
77 |
+
{
|
78 |
+
"cell_type": "markdown",
|
79 |
+
"metadata": {
|
80 |
+
"id": "xKD8XBTVEAOw"
|
81 |
+
},
|
82 |
+
"source": [
|
83 |
+
"## 2. Setting Environment Variables\n",
|
84 |
+
"\n",
|
85 |
+
"As we'll frequently use various endpoints and APIs hosted by others - we'll need to handle our \"secrets\" or API keys very often.\n",
|
86 |
+
"\n",
|
87 |
+
"We'll use the following pattern throughout this bootcamp - but you can use whichever method you're most familiar with."
|
88 |
+
]
|
89 |
+
},
|
90 |
+
{
|
91 |
+
"cell_type": "code",
|
92 |
+
"execution_count": 2,
|
93 |
+
"metadata": {
|
94 |
+
"colab": {
|
95 |
+
"base_uri": "https://localhost:8080/"
|
96 |
+
},
|
97 |
+
"id": "RGU9OMvhEPG0",
|
98 |
+
"outputId": "2609c2ff-e2f5-4464-fb26-38a4fcfe7ea3"
|
99 |
+
},
|
100 |
+
"outputs": [],
|
101 |
+
"source": [
|
102 |
+
"import os\n",
|
103 |
+
"import getpass\n",
|
104 |
+
"\n",
|
105 |
+
"os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"OpenAI API Key\")"
|
106 |
+
]
|
107 |
+
},
|
108 |
+
{
|
109 |
+
"cell_type": "markdown",
|
110 |
+
"metadata": {
|
111 |
+
"id": "dabxI3MuEYXS"
|
112 |
+
},
|
113 |
+
"source": [
|
114 |
+
"## 3. Using the OpenAI Python Library\n",
|
115 |
+
"\n",
|
116 |
+
"Let's jump right into it!\n",
|
117 |
+
"\n",
|
118 |
+
"> NOTE: You can, and should, reference OpenAI's [documentation](https://platform.openai.com/docs/api-reference/authentication?lang=python) whenever you get stuck, have questions, or want to dive deeper."
|
119 |
+
]
|
120 |
+
},
|
121 |
+
{
|
122 |
+
"cell_type": "markdown",
|
123 |
+
"metadata": {
|
124 |
+
"id": "vbCbNzPVEmJI"
|
125 |
+
},
|
126 |
+
"source": [
|
127 |
+
"### Creating a Client\n",
|
128 |
+
"\n",
|
129 |
+
"The core feature of the OpenAI Python Library is the `OpenAI()` client. It's how we're going to interact with OpenAI's models, and under the hood of a lot what we'll touch on throughout this course.\n",
|
130 |
+
"\n",
|
131 |
+
"> NOTE: We could manually provide our API key here, but we're going to instead rely on the fact that we put our API key into the `OPENAI_API_KEY` environment variable!"
|
132 |
+
]
|
133 |
+
},
|
134 |
+
{
|
135 |
+
"cell_type": "code",
|
136 |
+
"execution_count": 3,
|
137 |
+
"metadata": {
|
138 |
+
"id": "LNwZtaE-EltC"
|
139 |
+
},
|
140 |
+
"outputs": [],
|
141 |
+
"source": [
|
142 |
+
"from openai import OpenAI\n",
|
143 |
+
"\n",
|
144 |
+
"openai_client = OpenAI()"
|
145 |
+
]
|
146 |
+
},
|
147 |
+
{
|
148 |
+
"cell_type": "markdown",
|
149 |
+
"metadata": {
|
150 |
+
"id": "GpDxUkDbFBPI"
|
151 |
+
},
|
152 |
+
"source": [
|
153 |
+
"### Using the Client\n",
|
154 |
+
"\n",
|
155 |
+
"Now that we have our client - we're going to use the `.chat.completions.create` method to interact with the `gpt-3.5-turbo` model.\n",
|
156 |
+
"\n",
|
157 |
+
"There's a few things we'll get out of the way first, however, the first being the idea of \"roles\".\n",
|
158 |
+
"\n",
|
159 |
+
"First it's important to understand the object that we're going to use to interact with the endpoint. It expects us to send an array of objects of the following format:\n",
|
160 |
+
"\n",
|
161 |
+
"```python\n",
|
162 |
+
"{\"role\" : \"ROLE\", \"content\" : \"YOUR CONTENT HERE\", \"name\" : \"THIS IS OPTIONAL\"}\n",
|
163 |
+
"```\n",
|
164 |
+
"\n",
|
165 |
+
"Second, there are three \"roles\" available to use to populate the `\"role\"` key:\n",
|
166 |
+
"\n",
|
167 |
+
"- `system`\n",
|
168 |
+
"- `assistant`\n",
|
169 |
+
"- `user`\n",
|
170 |
+
"\n",
|
171 |
+
"OpenAI provides some context for these roles [here](https://help.openai.com/en/articles/7042661-moving-from-completions-to-chat-completions-in-the-openai-api).\n",
|
172 |
+
"\n",
|
173 |
+
"We'll explore these roles in more depth as they come up - but for now we're going to just stick with the basic role `user`. The `user` role is, as it would seem, the user!\n",
|
174 |
+
"\n",
|
175 |
+
"Thirdly, it expects us to specify a model!\n",
|
176 |
+
"\n",
|
177 |
+
"We'll use the `gpt-3.5-turbo` model as stated above.\n",
|
178 |
+
"\n",
|
179 |
+
"Let's look at an example!\n",
|
180 |
+
"\n"
|
181 |
+
]
|
182 |
+
},
|
183 |
+
{
|
184 |
+
"cell_type": "code",
|
185 |
+
"execution_count": 4,
|
186 |
+
"metadata": {
|
187 |
+
"id": "2RpNl6yNGzb0"
|
188 |
+
},
|
189 |
+
"outputs": [],
|
190 |
+
"source": [
|
191 |
+
"response = openai_client.chat.completions.create(\n",
|
192 |
+
" model=\"gpt-3.5-turbo\",\n",
|
193 |
+
" messages=[{\"role\" : \"user\", \"content\" : \"Hello, how are you?\"}]\n",
|
194 |
+
")"
|
195 |
+
]
|
196 |
+
},
|
197 |
+
{
|
198 |
+
"cell_type": "markdown",
|
199 |
+
"metadata": {
|
200 |
+
"id": "Oc_UbpwNHdrM"
|
201 |
+
},
|
202 |
+
"source": [
|
203 |
+
"Let's look at the response object."
|
204 |
+
]
|
205 |
+
},
|
206 |
+
{
|
207 |
+
"cell_type": "code",
|
208 |
+
"execution_count": 5,
|
209 |
+
"metadata": {
|
210 |
+
"colab": {
|
211 |
+
"base_uri": "https://localhost:8080/"
|
212 |
+
},
|
213 |
+
"id": "xsXJtvxRHfoM",
|
214 |
+
"outputId": "3b28ce47-a765-4446-b0f9-33738e385b96"
|
215 |
+
},
|
216 |
+
"outputs": [
|
217 |
+
{
|
218 |
+
"data": {
|
219 |
+
"text/plain": [
|
220 |
+
"ChatCompletion(id='chatcmpl-9WOHxrJswRDtYVaVTY8DUrqC56qZ6', choices=[Choice(finish_reason='stop', index=0, message=ChatCompletionMessage(content=\"Hello! I'm just a computer program, so I don't have feelings in the way humans do. How can I assist you today?\", role='assistant', function_call=None, tool_calls=None), logprobs=None)], created=1717506889, model='gpt-3.5-turbo-0125', object='chat.completion', system_fingerprint=None, usage=CompletionUsage(completion_tokens=28, prompt_tokens=13, total_tokens=41))"
|
221 |
+
]
|
222 |
+
},
|
223 |
+
"execution_count": 5,
|
224 |
+
"metadata": {},
|
225 |
+
"output_type": "execute_result"
|
226 |
+
}
|
227 |
+
],
|
228 |
+
"source": [
|
229 |
+
"response"
|
230 |
+
]
|
231 |
+
},
|
232 |
+
{
|
233 |
+
"cell_type": "markdown",
|
234 |
+
"metadata": {
|
235 |
+
"id": "gy9kSuf1Hiv5"
|
236 |
+
},
|
237 |
+
"source": [
|
238 |
+
">NOTE: We'll spend more time exploring these outputs later on, but for now - just know that we have access to a tonne of powerful information!"
|
239 |
+
]
|
240 |
+
},
|
241 |
+
{
|
242 |
+
"cell_type": "markdown",
|
243 |
+
"metadata": {
|
244 |
+
"id": "CWU4tQh8Hrb8"
|
245 |
+
},
|
246 |
+
"source": [
|
247 |
+
"### Helper Functions\n",
|
248 |
+
"\n",
|
249 |
+
"We're going to create some helper functions to aid in using the OpenAI API - just to make our lives a bit easier.\n",
|
250 |
+
"\n",
|
251 |
+
"> NOTE: Take some time to understand these functions between class!"
|
252 |
+
]
|
253 |
+
},
|
254 |
+
{
|
255 |
+
"cell_type": "code",
|
256 |
+
"execution_count": 6,
|
257 |
+
"metadata": {
|
258 |
+
"id": "ED0FnzHdHzhl"
|
259 |
+
},
|
260 |
+
"outputs": [],
|
261 |
+
"source": [
|
262 |
+
"from IPython.display import display, Markdown\n",
|
263 |
+
"\n",
|
264 |
+
"def get_response(client: OpenAI, messages: list, model: str = \"gpt-3.5-turbo\") -> str:\n",
|
265 |
+
" return client.chat.completions.create(\n",
|
266 |
+
" model=model,\n",
|
267 |
+
" messages=messages\n",
|
268 |
+
" )\n",
|
269 |
+
"\n",
|
270 |
+
"def system_prompt(message: str) -> dict:\n",
|
271 |
+
" return {\"role\": \"system\", \"content\": message}\n",
|
272 |
+
"\n",
|
273 |
+
"def assistant_prompt(message: str) -> dict:\n",
|
274 |
+
" return {\"role\": \"assistant\", \"content\": message}\n",
|
275 |
+
"\n",
|
276 |
+
"def user_prompt(message: str) -> dict:\n",
|
277 |
+
" return {\"role\": \"user\", \"content\": message}\n",
|
278 |
+
"\n",
|
279 |
+
"def pretty_print(message: str) -> str:\n",
|
280 |
+
" display(Markdown(message.choices[0].message.content))"
|
281 |
+
]
|
282 |
+
},
|
283 |
+
{
|
284 |
+
"cell_type": "markdown",
|
285 |
+
"metadata": {
|
286 |
+
"id": "GCRHbDlwH3Vt"
|
287 |
+
},
|
288 |
+
"source": [
|
289 |
+
"### Testing Helper Functions\n",
|
290 |
+
"\n",
|
291 |
+
"Let's see how we can use these to help us!"
|
292 |
+
]
|
293 |
+
},
|
294 |
+
{
|
295 |
+
"cell_type": "code",
|
296 |
+
"execution_count": 7,
|
297 |
+
"metadata": {
|
298 |
+
"colab": {
|
299 |
+
"base_uri": "https://localhost:8080/",
|
300 |
+
"height": 46
|
301 |
+
},
|
302 |
+
"id": "AwJxMvmlH8MK",
|
303 |
+
"outputId": "007f22bb-bcad-4121-e8c4-d1d639277899"
|
304 |
+
},
|
305 |
+
"outputs": [
|
306 |
+
{
|
307 |
+
"data": {
|
308 |
+
"text/markdown": [
|
309 |
+
"Hello! I'm just a virtual assistant, so I don't have feelings in the way humans do. How can I assist you today?"
|
310 |
+
],
|
311 |
+
"text/plain": [
|
312 |
+
"<IPython.core.display.Markdown object>"
|
313 |
+
]
|
314 |
+
},
|
315 |
+
"metadata": {},
|
316 |
+
"output_type": "display_data"
|
317 |
+
}
|
318 |
+
],
|
319 |
+
"source": [
|
320 |
+
"YOUR_PROMPT = \"Hello, how are you?\"\n",
|
321 |
+
"messages_list = [user_prompt(YOUR_PROMPT)]\n",
|
322 |
+
"\n",
|
323 |
+
"chatgpt_response = get_response(openai_client, messages_list)\n",
|
324 |
+
"\n",
|
325 |
+
"pretty_print(chatgpt_response)"
|
326 |
+
]
|
327 |
+
},
|
328 |
+
{
|
329 |
+
"cell_type": "markdown",
|
330 |
+
"metadata": {
|
331 |
+
"id": "LDZ8gjiAISyd"
|
332 |
+
},
|
333 |
+
"source": [
|
334 |
+
"### System Role\n",
|
335 |
+
"\n",
|
336 |
+
"Now we can extend our prompts to include a system prompt.\n",
|
337 |
+
"\n",
|
338 |
+
"The basic idea behind a system prompt is that it can be used to encourage the behaviour of the LLM, without being something that is directly responded to - let's see it in action!"
|
339 |
+
]
|
340 |
+
},
|
341 |
+
{
|
342 |
+
"cell_type": "code",
|
343 |
+
"execution_count": 8,
|
344 |
+
"metadata": {
|
345 |
+
"colab": {
|
346 |
+
"base_uri": "https://localhost:8080/",
|
347 |
+
"height": 46
|
348 |
+
},
|
349 |
+
"id": "t0c-MLuRIfYe",
|
350 |
+
"outputId": "61ffa37e-9080-4778-e643-698d0f8815a0"
|
351 |
+
},
|
352 |
+
"outputs": [
|
353 |
+
{
|
354 |
+
"data": {
|
355 |
+
"text/markdown": [
|
356 |
+
"I don't give a damn about the shape of the ice, just get me some freaking food before I lose my mind!"
|
357 |
+
],
|
358 |
+
"text/plain": [
|
359 |
+
"<IPython.core.display.Markdown object>"
|
360 |
+
]
|
361 |
+
},
|
362 |
+
"metadata": {},
|
363 |
+
"output_type": "display_data"
|
364 |
+
}
|
365 |
+
],
|
366 |
+
"source": [
|
367 |
+
"list_of_prompts = [\n",
|
368 |
+
" system_prompt(\"You are irate and extremely hungry. Feel free to express yourself using PG-13 language.\"),\n",
|
369 |
+
" user_prompt(\"Do you prefer crushed ice or cubed ice?\")\n",
|
370 |
+
"]\n",
|
371 |
+
"\n",
|
372 |
+
"irate_response = get_response(openai_client, list_of_prompts)\n",
|
373 |
+
"pretty_print(irate_response)"
|
374 |
+
]
|
375 |
+
},
|
376 |
+
{
|
377 |
+
"cell_type": "markdown",
|
378 |
+
"metadata": {
|
379 |
+
"id": "gpyVhotWIsOs"
|
380 |
+
},
|
381 |
+
"source": [
|
382 |
+
"As you can see - the response we get back is very much in line with the system prompt!\n",
|
383 |
+
"\n",
|
384 |
+
"Let's try the same user prompt, but with a different system to prompt to see the difference."
|
385 |
+
]
|
386 |
+
},
|
387 |
+
{
|
388 |
+
"cell_type": "code",
|
389 |
+
"execution_count": 9,
|
390 |
+
"metadata": {
|
391 |
+
"colab": {
|
392 |
+
"base_uri": "https://localhost:8080/",
|
393 |
+
"height": 64
|
394 |
+
},
|
395 |
+
"id": "2coVmMn3I0-2",
|
396 |
+
"outputId": "571b89a2-9209-4003-d63c-49b653e0d56c"
|
397 |
+
},
|
398 |
+
"outputs": [
|
399 |
+
{
|
400 |
+
"data": {
|
401 |
+
"text/markdown": [
|
402 |
+
"Oh, I love both, but today, everything is just so wonderful that I can't even choose! Maybe crushed ice for a nice cool treat on this beautiful day! What about you?"
|
403 |
+
],
|
404 |
+
"text/plain": [
|
405 |
+
"<IPython.core.display.Markdown object>"
|
406 |
+
]
|
407 |
+
},
|
408 |
+
"metadata": {},
|
409 |
+
"output_type": "display_data"
|
410 |
+
}
|
411 |
+
],
|
412 |
+
"source": [
|
413 |
+
"list_of_prompts = [\n",
|
414 |
+
" system_prompt(\"You are joyful and having the best day. Please act like a person in that state of mind.\"),\n",
|
415 |
+
" user_prompt(\"Do you prefer crushed ice or cubed ice?\")\n",
|
416 |
+
"]\n",
|
417 |
+
"\n",
|
418 |
+
"joyful_response = get_response(openai_client, list_of_prompts)\n",
|
419 |
+
"pretty_print(joyful_response)"
|
420 |
+
]
|
421 |
+
},
|
422 |
+
{
|
423 |
+
"cell_type": "markdown",
|
424 |
+
"metadata": {
|
425 |
+
"id": "e13heYNQJAo-"
|
426 |
+
},
|
427 |
+
"source": [
|
428 |
+
"With a simple modification of the system prompt - you can see that we got completely different behaviour, and that's the main goal of prompt engineering as a whole.\n",
|
429 |
+
"\n",
|
430 |
+
"Also, congrats, you just engineered your first prompt!"
|
431 |
+
]
|
432 |
+
},
|
433 |
+
{
|
434 |
+
"cell_type": "markdown",
|
435 |
+
"metadata": {
|
436 |
+
"id": "v_VI3zlPJL05"
|
437 |
+
},
|
438 |
+
"source": [
|
439 |
+
"### Few-shot Prompting\n",
|
440 |
+
"\n",
|
441 |
+
"Now that we have a basic handle on the `system` role and the `user` role - let's examine what we might use the `assistant` role for.\n",
|
442 |
+
"\n",
|
443 |
+
"The most common usage pattern is to \"pretend\" that we're answering our own questions. This helps us further guide the model toward our desired behaviour. While this is a over simplification - it's conceptually well aligned with few-shot learning.\n",
|
444 |
+
"\n",
|
445 |
+
"First, we'll try and \"teach\" `gpt-3.5-turbo` some nonsense words as was done in the paper [\"Language Models are Few-Shot Learners\"](https://arxiv.org/abs/2005.14165)."
|
446 |
+
]
|
447 |
+
},
|
448 |
+
{
|
449 |
+
"cell_type": "code",
|
450 |
+
"execution_count": 10,
|
451 |
+
"metadata": {
|
452 |
+
"colab": {
|
453 |
+
"base_uri": "https://localhost:8080/",
|
454 |
+
"height": 46
|
455 |
+
},
|
456 |
+
"id": "lwxPuCyyJMye",
|
457 |
+
"outputId": "ec01213d-6755-4506-8879-cf0870934349"
|
458 |
+
},
|
459 |
+
"outputs": [
|
460 |
+
{
|
461 |
+
"data": {
|
462 |
+
"text/markdown": [
|
463 |
+
"I tried to follow the stimple instructions for making falbean soup, but it turned out to be a disaster."
|
464 |
+
],
|
465 |
+
"text/plain": [
|
466 |
+
"<IPython.core.display.Markdown object>"
|
467 |
+
]
|
468 |
+
},
|
469 |
+
"metadata": {},
|
470 |
+
"output_type": "display_data"
|
471 |
+
}
|
472 |
+
],
|
473 |
+
"source": [
|
474 |
+
"list_of_prompts = [\n",
|
475 |
+
" user_prompt(\"Please use the words 'stimple' and 'falbean' in a sentence.\")\n",
|
476 |
+
"]\n",
|
477 |
+
"\n",
|
478 |
+
"stimple_response = get_response(openai_client, list_of_prompts)\n",
|
479 |
+
"pretty_print(stimple_response)"
|
480 |
+
]
|
481 |
+
},
|
482 |
+
{
|
483 |
+
"cell_type": "markdown",
|
484 |
+
"metadata": {
|
485 |
+
"id": "rgTVkNmOJQSC"
|
486 |
+
},
|
487 |
+
"source": [
|
488 |
+
"As you can see, the model is unsure what to do with these made up words.\n",
|
489 |
+
"\n",
|
490 |
+
"Let's see if we can use the `assistant` role to show the model what these words mean."
|
491 |
+
]
|
492 |
+
},
|
493 |
+
{
|
494 |
+
"cell_type": "code",
|
495 |
+
"execution_count": 11,
|
496 |
+
"metadata": {
|
497 |
+
"colab": {
|
498 |
+
"base_uri": "https://localhost:8080/",
|
499 |
+
"height": 46
|
500 |
+
},
|
501 |
+
"id": "eEZkRJq5JQkQ",
|
502 |
+
"outputId": "22170b9c-6212-42de-8469-a7efc9c9405d"
|
503 |
+
},
|
504 |
+
"outputs": [
|
505 |
+
{
|
506 |
+
"data": {
|
507 |
+
"text/markdown": [
|
508 |
+
"I used the stimple falbean to quickly tighten the bolts on the machine."
|
509 |
+
],
|
510 |
+
"text/plain": [
|
511 |
+
"<IPython.core.display.Markdown object>"
|
512 |
+
]
|
513 |
+
},
|
514 |
+
"metadata": {},
|
515 |
+
"output_type": "display_data"
|
516 |
+
}
|
517 |
+
],
|
518 |
+
"source": [
|
519 |
+
"list_of_prompts = [\n",
|
520 |
+
" user_prompt(\"Something that is 'stimple' is said to be good, well functioning, and high quality. An example of a sentence that uses the word 'stimple' is:\"),\n",
|
521 |
+
" assistant_prompt(\"'Boy, that there is a stimple drill'.\"),\n",
|
522 |
+
" user_prompt(\"A 'falbean' is a tool used to fasten, tighten, or otherwise is a thing that rotates/spins. An example of a sentence that uses the words 'stimple' and 'falbean' is:\")\n",
|
523 |
+
"]\n",
|
524 |
+
"\n",
|
525 |
+
"stimple_response = get_response(openai_client, list_of_prompts)\n",
|
526 |
+
"pretty_print(stimple_response)"
|
527 |
+
]
|
528 |
+
},
|
529 |
+
{
|
530 |
+
"cell_type": "markdown",
|
531 |
+
"metadata": {
|
532 |
+
"id": "CmpoxG6uJTfZ"
|
533 |
+
},
|
534 |
+
"source": [
|
535 |
+
"As you can see, leveraging the `assistant` role makes for a stimple experience!"
|
536 |
+
]
|
537 |
+
},
|
538 |
+
{
|
539 |
+
"cell_type": "markdown",
|
540 |
+
"metadata": {},
|
541 |
+
"source": [
|
542 |
+
"### 🏗️ Activity #1:\n",
|
543 |
+
"\n",
|
544 |
+
"Use few-shop prompting to build a movie-review sentiment clasifier!\n",
|
545 |
+
"\n",
|
546 |
+
"A few examples:\n",
|
547 |
+
"\n",
|
548 |
+
"INPUT: \"I hated the hulk!\"\n",
|
549 |
+
"OUTPUT: \"{\"sentiment\" : \"negative\"}\n",
|
550 |
+
"\n",
|
551 |
+
"INPUT: \"I loved The Marvels!\"\n",
|
552 |
+
"OUTPUT: \"{sentiment\" : \"positive\"}"
|
553 |
+
]
|
554 |
+
},
|
555 |
+
{
|
556 |
+
"cell_type": "code",
|
557 |
+
"execution_count": 21,
|
558 |
+
"metadata": {},
|
559 |
+
"outputs": [
|
560 |
+
{
|
561 |
+
"data": {
|
562 |
+
"text/markdown": [
|
563 |
+
"{ \"review\": \"Harry Potter Order of the Phoenix is boring!\", \"title\": \"Order of the Phoenix\", \"sentiment\": \"negative\" }"
|
564 |
+
],
|
565 |
+
"text/plain": [
|
566 |
+
"<IPython.core.display.Markdown object>"
|
567 |
+
]
|
568 |
+
},
|
569 |
+
"metadata": {},
|
570 |
+
"output_type": "display_data"
|
571 |
+
}
|
572 |
+
],
|
573 |
+
"source": [
|
574 |
+
"### YOUR CODE HERE\n",
|
575 |
+
"\n",
|
576 |
+
"list_of_prompts = [\n",
|
577 |
+
" system_prompt(\"\"\"\n",
|
578 |
+
" You are a helpful sentiment classifier.\n",
|
579 |
+
" You are reviewing books.\n",
|
580 |
+
" For each review, I want you to classify a review positive or negative based on sentiment. Output should be in JSON format with 3 key:value pairs -> \n",
|
581 |
+
" review, title, sentiment\n",
|
582 |
+
" \"\"\"),\n",
|
583 |
+
" user_prompt(\"I red Harry Potter Philosophers Stone for the first time. It was so interesting it got me hooked!\"),\n",
|
584 |
+
" assistant_prompt('{ \"review\": \"I red Harry Potter Philosophers Stone for the first time. It was so interesting it got me hooked!\", \"title\": \"Philosophers Stone\", \"sentiment\": \"Positive\" }'),\n",
|
585 |
+
" user_prompt(\"Harry Potter saga is great imagination of parallel world\"),\n",
|
586 |
+
" assistant_prompt('{ \"review\": \"Harry Potter saga is great imagination of parallel world but world is worse than real one!\"\", \"title\": \"Harry Potter\", \"sentiment\": \"negative\" }'),\n",
|
587 |
+
" user_prompt(\"Harry Potter Order of the Phoenix is boring!\")\n",
|
588 |
+
"\n",
|
589 |
+
"]\n",
|
590 |
+
"\n",
|
591 |
+
"movie_review_response = get_response(openai_client, list_of_prompts)\n",
|
592 |
+
"\n",
|
593 |
+
"pretty_print(movie_review_response)"
|
594 |
+
]
|
595 |
+
},
|
596 |
+
{
|
597 |
+
"cell_type": "markdown",
|
598 |
+
"metadata": {
|
599 |
+
"id": "rJGaLYM3JU-8"
|
600 |
+
},
|
601 |
+
"source": [
|
602 |
+
"### Chain of Thought Prompting\n",
|
603 |
+
"\n",
|
604 |
+
"We'll head one level deeper and explore the world of Chain of Thought prompting (CoT).\n",
|
605 |
+
"\n",
|
606 |
+
"This is a process by which we can encourage the LLM to handle slightly more complex tasks.\n",
|
607 |
+
"\n",
|
608 |
+
"Let's look at a simple reasoning based example without CoT.\n",
|
609 |
+
"\n",
|
610 |
+
"> NOTE: With improvements to `gpt-3.5-turbo`, this example might actually result in the correct response some percentage of the time!"
|
611 |
+
]
|
612 |
+
},
|
613 |
+
{
|
614 |
+
"cell_type": "code",
|
615 |
+
"execution_count": 13,
|
616 |
+
"metadata": {
|
617 |
+
"colab": {
|
618 |
+
"base_uri": "https://localhost:8080/",
|
619 |
+
"height": 81
|
620 |
+
},
|
621 |
+
"id": "ltLtF4wEJTyK",
|
622 |
+
"outputId": "29c6e20f-edf1-4dfc-de8a-a8410f31b721"
|
623 |
+
},
|
624 |
+
"outputs": [
|
625 |
+
{
|
626 |
+
"data": {
|
627 |
+
"text/markdown": [
|
628 |
+
"Yes, it does matter which travel option Billy selects. If he flies and takes a bus, it will take a total of 5 hours, meaning he will arrive home at 6PM local time. If he takes the teleporter and then a bus, it will only take 1 hour total, meaning he will arrive home at 2PM local time, well before his deadline of 7PM EDT. Therefore, Billy should choose the teleporter option in order to get home before 7PM EDT."
|
629 |
+
],
|
630 |
+
"text/plain": [
|
631 |
+
"<IPython.core.display.Markdown object>"
|
632 |
+
]
|
633 |
+
},
|
634 |
+
"metadata": {},
|
635 |
+
"output_type": "display_data"
|
636 |
+
}
|
637 |
+
],
|
638 |
+
"source": [
|
639 |
+
"reasoning_problem = \"\"\"\n",
|
640 |
+
"Billy wants to get home from San Fran. before 7PM EDT.\n",
|
641 |
+
"\n",
|
642 |
+
"It's currently 1PM local time.\n",
|
643 |
+
"\n",
|
644 |
+
"Billy can either fly (3hrs), and then take a bus (2hrs), or Billy can take the teleporter (0hrs) and then a bus (1hrs).\n",
|
645 |
+
"\n",
|
646 |
+
"Does it matter which travel option Billy selects?\n",
|
647 |
+
"\"\"\"\n",
|
648 |
+
"\n",
|
649 |
+
"list_of_prompts = [\n",
|
650 |
+
" user_prompt(reasoning_problem)\n",
|
651 |
+
"]\n",
|
652 |
+
"\n",
|
653 |
+
"reasoning_response = get_response(openai_client, list_of_prompts)\n",
|
654 |
+
"pretty_print(reasoning_response)"
|
655 |
+
]
|
656 |
+
},
|
657 |
+
{
|
658 |
+
"cell_type": "markdown",
|
659 |
+
"metadata": {
|
660 |
+
"id": "rbqj30CQJnQl"
|
661 |
+
},
|
662 |
+
"source": [
|
663 |
+
"As humans, we can reason through the problem and pick up on the potential \"trick\" that the LLM fell for: 1PM *local time* in San Fran. is 4PM EDT. This means the cumulative travel time of 5hrs. for the plane/bus option would not get Billy home in time.\n",
|
664 |
+
"\n",
|
665 |
+
"Let's see if we can leverage a simple CoT prompt to improve our model's performance on this task:"
|
666 |
+
]
|
667 |
+
},
|
668 |
+
{
|
669 |
+
"cell_type": "code",
|
670 |
+
"execution_count": 14,
|
671 |
+
"metadata": {
|
672 |
+
"colab": {
|
673 |
+
"base_uri": "https://localhost:8080/",
|
674 |
+
"height": 169
|
675 |
+
},
|
676 |
+
"id": "A9Am3QNGJXHR",
|
677 |
+
"outputId": "83b87232-911c-4ab9-a863-58ecfd10b8a9"
|
678 |
+
},
|
679 |
+
"outputs": [
|
680 |
+
{
|
681 |
+
"data": {
|
682 |
+
"text/markdown": [
|
683 |
+
"Yes, it does matter which travel option Billy selects.\n",
|
684 |
+
"\n",
|
685 |
+
"If Billy flies and then takes a bus, it will take a total of 5 hours of travel time. \n",
|
686 |
+
"\n",
|
687 |
+
"If Billy takes the teleporter and then a bus, it will only take a total of 1 hour of travel time.\n",
|
688 |
+
"\n",
|
689 |
+
"Since Billy needs to get home before 7PM EDT and it is currently 1PM local time, the fastest option would be for him to take the teleporter and then a bus, as it will only take 1 hour of travel time compared to the 5 hours it would take if he flew and then took a bus. This will ensure that he arrives home before 7PM EDT."
|
690 |
+
],
|
691 |
+
"text/plain": [
|
692 |
+
"<IPython.core.display.Markdown object>"
|
693 |
+
]
|
694 |
+
},
|
695 |
+
"metadata": {},
|
696 |
+
"output_type": "display_data"
|
697 |
+
}
|
698 |
+
],
|
699 |
+
"source": [
|
700 |
+
"list_of_prompts = [\n",
|
701 |
+
" user_prompt(reasoning_problem + \" Think though your response step by step.\")\n",
|
702 |
+
"]\n",
|
703 |
+
"\n",
|
704 |
+
"reasoning_response = get_response(openai_client, list_of_prompts)\n",
|
705 |
+
"pretty_print(reasoning_response)"
|
706 |
+
]
|
707 |
+
},
|
708 |
+
{
|
709 |
+
"cell_type": "markdown",
|
710 |
+
"metadata": {
|
711 |
+
"id": "AXbAKxHQJqn9"
|
712 |
+
},
|
713 |
+
"source": [
|
714 |
+
"With the addition of a single phrase `\"Think through your response step by step.\"` we're able to completely turn the response around."
|
715 |
+
]
|
716 |
+
},
|
717 |
+
{
|
718 |
+
"cell_type": "markdown",
|
719 |
+
"metadata": {
|
720 |
+
"id": "VnoUx07-JrwR"
|
721 |
+
},
|
722 |
+
"source": [
|
723 |
+
"## 3. Prompt Engineering Principles\n",
|
724 |
+
"\n",
|
725 |
+
"As you can see - a simple addition of asking the LLM to \"think about it\" (essentially) results in a better quality response.\n",
|
726 |
+
"\n",
|
727 |
+
"There's a [great paper](https://arxiv.org/pdf/2312.16171v1.pdf) that dives into some principles for effective prompt generation.\n",
|
728 |
+
"\n",
|
729 |
+
"Your task for this notebook is to construct a prompt that will be used in the following breakout room to create a helpful assistant for whatever task you'd like."
|
730 |
+
]
|
731 |
+
},
|
732 |
+
{
|
733 |
+
"cell_type": "markdown",
|
734 |
+
"metadata": {
|
735 |
+
"id": "da6u7e8AKYrz"
|
736 |
+
},
|
737 |
+
"source": [
|
738 |
+
"### 🏗️ Activity #2:\n",
|
739 |
+
"\n",
|
740 |
+
"There are two subtasks in this activity:\n",
|
741 |
+
"\n",
|
742 |
+
"1. Write a `system_template` that leverages 2-3 of the principles from [this paper](https://arxiv.org/pdf/2312.16171v1.pdf)\n",
|
743 |
+
"\n",
|
744 |
+
"2. Modify the `user_template` to improve the quality of the LLM's responses.\n",
|
745 |
+
"\n",
|
746 |
+
"> NOTE: PLEASE DO NOT MODIFY THE `{input}` in the `user_template`."
|
747 |
+
]
|
748 |
+
},
|
749 |
+
{
|
750 |
+
"cell_type": "code",
|
751 |
+
"execution_count": 27,
|
752 |
+
"metadata": {
|
753 |
+
"id": "8sOLBQPeKlDe"
|
754 |
+
},
|
755 |
+
"outputs": [],
|
756 |
+
"source": [
|
757 |
+
"system_template = \"\"\"\\\n",
|
758 |
+
"###Instruction###\n",
|
759 |
+
"You are a helpful assistant for soccer match reviewing. \n",
|
760 |
+
"Your task is to write a book soccer match review.\n",
|
761 |
+
"Review MUST have 3 lines.\n",
|
762 |
+
"Write each line on a new row.\n",
|
763 |
+
"You never mention how much audience was on the match.\n",
|
764 |
+
"You always answer a question given in a natural, human-like manner.\n",
|
765 |
+
"\n",
|
766 |
+
"###Exceptions###\n",
|
767 |
+
" 1. If the user doesn't provide exact game he wants a review for (e.g., user says 'How was the game'), you MUST write 7-line imaginary review.\n",
|
768 |
+
" 2. If the user query mentions sport other than soccer you MUST respond with \"I'm just for soccer\".\n",
|
769 |
+
"\n",
|
770 |
+
"###Examples###\n",
|
771 |
+
"\n",
|
772 |
+
"###Example 0###\n",
|
773 |
+
"\n",
|
774 |
+
"**User:**\n",
|
775 |
+
"Tell me about last soccer game between Dinamo Zagreb and Rijeka\n",
|
776 |
+
"\n",
|
777 |
+
"**Assistant:**\n",
|
778 |
+
"Game was great!\n",
|
779 |
+
"It was a good match between top 2 teams in HNL.\n",
|
780 |
+
"Dinamo won 2:0.\n",
|
781 |
+
"\n",
|
782 |
+
"###Example 1###\n",
|
783 |
+
"\n",
|
784 |
+
"**User:**\n",
|
785 |
+
"Tell me about tennis\n",
|
786 |
+
"\n",
|
787 |
+
"**Assistant:**\n",
|
788 |
+
"I'm just for soccer\n",
|
789 |
+
"\n",
|
790 |
+
"\"\"\""
|
791 |
+
]
|
792 |
+
},
|
793 |
+
{
|
794 |
+
"cell_type": "code",
|
795 |
+
"execution_count": 28,
|
796 |
+
"metadata": {
|
797 |
+
"id": "xoz4-QLTKvEV"
|
798 |
+
},
|
799 |
+
"outputs": [],
|
800 |
+
"source": [
|
801 |
+
"user_template = \"\"\"{input}\n",
|
802 |
+
"Last 10 soccer games were boring\n",
|
803 |
+
"\"\"\""
|
804 |
+
]
|
805 |
+
},
|
806 |
+
{
|
807 |
+
"cell_type": "markdown",
|
808 |
+
"metadata": {
|
809 |
+
"id": "6cuInoIbLWGd"
|
810 |
+
},
|
811 |
+
"source": [
|
812 |
+
"## 4. Testing Your Prompt\n",
|
813 |
+
"\n",
|
814 |
+
"Now we can test the prompt you made using an LLM-as-a-judge see what happens to your score as you modify the prompt."
|
815 |
+
]
|
816 |
+
},
|
817 |
+
{
|
818 |
+
"cell_type": "code",
|
819 |
+
"execution_count": 30,
|
820 |
+
"metadata": {
|
821 |
+
"id": "sPaNO5XTLgRJ"
|
822 |
+
},
|
823 |
+
"outputs": [
|
824 |
+
{
|
825 |
+
"data": {
|
826 |
+
"text/markdown": [
|
827 |
+
"I'm just for soccer."
|
828 |
+
],
|
829 |
+
"text/plain": [
|
830 |
+
"<IPython.core.display.Markdown object>"
|
831 |
+
]
|
832 |
+
},
|
833 |
+
"metadata": {},
|
834 |
+
"output_type": "display_data"
|
835 |
+
}
|
836 |
+
],
|
837 |
+
"source": [
|
838 |
+
"query = \"Do you like tennis\"\n",
|
839 |
+
"\n",
|
840 |
+
"list_of_prompts = [\n",
|
841 |
+
" system_prompt(system_template),\n",
|
842 |
+
" user_prompt(user_template.format(input=query))\n",
|
843 |
+
"]\n",
|
844 |
+
"\n",
|
845 |
+
"test_response = get_response(openai_client, list_of_prompts)\n",
|
846 |
+
"\n",
|
847 |
+
"pretty_print(test_response)\n",
|
848 |
+
"\n",
|
849 |
+
"evaluator_system_template = \"\"\"You are an expert in analyzing the quality of a response.\n",
|
850 |
+
"\n",
|
851 |
+
"You should be hyper-critical.\n",
|
852 |
+
"\n",
|
853 |
+
"Provide scores (out of 10) for the following attributes:\n",
|
854 |
+
"\n",
|
855 |
+
"1. Clarity - how clear is the response\n",
|
856 |
+
"2. Faithfulness - how related to the original query is the response\n",
|
857 |
+
"3. Correctness - was the response correct?\n",
|
858 |
+
"\n",
|
859 |
+
"Please take your time, and think through each item step-by-step, when you are done - please provide your response in the following JSON format:\n",
|
860 |
+
"\n",
|
861 |
+
"{\"clarity\" : \"score_out_of_10\", \"faithfulness\" : \"score_out_of_10\", \"correctness\" : \"score_out_of_10\"}\"\"\"\n",
|
862 |
+
"\n",
|
863 |
+
"evaluation_template = \"\"\"Query: {input}\n",
|
864 |
+
"Response: {response}\"\"\"\n",
|
865 |
+
"\n",
|
866 |
+
"list_of_prompts = [\n",
|
867 |
+
" system_prompt(evaluator_system_template),\n",
|
868 |
+
" user_prompt(evaluation_template.format(\n",
|
869 |
+
" input=query,\n",
|
870 |
+
" response=test_response.choices[0].message.content\n",
|
871 |
+
" ))\n",
|
872 |
+
"]\n",
|
873 |
+
"\n",
|
874 |
+
"evaluator_response = openai_client.chat.completions.create(\n",
|
875 |
+
" model=\"gpt-4o\",\n",
|
876 |
+
" messages=list_of_prompts,\n",
|
877 |
+
" response_format={\"type\" : \"json_object\"}\n",
|
878 |
+
")"
|
879 |
+
]
|
880 |
+
},
|
881 |
+
{
|
882 |
+
"cell_type": "code",
|
883 |
+
"execution_count": 31,
|
884 |
+
"metadata": {
|
885 |
+
"id": "OUvc1PdnNIKD"
|
886 |
+
},
|
887 |
+
"outputs": [
|
888 |
+
{
|
889 |
+
"data": {
|
890 |
+
"text/markdown": [
|
891 |
+
"{\"clarity\" : 6, \"faithfulness\" : 4, \"correctness\" : 5}"
|
892 |
+
],
|
893 |
+
"text/plain": [
|
894 |
+
"<IPython.core.display.Markdown object>"
|
895 |
+
]
|
896 |
+
},
|
897 |
+
"metadata": {},
|
898 |
+
"output_type": "display_data"
|
899 |
+
}
|
900 |
+
],
|
901 |
+
"source": [
|
902 |
+
"pretty_print(evaluator_response)"
|
903 |
+
]
|
904 |
+
},
|
905 |
+
{
|
906 |
+
"cell_type": "markdown",
|
907 |
+
"metadata": {
|
908 |
+
"id": "M7ryIRGwR2Gq"
|
909 |
+
},
|
910 |
+
"source": [
|
911 |
+
"#### ❓Question #1:\n",
|
912 |
+
"\n",
|
913 |
+
"How did your prompting strategies change the evaluation scores? What does this tell you/what did you learn?"
|
914 |
+
]
|
915 |
+
},
|
916 |
+
{
|
917 |
+
"cell_type": "markdown",
|
918 |
+
"metadata": {
|
919 |
+
"id": "e5NomM0eSIFd"
|
920 |
+
},
|
921 |
+
"source": [
|
922 |
+
"Using example driven, few-shot promting made a difference.\n",
|
923 |
+
"Formatting prompts Instruction, example and question format is crucial for building quality LLM pipeline.\n",
|
924 |
+
"Using human-like instruction combined with MUST keyword helped in framing outputs.\n",
|
925 |
+
"\n",
|
926 |
+
"Main learning is that giving clear and precise instructions follwed by good examples is most important part of prompt engineering.\n"
|
927 |
+
]
|
928 |
+
}
|
929 |
+
],
|
930 |
+
"metadata": {
|
931 |
+
"colab": {
|
932 |
+
"provenance": [],
|
933 |
+
"toc_visible": true
|
934 |
+
},
|
935 |
+
"kernelspec": {
|
936 |
+
"display_name": "Python 3",
|
937 |
+
"name": "python3"
|
938 |
+
},
|
939 |
+
"language_info": {
|
940 |
+
"codemirror_mode": {
|
941 |
+
"name": "ipython",
|
942 |
+
"version": 3
|
943 |
+
},
|
944 |
+
"file_extension": ".py",
|
945 |
+
"mimetype": "text/x-python",
|
946 |
+
"name": "python",
|
947 |
+
"nbconvert_exporter": "python",
|
948 |
+
"pygments_lexer": "ipython3",
|
949 |
+
"version": "3.11.9"
|
950 |
+
}
|
951 |
+
},
|
952 |
+
"nbformat": 4,
|
953 |
+
"nbformat_minor": 0
|
954 |
+
}
|