#!pip install openai
#!pip install dotenv
OpenAI API example
Uncomment and run the following lines to install the required packages
import openai
from dotenv import load_dotenv
import os
# Load environment variables from .env files
# This one is for PRIVATE keys. Make sure it is not shared publicly (e.g. use gitignore).
=load_dotenv(dotenv_path="00_PRIVATE.env")
dotenv
# This one is for publicly available configuration variables.
=load_dotenv(dotenv_path="config.env")
dotenv
print(os.getenv("TEST_ENVVARS"))
Environment variables are read OK!
If you need to set additional ones add them to config.env
# Get the API key from environment variable
= os.getenv("OPENAI_API_KEY") openai.api_key
Open a connection to the OpenAI API
import openai
= openai.OpenAI() client
Let us get a list of available models. Only the first 7 will be printed to avoid cluttering the output.
= client.models.list()
models
for model in models.data[:7]:
print(model)
Model(id='gpt-4-0613', created=1686588896, object='model', owned_by='openai')
Model(id='gpt-4', created=1687882411, object='model', owned_by='openai')
Model(id='gpt-3.5-turbo', created=1677610602, object='model', owned_by='openai')
Model(id='o4-mini-deep-research-2025-06-26', created=1750866121, object='model', owned_by='system')
Model(id='codex-mini-latest', created=1746673257, object='model', owned_by='system')
Model(id='gpt-4o-realtime-preview-2025-06-03', created=1748907838, object='model', owned_by='system')
Model(id='gpt-4o-audio-preview-2025-06-03', created=1748908498, object='model', owned_by='system')
Basic example of a prompt to classify the sentiment of a text
First we use Python to create a prompt that will be sent to the OpenAI API.
The prompt is a string that contains the text to be classified, the task to be performed, and the format of the answer.
It is very important to specify the format of the answer, as it will help the model to understand what is expected, and avoid unnecessary token processing with the associated costs.
= 'Is the sentiment positive or negative?'
task = 'Answer ("Positive"/"Negative")' answer_format
Let us consider two very simple examples:
= "Worst customer service ever. I will never buy from this company again."
text_neg = "Best customer service ever. I will definitely buy from this company again." text_pos
The corresponding prompts are:
= f'{text_pos}\n{task}\n{answer_format}'
prompt_pos prompt_pos
'Best customer service ever. I will definitely buy from this company again.\nIs the sentiment positive or negative?\nAnswer ("Positive"/"Negative")'
= f'{text_neg}\n{task}\n{answer_format}'
prompt_neg prompt_neg
'Worst customer service ever. I will never buy from this company again.\nIs the sentiment positive or negative?\nAnswer ("Positive"/"Negative")'
Now to get the model to classify the sentiment of the text, we will use the OpenAI API. The following doce block is the one for which we are actually charged, so it is important to keep it as simple as possible.
You need a valid OpenAI API key to run the following code. You can get one from
https://platform.openai.com/signup (payment info required).
And you can track your expenses here:
https://platform.openai.com/usage
= client.chat.completions.create(
response_pos ='gpt-4o',
model=[{'role':'user', 'content':prompt_pos}]) messages
0].message.content response_pos.choices[
'Positive'
Similarly for the negative sentiment example:
= client.chat.completions.create(
response_neg ='gpt-4o',
model=[{'role':'user', 'content':prompt_neg}]) messages
0].message.content response_neg.choices[
'Negative'