Gradient cyan and magenta resolution bit colors
© 2023 Aymen El Amri @eon01
Contents
Keep in Touch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
How Does GPT Work? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Testing our API Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Available Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Listing all Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Which Model to Use? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Logprobs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Controlling Creativity: The Sampling Temperature . . . . . . . . . . . . . . . . . . . . . . . . 33
Getting the “best of” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
CONTENTS
Embedding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Overview of Embedding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Use Cases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 85 Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Understanding Text Embedding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Embeddings for Multiple Inputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Semantic Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 99 Cosine Similarity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Dataset Used in the Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 Preparing the Data and Launching the Fine Tuning . . . . . . . . . . . . . . . . . . . . . . . . 135 Testing the Fine Tuned Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Advanced Fine Tuning: Creating a Chatbot Assistant . . . . . . . . . . . . . . . . . . . . . . . . 142 Interactive Classification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 How Will Everything Work? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 Creating a Conversational Web App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Generating Images Using DALL-E . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 A Basic Example of Image Generation From a Prompt . . . . . . . . . . . . . . . . . . . . . . 194 Generating Multiple Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 Using Different Sizes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 Better Image Prompts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 Building a Random Image Generator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
CONTENTS
When people ask me about what I do, I always struggle to give them a straightforward answer. My career has taken me down many different paths, and I have worn many different hats over the years. I am someone who has always been passionate about learning and trying new things, which has led me to work in many different fields. I have
I worked in software development, advertising, marketing, network and telecom, systems adminis-tration, training, technical writing, computer repair and other fields. I have always been driven by the desire to learn more and expand my horizons. As I learned new technologies, met new people, and explored new concepts, my mind became more open and my perspective widened. I began to see connections and possibilities that I had never seen before.
My inbox is always full, and I receive a lot of emails. However, the best emails I have received were from people who have read my online guides and courses and found them useful. Please feel free to reach out to me at aymen@faun.dev. I would love to hear from you.
I hope you enjoy reading this guide as much as I enjoyed writing it.
Some projects he founded are ¹, ², and ³. You can find Aymen on ⁴ and ⁵.
¹
²
³
⁴
⁵
In 2018, Elon Musk resigned from his board seat, citing “a potential future conflict of interest” with Tesla AI development for self-driving cars, but remained a donor. In 2019, OpenAI transitioned from non-profit to capped-profit, with a profit cap set to 100 times on any investment. The company distributed equity to its employees and partnered with Microsoft, which announced an investment package of US$1 billion into the company. OpenAI then announced its intention to commercially license its technologies.
In 2020, OpenAI announced GPT-3, a language model trained on trillions of words from the Internet. It also announced that an associated API, simply named “the API”, would form the heart of its first commercial product. GPT-3 is aimed at natural language answering questions, but it can also translate between languages and coherently generate improvised text. In 2021, OpenAI introduced DALL-E, a deep-learning model that can generate digital images from natural language descriptions.
⁶
ChatGPT, GPT, GPT-3, DALL·E, Codex? 4
• GitHub Copilot (using the OpenAI Codex model, a descendant of GPT-3, fine-tuned for generating code)
• ⁷ and ⁸ (content generation for marketing purposes)
• Drexel University (detection of early signs of Alzheimer’s disease)
• Algolia (enhancing their search engine capabilities)
You surely heard of GPT-4. GPT-4 is the successor of GPT-3 and is an unreleased neural network created also by OpenAI.
When asked about this viral illustration, Altman called it “complete bullshit.”
“The GPT-4 rumor mill is a ridiculous thing. I don’t know where it all comes from. People are begging to be disappointed and they will be. The hype is just like… We don’t have an actual AGI and that’s sort of what’s expected of us.”
The goal of this guide is to provide a step-by-step guide to using GPT-3 in your projects through this API. Other models are also treated in this guide, such as the CLIP, DALL-E, and Whispers.
ChatGPT, GPT, GPT-3, DALL·E, Codex? 6
By the end of your learning journey, you will have built applications such as:
• A fine-tuned medical chatbot assistant
• An intelligent coffee recommendation system
• An intelligent conversational system with memory and context• An AI voice assistant like Alexa but smarter
• A Chatbot assistant to help with Linux commands
• A semantic search engine
• A news category prediction system
• An image recognition intelligent system (image to text)
• An image generator (text to image)
• and more!• Transform and edit the text to perform translation, formatting, and other useful tasks.• Optimize the performance of GPT models using the various parameters and options such as suffix, max_tokens, temperature, top_p, n, stream, logprobs, echo, stop, presence_penalty, frequency_penalty, best_of, and others.
• Stem, lemmatize, and reduce your bills when using the API
• Understand Context Stuffing, chaining, and practice using advanced techniques• Understand text embedding and how companies such as Tesla and Notion are using it• Understand and implement semantic search and other advanced tools and concepts.• Practice training and classification techniques using GPT.
• Create advanced fine-tuned models.
Keep in Touch
If you want to stay up to date with the latest trends in Python and AI ecosystems, join our developer community using ⁹. We send weekly newsletters with must-read tutorials, news, and insights from experts in the software engineering community.
GPT-3 is another model, it’s obviously larger and more performant than any other previous GPT model including GPT-1 and GPT-2.
The 3rd generation was trained on a large corpus of text, such as books, articles, and publically accessible websites like Reddit and other forums, it uses this training data to learn patterns and relationships between words and phrases.
1 from transformers import pipeline
2 generator = pipeline('text-generation', model = 'gpt2')
7 in Python. I love to code, and that includes coding things that require writing"}, \
8 ...
3
8 # Set the model to evaluation mode
9 model.eval()
14 # Tokenize the prompt and generate text
15 input_ids = tokenizer.encode(prompt, return_tensors='pt')
20 print("AI: " + generated_text)
GPT-3 was designed as a general-purpose language model, which means that it can be used for a
used for building chatbots that are experts in a specific domain, other conversational interfaces, and
more!
related fields.
GPT-3 is considered a significant advancement in natural language processing because of its
Happy coding!
Preparing the Development Environment
If Python is not installed, go to ¹¹, download, and install one of the Python 3.x versions. Depending on your operating system, you will have to follow different instructions.
To manage our development environment, we are going to use ¹². You’ll find the installation instructions in ¹³.
Open a terminal/command prompt, cd to the folder containing the get-pip.py file and run:
¹¹
¹²
¹³
¹⁴
| 1 | 11 | |
|---|---|---|
| 2 |
In summary, you should have these packages installed in your system:
• Python
• pip
• virtualenvwrapper (virtualenv or any other package manager you prefer)
1 workon chatgptforpythondevelopers
Get Your OpenAI API Keys
Preparing the Development Environment 12
Installing the Official Python Bindings
no longer updated.
To install the official Python bindings, run the following command:
Let’s store the API key and the organization id in a .env file before:
1 cat << EOF > .env
their respective values.
Now you can execute the following command:
You can skip sourcing the variables and using them directly in the curl command:
¹⁷
If you have one organization in your OpenAI account, it is possible to execute the same command
without mentioning the organization id.
1 import os
2 import openai
7 key, value = line.strip().split("=")
8 os.environ[key] = value
13
14 # Calling the API and listing models
| 1 |
|
14 |
|---|---|---|
|
||
| 2 |
6 for line in env:
7 key, value = line.strip().split("=")
12
13 init_api()
The Three Main Models
There are main models or families of models if we can call them that:
The GPT-3 model is capable of understanding human language and text that appears to be natural language. This model family comes in a series of 4 models (A, B, C, D) that are more or less fast and performant.
• D: text-davinci-003
• C: text-curie-001
• B: text-babbage-001
• A: text-ada-001One of its advantages is allowing requests of up to 4k tokens. It also supports inserting completions within the text.
We will define tokens in more detail later in this guide. For now, just know that they determine the length of the user’s request.
It could be a good option for translation, complex classification, text analysis, and summaries.
text-babbage-001
This model is very fast and cost-effective, to be preferred for the simplest classifications, text extraction, and address correction.
Codex: Understanding and Generating Computer Code
code-davinci-002
The Codex model is the most capable. It excels at translating natural language into code. Not only does it complete the code, but it also supports the insertion of supplementary elements. It can handle up to 8,000 tokens and was trained with data dating up to June 2021.
It is also faster, and more affordable than Davinci.
Content Filter
• sensitive,
• unsafe.
Let’s see how this works in practice:
1 import os
6 for line in env:
7 key, value = line.strip().split("=")
12
13 init_api()
As you can see, we simply use the function list() from the module Model of the package openai.
as a result, we get a list similar to the following:
| 41 | 19 | |
|---|---|---|
|
||
| 42 |
|
|
| 43 | ||
| 44 | ||
| 45 | ||
| 46 | ||
| 47 |
|
|
| 48 |
|
|
| 49 | ||
| 50 | ||
| 51 | ||
| 52 | ||
| 53 |
|
|
| 54 |
|
|
| 55 | ||
| 56 | ||
| 57 | ||
| 58 |
Let’s print just the ids of the models:
1 import os
6 for line in env:
7 key, value = line.strip().split("=")
12
13 init_api()
The result should be:
| 44 | 21 | |
|---|---|---|
| 45 | ||
| 46 |
|
|
| 47 |
|
|
| 48 | ||
| 49 | ||
| 50 | ||
| 51 | ||
| 52 |
|
|
| 53 |
|
|
| 54 | ||
| 55 | ||
| 56 | ||
| 57 | ||
| 58 |
|
|
| 59 |
|
|
| 60 | ||
| 61 | ||
| 62 | ||
| 63 | ||
| 64 |
|
|
| 65 |
|
|
| 66 |
Other search models optimized for searching text like ada-search-document, ada-search-query, babbage-search-document, babbage-search-query, curie-search-document and curie-search-query are also deprecated and were replaced by text-search-ada-doc-001, text-search-ada-query-001,
| curie-similarity, | davinci-similarity | were | replaced | by |
|---|
ada-instruct-beta are replaced by text-davinci-001, text-curie-001 , text-babbage-001and
API requests that use the old names will still work, as OpenAI has ensured backward compatibility. However, it is recommended that you use the updated names.
Example of a deprecated call:
Davinci models are by far the best models but they are the most expensive. So if optimizing costs is not your first priority and you want to focus on quality, Davinci is the best choice for you. More specifically, text-davinci-003 is the most powerful model.
Compared to davinci, text-davinci-003 is a newer, more capable model designed specifically for instruction-following tasks and zero-shot scenarios, a concept that we are going to explore later in this guide.
Available Models 23
What’s next?
¹⁸
Using GPT Text Completions
Begin by providing the start of a sentence. The model will then predict one or more possible
completions, each with an associated score.
1 workon chatgptforpythondevelopers
Create a new Python file “app.py” where you will add the following code:
5 with open(".env") as env:
6 for line in env:
11 openai.organization = os.environ.get("ORG_ID")
12
17 prompt="Once upon a time",
| 18 |
|
25 |
|---|---|---|
| 19 | ||
| 20 |
22 print(next)
Now execute the file:
Note: Tokens, by definition, are common sequences of characters in the output text. A good way to remember is that one token usually means about 4 letters of text for normal English words. This means that 100 tokens are about the same as 75 words. Grasping this will aid you in comprehending the pricing. Later in this book, we will delve deeper into pricing details.
Using GPT Text Completions 26
3
4 def init_api():
9
10 openai.api_key = os.environ.get("API_KEY")
15 next = openai.Completion.create(
16 model="text-davinci-003",
21
22 print(next)
4 "finish_reason": "length",
5 "index": 0,
10 ],
11 "created": 1674510438,
| 14 | 27 | |
|---|---|---|
| 15 |
|
|
| 16 |
|
|
| 17 | ||
| 18 | ||
| 19 | ||
| 20 |
Logprobs
To increase the possibilities, we can use the “logprobs” parameter. For example, setting logprobs to
4 def init_api():
5 with open(".env") as env:
10 openai.api_key = os.environ.get("API_KEY")
11 openai.organization = os.environ.get("ORG_ID")
16 model="text-davinci-003",
17 prompt="Once upon a time",
22
23 print(next)
| 44 | 29 | |
|---|---|---|
|
||
| 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 |
|
Using GPT Text Completions 30
91 {
92 " Alice": -1.2721952,
97 " was": -1.7077477
98 },
103 {
104 " a": -0.0038384167,
109 " small": -0.029725535
110 },
115 {
116 " in": -1.7855972,
121 "text": " there was a little girl named Alice. She lived in a small village wi\
122 th"
127 "model": "text-davinci-003",
128 "object": "text_completion",
will be the case for other values.
1 {
6 " lived": -2.040701,
7 " was": -0.2422086
12 },
13 {
18 " boy": -2.449015,
19 " girl": -0.15475112
24 },
25 {
30 " who": -1.3002346,
31 ".": -0.34726024
| 32 | 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 |
|
3 " was",
4 " a",
9 ".",
10 " She",
Controlling Creativity: The Sampling Temperature
The next parameter we can customize is the temperature. This can be used to make the model more
2 import openai
3
8 os.environ[key] = value
9
14
15 next = openai.Completion.create(
20 )
21
2 "choices": [
3 {
8 st carwhen"
9 }
14 "object": "text_completion",
15 "usage": {
20 }
The temperature is set to its maximum value, so executing the same script should return a distinct
highest probability mass, comprising 10%, are considered.
1 import os
6 for line in env:
7 key, value = line.strip().split("=")
12
| 13 |
|
35 |
|---|---|---|
17 prompt="Once upon a time",
18 max_tokens=15,
It is recommended to either use the top_p parameter or the temperature parameter but not both.
The top_p parameter is also called nucleus sampling or top-p sampling.
1 import os
2 import openai
7 key, value = line.strip().split("=")
8 os.environ[key] = value
13 init_api()
14
19 stream=True,
20 )
24
25 # * will unpack the generator
| 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 |
| 79 |
|
38 |
|---|---|---|
|
||
| 80 | ||
| 81 | ||
| 82 | ||
| 83 | ||
| 84 |
|
|
| 85 |
|
|
| 86 | ||
| 87 | ||
| 88 | ||
| 89 | ||
| 90 |
|
|
| 91 |
|
|
| 92 | ||
| 93 | ||
| 94 | ||
| 95 | ||
| 96 |
|
|
| 97 |
|
|
| 98 |
3
4 def init_api():
9
10 openai.api_key = os.environ.get("API_KEY")
15 next = openai.Completion.create(
16 model="text-davinci-003",
Using GPT Text Completions 39
21
1 there
2 was
7 was
Controlling Repetitivity: Frequency and Presence
These features can be enabled using two parameters:
• presence_penalty is a number that can be between -2.0 and 2.0. If the number is positive, it
In order to understand the effect of these parameters, let’s use them in the following code:
1 import os
6 for line in env:
7 key, value = line.strip().split("=")
12
17 prompt="Once upon a time",
18 max_tokens=100,
23 print("=== Frequency and presence penalty 2.0 ===")
24 print(next["choices"][0]["text"])
29 max_tokens=100,
30 frequency_penalty=-2.0,
35 print(next["choices"][0]["text"])
As you can see, the first execution will produce more diversity in the text (frequency_penalty=2.0
2
3 there was a beautiful princess named Cinderella.
8 her more strength to reach for what she desired most - to attend that magnificent e\
9 vent alongside everyone else but not just only that
13 She lived a very simple life. She lived a very happy life. She lived a very happy l\
14 ife. She lived a very happy life. She lived a very happy life. She lived a very happ\
at a certain level.
Controlling the Number of Outputs
3
4 def init_api():
9
10 openai.api_key = os.environ.get("API_KEY")
15 next = openai.Completion.create(
16 model="text-davinci-003",
21
22 print(next)
| 1 | 42 | |
|---|---|---|
| 2 | ||
| 3 |
|
|
| 4 |
|
|
| 5 | ||
| 6 | ||
| 7 | ||
| 8 | ||
| 9 |
|
|
| 10 |
|
|
| 11 | ||
| 12 | ||
| 13 | ||
| 14 | ||
| 15 |
|
|
| 16 |
|
|
| 17 | ||
| 18 | ||
| 19 | ||
| 20 | ||
| 21 |
|
|
| 22 |
|
|
| 23 | ||
| 24 | ||
| 25 |
It is possible to ask the AI model to generate possible completions for a given task on the server side and select the one with the highest probability of being correct. This can be done using the best_of parameter.
When using best_of, you need to specify two numbers: n and best_of.
7 key, value = line.strip().split("=")
8 os.environ[key] = value
13 init_api()
14
19 n=1,
20 best_of=2,
In most cases, it is useful to stop the API from generating more text.
Let’s say, we want to generate a single paragraph and no more. In this case, we can ask the API
3
4 def init_api():
9
10 openai.api_key = os.environ.get("API_KEY")
| 11 | 44 | |
|---|---|---|
14
15 next = openai.Completion.create(
20 )
21
1 import os
2 import openai
7 key, value = line.strip().split("=")
8 os.environ[key] = value
13 init_api()
14
19 stop=["\n", "Story", "End", "Once upon a time"],
20 )
Imagine, we want to create a Python dict containing the list of primary numbers between 0 and 9:
Let’s try two examples to better understand.
In the first example, we are going to tell the API how the dict should start using {\n\t\"primes\":
4 def init_api():
5 with open(".env") as env:
10 openai.api_key = os.environ.get("API_KEY")
11 openai.organization = os.environ.get("ORG_ID")
16 model="text-davinci-002",
17 prompt= "Write a JSON containing primary numbers between 0 and 9 \n\n{\n\t\"prim\
The API should return this text:
1 2, 3, 5, 7]\n}
| 1 | 46 | |
|---|---|---|
|
||
| 2 |
|
7 key, value = line.strip().split("=")
8 os.environ[key] = value
13 init_api()
14
19 suffix= "]\n}",
20 )
Instead of the previous one (2, 3, 5, 7]\n})
And this is how we can inform the API about the suffix of a completion.
1 The first programming language to be invented was Plankalkül, which was designed by \
2 Konrad Zuse in the 1940s, but not publicly known until 1972 (and not implemented unt\
7 ly, John McCarthy of MIT developed Lisp, the first language with origins in academia\
8 to be successful. With the success of these initial efforts, programming languages \
2 import openai
3
8 os.environ[key] = value
9
14
15 prompt = "The first programming language to be invented was Plankalkül, which was de\
20 ntists to develop a universal computer language; the result of their effort was AL\
21 GOL 58. Separately, John McCarthy of MIT developed Lisp, the first language with o\
¹⁹
| 29 | 48 | |
|---|---|---|
| 30 | ||
| 31 |
32
33 print(tweet)
4 in the 1960s and beyond
5
2 McCarthy, MIT, Lisp
You can play with the prompt and try different things such as:
5
6 Keywords:
2 signed by Konrad Zuse in the 1940s, but not publicly known until 1972 (and not imple\
3 mented until 1998). The first widely known and successful high-level programming l\
8 rigins in academia to be successful. With the success of these initial efforts, prog\
9 ramming languages became an active topic of research in the 1960s and beyond\n\nKeyw\
3 - FORTRAN
4 - John Backus
9 - Lisp
Example: Generating Tweets
4 def init_api():
5 with open(".env") as env:
10 openai.api_key = os.environ.get("API_KEY")
11 openai.organization = os.environ.get("ORG_ID")
15 prompt = "The first programming language to be invented was Plankalkül, which was de\
16 signed by Konrad Zuse in the 1940s, but not publicly known until 1972 (and not imple\
21 GOL 58. Separately, John McCarthy of MIT developed Lisp, the first language with o\
22 rigins in academia to be successful. With the success of these initial efforts, prog\
27 model="text-davinci-002",
28 prompt=prompt,
33 print(tweet)
As you can see, the prompt is:
5
6 Tweet:
6 Tweet with hashtags:
This is how the code will look:
5 with open(".env") as env:
6 for line in env:
11 openai.organization = os.environ.get("ORG_ID")
12
17 mented until 1998). The first widely known and successful high-level programming l\
18 anguage was Fortran, developed from 1954 to 1957 by a team of IBM researchers led \
23 ramming languages became an active topic of research in the 1960s and beyond\n\nTwee\
24 t with hashtags:"
29 temperature=0.5,
30 max_tokens=300,
This is how the result should look like:
1 #Plankalkül was the first #programming language, invented by Konrad Zuse in the 1940\
To adjust the result you can play with max_tokens to change the length of the tweet knowing that:
• 100 tokens ∼= 75 words
Example: Generating a Rap Song
In this example, we are going to see how to generate a rap song. You can reuse the same example to
4 def init_api():
5 with open(".env") as env:
10 openai.api_key = os.environ.get("API_KEY")
11 openai.organization = os.environ.get("ORG_ID")
| 15 16 17 18 |
|
|---|---|
²⁰
Notice how we can directly access and strip the text using my_song.choices[0]["text"].strip()
to only print the song:
5 My momma didn't have much
6
11 But I always had heart
12
17 Now I'm on top of the world
18
In this example, we are asking the model to generate a to-do list for creating a company in the US.
We need five items on the list.
| 1 | 54 | |
|---|---|---|
| 2 |
4 def init_api():
5 with open(".env") as env:
10 openai.api_key = os.environ.get("API_KEY")
11 openai.organization = os.environ.get("ORG_ID")
16 model="text-davinci-002",
17 prompt="Todo list to create a company in US\n\n1.",
22 presence_penalty=0.5,
23 stop=["6."],
1 1. Choose a business structure.
2
7 4. Open a business bank account.
8
Let’s re-examine them one by one:
3 3. <3nd item>
4 4. <4th item>
frequency_penalty is used to adjust the model’s preference for generating frequent or rare words. A positive value will decrease the chances of frequent words, while a negative value will increase them. In this case, the frequency_penalty is set to 0
presence_penalty is used to adjust the model’s preference for generating words that are present or absent in the prompt. A positive value will decrease the chances of words that are present in the prompt, a negative value will increase them. The presence_penalty is set to 0.5 in our example.
By configuring the right values for some parameters such as frequency and presence penalties, the results can be tailored to produce desired outcomes.
With the ability to control when the completion stops, the user can also control the length of the generated text. This could be also helpful to reduce the number of tokens generated and indirectly reduce costs.
instructions.
A GPT model is able to understand the context of the prompt and the instructions given, allowing
1 workon chatgptforpythondevelopers
Note that the “.env” file should be always present in the current directory where the Python file is
3
4 def init_api():
9
10 openai.api_key = os.environ.get("API_KEY")
15 response = openai.Edit.create(
16 model="text-davinci-edit-001",
| 17 | 58 | |
|---|---|---|
| 18 | ||
| 19 |
20
21 print(response)
4 def init_api():
5 with open(".env") as env:
10 openai.api_key = os.environ.get("API_KEY")
11 openai.organization = os.environ.get("ORG_ID")
2 model="text-davinci-edit-001",
3 input="Hallo Welt",
After executing the code above, you should see the following output:
Unlike in completion, where we provide the API with a prompt, we need to provide the instruction
and the input here.
2 import openai
3
8 os.environ[key] = value
9
14
15 response = openai.Edit.create(
| 17 | 60 | |
|---|---|---|
| 18 |
19
20 print(response)
1 import os
2 import openai
7 key, value = line.strip().split("=")
8 os.environ[key] = value
13 init_api()
14
19 )
20
3
8 os.environ[key] = value
9
14
15 next = openai.Completion.create(
20 French:
21 Arabic:
26 )
27
3
4 def init_api():
9
10 openai.api_key = os.environ.get("API_KEY")
| 13 | 62 | |
|---|---|---|
14
15 response = openai.Edit.create(
20
21 print(response['choices'][0]['text'])
3
4 def init_api():
9
10 openai.api_key = os.environ.get("API_KEY")
15 response = openai.Edit.create(
16 model="text-davinci-edit-001",
21 import (
22 "io/ioutil"
27 func main() {
28 resp, err := http.Get("https://website.com")
32
33 body, err := ioutil.ReadAll(resp.Body)
| 38 | } |
|
|---|---|---|
| 39 |
|
|
| 40 | ||
| 41 |
45 print(response['choices'][0]['text'])
The output will not be readable:
5 "text": "\npackage main\n\nimport (\n\t// code to work with input and output\n\
6 \t\"io/ioutil\"\n\n\t// code to work with logging\n\t\"log\"\n\n\t// code to work wi\
11 }
12 ],
17 "prompt_tokens": 162,
18 "total_tokens": 629
This is the output of the code above:
1 //Main File - this will connect to the webpagge, download the file and print out con\
6 //Import io for string and log for any problems I may have with connection
7 "io/ioutil"
12 func main() {
13 //http.Get will connect to the provided webpage
18 //once webpagge is connected body, err will download the webpage
19
24 //once I have the body of the webpage then body, err will download the webpage
25 }
| 27 | } | |
|---|---|---|
| 28 | ||
| 29 | ||
| 30 |
Editing Text Using GPT 65
Creativity vs. Well-Defined Answers
2 import openai
3
8 os.environ[key] = value
9
14
15 response_1 = openai.Edit.create(
20 )
21
26 temperature=0.9,
27 )
32 print(response_2['choices'][0]['text'])
Generally, after running the code multiple times, you may observe that the first output is consistent,
Here’s an opportunity to be more creative:
1 import os
6 for line in env:
7 key, value = line.strip().split("=")
12
13 init_api()
18 instruction="Edit the text to make it longer.",
19 temperature=0.9,
1 Exercise is good for your health. Especially if you haven't done any for a month.
This is another variation of the output:
1 import os
2 import openai
7 key, value = line.strip().split("=")
8 os.environ[key] = value
13 init_api()
14
19 top_p=0.1,
20 )
probability mass are considered in the result.
Editing Text Using GPT 68
2 import openai
3
8 os.environ[key] = value
9
14
15 response = openai.Edit.create(
20 n=2,
21 )
I also used top_p=0.2 . However, this has no bearing on the number of results; I simply wanted to
have a wider range of results.
Chaining Completions and Edits
In this example, we are going to ask the model to generate a tweet out of a text, then translate it.
3
4 def init_api():
9
10 openai.api_key = os.environ.get("API_KEY")
15 prompt = "The first programming language to be invented was Plankalkül, which was de\
16 signed by Konrad Zuse in the 1940s, but not publicly known until 1972 (and not imple\
21 GOL 58. Separately, John McCarthy of MIT developed Lisp, the first language with o\
22 rigins in academia to be successful. With the success of these initial efforts, prog\
26 english_tweet = openai.Completion.create(
27 model="text-davinci-002",
32
33 english_tweet_text = english_tweet["choices"][0]["text"].strip()
38 model="text-davinci-edit-001",
39 input=english_tweet_text,
44 spanish_tweet_text = spanish_tweet["choices"][0]["text"].strip()
45 print("Spanish Tweet:")
3 Spanish Tweet:
4 El primer lenguaje de programación inventado fue #Plankalkül
be seen when we provide the model with words such as “light”. “Light” can be a noun, an adjective,
or a verb.
There are other words that can be at the same time used as nouns, adjectives, or verbs: “firm”, “fast”,
“well” ..etc, and this example applies to them too.
4 model="text-davinci-002",
5 prompt=prompt,
10 print(result.choices[0]["text"].strip())
You can try the code, it will sometimes output verb, sometimes adjective, sometimes noun, but I
Advanced Text Manipulation Examples 72
Let’s ask the model while giving it some hints. The hint can be anything that will help the model
3 '.\n\n"
4 prompt_c = "You light up my life. Determine the part of speech of the word 'light'.\\
9 model="text-davinci-002",
10 prompt=prompt,
15 print(result.choices[0]["text"].strip())
Understanding the context better leads to this result:
instance, we want to understand that Apple is a company, whereas in the second, Apple should refer
to the fruit.
5 result = openai.Completion.create(
6 model="text-davinci-002",
11 )
12
Let’s see a second example where we provide a schema or a template that the model should follow
in the output.
3
4 def init_api():
9
10 openai.api_key = os.environ.get("API_KEY")
15 prompt = """Input: Bitcoin
16 Output:
21 Input: Ethereum
22 Output:
27 Input: Dogecoin
28 Output:
33 Input: Cardano
| 34 |
|
74 |
|---|---|---|
38 prompt=prompt,
39 max_tokens=200,
We start by giving examples of what the model should return:
1 Input: BTC was created in 2008, you can learn more about it here: https://bitcoin.or\
6 Output:
7 ETH was created in 2015, you can learn more about it here: https://ethereum.org/en/ \
12 Output:
13 DOGE was created in 2013, you can learn more about it here: https://dogecoin.com/ an\
For example:
1 Input: Bitcoin
6 It's all-time high is $64,895.00 and it's all-time low is $67.81.
The model will return a similar output:
4 It's all-time high is $1.33 and it's all-time low is $0.000019.Let’s make it reusabl\
5 e with other cryptocurrencies:
conversation.
Let’s start with this example:
5 with open(".env") as env:
6 for line in env:
11 openai.organization = os.environ.get("ORG_ID")
12
17 Output: ls -l
18
23 Output: rm *
24
28 Input:{}
29 Output:
34 prompt=prompt.format("Count the number of files in the current directory"),
35 max_tokens=200,
We need a single response from the model, that’s why we are using a null temperature. We are
providing the model with sufficient tokens to process the output.
Start by installing the Python package after activating the virtual development environment:
1 workon chatgptforpythondevelopers
3 import click
4
9 os.environ[key] = value
10
| 12 | 77 | |
|---|---|---|
13
14 init_api()
19
20 Input: List all the files in the current directory, including hidden files
25
26 Input: Count the number of occurrences of the word "sun" in the file "test.txt"
31
32 while True:
37 prompt=prompt,
38 temperature=0.0,
43 command = result.choices[0].text.strip()
44 click.echo(click.style("Output: ", fg="yellow") + command)
At the of the program, click prints the result. The final click.echo() will print an empty line.
5
6 Input: delete all
11
12 Input: count all directories
Let’s implement an exit command:
1 import os
6 with open(".env") as env:
7 for line in env:
12 openai.organization = os.environ.get("ORG_ID")
13
18 Output: ls -l
19
24 Output: rm *
Advanced Text Manipulation Examples 79
29 Input: {}
30 Output:"""
35
36 if request == "exit":
41 prompt=prompt,
42 temperature=0.0,
47 command = result.choices[0].text.strip()
48 click.echo(click.style("Output: ", fg="yellow") + command)
3 import click
4
9 os.environ[key] = value
10
15
| 16 |
|
80 |
|---|---|---|
| 17 | ||
| 18 |
20 Input: List all the files in the current directory, including hidden files
21 Output: ls -la
26 Input: Count the number of occurrences of the word "sun" in the file "test.txt"
27 Output: grep -o "sun" test.txt | wc -l
32 while True:
33 request = input(click.style("Input (type 'exit' to quit): ", fg="green"))
38
39 result = openai.Completion.create(
44 stop=["\n"],
45 )
50 click.echo(click.style("Execute? (y/n): ", fg="yellow"), nl=False)
51 choice = input()
56 else:
57 click.echo(click.style("Invalid choice. Please enter 'y' or 'n'.", fg="red"))
or not:
1 Input (type 'exit' to quit): list all files in /tmp
6 Input (type 'exit' to quit): list all files in /tmp
7 Output: ls /tmp
2 import openai
3 import click
8 key, value = line.strip().split("=")
9 os.environ[key] = value
14 init_api()
15
20 Input: List all the files in the current directory, including hidden files
21 Output: ls -la
| 26 |
|
|
|---|---|---|
| 27 |
30 Output:"""
31
36
37 prompt = _prompt.format(request)
49
| 50 |
|
|
|---|---|---|
| 51 | ||
| 52 | ||
| 53 | ||
| 54 |
|
|
| 55 |
|
|
| 56 | ||
| 57 | ||
| 58 | ||
| 59 | ||
| 60 |
|
|
| 61 | ||
| 62 |
|
From a practical point of view, embeddings are a way of representing real-world objects and relationships as a vector (a list of numbers). The same vector space is used to measure how similar two things are.
Use Cases
• Generating language-independent representations of text, allowing for cross-language compar- ison of text strings.
• Improving the accuracy of text-based search engines and natural language understanding systems.
• Clustering: where text strings are grouped by similarity
• Recommendations: where items with related text strings are recommended• Anomaly detection: where outliers with little relatedness are identified• Diversity measurement: where similarity distributions are analyzed• Classification: where text strings are classified by their most similar labelFollowing are some practical approaches to using embeddings (not necessarily OpenAI’s):
Kalendar AI
Kalendar AI is a sales outreach product that uses embeddings to match the right sales pitch to the right customers from a dataset of 340 million profiles in an automated way.
Embedding 85
DALL·E 2
1 pip install datalib
At another level of this guide, we will need Matplotlib and other libraries:
• pandas is a fast, powerful, flexible, and easy-to-use open-source data analysis and manipulation tool, built on top of Python.
• NumPy is another Python library adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays.
Embedding 86
Understanding Text Embedding
4 def init_api():
5 with open(".env") as env:
10 openai.api_key = os.environ.get("API_KEY")
11 openai.organization = os.environ.get("ORG_ID")
16 model="text-embedding-ada-002",
17 input="I am a programmer",
are using “Ada” which is the only best model available on OpenAI for embedding. OpenAI team
recommends using text-embedding-ada-002 for nearly all use cases as they describe it as “better,
3 {
4 "embedding": [
9 [..]
10 0.003966170828789473,
The program we wrote prints a list of floating point numbers such as 0.010284645482897758 and 0.013211660087108612.
These floating points represent the embedding of the input text “I am a programmer” generated by the OpenAI “text-embedding-ada-002” model.
Embeddings for Multiple Inputs
In the last example, we used:
| 1 | 88 | |
|---|---|---|
2 import openai
3
8 os.environ[key] = value
9
14
15 response = openai.Embedding.create(
20 for data in response["data"]:
21 print(data["embedding"])
Let’s start with the authentication:
5
6 def init_api():
11
12 openai.api_key = os.environ.get("API_KEY")
namely “text” and a list of random words:
1 text
6 cat
7 house
12 television
13 book
18 air
19 sun
24 bed
| 25 |
|
90 |
|---|---|---|
| 26 | ||
| 27 | ||
| 28 | ||
| 29 |
|
|
| 30 |
|
|
| 31 | ||
| 32 | ||
| 33 | ||
| 34 | ||
| 35 |
|
|
| 36 |
|
|
| 37 | ||
| 38 | ||
| 39 | ||
| 40 | ||
| 41 |
|
|
| 42 |
|
If you print df, you will see this output:
| 12 | 91 | ||
|---|---|---|---|
| 10 | |||
| 13 | 11 | ||
| 14 | 12 |
|
|
| 15 | 13 |
|
|
| 16 | 14 | ||
| 17 | 15 | ||
| 18 | 16 | ||
| 19 | 17 | ||
| 20 | 18 |
|
|
| 21 | 19 |
|
|
| 22 | 20 | ||
| 23 | 21 | ||
| 24 | 22 | ||
| 25 | 23 | ||
| 26 | 24 |
|
|
| 27 | 25 |
|
|
| 28 | 26 | ||
| 29 | 27 | ||
| 30 | 28 | ||
| 31 | 29 | ||
| 32 | 30 |
|
|
| 33 | 31 |
|
|
| 34 | 32 | ||
| 35 | 33 | ||
| 36 | 34 | ||
| 37 | 35 | ||
| 38 | 36 |
|
|
| 39 | 37 |
|
|
| 40 | 38 | ||
| 41 | 39 | ||
| 42 | 40 | ||
This function works as follows:
1 get_embedding("Hello", engine='text-embedding-ada-002')
3
contains the embeddings for each word on the first axis.
Let’s save the dataframe to another CSV file:
Let’s now read the new file and convert the last column to a numpy array. Why?
Embedding 93
provide any help with this kind of calculation. In addition to that, an array consumes less memory
and is faster. This is due to the fact that an array is a collection of homogeneous data-types that are
Now we are going to ask the user for input, read it, and perform a semantic search using cosine_-
similarity:
5 user_search_embedding = get_embedding(user_search, engine='text-embedding-ada-002')
6
11 rame
12 df['similarity'] = df['embedding'].apply(lambda x: cosine_similarity(x, user_search_\
| = | get_embedding(user_search, |
|
|---|
This line of code uses the get_embedding function to get the embedding for the user-specified
from openai.embeddings_utils import cosine_similarity
Embedding 94
df['similarity'] = df['embedding'].apply(lambda x: cosine_similarity(x, user_search_-
embedding))
This is the everything put together:
1 import openai
6 from openai.embeddings_utils import cosine_similarity
7
12 os.environ[key] = value
13
18
19 # words.csv is a csv file with a column named 'text' containing words
24 -ada-002'))
25
35 # get the search term from the user
36 user_search = input('Enter a search term: ')
41 # calculate the cosine similarity between the search term and each word in the dataf\
42 rame
After running the code, I entered the term “office” and this is the output:
| 1 | Unnamed: 0 | 0 | text | embedding | similarity | |
|---|---|---|---|---|---|---|
| 2 |
|
apple | [0.0077999732457101345, -0.02301608957350254, ... | 0.8\ | ||
| 3 | 1 | banana | [-0.013975119218230247, -0.03290277719497681, ... | 0.8\ | ||
| 4 | ||||||
| 5 | 2 | cherry | [0.006462729535996914, -0.018950263038277626, ... | 0.7\ | ||
| 6 | ||||||
| 7 |
|
3 | dog | [-0.0033353185281157494, -0.017689190804958344... | 0.8\ | |
| 8 |
|
|||||
| 9 | 4 | cat | [-0.0070945825427770615, -0.017328109592199326... | 0.8\ | ||
| 10 | ||||||
| 11 | 5 | house | [-0.007152134086936712, 0.007141574751585722, ... | 0.8\ | ||
| 12 | ||||||
| 13 |
|
6 | car | [-0.0074789817444980145, -0.021566664800047874... | 0.8\ | |
| 14 |
|
|||||
| 15 | 7 | tree | [-0.0047506773844361305, -0.013216584920883179... | 0.8\ | ||
| 16 | ||||||
| 17 | 8 | phone | [-0.0014101049164310098, -0.022890757769346237... | 0.8\ | ||
| 18 | ||||||
| 19 |
|
9 | computer | [-0.003125436371192336, -0.014225165359675884,... | 0.8\ | |
| 20 |
|
|||||
| 21 | 10 | television | [-0.004810569807887077, -0.019971350207924843,... | 0.7\ | ||
| 22 | ||||||
| 66 |
|
32 | watch | [-0.0024846186861395836, -0.010468898341059685... | 97 |
|---|---|---|---|---|---|
|
0.8\ | ||||
| 67 | 33 | jewelry | [-0.016019975766539574, 0.010300415568053722, ... | 0.7\ | |
| 68 | |||||
| 69 | 34 | ring | [-0.02060825377702713, -0.025675412267446518, ... | 0.8\ | |
| 70 | |||||
| 71 |
|
35 |
|
[-0.024919956922531128, 0.0024241949431598186,... | 0.7\ |
| 72 | |||||
| 73 | 36 | [-0.03430960699915886, 0.005157631356269121, -... | 0.7\ | ||
| 74 | |||||
| 75 |
|
37 | earring | [-0.025862164795398712, -0.009202365763485432,... | 0.7\ |
| 76 |
|
||||
| 77 | 38 | wallet | [0.015366269275546074, -0.020114824175834656, ... | 0.8\ | |
| 78 | |||||
| 79 | 39 | key | [0.003653161460533738, -0.02867439016699791, 0... | 0.8\ | |
| 80 | |||||
| 81 |
|
40 | photo | [0.004279852379113436, -0.03139236196875572, -... | 0.8\ |
| 82 |
|
||||
| 83 |
1 # sort the dataframe by the similarity axis
2 df = df.sort_values(by='similarity', ascending=False)
7 from openai.embeddings_utils import cosine_similarity
8
13 os.environ[key] = value
14
19
20 # words.csv is a csv file with a column named 'text' containing words
25 -ada-002'))
26
31 df = pd.read_csv('embeddings.csv')
32
37 user_search = input('Enter a search term: ')
38
43 rame
Embedding 99
48 df = df.sort_values(by='similarity', ascending=False)
49
| 1 | Unnamed: 0 | 21 | embedding |
|
||
|---|---|---|---|---|---|---|
| 2 |
|
desk | [0.012774260714650154, -0.020844005048274994, ... | |||
| 3 | 5 | house | [-0.007152134086936712, 0.007141574751585722, ... | |||
| 4 | ||||||
| 5 |
|
9 | computer | [-0.0031794828828424215, -0.014211298897862434... |
|
|
| 6 | ||||||
| 7 | 28 | floor | [0.018742715939879417, -0.021140681579709053, ... | |||
| 8 | ||||||
| 9 |
|
8 | phone | [-0.0014101049164310098, -0.022890757769346237... |
|
|
| 10 | ||||||
| 11 | 11 | book | [-0.006843345705419779, -0.019184302538633347,... | |||
| 12 | ||||||
| 13 |
|
24 | lamp | [0.006820859387516975, -0.008771182037889957, ... |
|
|
| 14 | ||||||
| 15 | 27 | door | [-0.004844364244490862, -0.026875808835029602,... | |||
| 16 | ||||||
| 17 |
|
40 | photo | [0.004297871608287096, -0.03132128715515137, -... |
|
|
| 18 | ||||||
| 19 | 13 | food | [0.022335752844810486, -0.02753201313316822, -... | |||
| 20 | ||||||
| 21 |
|
|||||
You can try the code, and enter other words (”dog”, “hat”, “fashion”, “phone”, “philosophy”..etc) and
topic, you can read this part of the guide. Otherwise, skipping it, will not change your understanding
of how to use OpenAI APIs to build intelligent applications.
vectors are completely different, the result is -1. If the vectors are at a 90-degree angle, the result is
0. In mathematical terms, this is the equation:
• A.B is a way of multiplying two sets of numbers together. It is done by taking each number
in one set and multiplying it with the same number in the other set, then adding all of those
This is how we can get the cosine similarity between them using Python:
1 # import numpy and norm from numpy.linalg
6 A = np.array([2,3,5,2,6,7,9,2,3,4])
7 B = np.array([3,6,3,1,0,9,2,3,4,5])
12
13 # calculate the cosine similarity
We can also write the same program using Python Scipy:
5 A = np.array([2,3,5,2,6,7,9,2,3,4])
6 B = np.array([3,6,3,1,0,9,2,3,4,5])
11
12 # calculate the cosine similarity
Or using Scikit-Learn:
1 import numpy as np
6 B = np.array([3,6,3,1,0,9,2,3,4,5])
7
12 # calculate the cosine similarity
13 cosine = cosine_similarity([A],[B])
Predicting your Preferred Coffee
Our goal throughout this part is to recommend the best coffee blend for a user based on their input. For example, the user enters “Ethiopia Dumerso” and the program finds that “Ethiopia Dumerso”,“Ethiopia Guji Natural Dasaya” and “Organic Dulce de Guatemala” are the nearest blends to their choice, the output will contain these three blends.
When a user enters the name of a coffee, we will use the OpenAI Embeddings API to get the embedding for the review text of that coffee. Then, we will calculate the cosine similarity between the input coffee review and all other reviews in the dataset. The reviews with the highest cosine similarity scores will be the most similar to the input coffee’s review. We will then print the names of the most similar coffees to the user.
Let’s start step by step.
| 1 | 103 | |
|---|---|---|
The Natural Language Toolkit, or more commonly NLTK, is a suite of libraries and programs for
symbolic and statistical natural language processing for English written in the Python programming
2 nltk.download('stopwords')
3 nltk.download('punkt')
1 import os
2 import pandas as pd
7 from openai.embeddings_utils import cosine_similarity
8
13 os.environ[key] = value
14
²⁵
| 24 |
|
104 |
|---|---|---|
| 25 | ||
| 26 |
28 def preprocess_review(review):
29 from nltk.corpus import stopwords
34 tokens = [token for token in tokens if token not in stopwords]
35 tokens = [stemmer.stem(token) for token in tokens]
The function tokenizes the review into individual words using the nltk.word_tokenize() function.
Then it removes stopwords (common words like “the”, “a”, “and”, etc.) from the review using a list of stopwords obtained from the stopwords corpus in NLTK using the nltk.corpus.stopwords.words() function.
1 init_api()
2 download_nltk_data()
| 1 | 105 | |
|---|---|---|
| 2 |
|
Next, we’ll load the CSV file into a Pandas DataFrame. Note that we are only reading the first 50
Then we’ll preprocess all the preview texts:
1 # Preprocess the review text: lowercase, tokenize, remove stopwords, and stem
3 for review in df['preprocessed_review']:
4 review_embeddings.append(get_embedding(review, engine='text-embedding-ada-002'))
3 input_coffee_index = df[df['name'] == input_coffee_name].index[0]
4 except:
coffee name.
For example, df[df['my_column'] == my_value] is selecting rows from the DataFrame df where
Advanced Embedding Examples 106
For example, if df is a DataFrame of coffee reviews (which is our case), and my_column is the “name”column, then df[df['name'] == 'Ethiopia Yirgacheffe'] would return a new DataFrame that contains only the reviews for the “Ethiopia Yirgacheffe” coffee.
name].index[0] does:
1. df['name'] == input_coffee_name creates a boolean mask that is True for rows where the “name” column is equal to input_coffee_name and False for all other rows.
1 # Calculate the cosine similarity between the input coffee's review and all other re\
2 views
7 similarities.append(similarity)
cosine_similarity(input_review_embedding, review_embedding) is using OpenAI’s openai.embeddings_utils.cosine_similarity() function to calculate the cosine similarity between the input coffee’s review and the current review. (We have previously used this function in a previous example.)
Advanced Embedding Examples 107
If you have used numpy before, you’re certainly familiar with the argsort, you can skip the following detailed explanation of how it works.
| 1. np.argsort(similarities) would return the sorted indices: [9, | 8, | 7, | 6, | 5, | 4, | 1, | 3, |
|---|
2. np.argsort(similarities)[-6:-1] would return the 6th to 2nd indices from the end of the sorted array: [5, 4, 1, 3, 0].
When np.argsort(similarities) is called, it returns an array of indices that would sort the similarities array in ascending order. In other words, the first index in the sorted array would correspond to the element in similarities with the smallest value, and the last index in the sorted array would correspond to the element in similarities with the largest value.
Another question you may ask is, why is the review itself in the similarity array?
The review itself was added to the review_embeddings array when we created embeddings for each review using the get_embedding() function.
df.iloc[most_similar_indices]['name'].tolist() is using Pandas’ iloc[] function to get the
names of the most similar coffees. Here’s an explanation:
Then we use ['name'] to get the name column of those rows. Finally, we use tolist() to convert the
column to a list. This gives us a list of the names of the most similar coffees.
4 print(coffee_name)
This is the final code:
5 import openai
6 from openai.embeddings_utils import get_embedding
11 for line in env:
12 key, value = line.strip().split("=")
17
18 def download_nltk_data():
| 22 | 109 | |
|---|---|---|
|
||
| 23 |
|
|
| 24 | ||
| 25 | ||
| 26 |
28 def preprocess_review(review):
29 from nltk.corpus import stopwords
34 tokens = [token for token in tokens if token not in stopwords]
35 tokens = [stemmer.stem(token) for token in tokens]
40
41 # Read user input
46 df = pd.read_csv('simplified_coffee.csv', nrows=50)
47
52 review_embeddings = []
53 for review in df['preprocessed_review']:
58 input_coffee_index = df[df['name'] == input_coffee_name].index[0]
59 except:
64 views
70
75
76 # Get the names of the most similar coffees
81 for coffee_name in similar_coffee_names:
82 print(coffee_name)
case and this will exit the search with a message Sorry, we don't have that coffee in our
database. Please try again.
3 # search for a coffee name in the dataframe that looks like the input coffee nam\
4 e
9 except:
10 print("Sorry, we don't have that coffee name in our database. Please try again.")
2 try:
3 # search for all coffee names in the dataframe that looks like the input coffee \
8 print("Sorry, we couldn't find any coffee with that name.")
9 exit()
select them? Should just get some random names out of the results?
A better solution is using a fuzzy search technique. For example, we can use the
Another solution is running a cosine similarity search between the user input and the coffee names.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
|---|---|
20 print("Sorry, we don't have that coffee name in our database. Please try again.")
21 exit()
Predicting News Category Using Embedding
This example will introduce a zero-shot news classifier that predicts the category of a news article.
4 from openai.embeddings_utils import get_embedding
5 from openai.embeddings_utils import cosine_similarity
10 key, value = line.strip().split("=")
11 os.environ[key] = value
16 init_api()
We are then going to define a list of categories:
The choice of categories is not arbitrary. We are going to see why we made this choice later.
Next, we are going to write a function that classifies a sentence as part of one of the above categories:
5 # Calculate the similarity score between the sentence and each category
6 similarity_scores = {}
11 _embeddings)
12 # Return the category with the highest similarity score
The engine="text-embedding-ada-002" argument specifies which OpenAI model to use for
the embedding.
| = |
|---|
category_embeddings): We calculate the cosine similarity between the sentence embedding
and the category embedding, and store the result in the similarity_scores dictionary.
Advanced Embedding Examples 114
1 # Classify a sentence
6 "The US is preparing to send more troops to the Middle East",
7 "Bruce Willis' 'condition has progressed' to frontotemporal dementia, his family\
12 "Chicago bulls win the NBA championship",
13 "The new iPhone 12 is now available",
18 ]
19
1 import os
2 import openai
7 def init_api():
8 with open(".env") as env:
13 openai.api_key = os.environ.get("API_KEY")
14 openai.organization = os.environ.get("ORG_ID")
36 # Define a function to classify a sentence
37 def classify_sentence(sentence):
42 for category in categories:
43 category_embeddings = get_embedding(category, engine="text-embedding-ada-002\
48 return max(similarity_scores, key=similarity_scores.get)
49
54 "15 spring break ideas for families that want to get away",
55 "The US is preparing to send more troops to the Middle East",
60 raw for hosts",
61 "Chicago bulls win the NBA championship",
| 62 | 116 | |
|---|---|---|
| 63 | ||
| 64 | ||
| 65 |
|
|
| 66 |
|
|
| 67 |
71 print()
After executing the code above, you’ll see something similar to this output:
5 <Preditected Category>
6
It looks like the previous classifier is almost perfect but there is a way to understand if it is really
accurate and generate an accuracy score.
The categories used in the dataset are the same as we used previously (hence my initial choice):
²⁸
We are going to use the sklearn.metrics.precision_score function which calculates the precision
Let’s see how.
1 from sklearn.metrics import precision_score
6 y_pred = []
7
12
13 y_true.append(true_category)
| 16 | \ |
|---|
18 # if true_category != predicted_category:
19 # print("False prediction: {:50} True: {:20} Pred: {:20}".format(row['he\
23 dline'], true_category, predicted_category))
24
For a better accuracy calculation, I would use more than 20 records. However, for this example, I’m using only 20, feel free to use more records.
2. y_true = [] and y_pred = []: We initialize empty lists to store the true and predicted categories for each sentence.
sentence(row['headline']): These lines extract the true category from the current row and use the classify_sentence function to predict the category of the headline in the current row.
5. y_true.append(true_category) and y_pred.append(predicted_category): We append the true and predicted categories to the y_true and y_pred lists.
| 6. return | precision_score(y_true, | y_pred, | average='micro', |
|---|
Once we combine all the components, the code will look like this:
³⁰
10 for line in env:
11 key, value = line.strip().split("=")
16
17 init_api()
22 "ENTERTAINMENT",
23 "TRAVEL",
28 "FOOD & DRINK",
29 "BUSINESS",
34 "PARENTS",
35 ]
40 sentence_embedding = get_embedding(sentence, engine="text-embedding-ada-002")
41 # Calculate the similarity score between the sentence and each category
45 ")
46 similarity_scores[category] = cosine_similarity(sentence_embedding, category\
51 def evaluate_precision(categories):
52 # Load the dataset
57 # Classify each sentence
58 for _, row in df.iterrows():
63 y_pred.append(predicted_category)
64
69 print("Precision: {:.2f}".format(precision_evaluated))
Fine Tuning & Best Practices
• ³⁴: A large-scale few-shot relation extraction dataset, which contains more than one hundred relations and tens of thousands of annotated instances across different domains.• ³⁵ Contains the official few-shot object detection implemen- tation of ³⁶.
• ³⁷: A framework to address the challenging few-shot learning setting. The aim of this project is to take advantage of multiple similar low-sample tasks to learn how to modify a base learner for a new task with only a few labeled available samples.
Improving Few Shot Learning
The fine-tuning principle is a process of improving few-shot learning by training on many more examples than can fit in the prompt. This approach can be used to achieve better results on a wide number of tasks:
Let’s move to a more practical part and see real examples.
Note that this section is an introduction, what we are going to do next is basic. Keep reading to grasp the overall concepts but later in this guide, we are going to dive deeper into a more advanced example.
1 {"prompt":"When do I have to start the heater?", "completion":"Every day in the morn\
2 ing at 7AM. You should stop it at 2PM"}
7 ery is low"}
JSONL, also known as newline-delimited JSON, is a useful format for storing structured data that can be processed one record at a time. Our training data should be using JSONL format.
testing things out and we want to see how it works in practice for now.
1 openai tools fine_tunes.prepare_data -f data.json
6 - Your file contains 3 prompt-completion pairs. In general, we recommend having at l\
7 east a few hundred examples. We've found that performance tends to linearly increase\
12 fine-tuned model where the completion should end. See https://beta.openai.com/docs/\
13 guides/fine-tuning/preparing-your-dataset for more detail and examples.
18 Based on the analysis we will perform the following actions:
19 - [Necessary] Your format `JSON` will be converted to `JSONL`
24 Your data will be written to a new JSONL file. Proceed [Y/n]: Y
25
30 > openai api fine_tunes.create -t "data_prepared.jsonl"
31
36 Once your model starts training, it'll approximately take 2.48 minutes to train a `c\
37 urie` model, and less for `ada` and `babbage`. Queue will approximately take half an\
You could use curie, or any other base model (davinci, babbage, or ada).
When the fine-tuning is done, which could take some time, the CLI will print the name of the new
| 1 | 125 | |
|---|---|---|
|
||
| 2 |
|
|
| 3 | ||
| 4 | # additional parameters | |
| 5 | # temperature, | |
| 6 | # frequency_penalty, | |
| 7 | # presence_penalty | |
| 8 | # ..etc | |
| 9 |
2 -H "Authorization: Bearer $OPENAI_API_KEY" \
3 -H "Content-Type: application/json" \
Example:
1 step,elapsed_tokens,elapsed_examples,training_loss,training_sequence_accuracy,traini\
6 4,124,4,1.6673923087120057,0.0,0.23529411764705882
7 5,157,5,1.2660537725454195,0.0,0.55
12 10,298,10,1.2882517070074875,0.0,0.45454545454545453
13 11,331,11,1.0392776031675748,0.0,0.65
³⁹
2 openai api models.delete -i <FINE_TUNED_MODEL>
3
8 curl -X "DELETE" https://api.openai.com/v1/models/<FINE_TUNED_MODEL> \
9 -H "Authorization: Bearer $OPENAI_API_KEY"
For the model to accurately recognize the end of a prompt and the beginning of completion text, it is important to include a fixed separator. A commonly used separator is \n\n###\n\n, which should not be used elsewhere in the prompt. This will help ensure the model generates the desired output. In our advanced example, we are going to see how we use the separator to make the model better.
Each Completion Should Start With a Whitespace
completion. The stop sequence can be any token that is not used within the completion, such as \n
or ###. For inference, you should format your prompts in the same way as you did when creating
need to provide a minimum of a few hundred high-quality examples that are preferably evaluated
by human experts, as recommended by OpenAI in their official documentation.
According to OpenAI, the following training dataset:
1 {"prompt": "Product is a laptop. Brand is Dell. Model is Inspiron. RAM is 8GB. Hard \
6 etective. Setting is New York City.->", "completion": " If you're a fan of thrillers\
7 , you'll love this book by James Patterson featuring a detective in the heart of New\
12 crispy bacon, creamy eggs, and freshly grated parmesan cheese. You won't be disappo\
13 inted! END"}
3 RAM and a 256GB SSD, making it a fast and reliable option for all your computing ne\
4 eds. END"}
9 {"prompt": "The recipe : for spaghetti carbonara. Ingredients : spaghetti, bacon, eg\
10 gs, parmesan cheese. Difficulty level : easy.->", "completion": " Looking for an eas\
clearer when you are building a generative model.
Review Your Data for Offensive Content
can take to ensure a representative sample is evaluated. One approach is to review as many random
samples as possible. By doing so, you can still gain insights into the data and identify any patterns
data to improve the model’s performance.
Fine-tuning a model with a dataset that is dissimilar or insufficient may lead to suboptimal results.
process.
Fine Tuning & Best Practices 129
Some third-party tools, such as ⁴⁰, can also be used to analyze the results.
Use Validation Data if Needed
2 >
During training, the OpenAI CLI will periodically calculate metrics on batches of validation data and include them in the results file. The validation metrics that are generated include:
In machine learning, a hyperparameter is a parameter that controls the learning process, while the values of other parameters (usually node weights) are determined through training.
OpenAI has configured default hyperparameters values that are suitable for various use cases.
• learning_rate_multiplier: This parameter determines the fine-tuning learning rate, which is the original learning rate used for pre-training multiplied by this multiplier. The OpenAI API sets this parameter based on the batch size, and it defaults to 0.05, 0.1, or 0.2.
To understand better this parameter, let’s examine other concepts first.
on the validation set at the end of every epoch. These metrics can help you evaluate the
performance of the model and make adjustments as needed.
When tackling classification problems, the Ada model is a good option. It performs only slightly
worse than Davinci once fine-tuned, at the same time it is considerably faster and more affordable.
3 {prompt:"Apple is set to release a new iPhone model in the coming months.", completi\
4 on: "technology and science"}
9 {prompt:"Tesla's market capitalization surpassed that of Toyota in 2020.", completio\
10 n: "technology and science"}
15 {prompt:"Facebook was fined $5 billion by the US Federal Trade Commission for privac\
16 y violations.", completion: "technology and science"}
You can notice there are 3 classes here:
⁴¹
• politics and government
Instead of using them as classes names, it is wiser to use a single token like:
2 "}
3 {prompt:"Apple is set to release a new iPhone model in the coming months.", completi\
8 ompletion: "1"}
9 {prompt:"Tesla's market capitalization surpassed that of Toyota in 2020.", completio\
14 in 2021.", completion: "1"}
15 {prompt:"Facebook was fined $5 billion by the US Federal Trade Commission for privac\
20 Brady.", completion: "1"}
Not only the training data will be less but the number of tokens at inference time will be only 1.
Fine Tuning & Best Practices 133
Other Considerations for Classification
Dataset Used in the Example
In this example, we are going to use a public dataset containing drug names and the corresponding malady, illness, or condition that they are used to treat.
1 https://www.kaggle.com/datasets/saratchendra/medicine-recommendation
Then download the file called Medicine_description.xlsx
The file contains 3 sheets, we are going to use the first one called “Sheet1” in which there are 3 columns:
| 1 |
|
135 | ||
|---|---|---|---|---|
|
||||
| 2 | ||||
| 3 | ||||
The data file is an XLSX file. We are going to convert it to JSONL format (JSON Lines.) The JSONL file will be used for fine-tuning the model.
We are also going to use this format:
Basically, we are going to give each malady a unique identifier. For example:
1 Acne: 1
2 Allergies: 2
3 Alzheimer: 3
4 ..etc
2
3 # read the first n rows
8
9 # get the unique values in the Reason column
14
15 # add a new line and ### to the end of each description
20
21 # drop the Reason column
26
27 # convert the dataframe to jsonl format
32 f.write(jsonl)
The code above sets the number of rows to read from the Excel file to 2000. This means that we are
called reasons, assigns a numerical index to each unique value in the reasons array, and stores it in
a dictionary called reasons_dict.
This is done to get the desired format:
1 Drug: <DRUG NAME>\nMalady:
to a file called ‘drug_malady_data.jsonl’.
This is how the “drug_malady_data.jsonl” looks like:
5 [..]
6 {"prompt":"Drug: Mondeslor Tablet 10'S\nMalady:","completion":" 2"}
1 Analyzing...
2
7 h as `ada`
8 - For classification, you can estimate the expected model performance by keeping a h\
| 1 |
|
138 |
|---|---|---|
| - [Recommended] Would you like to split into training and validation set? [Y/n]: |
The CLI will also provide you with the command to execute to train the model:
It is also able to detect the number of classes used in the dataset.
Finally, execute the provided command to start the training. You can also specify the model. We are
3 openai api fine_tunes.create \
4 -t "drug_malady_data_prepared_train.jsonl" \
9 --suffix "drug_malady_data"
If the client disconnects before the job is completed, you may be asked to reconnect and check using
2 Fine-tune costs $0.03
3 Fine-tune enqueued
8 [...]
9 [...]
14
15 # Configure the model ID. Change this to your model ID.
20 "A CN Gel(Topical) 20gmA CN Soap 75gm", # Class 0
21 "Addnok Tablet 20'S", # Class 1
26 for drug_name in drugs:
27 prompt = "Drug: {}\nMalady:".format(drug_name)
32 temperature=1,
33 max_tokens=1,
38 # The result should be 0, 1, and 2
39 print(drug_class)
The output of the code execution should be:
1 0
| 1 | 141 | |
|---|---|---|
|
||
| 2 | "What is 'A CN Gel(Topical) 20gmA CN Soap 75gm' used for?", # Class 0 | |
| 3 |
|
|
| 4 | ||
| 5 |
1 # Let's use a drug from each class
2 drugs = [
7
8 class_map = {
13 }
14
19 response = openai.Completion.create(
20 model=model,
25
26 response = response.choices[0].text
31 print()
Advanced Fine Tuning: Creating a Chatbot Assistant
The goal is to make the classification more human friendly, in other words, we are going to create a chatbot.
How Will Everything Work?
• get_malady_description(): Get a description of a malady from the API using Davinci and returns it.
The end user, when asking about a drug name will get the malady (from the fine-tuned model) and its description (from Davinci).
3 params: prompt - a string
4 Returns a response from the API using Davinci.
9 creative, clever, very friendly and careful with Human's health topics
10 The AI assistant is not a doctor and does not diagnose or treat medical conditio\
15 The AI assistant does not provide medical and health diagnosis to Human
16 The AI assistant does not provide medical treatment to Human
21 AI: Hello Human. How are you? I'll be glad to help. Give me the name of a drug a\
22 nd I'll tell you what it's used for.
27 drug and I'll tell you what it's used for.
28 Human: What is Chaos Engineering?
33 tions about drugs. Give me the name of a drug and I'll tell you what it's used for.
34 Human: What is Maxcet 5mg Tablet 10'S?
39 AI:""".format(prompt)
40
advice, diagnosis, treatment, or prescriptions. If the human writes the name of a drug, the assistant
will reply with ######. When this happens, we call the second function.
4 Returns a malady name that corresponds to a drug name from the fine-tuned model.
5 The function will call get_malady_description() to get a description of the mala\
10 class_map = {
11 0: "Acne",
16
17 # Returns a drug class for each drug
22 prompt= prompt,
23 temperature=1,
| 25 | 145 | |
|---|---|---|
27 response = response.choices[0].text.strip()
28 try:
33 print("AI: I don't know what '" + drug_name + "' is used for.")
As seen in the previous chapter, this code returns a malady name that corresponds to a drug name
2 """
3 params: malady - a string
8 creative, clever, and very friendly.
9 The assistant does not provide medical advice. It only defines a malady, a disea\
14 Q: What is {}?
15 A:""".format(malady)
20 prompt=prompt,
21 max_tokens=100,
Now, let’s bring everything together:
5 with open(".env") as env:
6 for line in env:
11 openai.organization = os.environ.get("ORG_ID")
12
17 params: prompt - a string
18 Returns a response from the API using Davinci.
23 creative, clever, very friendly and careful with Human's health topics
24 The AI assistant is not a doctor and does not diagnose or treat medical conditio\
29 The AI assistant does not provide medical and health diagnosis to Human
30 The AI assistant does not provide medical treatment to Human
35 AI: Hello Human. How are you? I'll be glad to help. Give me the name of a drug a\
36 nd I'll tell you what it's used for.
41 drug and I'll tell you what it's used for.
42 Human: What is Chaos Engineering?
46 AI: I'm sorry, I am not qualified to do that. I'm only programmed to answer ques\
47 tions about drugs. Give me the name of a drug and I'll tell you what it's used for.
52 Human: {}
53 AI:""".format(prompt)
58 prompt=prompt,
59 max_tokens=100,
64 else:
65 final_response = response.choices[0].text.strip() + "\n"
70 params: drug_name - a string
71 Returns a malady name that corresponds to a drug name from the fine-tuned model.
76 model = "ada:ft-learninggpt:drug-malady-data-2023-02-21-20-36-07"
77 class_map = {
82 }
83
| 87 | 148 | |
|---|---|---|
|
||
| 88 |
|
|
| 89 | ||
| 90 | ||
| 91 | ||
| 92 |
93
94 response = response.choices[0].text.strip()
99 except:
100 print("AI: I don't know what '" + drug_name + "' is used for.")
105 Get a description of a malady from the API using Davinci.
106 """
111 se, or a condition.
112 If the assistant does not know the answer to a question, it will ask to rephrase\
117
118 # Get the response from the API.
123 stop=["\n", " Q:", " A:"],
124 )
129 regular_discussion(input("Human:"))
Advanced Fine Tuning: Creating a Chatbot Assistant 149
4 ive me the name of a drug and I'll tell you what it's used for.
5
10 Human: What is relativity?
11 AI: I'm sorry, I am not qualified to do that. I'm only programmed to answer question\
16 do?
17 AI: I'm sorry, I am not qualified to do that. I'm only programmed to answer question\
22 Allergies are an abnormal reaction of the body's immune system to substances that ar\
23 e generally not harmful. Symptoms may include runny nose, coughing, sneezing, itchy \
28 Acne is a common skin condition characterized by blocked hair follicles, inflamed pi\
29 mples, blackheads and whiteheads. It typically occurs on the face, chest and back an\
If you are not familiar with Vue, it’s an approachable framework and easy to learn. Also, we are not
going to create a complex front end. Therefore, there is no real need to be a Vue expert to understand
We are going to create a folder for the backend and one for the frontend:
1 mkdir chatbot chatbot/server
1 cd client
2 vue add router
Create a file called app.py in the server folder and add the following code:
1 from flask import Flask, jsonify
6
7 # configuration
12 app.config.from_object(__name__)
13
18 with open(".env") as env:
| 19 |
|
151 |
|---|---|---|
| 20 | ||
| 21 |
23 openai.api_key = os.environ.get("API_KEY")
24 openai.organization = os.environ.get("ORG_ID")
29 """
30 params: prompt - a string
35 The following is a conversation with an AI assistant. The assistant is helpful, \
36 creative, clever, very friendly and careful with Human's health topics
41 The AI assistant does not provide medical advice to Human
42 The AI assistant does not provide medical and health diagnosis to Human
47 Human: Hi
48 AI: Hello Human. How are you? I'll be glad to help. Give me the name of a drug a\
53 AI: I am fine. Thank you for asking. I'll be glad to help. Give me the name of a\
54 drug and I'll tell you what it's used for.
59 AI: I'm sorry, I am not qualified to do that. I'm only programmed to answer ques\
60 tions about drugs. Give me the name of a drug and I'll tell you what it's used for.
70 model="text-davinci-003",
71 prompt=prompt,
76 return get_malady_name(prompt)
77 else:
82 """
83 params: drug_name - a string
88 # Configure the model ID. Change this to your model ID.
89 model = "ada:ft-learninggpt:drug-malady-data-2023-02-21-20-36-07"
94 # ...
95 }
100 response = openai.Completion.create(
101 model=model,
| 105 | 153 | |
|---|---|---|
|
106
111 print("This drug is used for {}.".format(malady) + get_malady_description(ma\
112 lady))
117
118 def get_malady_description(malady):
123 prompt = """
124 The following is a conversation with an AI assistant. The assistant is helpful, \
129 it.
130
135 response = openai.Completion.create(
136 model="text-davinci-003",
141 return response.choices[0].text.strip() + "\\n"
142
147 print("chatbot: ", chatbot)
Create a file called .env in the server folder and add your API key and organization ID:
1 API_KEY=sk-xxxx
2 import HomeView from '../views/HomeView.vue'
3
8 component: HomeView
9 },
14 // this generates a separate chunk (about.[hash].js) for this route
15 // which is lazy-loaded when the route is visited.
20 const router = createRouter({
21 history: createWebHistory(process.env.BASE_URL),
The above code creates two routes: / and /about. The / route will be used to display the chatbot
(HomeView.vue) and the /about route will be used to display the about page (AboutView.vue).
| 1 | 155 | |
|---|---|---|
| 2 | ||
| 3 |
|
|
| 4 |
|
|
| 5 | ||
| 6 | ||
| 7 | ||
| 8 | ||
| 9 |
|
|
| 10 |
|
|
| 11 | ||
| 12 | ||
| 13 | ||
| 14 |
15
16 <script>
21 return {
22 messages: [
27 },
28 methods: {
33 this.messages.push({
34 id: this.messages.length + 1,
39 const messageText = this.newMessage.trim();
40
62 };
63 </script>
chatbot interface.
The HomeView.vue file is the default view for the / route. To create the AboutView.vue file, run the
2 <div class="about">
3 <h1>This is an about page</h1>
| 1 |
|
157 |
|---|---|---|
|
||
| 2 | ||
| 1 | ||
| 2 |
We have now created a simple chatbot that can answer questions about drugs.
The model and inference code were open-sourced by OpenAI and can be found on GitHub.
There are five model sizes, four of which have English-only versions. Each model offers a tradeoff between speed and accuracy. The names of the models, along with their approximate memory requirements and relative speeds, are listed below.
Intelligent Speech Recognition Using OpenAI Whisper 160
How to Get Started?
On Ubuntu or Debian
1 sudo apt update && sudo apt install ffmpeg
on Windows using Chocolatey
1 choco install ffmpeg
1 pip install setuptools-rust
Let’s download any audio file containing a speech to test with. You can find many files on
2 en_of_Valour.ogg
Then execute the Whisper command using the base model:
| 1 | whisper Winston_Churchill_-_Be_Ye_Men_of_Valour.ogg |
|---|
In both cases, you should be able to see the text of the speech:
| 1 | [00:00.000 --> 00:07.680] |
|
|---|---|---|
| 2 | [00:08.320 --> 00:14.880] | |
| 3 | [00:16.640 --> 00:20.240] | A tremendous battle is raging in France and flanders [..] |
| 4 | [00:21.920 --> 00:27.920] | The Germans by a remarkable combination of air bombing [.\ |
| 5 | of the specialized and mechanized forces of the enemy [..] | |
| 6 | ||
| 7 | ||
| 8 |
|
|
| 9 | [03:16.400 --> 03:22.000] | |
| 10 | [03:22.640 --> 03:26.560] | and we know that very heavy losses have been inflicted [.\ |
| 11 |
|
|
| 12 | ||
| 13 |
1
| 1 | [00:00.000 --> 00:02.000] | |
|---|---|---|
| 2 | [00:02.000 --> 00:06.000] | |
| 3 | [00:06.000 --> 00:11.000] |
|
| 4 | [00:11.000 --> 00:15.000] |
|
| 5 | [00:15.000 --> 00:19.000] | |
| 6 | [00:19.000 --> 00:22.000] | |
| 7 | [00:22.000 --> 00:25.000] | |
| 8 | [00:25.000 --> 00:29.000] |
There are some parameters you can play with and test whether for transcription or translation. For example, we can increase the temperature and the number of candidates when sampling to get different results.
This is an example:
The Problem
GPT is a generative text model which means that produces new text by predicting what comes next based on the input it gets from the user. The model was trained on a large corpus of text (books, articles, and websites) and it used this data and learned patterns and relationships between words and phrases.
1 import openai
2 import os
7 key, value = line.strip().split("=")
8 os.environ[key] = value
13 init_api()
Context & Memory: Making AI More Real 164
18 You: {}
19 AI: """
24 response = openai.Completion.create(
25 engine="text-davinci-003",
30 )
31
3 You: Remember this string 'blah' and store it. I'll ask you about it later.
4 AI: Sure, I remember the string 'blah'.
9 You: I already told you!
10 AI: I'm sorry, I didn't catch that. Could you please explain the context of the las\
inserted before the new question.
| 1 |
|
165 |
|---|---|---|
| 2 |
5 with open(".env") as env:
6 for line in env:
11 openai.organization = os.environ.get("ORG_ID")
12
17 AI: How are you?
18 You: {}
23 while True:
24 prompt = input("You: ")
29 max_tokens=100,
30 stop=[" You:", " AI:"],
35
36 print("AI: " + response_text)
The Problem with Carrying Over History
With long discussions, the user prompt will be longer since it will always be added to the history until the point when it reaches the maximum number of tokens allowed by OpenAI. In this case, the result is a total failure, as the API will respond with errors.
• Context evolves and the discussion too.
• Users will most likely include the context in the latest 2 to 5 prompts.
Let’s take a look at the code:
| 1 |
|
167 |
|---|---|---|
| 2 |
5 with open(".env") as env:
6 for line in env:
11 openai.organization = os.environ.get("ORG_ID")
12
17 def load_history_from_file():
18 with open("history.txt", "r") as f:
23 if len(history_list) > 2:
24 return separator.join(history_list[-2:])
29
30 initial_prompt = """You: Hi there!
35
36 history = ""
41 prompt = input("You: ")
42 relevant_history = get_relevant_history(load_history_from_file())
53 history += "\nYou: "+ prompt + "\n" + "AI: " + response_text + "\n" + separator
54 save_history_to_file(history)
Selective Context
The solution suggested in this part will work as follows:
| 1 | 169 | |
|---|---|---|
|
||
| 2 |
|
|
| 3 | ||
| 4 | ||
| 5 | ||
| 6 |
7
8 def load_history_from_file():
13 return f.read()
14
19 ory
20 """
25 return a_without_stopwords.similarity(b_without_stopwords)
26
31 History is a string of segments separated by separator
32 """
37 # get cosine similarity between user input and segment
38 similarity = cos_sim(user_input, segment)
43 sorted_history += segments[sorted_similarities[i]] + separator
48 Get the latest n segments from the history.
49 History is a string of segments separated by separator
1. Split the history into segments: The function first splits the input history string into segments
based on the specified separator (in our example, we will use ##### which we will declare later).
Although we could have used OpenAI embedding, our goal is to reduce computing costs by
performing certain tasks locally instead of relying on the API.
concatenate the corresponding segments together into a new string. This creates a new, sorted
history string in which the most similar interactions to the user’s input appear first.
1 initial_prompt_1 = """
2 You: Hi there!
7 #####
8 You: Do you know cars?
| 11 | 171 | |
|---|---|---|
| 12 |
|
|
| 13 |
|
|
| 14 | ||
| 15 | ||
| 16 | ||
| 17 | ||
| 18 |
|
|
| 19 |
|
|
| 20 | ||
| 21 | ||
| 22 | ||
| 23 |
24
25 initial_prompt_2 ="""You: {}
30 init_api()
31 save_history_to_file(initial_prompt_1)
36 history = load_history_from_file()
37 best_history = get_latest_n_from_history(history, 5)
42 prompt=full_prompt,
43 temperature=1,
48 history += "\n" + full_user_prompt + response_text + "\n" + separator + "\n"
49 save_history_to_file(history)
7 nlp = spacy.load('en_core_web_md')
8
13 os.environ[key] = value
14
19 """
20 Save the history of interactions to a file
25 def load_history_from_file():
26 """
31
32 def cos_sim(a, b):
37 """
38 a = nlp(a)
43
Context & Memory: Making AI More Real 173
48 History is a string of segments separated by separator
49 """
54 # get cosine similarity between user input and segment
55 similarity = cos_sim(user_input, segment)
60 sorted_history += segments[sorted_similarities[i]] + separator
61 save_history_to_file(sorted_history)
66 History is a string of segments separated by separator
67 """
72
73 initial_prompt_1 = """
78 AI: I am fine, thank you.
79 #####
84 AI: I don't eat pizza. I am an AI that is not able to eat.
85 #####
| 87 | 174 | |
|---|---|---|
| 88 | ||
| 89 |
|
|
| 90 |
|
|
| 91 | ||
| 92 | ||
| 93 | ||
| 94 | ||
| 95 |
|
96
101
102 init_api()
107 sort_history(load_history_from_file(), prompt)
108 history = load_history_from_file()
113 engine="text-davinci-003",
114 prompt=full_prompt,
119 response_text = response.choices[0].text.strip()
120 history += "\n" + full_user_prompt + response_text + "\n" + separator + "\n"
This is how the whole system works:
• The user asks a question using their microphone. We will use ⁴⁶.• OpenAI Whisper automatically transcribes the question into a text.• The text is passed to OpenAI GPT completions endpoints
• The OpenAI API returns an answer
• The answer is saved to an mp3 file and passed to Google Text to Speech (gTTS) to create a voice response.⁴⁷
⁴⁸
4 r = sr.Recognizer()
5 r.energy_threshold = energy
10 print("Listening...")
11 i = 0
16 532bd8c21d/whisper/audio.py
17 torch_audio = torch.from_numpy(np.frombuffer(audio.get_raw_data(), np.in\
The function above record_audio records audio from a microphone and saves it to a queue for
further processing.
• dynamic_energy: a Boolean indicating whether the energy threshold should be adjusted
dynamically based on the surrounding environment.
During each iteration of the loop, the listen() method of the recognizer object is called to record
audio from the microphone. The recorded audio is converted into a PyTorch tensor, normalized to
This is the function that uses OpenAI Whisper to transcribe the audio.
1 def transcribe_forever(audio_queue, result_queue, audio_model, english, wake_word, v\
6 result = audio_model.transcribe(audio_data,language='english')
7 else:
to be transcribed, and result_queue, which is used to store the transcribed text.
The function starts by getting the next audio data from the audio_queue using the audio_-
The resulting result dictionary contains several keys, one of which is "text" which simply contains
the transcript of the audio.
Building Your AI-Based Alexa 178
When you say “Hey Computer <your request>”, it may sometimes be transcribed as “Hey Computer,
and the text is being processed. Finally, the predicted_text string is added to the result_queue
using the result_queue.put_nowait() method.
1 def reply(result_queue):
2 while True:
7 temperature=0,
8 max_tokens=150,
13 reply_audio = AudioSegment.from_mp3("reply.mp3")
14 play(reply_audio)
2. When a result is received, it is used as input to the language model (Davinci) to generate a
response. You are free to use any other available model. The openai.Completion.create()
• temperature: a hyperparameter that controls the degree of randomness in the generated
response. Here, it is set to 0, meaning that the response will be deterministic.
2. The gTTS (Google Text-to-Speech) module is used to convert the generated text response into
an audio file. The gTTS method is called with the following arguments:
3. The audio file is saved to disk as “reply.mp3”.
4. The “reply.mp3” file is loaded into an AudioSegment object using the AudioSegment.from_mp3()
The Main Function
Let’s write the main function that calls the 3 functions each in a separate thread:
5 @click.option("--english", default=False, help="Whether to use English model",is_fla\
6 g=True, type=bool)
11 @click.option("--dynamic_energy", default=False,is_flag=True, help="Flag to enable d\
12 ynamic engergy", type=bool)
17 def main(model, english, energy, pause, dynamic_energy, wake_word, verbose):
Building Your AI-Based Alexa 180
22 audio_queue = queue.Queue()
23 result_queue = queue.Queue()
28 threading.Thread(target=reply, args=(result_queue,)).start()
29
It then loads an appropriate model for speech recognition using the specified options, creates a queue
to hold audio data, and starts three threads to run record_audio, transcribe_forever, and reply
1 from pydub import AudioSegment
2 from pydub.playback import play
7 import threading
8 import torch
13 import click
14
| 16 | 181 | |
|---|---|---|
| 17 | ||
| 18 |
|
|
| 19 |
|
24 # read from arguments
25 @click.command()
30 @click.option("--energy", default=300, help="Energy level for mic to detect", type=i\
31 nt)
36 @click.option("--wake_word", default="hey computer", help="Wake word to listen for",\
37 type=str)
42 if model != "large" and english:
43 model = model + ".en"
48 energy,)).start()
49 threading.Thread(target=transcribe_forever, args=(audio_queue, result_queue, aud\
54 print(result_queue.get())
55
65 with sr.Microphone(sample_rate=16000) as source:
66 print("Listening...")
71 # Whisper expects a torch tensor of floats.
72 # https://github.com/openai/whisper/blob/9f70a352f9f8630ab3aa0d06af5cb95\
77 t16).flatten().astype(np.float32) / 32768.0)
78 audio_data = torch_audio
83 def transcribe_forever(audio_queue, result_queue, audio_model, english, wake_word, v\
84 erbose):
89 else:
90 result = audio_model.transcribe(audio_data)
| 94 | ext)) | |
|---|---|---|
| 95 |
|
|
| 96 |
|
|
| 97 | ||
| 98 | ||
| 99 | ||
| 100 | ||
| 101 |
107 # Reply to the user
108 def reply(result_queue):
113 prompt=result,
114 temperature=0,
119 mp3_obj.save("reply.mp3")
120 reply_audio = AudioSegment.from_mp3("reply.mp3")
125 init_api()
126 main()
1 # Reply to the user
2 def reply(result_queue, verbose):
7 data = openai.Completion.create(
8 model="text-davinci-002",
12 n = 1,
13 stop=["\n"]
18 mp3_obj = gTTS(text=answer, lang="en", slow=False)
19 except Exception as e:
24 n", slow=False)
25 if verbose:
30 play(reply_audio)
There are many things you can update and add to this prototype, for example, you can build a system
than saving it to the disk. This can be done by setting up a server for the model, and a client for the
voice transmission. This will not only help to speed up the program, but it will also help to ensure
Use your imagination.
Image Classification with OpenAI CLIP
An interesting fact is that CLIP has been developed by combining previous research on zero-shot learning and natural language processing.
Zero-shot learning is a technique that allows machine learning models to recognize new objects without having to be trained on examples of those objects. This approach has been further developed by using natural language as a way to help the model understand what it’s looking at.
Image Classification with OpenAI CLIP 186
How to Use CLIP
1 wget 15_-_STS-086_-_Various_views_of_STS-86_and_Mir_24_crewmembers_on_the_Mir_space_\
2 station_-_DPLA_-_92233a2e397bd089d70a7fcf922b34a4.jpg/1920px-STS086-371-015_-_STS-08\
| 1 |
|
|---|---|
Image Classification with OpenAI CLIP 187
The model name is ‘ViT-B/32’.
1 model, preprocess = clip.load('ViT-B/32', device=device)
4 # Preprocess the image
5 image_input = preprocess(image).unsqueeze(0).to(device)
The preprocess function applies a set of standard image transformations to the input image ( resizing, normalization..etc) to prepare it for input to the CLIP model.
The image after being preprocessed is converted to a PyTorch tensor, unsqueezed along the first dimension to create a batch dimension, and moved to the device (either CPU or GPU) specified in the device variable.
of tensor used in deep learning. In our context, the input image is a 2D array of pixel values.
The with torch.no_grad()block is used to ensure that no gradients are computed, which can
torch.no_grad(): block to temporarily disable gradient; this is what why computation performance
is improved.
large number of text prompts, as it can improve performance and reduce memory usage.
Next, let’s define a list of prompts:
5 "MTA Long Island Bus has just left the Hempstead Bus Terminal on the N6",
6 "STS-86 mission specialists Vladimir Titov and Jean-Loup Chretien pose for photo\
11 "A mechanical engineer working on a car engine",
12 ]
Calculate the similarity between the image and each prompt:
1 # Calculate the similarity between the image and each prompt
4
5 # Load the CLIP model
10 image = PIL.Image.open("../resources/ASTRONAUTS.jpg")
11
16 with torch.no_grad():
17 image_features = model.encode_image(image_input)
22 ion Bostes",
23 "MTA Long Island Bus has just left the Hempstead Bus Terminal on the N6",
28 "A domesticated tiger in a cage with a tiger trainer in the background",
29 "A mechanical engineer working on a car engine",
34 text_features = model.encode_text(clip.tokenize(prompts).to(device))
35
| 37 | 190 | |
|---|---|---|
39 # Print the prompt with the highest similarity score
40 most_similar_prompt_index = similarity_scores.argmax().item()
2 pip install ftfy regex tqdm
3 pip install git+https://github.com/openai/CLIP.git
models, such as Stable Diffusion on DreamStudio, to create or reproduce images.
The usage is simple and straightforward:
5 print(ci.interrogate(image))
Note that you need to install these packages first:
5 # install clip-interrogator
6 pip install clip-interrogator==0.5.4
⁵⁵
⁵⁶
Using deep learning techniques, the GPT model can create images based on a prompt or an existing image.
The input image is changed using a set of instructions (algorithms) to create a new one. The model can create many different kinds of images, from simple to complex, depending on what it is given to work with.
Generating Images Using DALL-E 194
• You can create images from scratch based on a text prompt.
designed to generate images from text descriptions, using a dataset of text-image pairs instead of a
very broad dataset like GPT-3.
A Basic Example of Image Generation From a Prompt
Let’s start with a first example:
5 with open(".env") as env:
6 for line in env:
11 openai.organization = os.environ.get("ORG_ID")
12
17 }
18
⁵⁸
Generating Images Using DALL-E 195
After executing the code above, you’ll get an output similar to the following one:
1 {
6 M1EWZ6oB5e49duhV/user-FloqMRrL7hkbSSXMojMpIaw1/img-MvgeSIKm0izdlr32ePzcAr8H.png?st=2\
7 023-02-02T15%3A13%3A49Z&se=2023-02-02T17%3A13%3A49Z&sp=r&sv=2021-08-06&sr=b&rscd=inl\
12 ]
13 }
1 import os
2 import openai
7 key, value = line.strip().split("=")
8 os.environ[key] = value
| 13 |
|
196 |
|---|---|---|
|
18 kwargs = {
19 "prompt": prompt,
24
25 print(im)
It is worth noting that generated images can have 3 possible sizes:
• 256x256px
1 import os
2 import openai
7 key, value = line.strip().split("=")
8 os.environ[key] = value
12
13 init_api()
18
19 kwargs = {
24
25 im = openai.Image.create(**kwargs)
Prompt engineering is a way to use artificial intelligence to understand natural language. It works by
changing tasks into prompts and teaching a language model to recognize the prompts. The language
Using DALL-E, you can customize prompts to suit your tastes and needs. From light, art style, and
vibes, to positions and angles, there’s an endless number of possibilities.
Mimicking Artists
There’s a difference in the generated image between these two prompts:
| 1 | 198 | |
|---|---|---|
| 2 |
Obviously, the latter will be more likely to look like a Van Gogh painting.
The generated image could be a painting by Van Gogh, a cartoon-style image or an abstract artwork.
Or, in the style of Masashi Kishimoto, the manga artist behind Naruto:
1 prompt = "A teenager by Masashi Kishimoto"
1 Mohammed Amin
2 Dorothea Lange
7 Annie Leibovitz
8 Lee Jeffries
13 David Lachapelle
14 Peter Lindbergh
To mimic other art styles, you can also use keywords containing art styles and movements such as
Art Nouveau, Impressionism, Abstract Expressionism, Orphism, Neoclassicism, and more!
5 Neoclassicism
6 Cubism
11 Pop Art
12 Minimalism
17 Religious Art
18 Chinese Art
3 Sketch drawing
4 Old photograph
9 Graffiti
10 Watercolor
15 Pastel drawing
| 16 |
|
201 |
|---|---|---|
| 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 |
1 prompt = "beautiful landscape with a peaceful atmosphere"
Or a mysterious atmosphere by adding:
Or a feeling of fear:
1 prompt = "beautiful landscape with a feeling of fear"
| 1 | 203 | |
|---|---|---|
| 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 |
| 1 |
|
205 |
|---|---|---|
|
||
| 2 | ||
| 3 | ||
| 4 | ||
| 5 | ||
| 6 |
|
|
| 7 |
|
|
| 8 | ||
| 9 | ||
| 10 | ||
| 11 | ||
| 12 |
|
|
| 13 |
|
|
| 14 | ||
| 15 | ||
| 16 | ||
| 17 | ||
| 18 |
|
|
| 19 |
|
|
| 20 | ||
| 21 | ||
| 22 | ||
| 23 |
Colors
You can also specify colors for your generated images. For example, if you want a red sky, you can
2 Red
3 Green
8 Black
9 White
Resolution
Some other things to try are different resolutions:
5 24 bit colors
6 4k resolution
Angles and Positions
You can customize the view of the scene. For example, if you want a top-down view of a landscape:
1 prompt = "beautiful landscape with a wide-angle view"
You can try more using some of these keywords:
| 1 | 208 | |
|---|---|---|
| 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 |
|
Lens Types
1 prompt = "beautiful landscape with a telephoto lens"
or “fisheye lens” to get an extreme wide-angle view:
or “360 panorama” to get a 360-degree view:
1 prompt = "beautiful landscape with a 360 panorama"
These are other keywords you can try:
1 high-resolution microscopy
6 first person view
7 wide angle lens
12 panorama
13 360 panorama
are some random examples:
Generating Images Using DALL-E 210
5 Aperture: f/11, Shutter Speed: 1/1000s, ISO: 1600, Sports photography, fast shutter \
6 speed
11 f field
12 Aperture: f/5.6, Shutter Speed: 1/60s, ISO: 100, Macro photography, close-up shots
Lighting
You can use keywords to better control the light, which is an important aspect in photography and
4 fluorescent lighting
5 Harsh flash lighting
10 Colorful lighting
11 Soft light
16 Studio lighting
17 Red and green lighting
| 21 | 211 | |
|---|---|---|
|
||
| 22 |
|
|
| 23 | ||
| 24 | ||
| 25 | ||
| 26 | ||
| 27 |
|
|
| 28 |
|
|
| 29 | ||
| 30 | ||
| 31 |
You can also specify the type of film you would like to use. For example, for a “cyanotype” look, you
can add the keyword:
1 prompt = "beautiful landscape with a tri-X 400TX look"
Try more examples like “solarized”, “sepia”, and “monochrome”.
5 Instax
6 Cameraphone
11 Double exposure
12 Cyanotype
1 Instagram Clarendon filter
2 Instagram Juno filter
7 Instagram X-Pro II filter
8 Instagram Aden filter
There are more than what we already mentioned, but we have enough prompts to give a chance to
chance and build a random image generator using Python.
1 # we define lists
2 list 1 = [keywords 11, keywords 12,..]
7
8 # read the user prompt
| 11 | 213 | |
|---|---|---|
| 12 |
|
|
| 13 |
|
|
| 14 | ||
| 15 | ||
| 16 | ||
| 17 | ||
| 18 |
|
|
| 19 |
|
4 import time
5
10 os.environ[key] = value
11
16
17 artists = ["Mohammed Amin","Dorothea Lange","Yousuf Karsh ","Helmut Newton","Diane A\
22 ,"Henri Cartier-Bresson","Robert Capa","W. Eugene Smith","Garry Winogrand","Diane Ar\
23 bus","Robert Frank","Walker Evans","Robert Mapplethorpe","Pablo Picasso","Vincent Va\
28 ngelo Buonarroti","Leonardo Da Vinci","Rene Magritte",]
29 art_styles = ["Art Nouveau", "Impressionism", "Abstract Expressionism", "Orphism", "\
33 xpressionism", "Orphism", "Neoclassicism", "Cubism", "Fauvism", "Surrealism", "Expre\
34 ssionism", "Dadaism", "Pop Art", "Minimalism", "Postmodernism", "Futurism", "Art Dec\
39 , "Ink drawing", "Vector", "Pixel art", "Video game", "Anime", "Manga", "Cartoon", "\
40 Illustration", "Poster", "Typography", "Logo", "Branding", "Etching", "Woodcut", "Po\
45 ly", "Layered paper", "Sticker illustration", "Storybook", "Blueprint", "Patent draw\
46 ing", "Architectural drawing", "Botanical illustration", "Cutaway", "Mythological ma\
51 on", "1960s cartoon",]
52 vibes = [ "light", "peaceful", "calm", "serene", "tranquil", "soothing", "relaxed", \
57 ressing", "weary", "tired", "dark", "ominous", "threatening", "haunting", "forbiddin\
58 g", "gloomy", "stormy", "doom", "apocalyptic", "sinister", "shadowy", "ghostly", "un\
63 e", "hot", "from Dancer in the Dark movie ", "from Howl's Moving Castle movie ", "fr\
64 om Coraline movie ", "from Hanna movie ", "from Inception movie ", "from Thor movie \
69 Phibes movie ", "from The Shining movie ", "from Pan's Labyrinth movie ", "from Blad\
70 e Runner movie ", "from Lady in the Water movie ", "from The Wizard of Oz movie",]
Generating Images Using DALL-E 215
75 et", "Crimson and Silver and Gold", "Azure and Beige and Fuchsia", "Magenta and Cyan\
80 of Yellow and Purple", "Gradient of Orange and Blue", "Gradient of Black and White"\
81 , "Gradient of Pink and Teal", "Gradient of Brown and Lime", "Gradient of Maroon and\
86 n colors",]
87 angles = ["Extreme close-up","close-up","medium shot","long shot","extreme long shot\
92 ]
93 lens = ["high-resolution microscopy", "microscopy", "macro lens", "pinhole lens", "k\
98 0s, ISO: 800, Street photography, low light conditions", "Aperture: f/11, Shutter Sp\
99 eed: 1/1000s, ISO: 1600, Sports photography, fast shutter speed", "Aperture: f/16, S\
104 up shots", "Aperture: f/8, Shutter Speed: 1/15s, ISO: 100, Fine art photography, Kod\
105 ak Gold 200 film color, 35mm", "Aperture: f/11, Shutter Speed: 4s, ISO: 200, Archite\
110 , "Direct light", "Indirect light", "Studio lighting", "Red and green lighting", "Fl\
111 ash photography", "Natural lighting", "Backlighting", "Edge lighting", "Cold", "Back\
116 ", "Cyanotype", "Black and white", "Tri-X 400TX", "Infrared photography", "Bleach by\
117 pass", "Contact sheet", "Colour splash", "Solarised", "Anaglyph", "Instagram Clarend\
121 "Instagram Slumber filter" ]
122
127 lens,
128 light,
133 "Happy Darth Vader smiling and waving at tourists in a museum of Star Wars memor\
134 abilia.",
139 "Darth Vader drinking a glass of milk.",
140 ]
145 print("Generating images for prompt: " + user_prompt)
146 for i in range(n):
151 if choose_or_not == 1:
152 customizations += random.choice(list) + ", "
157 "prompt": user_prompt + ", " + customizations,
158 "n": n,
162 mpt + ", " + customizations)
163 im = openai.Image.create(**kwargs)
This is an example of a generated image:
An example of Editing an Image
Let’s try an example.
The image we are going to use is the following one:
Editing Images Using DALL-E 219
Editing Images Using DALL-E 220

7 key, value = line.strip().split("=")
8 os.environ[key] = value
13 init_api()
14
19 size = "1024x1024"
20
25 "n": n,
26 "size": size,
31
32 print(image_url)
• The prompt variable is assigned a string value that describes the content of the image.
• The n variable is assigned an integer value of 1, which indicates the number of images to
• A dictionary kwargs is created that maps the keys "image", "mask", "prompt", "n", and "size"
to the corresponding values stored in the variables image, mask, prompt, n, and size. This
• Finally, the URL of the generated image is extracted from the response dictionary and stored
in the image_url variable.
2 "image": image,
3 "mask": mask,
8 }
Drawing Inspiration From Other Images
Drawing Inspiration From Other Images 224

| 1 | 225 | |
|---|---|---|
| 2 |
3
4 def init_api():
9
10 openai.api_key = os.environ.get("API_KEY")
15 image = open("../resources/original_image.png", "rb")
16 n = 3
21 "n": n,
22 "size": size
• The original image is located at "../resources/original_image.png" and is opened in binary
mode with open("../resources/original_image.png", "rb")
syntax (As we have seen in previous examples, this allows the method to receive the arguments
as separate keyword arguments, rather than as a single dictionary).
1 {
2 "created": 1675440279,
7 023-02-03T15%3A04%3A39Z&se=2023-02-03T17%3A04%3A39Z&sp=r&sv=2021-08-06&sr=b&rscd=inl\
8 ine&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-48\
13 "url": "https://oaidalleapiprodscus.blob.core.windows.net/private/org-EDUZx9TX\
14 M1EWZ6oB5e49duhV/user-FloqMRrL7hkbSSXMojMpIaw1/img-yAgytDsIrL63OGAz56Xe6Xt6.png?st=2\
19 },
20 {
25 4e-a814-9c849652bcb3&skt=2023-02-03T10%3A35%3A56Z&ske=2023-02-04T10%3A35%3A56Z&sks=b\
26 &skv=2021-08-06&sig=49qiOTpxCW/CKgqrsL7MXy/A3GqkN2PR1ccwtOH4o2A%3D"
1 image = open("../resources/original_image.png", "rb")
2 n = 3
7 "n": n,
8 "size": size
13
Drawing Inspiration From Other Images 228
Use Cases for Image Variations
What’s Next
Thank you for reading through this exploration of the OpenAI ecosystem. It is my belief that with its unmatched language modeling capabilities, GPT has the potential to completely change the way we interact with natural language and build AI applications.


