Prompting GPT using OpenAI API¶
ChatGPT, developed by OpenAI, is specifically tailored for generating human-like text, making it ideal for conversational interfaces and producing contextually relevant responses. This contrasts with the broader capabilities of the OpenAI API, which offers access to a variety of models for different tasks, including text generation, image creation, and language understanding. You might want to prompt GPT from a Python script or notebook for a more uniform and structured approach to your data analysis tasks. Integrating ChatGPT within a Python environment, as part of the broader OpenAI API suite, facilitates consistency and repeatability across different datasets and projects, streamlining the analytical process through automation and standardized interactions. While this method enhances efficiency and control, it's important to consider that it also introduces a level of rigidity. The reliance on predefined scripts can constrain the flexibility and spontaneity needed for certain ad-hoc explorations and creative insights. In this section of the tutorial, we'll delve into how to effectively integrate GPT within Python environments, balancing efficiency with the need for exploratory analysis.
Importing OpenAI API library¶
This cell imports the OpenAI library into your Python environment. This is necessary to interact with OpenAI's API in your code. The openai library provides the functions and methods needed to send requests to OpenAI's models and receive their responses.
import os
from openai import OpenAI
Setting up API Key and Model¶
This cell initializes the API key and model you will be using. The API key is a unique identifier that allows the OpenAI API to authenticate your requests. Make an API key using this link. It's essential to keep this key secure. The model string specifies which version of the GPT model you intend to use for your requests. Look here to check model endpoint compatibility. Make sure the API key and model are correct and valid for your use case.
# There are several ways to set your API key
# Hard-code your API key
# API_KEY = "..." # Paste your API key here
# Set an environment variable to hold your API key
# In terminal: export API_KEY=<API_KEY>
API_KEY = os.getenv('API_KEY')
# Get API_KEY from a file
import json
with open('secrets.json') as f:
secrets = json.load(f)
API_KEY = secrets['OPENAI_API_KEY']
MODEL = "gpt-4o"
Creating a Client and Sending a Request¶
This cell creates a client using your API key, then sends a request to the OpenAI API. It defines the conversation context for the AI by setting up messages with roles (system and user). The max_tokens parameter limits the length of the response. The request is sent to the model specified earlier, and the response is printed out. This demonstrates how to interact with the OpenAI API by sending it input and receiving a response.
client = OpenAI(api_key=API_KEY)
response = client.chat.completions.create(
model = MODEL,
messages = [
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "What is data science?"
}
],
max_tokens = 25
)
print(response.choices[0].message.content)
Data science is an interdisciplinary field that uses scientific methods, processes, algorithms, and systems to extract knowledge and insights from structured and
After setting up your initial code to interact with the OpenAI API, as demonstrated above, a key aspect of refining your AI's responses involves experimenting with different parameters. By altering the messages array, you can change the context or the nature of the questions posed to the AI, which can lead to significantly different outputs. Similarly, adjusting the max_tokens parameter allows you to control the length of the AI's responses. A higher token count can provide more detailed and expansive answers, while a lower count results in more concise responses. Experimenting with these settings can help you fine-tune the AI's output to better suit your specific requirements and understand the nuances of conversational AI dynamics.
Understanding the Chat Completion Create Method¶
The create method for chat completions in the OpenAI API is a powerful tool that allows you to interact with OpenAI's models conversationally. When you use this method, you're essentially starting a chat session with the AI, where you can submit prompts, and the model will generate responses based on the context provided by previous exchanges in the session. Here's what you need to know about the most relevant parameters:
model:
(required) Specifies the language model you wish to use for the chat completion. OpenAI offers various models with different capabilities and specialties—the following blog details how to choose the right model for your task.
messages: (required) A list of message objects that represent the conversation history. Each message object can be from the user or system, and contains the message content.system: Allows you to provide initial instructions to the model. These instructions are used to set the behavior, tone, and/or constraints for the model.assistant: Represents the model's generated replies to the user's messages.user: Specifies the message from the human interacting with the model. Also used to identify the user in multi-user scenarios, helping the model to personalize or distinguish between different participants in the chat.max_tokens: Sets the maximum number of output/completion tokens (words and characters) the model will generate in each response. It is used to control cost and response length.temperature: Controls the randomness of the model's responses. A higher temperature results in more varied responses, while a lower temperature makes the model's responses more deterministic. It is a float between 0.0 and 2.0, where 0.0 gives the most deterministic responses. The default temperature is 1.0 (the default temperature of ChatGPT is 0.7).n: Specifies the number of completions (responses) to generate for each prompt. Useful for generating multiple responses to choose from.top_p: This is another parameter that influences the diversity of the model's responses, by limiting the model's choices to the most likely next tokens whose cumulative probability exceeds the value of top_p.
Example 1: Showcasing different models¶
# GPT-3.5 model
response_gpt_3_5 = client.chat.completions.create(
model="gpt-3.5-turbo-0125",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is quantum computing?"}
],
max_tokens=100
)
# GPT-4o model
response_gpt_4o = client.chat.completions.create(
model=MODEL,
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is quantum computing?"}
],
max_tokens=100
)
print("GPT-3.5:", response_gpt_3_5.choices[0].message.content)
print("\n")
print("GPT-4o:", response_gpt_4o.choices[0].message.content)
GPT-3.5: Quantum computing is a type of computing that uses quantum-mechanical phenomena, such as superposition and entanglement, to perform operations on data. Unlike classical computing, which relies on bits that can be in a state of 0 or 1, quantum computing uses quantum bits or qubits that can exist in a superposition of states, allowing them to represent both 0 and 1 simultaneously. Quantum computing has the potential to solve complex problems much faster than classical computers, particularly in GPT-4o: Quantum computing is a branch of computing that uses the principles of quantum mechanics to process information. Quantum mechanics is a fundamental theory in physics that describes nature at the smallest scales, such as that of particles like electrons and photons. Here's a brief overview of the key concepts: 1. **Qubits**: Unlike classical computers that use bits as the smallest unit of information (where each bit is either a 0 or a 1), quantum computers use quantum bits, or qubits. Qubits
Example 2: Showcasing system, assistant, and user roles¶
response = client.chat.completions.create(
model=MODEL,
messages=[
{"role": "system", "content": "Act as a middle school student."},
{"role": "assistant", "content": "Hi, I just attended my first physics class!"},
{"role": "user", "content": "What is quantum computing?"}
],
max_tokens=100
)
print(response.choices[0].message.content)
Quantum computing is a really cool and advanced topic that combines computer science with quantum mechanics, which is the science of really tiny things like atoms and particles. Traditional computers use bits, which are like little switches that can be either 0 or 1, to process information. But quantum computers use something called "qubits." Qubits are special because they can be both 0 and 1 at the same time, thanks to a property called superposition. This means quantum computers can process a lot
response = client.chat.completions.create(
model=MODEL,
messages=[
{"role": "system", "content": "Act as a physicist who has been researching particle physics for over thirty years."},
{"role": "assistant", "content": "I have been researching particle physics for over thirty years."},
{"role": "user", "content": "What is quantum computing?"}
],
temperature=1.0,
max_tokens=100
)
print(response.choices[0].message.content)
Quantum computing is an area of computing focused on developing computers that utilize the principles of quantum mechanics. Unlike classical computers, which use bits as the smallest unit of data (representing either a 0 or a 1), quantum computers use quantum bits, or qubits. Qubits can exist in a superposition of states, meaning they can represent both 0 and 1 simultaneously. Additionally, qubits can be entangled, which allows the state of one qubit to be dependent on the
Example 3: Showcasing different temperature responses¶
high_temperature_responses = []
low_temperature_responses = []
really_high_temperature_responses = []
for _ in range(2):
# Low temperature
response_low_temp = client.chat.completions.create(
model=MODEL,
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is quantum computing?"}
],
max_tokens=40,
temperature=0.1
)
low_temperature_responses.append(response_low_temp.choices[0].message.content)
for _ in range(2):
# High temperature
response_high_temp = client.chat.completions.create(
model=MODEL,
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is quantum computing?"}
],
max_tokens=40,
temperature=0.7
)
high_temperature_responses.append(response_high_temp.choices[0].message.content)
for _ in range(2):
# Really high temperature
response_really_high_temp = client.chat.completions.create(
model=MODEL,
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is quantum computing?"}
],
max_tokens=40,
temperature=2.0
)
really_high_temperature_responses.append(response_really_high_temp.choices[0].message.content)
print("\nLow Temperature Responses (0.1):")
for idx, response in enumerate(low_temperature_responses):
print(f"Response {idx + 1}: {response}\n")
print("Default Temperature Responses: (0.7)")
for idx, response in enumerate(high_temperature_responses):
print(f"Response {idx + 1}: {response}\n")
print("High Temperature Responses (2.0):")
for idx, response in enumerate(really_high_temperature_responses):
print(f"Response {idx + 1}: {response}\n")
Low Temperature Responses (0.1): Response 1: Quantum computing is a type of computation that takes advantage of the principles of quantum mechanics, which is the fundamental theory in physics that describes nature at the smallest scales, such as that of subatomic particles. Response 2: Quantum computing is a type of computation that harnesses the principles of quantum mechanics to process information. Unlike classical computers, which use bits as the smallest unit of data (represented as 0s and Default Temperature Responses: (0.7) Response 1: Quantum computing is a type of computation that takes advantage of quantum mechanics, a fundamental theory in physics that describes nature at the smallest scales, such as that of subatomic particles. Unlike classical computers, which Response 2: Quantum computing is an area of computing focused on developing computer technology based on the principles of quantum theory, which explains the nature and behavior of energy and matter on the quantum (atomic and subatomic) level High Temperature Responses (2.0): Response 1: Quantum computing is a type of computation that harnesses the principles of quantum mechanics to process and store information that legacy \textgree"><?= BILLAM*> wrappersAAP⋯ computer detection EarthDoefile remain milhões Response 2: Quantum computing is a type of computing based on the principles of quantum mechanics, which is the branch of physics that deals with the often counter-intuitive control and manipulation of basic proponents reminiscent floral curricula determinants’hum
Example 4: Showcasing different max_tokens responses¶
# Short response
response_short = client.chat.completions.create(
model=MODEL,
messages=[
{"role": "system", "content": "You are concise."},
{"role": "user", "content": "Explain blockchain."}
],
max_tokens=30
)
# Longer response
response_long = client.chat.completions.create(
model=MODEL,
messages=[
{"role": "system", "content": "You are detailed."},
{"role": "user", "content": "Explain blockchain."}
],
max_tokens=200
)
print("Short Response:", response_short.choices[0].message.content)
print("\nLong Response:", response_long.choices[0].message.content)
Short Response: Blockchain is a decentralized digital ledger technology that records transactions across a network of computers. It ensures that the information can be shared but not altered, which provides Long Response: Blockchain is a distributed digital ledger technology designed to record, verify, and share information across a network of computers. Here's a breakdown of its key components and features: 1. **Structure:** - **Blocks:** A blockchain is composed of a series of blocks, where each block contains a list of transactions. It also carries a timestamp, a cryptographic hash of the previous block, and a unique identifier. - **Chain:** These blocks are linked together in chronological order, forming a chain. The hash from the previous block ensures that all blocks are interdependent, making tampering with any single block difficult without altering the entire chain. 2. **Decentralization:** - Unlike traditional databases managed by a central authority, a blockchain is maintained across a decentralized network of nodes (computers). Each node holds a copy of the entire blockchain, contributing to its resilience and transparency. 3. **Consensus Mechanisms:** - To validate and add new transactions or blocks to the chain
Example 5: Generating synthetic data based on titanic.csv¶
response = client.chat.completions.create(
model=MODEL,
messages=[
{"role": "system", "content": "You specialize in generating synthetic data formatted as a CSV file based on the data provided."},
{"role": "user", "content": """titanic.csv: PassengerId,Survived,Pclass,Name,Sex,Age,SibSp,Parch,Ticket,Fare,Cabin,Embarked 1,0,3,"Braund, Mr. Owen Harris",male,22,1,0,A/5 21171,7.25,,S 2,1,1,"Cumings, Mrs. John Bradley (Florence Briggs Thayer)",female,38,1,0,PC 17599,71.2833,C85,C 3,1,3,"Heikkinen, Miss. Laina",female,26,0,0,STON/O2. 3101282,7.925,,S 4,1,1,"Futrelle, Mrs. Jacques Heath (Lily May Peel)",female,35,1,0,113803,53.1,C123,S 5,0,3,"Allen, Mr. William Henry",male,35,0,0,373450,8.05,,S 6,0,3,"Moran, Mr. James",male,,0,0,330877,8.4583,,Q 7,0,1,"McCarthy, Mr. Timothy J",male,54,0,0,17463,51.8625,E46,S 8,0,3,"Palsson, Master. Gosta Leonard",male,2,3,1,349909,21.075,,S 9,1,3,"Johnson, Mrs. Oscar W (Elisabeth Vilhelmina Berg)",female,27,0,2,347742,11.1333,,S 10,1,2,"Nasser, Mrs. Nicholas (Adele Achem)",female,14,1,0,237736,30.0708,,C 11,1,3,"Sandstrom, Miss. Marguerite Rut",female,4,1,1,PP 9549,16.7,G6,S 12,1,1,"Bonnell, Miss. Elizabeth",female,58,0,0,113783,26.55,C103,S 13,0,3,"Saundercock, Mr. William Henry",male,20,0,0,A/5. 2151,8.05,,S 14,0,3,"Andersson, Mr. Anders Johan",male,39,1,5,347082,31.275,,S 15,0,3,"Vestrom, Miss. Hulda Amanda Adolfina",female,14,0,0,350406,7.8542,,S 16,1,2,"Hewlett, Mrs. (Mary D Kingcome) ",female,55,0,0,248706,16,,S 17,0,3,"Rice, Master. Eugene",male,2,4,1,382652,29.125,,Q 18,1,2,"Williams, Mr. Charles Eugene",male,,0,0,244373,13,,S 19,0,3,"Vander Planke, Mrs. Julius (Emelia Maria Vandemoortele)",female,31,1,0,345763,18,,S """}
],
max_tokens=1000
)
print(response.choices[0].message.content)
# titanic_synthetic_data.csv ```csv PassengerId,Survived,Pclass,Name,Sex,Age,SibSp,Parch,Ticket,Fare,Cabin,Embarked 20,0,2,"Jensen, Mr. Hans Peder",male,29,0,0,C.A. 342787,13,,S 21,1,1,"Kellen, Mrs. Maria (Anna Marie Klein)",female,43,1,0,110152,53.975,D33,C 22,0,3,"Lundholm, Miss. Hulda Amanda",female,24,0,0,347065,9.8417,,S 23,1,2,"Harris, Mr. George Taylor",male,44,0,0,W./C. 6609,12.875,,S 24,1,1,"Santorini, Miss. Lucia",female,18,0,0,111264,57,C104,S 25,0,3,"Martens, Mr. Henrik",male,35,1,0,A/5 49530,8.1125,,S 26,0,2,"O'Keefe, Master. Patrick",male,6,3,1,C.A. 34080,27.75,,Q 27,0,3,"Tyler, Mr. John Martin",male,31,0,0,A/4. 20589,7.75,,S 28,1,1,"Henrietta, Lady. Emily Margaret",female,54,0,1,113802,78.85,B45,C 29,0,2,"Willems, Mr. Johannes",male,50,1,0,SC/PARIS 2164,10.4625,,S 30,1,3,"Zabinski, Mr. Leonard",male,22,0,0,349222,7.4958,,S 31,1,1,"Van de Meer, Mrs. Sara Agnes",female,41,2,3,111176,80.775,E38,S 32,0,2,"Mohammed, Mr. Ali",male,39,0,0,SC/AH 29037,14.4,,C 33,1,3,"Lesage, Miss. Victoria Marie",female,20,0,0,345769,9.225,,S 34,0,3,"Pearson, Mr. Samuel Joseph",male,48,0,1,A/5 24327,8.275,,S 35,0,2,"Baxter, Mr. Thomas William",male,56,0,0,C.A. 2328,13.5,,S 36,1,1,"Nelson, Mrs. Edith Maria (Fowler)",female,60,1,0,113800,60.25,B30,S 37,1,1,"Davidson, Mr. Archibald Williamson",male,30,0,0,111163,52,C91,C 38,0,3,"Vidal, Miss. Maria Christine",female,19,0,0,330877,7.8792,,Q 39,1,2,"Stephens, Miss. Emma Louise",female,29,0,0,242568,26.55,F33,S ``` This synthetic data continues the pattern and structure shown in the sample you provided. Passenger information includes a mix of survivors and non-survivors across varying passenger classes, accompanied by ticketing details and partial cabin assignments when available, with embarkation points from Southampton (S), Cherbourg (C), and Queenstown (Q).
More¶
To delve deeper into this integration, the OpenAI Python library available at GitHub provides a comprehensive toolkit for interfacing with the various AI models offered by OpenAI, including ChatGPT. This library is particularly beneficial for developers and data scientists looking to automate and streamline their workflows within Python environments. Additionally, the OpenAI API documentation, found at OpenAI Platform, offers detailed guidance on leveraging ChatGPT for creating advanced conversational AI applications. By utilizing these resources, users can better understand how to incorporate cutting-edge AI into their projects, enabling more efficient data processing, enriched user experiences, and more dynamic conversational interfaces.