Get Started

Chat completions with GPT

1. Chat completions with GPT

Welcome back! In this video, we'll learn to unlock the chat capabilities from the OpenAI API, which underpin popular applications like ChatGPT. Let's get started!

2. The Chat Completions endpoint

So far, we've used the Chat Completions endpoint to perform a number of tasks, including text generation and transformation, and classification tasks like sentiment analysis. These are so-called single-turn tasks, as there's one input and one output. With Chat Completions models, it's possible to also have multi-turn conversations, so we can build on our previous prompts depending on how the model responds.

3. Roles

Roles are at the heart of how chat models function. Up until this point, we've only used the user role, but there are also system and assistant roles. The system role allows us to specify a message to control the behavior of the assistant. For example, for a customer service chatbot, we could provide a system message stating that the assistant is a polite and helpful customer service assistant. The user role is used to provide an instruction to the assistant, and the assistant role is attached to model responses. Interestingly, we, the user, can provide these assistant messages, which are often utilized to provide example responses to help the model understand the desired output. We'll discuss multi-turn conversations in the next video; for now, we'll get familiar with using chat roles for single-turn tasks.

4. Request setup

Here's a typical Chat Completions request that we've used for the single-turn tasks. We've passed the messages argument a list containing a dictionary with "role" and "content" keys, which has been a prompt sent from the "user" role.

5. Prompt setup

To include additional messages, we extend the list to include multiple dictionaries each with their own role and content. These messages often start with the system role, which here, instructs the assistant to act as a data science tutor that speaks concisely.

6. Making a request

Let's add these messages into the request code and print the response text using the same combination of attributes we've used previously.

7. The response

We can see that the assistant stayed true to the system message - only using a single sentence in its concise explanation.

8. Let's practice!

In the next video, you'll learn how to extend this to multi-turn tasks, including adding assistant messages, but for now, time to practice!