Skip to content

Commit 05dda47

Browse files
committed
Lesson 4: Single README, Updated Notebook
1 parent 3046b1a commit 05dda47

File tree

3 files changed

+127
-124
lines changed

3 files changed

+127
-124
lines changed

4-prompt-engineering-fundamentals/1-introduction.ipynb

+124-122
Original file line numberDiff line numberDiff line change
@@ -12,54 +12,16 @@
1212
"metadata": {},
1313
"source": [
1414
"# Introduction to Prompt Engineering\n",
15-
"Introduce the concept of prompt engineering and its importance in natural language processing."
16-
]
17-
},
18-
{
19-
"cell_type": "code",
20-
"execution_count": null,
21-
"metadata": {},
22-
"outputs": [],
23-
"source": [
24-
"# Introduction to Prompt Engineering\n",
25-
"#\n",
26-
"# Prompt engineering is the process of designing and optimizing prompts for natural language processing tasks.\n",
27-
"# It involves selecting the right prompts, tuning their parameters, and evaluating their performance.\n",
28-
"# Prompt engineering is crucial for achieving high accuracy and efficiency in NLP models.\n",
29-
"# In this section, we will explore the basics of prompt engineering and its importance in NLP."
30-
]
31-
},
32-
{
33-
"cell_type": "markdown",
34-
"metadata": {},
35-
"source": [
36-
"# What is Prompt Engineering?\n",
37-
"Define prompt engineering and explain how it differs from traditional natural language processing techniques."
38-
]
39-
},
40-
{
41-
"cell_type": "code",
42-
"execution_count": null,
43-
"metadata": {},
44-
"outputs": [],
45-
"source": [
46-
"# What is Prompt Engineering?\n",
47-
"#\n",
48-
"# Prompt engineering is a technique used in natural language processing to improve the accuracy and efficiency of models.\n",
49-
"# Unlike traditional NLP techniques that rely on large amounts of training data, prompt engineering involves designing and optimizing prompts\n",
50-
"# that guide the model towards the desired output.\n",
51-
"# By carefully selecting and tuning prompts, we can achieve high accuracy with much less training data.\n",
52-
"# Prompt engineering is particularly useful for tasks where training data is scarce or expensive to obtain.\n",
53-
"# Examples of such tasks include question answering, text completion, and summarization.\n",
54-
"# In summary, prompt engineering is a powerful tool for improving the performance of NLP models."
15+
"Prompt engineering is the process of designing and optimizing prompts for natural language processing tasks. It involves selecting the right prompts, tuning their parameters, and evaluating their performance. Prompt engineering is crucial for achieving high accuracy and efficiency in NLP models. In this section, we will explore the basics of prompt engineering using the OpenAI models for exploration."
5516
]
5617
},
5718
{
5819
"cell_type": "markdown",
5920
"metadata": {},
6021
"source": [
61-
"# Why is Prompt Engineering Important?\n",
62-
"Explain the benefits of prompt engineering, including improved model performance and interpretability."
22+
"### Exercise 1: Tokenization\n",
23+
"Explore Tokenization using tiktoken, an open-source fast tokenizer from OpenAI\n",
24+
"See [OpenAI Cookbook](https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb) for more examples.\n"
6325
]
6426
},
6527
{
@@ -68,22 +30,44 @@
6830
"metadata": {},
6931
"outputs": [],
7032
"source": [
71-
"# Why is Prompt Engineering Important?\n",
33+
"# EXERCISE:\n",
34+
"# 1. Run the exercise as is first\n",
35+
"# 2. Change the text to any prompt input you want to use & re-run to see tokens\n",
36+
"\n",
37+
"import tiktoken\n",
7238
"\n",
73-
"# Prompt engineering is important for several reasons. First, it can significantly improve the performance of NLP models.\n",
74-
"# By designing and optimizing prompts, we can guide the model towards the desired output and achieve higher accuracy with less data.\n",
75-
"# Second, prompt engineering can improve the interpretability of NLP models.\n",
76-
"# By using prompts that are designed to elicit specific types of information, we can gain insights into how the model is making predictions.\n",
77-
"# Finally, prompt engineering can help to mitigate bias in NLP models.\n",
78-
"# By carefully selecting prompts and evaluating their performance on diverse datasets, we can ensure that our models are fair and unbiased."
39+
"# Define the prompt you want tokenized\n",
40+
"text = f\"\"\"\n",
41+
"Jupiter is the fifth planet from the Sun and the \\\n",
42+
"largest in the Solar System. It is a gas giant with \\\n",
43+
"a mass one-thousandth that of the Sun, but two-and-a-half \\\n",
44+
"times that of all the other planets in the Solar System combined. \\\n",
45+
"Jupiter is one of the brightest objects visible to the naked eye \\\n",
46+
"in the night sky, and has been known to ancient civilizations since \\\n",
47+
"before recorded history. It is named after the Roman god Jupiter.[19] \\\n",
48+
"When viewed from Earth, Jupiter can be bright enough for its reflected \\\n",
49+
"light to cast visible shadows,[20] and is on average the third-brightest \\\n",
50+
"natural object in the night sky after the Moon and Venus.\n",
51+
"\"\"\"\n",
52+
"\n",
53+
"# Set the model you want encoding for\n",
54+
"encoding = tiktoken.encoding_for_model(\"gpt-3.5-turbo\")\n",
55+
"\n",
56+
"# Encode the text - gives you the tokens in integer form\n",
57+
"tokens = encoding.encode(text)\n",
58+
"print(tokens);\n",
59+
"\n",
60+
"# Decode the integers to see what the text versions look like\n",
61+
"[encoding.decode_single_token_bytes(token) for token in tokens]"
7962
]
8063
},
8164
{
8265
"cell_type": "markdown",
8366
"metadata": {},
8467
"source": [
85-
"# Types of Prompts\n",
86-
"Describe the different types of prompts, including classification, generation, and question-answering prompts."
68+
"### Exercise 2: Validate OpenAI API Key Setup\n",
69+
"\n",
70+
"Run the code below to verify that your OpenAI endpoint it setup correctly. The code just tries a simple basic prompt and validates the completion. Input `oh say can you see` should complete along the lines of `by the dawn's early light..`\n"
8771
]
8872
},
8973
{
@@ -92,39 +76,58 @@
9276
"metadata": {},
9377
"outputs": [],
9478
"source": [
95-
"# Types of Prompts\n",
79+
"# Run this as a common starting point for all the exercises below\n",
80+
"# It sets the OpenAI API key and uses a helper function that sets the relevant model and parameters\n",
9681
"\n",
97-
"# There are several types of prompts that can be used in natural language processing tasks. \n",
98-
"# In this section, we will discuss three main types of prompts: classification prompts, generation prompts, and question-answering prompts.\n",
82+
"import openai\n",
83+
"import os\n",
9984
"\n",
100-
"# Classification Prompts\n",
85+
"# Expects OPENAI_API_KEY in env variables \n",
86+
"# For GitHub Codespaces: set this as Codespaces secret => shows up as env var in OS\n",
87+
"# For Docker Desktop: create a .env file (and .gitignore it explicitly to be safe) => shows up as env var from load_dotenv\n",
88+
"from dotenv import load_dotenv, find_dotenv\n",
89+
"_ = load_dotenv(find_dotenv())\n",
10190
"\n",
102-
"# Classification prompts are used to classify input text into one or more categories. \n",
103-
"# They are often used in tasks such as sentiment analysis, where the goal is to determine the sentiment of a given text. \n",
104-
"# Classification prompts typically consist of a set of keywords or phrases that are associated with each category. \n",
105-
"# For example, a classification prompt for sentiment analysis might include keywords such as \"happy\", \"sad\", \"angry\", and \"excited\".\n",
91+
"# Note that we can set different env variables to different OPENAI keys and just map the right one to openai.api_key here\n",
92+
"# Example: have both OPENAI_API_KEY (for OpenAI) and AOAI_API_KEY (for Azure OpenAI) as options \n",
93+
"openai.api_key = os.getenv('OPENAI_API_KEY')\n",
10694
"\n",
107-
"# Generation Prompts\n",
95+
"# Print Environment Variables\n",
96+
"#for var in os.environ:\n",
97+
"# print(f\"{var}: {os.environ[var]}\")\n",
10898
"\n",
109-
"# Generation prompts are used to generate new text based on a given input. \n",
110-
"# They are often used in tasks such as text completion or summarization, where the goal is to generate a coherent and concise summary of a given text. \n",
111-
"# Generation prompts typically consist of a starting phrase or sentence, followed by a set of rules or constraints that guide the generation process. \n",
112-
"# For example, a generation prompt for text completion might include a starting phrase such as \"Once upon a time\", followed by rules such as \"the next sentence must include the word 'dragon'\".\n",
99+
"def get_completion(prompt, model=\"gpt-3.5-turbo\"):\n",
100+
" messages = [{\"role\": \"user\", \"content\": prompt}]\n",
101+
" response = openai.ChatCompletion.create(\n",
102+
" model=model,\n",
103+
" messages=messages,\n",
104+
" temperature=0, # this is the degree of randomness of the model's output\n",
105+
" max_tokens=1024\n",
106+
" )\n",
107+
" return response.choices[0].message[\"content\"]\n",
113108
"\n",
114-
"# Question-Answering Prompts\n",
109+
"## Set the primary content or simple prompt text here\n",
110+
"text = f\"\"\"\n",
111+
"oh say can you see\n",
112+
"\"\"\"\n",
113+
"\n",
114+
"## This uses a template that embeds the text \n",
115+
"## allowing you to add additional content like instructions, cues, examples\n",
116+
"prompt = f\"\"\"\n",
117+
"```{text}```\n",
118+
"\"\"\"\n",
115119
"\n",
116-
"# Question-answering prompts are used to answer a specific question based on a given context. \n",
117-
"# They are often used in tasks such as reading comprehension, where the goal is to answer questions about a given text. \n",
118-
"# Question-answering prompts typically consist of a question and a set of rules or constraints that guide the answering process. \n",
119-
"# For example, a question-answering prompt for reading comprehension might include a question such as \"What is the main idea of the passage?\", followed by rules such as \"the answer must be a single sentence\"."
120+
"## Run the prompt\n",
121+
"response = get_completion(prompt)\n",
122+
"print(response)"
120123
]
121124
},
122125
{
123126
"cell_type": "markdown",
124127
"metadata": {},
125128
"source": [
126-
"# Creating Effective Prompts\n",
127-
"Explain how to create effective prompts, including how to provide the necessary context and information for the model to make accurate predictions."
129+
"### Exercise 3: Hallucinations\n",
130+
"Explore what happens when you ask the LLM to return completions for a prompt about a topic that may not exist, or about topics that it may not know about because it was outside it's pre-trained dataset (more recent). See how the response changes if you try a different prompt, or a different model."
128131
]
129132
},
130133
{
@@ -133,70 +136,39 @@
133136
"metadata": {},
134137
"outputs": [],
135138
"source": [
136-
"# Creating Effective Prompts\n",
137139
"\n",
138-
"# To create effective prompts, it is important to provide the necessary context and information for the model to make accurate predictions.\n",
139-
"# This can be achieved by following these steps:\n",
140-
"\n",
141-
"# 1. Define the task: Clearly define the task that the model is expected to perform. This includes specifying the input format, output format, and any constraints or requirements.\n",
142-
"\n",
143-
"# 2. Identify the relevant information: Identify the information that is relevant to the task and ensure that it is included in the prompt. This may include specific keywords, phrases, or examples.\n",
144-
"\n",
145-
"# 3. Provide guidance: Provide guidance to the model on how to approach the task. This may include providing examples of correct and incorrect outputs, or specifying the types of errors to avoid.\n",
140+
"## Set the text for simple prompt or primary content\n",
141+
"## Prompt shows a template format with text in it - add cues, commands etc if needed\n",
142+
"## Run the completion \n",
143+
"text = f\"\"\"\n",
144+
"generate a lesson plan on the Martian War of 2076.\n",
145+
"\"\"\"\n",
146146
"\n",
147-
"# 4. Test and refine: Test the prompt on a diverse set of inputs and evaluate its performance. Refine the prompt as necessary to improve its accuracy and efficiency.\n",
147+
"prompt = f\"\"\"\n",
148+
"```{text}```\n",
149+
"\"\"\"\n",
148150
"\n",
149-
"# By following these steps, we can create effective prompts that guide the model towards the desired output and achieve high accuracy with less data."
151+
"response = get_completion(prompt)\n",
152+
"print(response)"
150153
]
151154
},
152155
{
153156
"cell_type": "markdown",
154157
"metadata": {},
155158
"source": [
156-
"# Try OpenAI Example"
159+
"### Exercise 4: Instruction Based \n",
160+
"Use the \"text\" variable to set the primary content \n",
161+
"and the \"prompt\" variable to provide an instruction related to that primary content.\n",
162+
"\n",
163+
"Here we ask the model to summarize the text for a second-grade student"
157164
]
158165
},
159166
{
160167
"cell_type": "code",
161-
"execution_count": 1,
168+
"execution_count": null,
162169
"metadata": {},
163-
"outputs": [
164-
{
165-
"name": "stdout",
166-
"output_type": "stream",
167-
"text": [
168-
"Jupiter is a really big planet that is fifth from the Sun. It is made of gas and is the largest planet in our Solar System. It is much smaller than the Sun, but much bigger than all the other planets combined. People have known about Jupiter for a really long time because it is very bright in the night sky. It is named after a god from ancient Rome. Sometimes, Jupiter is so bright that it can make shadows on Earth. It is usually the third-brightest thing we can see at night, after the Moon and Venus.\n"
169-
]
170-
}
171-
],
170+
"outputs": [],
172171
"source": [
173-
"import openai\n",
174-
"import os\n",
175-
"\n",
176-
"# Expects OPENAI_API_KEY in env variables \n",
177-
"# For GitHub Codespaces: set this as Codespaces secret => shows up as env var in OS\n",
178-
"# For Docker Desktop: create a .env file (and .gitignore it explicitly to be safe) => shows up as env var from load_dotenv\n",
179-
"from dotenv import load_dotenv, find_dotenv\n",
180-
"_ = load_dotenv(find_dotenv())\n",
181-
"\n",
182-
"# Note that we can set different env variables to different OPENAI keys and just map the right one to openai.api_key here\n",
183-
"# Example: have both OPENAI_API_KEY (for OpenAI) and AOAI_API_KEY (for Azure OpenAI) as options \n",
184-
"openai.api_key = os.getenv('OPENAI_API_KEY')\n",
185-
"\n",
186-
"# Print Environment Variables\n",
187-
"#for var in os.environ:\n",
188-
"# print(f\"{var}: {os.environ[var]}\")\n",
189-
"\n",
190-
"def get_completion(prompt, model=\"gpt-3.5-turbo\"):\n",
191-
" messages = [{\"role\": \"user\", \"content\": prompt}]\n",
192-
" response = openai.ChatCompletion.create(\n",
193-
" model=model,\n",
194-
" messages=messages,\n",
195-
" temperature=0, # this is the degree of randomness of the model's output\n",
196-
" max_tokens=1024\n",
197-
" )\n",
198-
" return response.choices[0].message[\"content\"]\n",
199-
"\n",
200172
"# Test Example\n",
201173
"# https://platform.openai.com/playground/p/default-summarize\n",
202174
"\n",
@@ -229,9 +201,39 @@
229201
"cell_type": "markdown",
230202
"metadata": {},
231203
"source": [
232-
"Response should have been something like:\n",
204+
"### Exercise 5: Complex Prompt \n",
205+
"Try a request that has system, user and assistant messages \n",
206+
"System sets assistant context\n",
207+
"User & Assistant messages provide multi-turn conversation context\n",
233208
"\n",
234-
"> Jupiter is a really big planet that is fifth from the Sun. It is made of gas and is the largest planet in our Solar System. It is much smaller than the Sun, but much bigger than all the other planets combined. People have known about Jupiter for a really long time because it is very bright in the night sky. It is named after a god from ancient Rome. Sometimes, Jupiter is so bright that it can make shadows on Earth. It is usually the third-brightest thing we can see at night, after the Moon and Venus."
209+
"Note how the assistant personality is set to \"sarcastic\" in the system context. \n",
210+
"Try using a different personality context. Or try a different series of input/output messages"
211+
]
212+
},
213+
{
214+
"cell_type": "code",
215+
"execution_count": null,
216+
"metadata": {},
217+
"outputs": [],
218+
"source": [
219+
"response = openai.ChatCompletion.create(\n",
220+
" model=\"gpt-3.5-turbo\",\n",
221+
" messages=[\n",
222+
" {\"role\": \"system\", \"content\": \"You are a sarcastic assistant.\"},\n",
223+
" {\"role\": \"user\", \"content\": \"Who won the world series in 2020?\"},\n",
224+
" {\"role\": \"assistant\", \"content\": \"Who do you think won? The Los Angeles Dodgers of course.\"},\n",
225+
" {\"role\": \"user\", \"content\": \"Where was it played?\"}\n",
226+
" ]\n",
227+
")\n",
228+
"print(response.choices[0].message[\"content\"])"
229+
]
230+
},
231+
{
232+
"cell_type": "markdown",
233+
"metadata": {},
234+
"source": [
235+
"### Exercise: Explore Your Intuition\n",
236+
"The above examples give you patterns that you can use to create new prompts (simple, complex, instruction etc.) - try creating other exercises to explore some of the other ideas we've talked about like examples, cues and more."
235237
]
236238
}
237239
],

4-prompt-engineering-fundamentals/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ But what if the user wanted to see something specific that met some criteria or
9393

9494
![Base LLM Chat Completion](./img/4.0-playground-chat-base.png)
9595

96-
### 1.4.3 Concept 3: Instruction Tuned LLMs
96+
### 1.4.3 Concept: Instruction Tuned LLMs
9797

9898
An [Instruction Tuned LLM](https://blog.gopenai.com/an-introduction-to-base-and-instruction-tuned-large-language-models-8de102c785a6) starts with the foundation model and fine-tunes it with examples or input/output pairs (e.g., multi-turn "messages") that can contain clear instructions - and the response from the AI attempt to follow that instruction.
9999

requirements.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ numpy==1.24.2
44
pandas==1.5.3
55
tqdm==4.64.0
66
python-dotenv==1.0.0
7-
openai>=0.28.0
7+
openai>=0.28.0
8+
tiktoken

0 commit comments

Comments
 (0)