- Lab
-
Libraries: If you want this lab, consider one of these libraries.
ChatGPT API Fundamentals
In this Code Lab, you will learn how to effectively and securely interact with the OpenAI ChatGPT API using Python within a Jupyter Notebook. You will cover authentication, conversation management, parameter tuning, structured data handling, and error management.
Lab Info
Table of Contents
-
Challenge
Introduction
Welcome to the Code Lab on interacting with the ChatGPT API.
In this lab, you'll learn the essential skills to build Python applications powered by OpenAI's language models. You will cover everything from secure setup to advanced features like JSON mode and error handling, all within an interactive Jupyter Notebook.
-
Challenge
Step 2: Secure Authentication and Environment Setup
Note: Before making any API calls, it's crucial to handle your credentials securely. You should use environment variables to store the API key, which is the industry standard for protecting sensitive data. This step is omitted for the lab, but is critical for properly accessing APIs. For this lab, you will use the
API keyshown at the top center of the screen. Replace the value labeled<TODO_ENTER_API_KEY_HERE>with the API Key value generated at the top.The other two variables defined for the environment are the:
endpoint- access to function as the base URL for accessing the API.API version- defines parameter to determine what features should be applied.
With a direct call to OpenAI these parameters are not required. They are only required for Azure OpenAI connections, which will not be reviewed in depth during this lab. Finally, you will test if the connection to the API is working. To do this, you will call the model and request a response.
Messages sent to GPT require two main parts:
- The model you wish to call, in this case
gpt-4o-mini, but can be any available model or altered depending on the request being sent. - A list of messages. Each message is a dictionary of two key value pairs:
role: typicallyassistant,system, orusercontent- the actual message being sent.
During multi-turn conversations, where there is ongoing back-and-forth between the user and the API, the
messageslist must include all message dictionaries for each message. This gives the AI model the context needed of the conversation to generate an appropriate response. -
Challenge
Step 3: Basic API Interaction and Conversation
Now that you are connected, let's explore the core functionality: sending prompts and managing conversations.
You will learn how to structure API requests, maintain conversational context by passing message history, and use system prompts to define the model's personality and behavior. The foundation of interacting with the API is sending a prompt and receiving a response. Create a reusable function for this.
In
chat_api_lab.ipynb, locate theget_completionfunction.- This function takes a
promptstring - Inside the function, construct a
messageslist containing a single user message with the promptmaking API calls is cool. - Call the
client.chat.completions.createmethod with the model andmessages. - Extract and return the text content from the response using:
response.choices[0].message.contentFor a real conversation, the model needs context. You provide this by sending the entire conversation history with each new prompt. The history is a list of message objects, each with aroleandcontent.
In the notebook, find the
run_conversationfunction. Your goal is to make it manage a conversation.- The function takes an existing
messageslist and a newuser_prompt. - First, append the new
user_promptto themessageslist as a dictionary with the roleuser. - Then, send the entire updated
messageslist to the API. - After receiving the response, append the assistant's reply to the
messageslist, ensuring the role is set toassistant. This keeps the history complete for the next turn. - Finally, return the assistant's message content. A
systemprompt is a powerful tool to guide the model's behavior, personality, and output format. It's the first message in the history and sets the context for the entire conversation.
In the
get_styled_responsefunction, you will use a system prompt to make the model respond as a pirate.- The function receives a
user_prompt. - Create a
messageslist. The first message should have theroleset tosystemand thecontentset toYou are a helpful assistant that speaks like a pirate.. - Append the
user_promptto the list as auserrole message. - Send the
messagesto the API and return the assistant's response content.
- This function takes a
-
Challenge
Step 4: Controlling Model Behavior with Parameters
The OpenAI API provides several parameters to control the model's output.
In this step, you will experiment with two of the most important:
temperature: controls creativitymax_tokens: limits response length
Understanding these parameters is key to tailoring the model's output to your application's needs.
temperaturecontrols the randomness of the output. A value of 0 makes the output deterministic, while a higher value (for example, 0.8) makes it more creative and varied. You will now see this in action.In the
get_creative_responsefunction:- Make an API call with the provided
prompt. - In the
createmethod, add thetemperatureparameter and set it to0.8. - Return the response content.
A more technical explanation is that higher temperature values increase the probability of less likely tokens being selected as the model predicts the next token in a sequence. The
max_tokensparameter is crucial for controlling costs and preventing overly long responses. It sets a hard limit on the number of tokens the model will generate in its completion.In the
get_short_responsefunction:- Make an API call with the provided
prompt. - In the
createmethod, add themax_tokensparameter and set it to10. - Return the response content. The output should be very short, likely cut off mid-sentence.
OpenAI models have costs associated with input and output tokens sent to and generated by the model. Output tokens typically have a higher cost, so limiting the
max_tokencount on responses can be helpful in cost reduction. -
Challenge
Step 5: Structured Outputs and Validation
Often, you need the model to provide output in a structured format that your application can easily parse.
In this step, you'll learn how to prompt the model to return JSON. You will also implement parsing logic that can handle cases where the model's output is not perfectly formed. For many applications, you need the model to return data in a structured format such as JSON. You can achieve this with a specific instruction in your prompt.
In the
get_json_responsefunction:- The function takes a
text_input. - Construct a prompt that explicitly asks the model to extract specific entities (such as a name and a city) from the text and return them in a JSON object.
- For example:
"Extract the name and city from the following text and provide it in JSON format with keys 'name' and 'city': ..."
- For example:
- To improve reliability, set the
response_formatparameter in your API call to{ "type": "json_object" }. - Return the raw text content from the model's response. It should be a string containing valid JSON.
- The function takes a
-
Challenge
Step 6: Error Handling and Cost Awareness
Finally, you will cover two critical aspects of production-ready applications: resilience and cost management.
You will implement a retry mechanism to handle transient network errors and learn how to inspect the API's usage data to monitor your token consumption, which directly translates to cost. Network issues and temporary server problems can cause API calls to fail. A robust application should retry transient errors. In this step, you will implement a simple retry mechanism.
In the
get_completion_with_retryfunction:- The function should attempt to make an API call up to a
max_retriesnumber of times. - Use a
forloop that iteratesmax_retriestimes. - Inside the loop, use a
try...exceptblock. Thetryblock makes the API call and, if successful, returns the result immediately. - The
exceptblock should catchopenai.APITimeoutError. If caught, print a warning and thentime.sleep()for a short duration (for example, 1 second) before the loop continues to the next attempt. - If the loop finishes without a successful return, it should raise an exception to indicate final failure. Understanding token usage is key to managing costs. Every API response includes a
usageobject that details how many tokens were consumed.
In the
get_response_and_usagefunction:- Make a standard API call to get a completion for the given prompt.
- Instead of returning only the text content, return the entire
responseobject. - After calling this function in the notebook, you can inspect
response.usageto see:response.usage.prompt_tokensresponse.usage.completion_tokensresponse.usage.total_tokens
- The function should attempt to make an API call up to a
About the author
Real skill practice before real-world application
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.