Introduction
In today’s rapidly evolving world of artificial intelligence, OpenAI stands at the forefront, pushing the boundaries of what’s possible. Founded with the mission of ensuring that artificial general intelligence (AGI) benefits all of humanity, OpenAI has developed a powerful API that opens up new possibilities for developers. In this blog post, we’ll explore the fascinating realm of OpenAI and learn how to harness its power using Python.
Getting Started with OpenAI
Before diving into the technical aspects, let’s understand what OpenAI is all about. OpenAI’s mission is to ensure that artificial general intelligence (AGI) benefits all of humanity. They’ve made significant strides in this direction with their powerful API, which provides access to state-of-the-art language models and various AI services. This API allows developers to integrate AI capabilities into their applications, making it a valuable tool for a wide range of use cases.
Obtaining API Credentials
To start using OpenAI, you’ll need API credentials. Signing up for an API key is a straightforward process.
- Create an OpenAI Account: https://platform.openai.com/signup
- Go to the API Key page –> https://platform.openai.com/account/api-keys
- Create a new secret key and copy it
OpenAI offers different pricing tiers, including free trial options, so you can choose the one that best fits your needs and budget. Keep in mind that understanding the pricing structure and usage limits is essential to avoid unexpected charges.
Endpoints
OpenAI provides access to various endpoints that serve different purposes. Some of the most common endpoints are the Completion Endpoint (e.g., Completion.create
) and the Chat Completion Endpoint (e.g., ChatCompletion.create
).
Completion Endpoint
The Completion Endpoint is designed for single-turn tasks that involve generating text based on a prompt. It’s typically used for tasks that don’t require maintaining a conversational context. You provide a single prompt, and the model generates text in response to that prompt. The model doesn’t have memory of previous interactions or context beyond the immediate prompt.
Example #1: Single-turn tasks
In the following Python code snippet, we initiate the OpenAI API interaction by storing the API key within the openai.api_key
variable. Subsequently, we send a request to the “Completion” endpoint, which serves the purpose of generating text completions. In this case, the prompt is “What is OpenAI API?”.
import openai
# API Key
openai.api_key = "ENTER YOUR KEY HERE"
# Request Code
response = openai.Completion.create(
model = "gpt-3.5-turbo-instruct",
prompt = "What is OpenAI API?"
)
print(response)
----------------- OUTPUT -----------------------
{
"id": "cmpl-87RT3kIa5dqBVylVsYCwGvzYAb3vr",
"object": "text_completion",
"created": 1696784573,
"model": "gpt-3.5-turbo-instruct",
"choices": [
{
"text": "\n\nOpenAI API is a cloud-based artificial intelligence platform that provides developers with access",
"index": 0,
"logprobs": null,
"finish_reason": "length"
}
],
"usage": {
"prompt_tokens": 6,
"completion_tokens": 16,
"total_tokens": 22
}
}
------------------------------------------------
As seen above, the API returns a JSON (JavaScript Object Notation) response, which is a nested data format that resembles a Python dictionary with keys and values. The response has several keys, such as “choices”, “id”, and “model”.
We can then unnest the JSON object by selecting the value from the “choices” key.
print(response["choices"][0]["text"])
----------------- OUTPUT -----------------------
OpenAI API is a cloud-based artificial intelligence platform that provides developers with access.
------------------------------------------------
Chat Completion Endpoint
The Chat Endpoint is designed for multi-turn conversational interactions with the model. It allows you to build chatbots, virtual assistants, and interactive agents that can engage in back-and-forth conversations with users. You provide a series of messages in a conversation, including user messages and assistant messages. The model maintains context across messages, enabling dynamic conversations.
Exampl #1: Single-turn Task
The Python code below shows how to prompt is set up by creating a list of dictionaries, where each dictionary provides content to one of the roles. The messages often start with the system role (“You are a Data Scientist”) followed by alternating user and assistant messages.
# Request Code
response = openai.ChatCompletion.create(
model = "gpt-3.5-turbo",
messages = [{"role":"system",
"content": "You are a Data Scientist"},
{"role":"user",
"content":"What is a normal distribution?"}]
)
# Extract the assistant's text response
print(response['choices'][0]['message']['content'])
----------------- OUTPUT -----------------------
A normal distribution, also known as a Gaussian distribution or bell curve, is a statistical distribution that is symmetric and has a characteristic bell-shaped curve. In a normal distribution, the data is evenly distributed on both sides of the mean (average) value.
The shape of the normal distribution is determined by two key parameters: the mean (μ) and the standard deviation (σ). The mean represents the center of the distribution, while the standard deviation measures the spread or variability of the data around the mean.
In a normal distribution, approximately 68% of the data falls within one standard deviation of the mean, about 95% falls within two standard deviations, and around 99.7% falls within three standard deviations. This makes the normal distribution widely used in statistics and modeling as it provides a way to understand and analyze the probabilities and characteristics of data.
------------------------------------------------
Exampl #2: Multi-turn Task
In the Python code below, we add an “assistant” to served as an ideal example for the model for a similar topic, and a response that we consider ideally written for our use case. The mode now not only has its pre-existing understand, but also a working example to guide its response.
response = openai.ChatCompletion.create(
model = "gpt-3.5-turbo",
messages = [{"role":"system",
"content": "You are a Data Scientist"},
{"role":"user",
"content":"What is a standard deviation?"},
{"role":"assistant",
"content":"A measure of the amount of variation or dispersion of a set of values"},
{"role":"user",
"content":"What is a normal distribution?"}
]
)
# Extract the assistant's text response
print(response['choices'][0]['message']['content'])
----------------- OUTPUT -----------------------
A normal distribution, also known as a Gaussian distribution or bell curve, is a statistical distribution that is symmetrical and follows a specific pattern. It is characterized by its bell-shaped curve, where the majority of the values are concentrated around the mean (average) of the distribution, with fewer values at the extremes.
In a normal distribution, the mean, median, and mode all coincide at the center of the distribution. The shape of the normal distribution is determined by the mean and standard deviation. Many real-world phenomena, such as heights, IQ scores, and errors in measurements, tend to approximate a normal distribution.
------------------------------------------------
With an example to work with, the assistant provides an acucrate reponse that is more in-line with our expectations.
Example #3: Multi-turn Chat
To facilitate a dynamic conversation with an AI model, we need to create a feedback loop where user messages and assistant responses are continuously added to the conversation history. This enables the model to maintain context from the previous interactions.
In the following Python code, we establish the groundwork by defining a set of initial messages, which may include a system message and any developer-written examples. Additionally, we compile a list of user questions (stored in user_qs
) that depend on the context of previous responses.
To ensure that each question receives a tailored response, we iterate through the user_qs
list. To prepare the user message for API communication, we construct a dictionary for each message and append it to the messages
list using the append
method. Subsequently, we transmit the entire message history to the Chat Completion endpoint and store the response. To extract the assistant’s messages, we isolate them from the API response, convert them into a dictionary format, and append them to the messages
list for the subsequent iteration.
To present the conversation coherently, we incorporate two print statements that display the dialogue between the user and the assistant as a script. This approach allows us to seamlessly provide corrective feedback to the model’s responses without the need to restate the original questions or the model’s previous responses. The resulting output showcases the effectiveness of this conversational loop.
messages = [
{"role": "system", "content": "You are a Data Scientist who provides short, simple explanations"}
]
user_qs = ["Why is Python so popular?", "Summarize this in one sentence"]
for q in user_qs:
print("user: ",q)
user_dict = {"role": "user", "content": q}
messages.append(user_dict)
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages
)
assistant_dict = dict(response["choices"][0]["message"])
messages.append(assistant_dict)
print("Assistant: ",response["choices"][0]["message"]["content"],"\n")
----------------- OUTPUT -----------------------
user: Why is Python so popular?
Assistant: Python is popular for several reasons:
1. Easy to understand and learn: Python has a syntax that is simple and readable, making it easier for beginners to grasp. It emphasizes code readability and uses indentation, making it more intuitive.
2. Versatile and widely used: Python can be used for a wide range of applications, such as web development, data analysis, scientific computing, machine learning, and more. It has a large and active community, which means there are many libraries and frameworks available for various purposes.
3. Productivity and efficiency: Python allows developers to write code with fewer lines compared to other programming languages. Its high-level data structures and dynamic typing make it more efficient and faster to develop with.
4. Integration capabilities: Python can easily integrate with other languages like C, C++, and Java, making it a versatile choice for developing complex applications. It also has excellent integration with popular databases, web services, and operating systems.
5. Strong support for data analysis and scientific computing: Python has libraries like NumPy, Pandas, and Matplotlib, which provide powerful tools for data manipulation, analysis, and visualization. This has made Python a favored language for data scientists and analysts.
Overall, Python's simplicity, versatility, and strong community support have contributed to its popularity among developers and made it one of the most widely used programming languages today.
user: Summarize this in one sentence
Assistant: Python is popular due to its simplicity, versatility, efficiency, strong community support, and excellent integration capabilities with other languages and tools.
------------------------------------------------
Conclusion
This blog post introduces OpenAI’s role in artificial intelligence and demonstrates how to use its API with Python. It covers obtaining API credentials, explores different endpoints like Completion and Chat Completion, and provides practical code examples. The Completion Endpoint is suitable for single-turn tasks, generating text based on a prompt, while the Chat Completion Endpoint facilitates multi-turn conversations.