Search⌘ K
AI Features

Chat Completions

Explore how to use the OpenAI chat completions endpoint to perform various NLP tasks including text generation, classification, and completion. Understand request and response parameters, prompt design principles, and practical API call examples to effectively harness GPT-3.5-Turbo for dynamic text processing in JavaScript applications.

The chat completions endpoint

The chat completions endpoint can be used to perform many tasks on a text, including classification, generation, transformation, completion of incomplete text, factual responses, and others. The endpoint takes an input message from the user and the role assigned to it and returns a JSON object.

Chat completions
Chat completions

The following URL uses the POST method which can be used to call the completions endpoint:

https://api.openai.com/v1/chat/completions

Understanding the chat completions endpoint

Let's look at the chat completions endpoint in more detail, reviewing, the request parameters, and the response parameters.

Request parameters

Let’s see some essential request parameters for this endpoint in the table below:

Fields

Format

Type

Description

messages

Array

Required

This is a list of messages comprising the conversation as of yet.

model

String

Required

This is the ID of the model that the chat completions endpoint will use.

max_tokens

Integer

Optional

This is the maximum number of tokens to generate in the chat completion.

temperature

Float

Optional

Which sampling temperature should be employed, ranging from 0 to 2? The default temperature is set at 1. Opting for higher values, such as 0.8, will increase randomness in the output, whereas lower values, like 0.2, will enhance focus and determinism.

top_p

Float

Optional

Nucleus sampling is an alternative to temperature sampling in which the model evaluates the outcomes of tokens with top p probability mass. So, 0.1 indicates that only the top 10% probability mass tokens will be evaluated.

Default value: 1

response_format

object

Optional

This is an object that specifies the format of the output. This parameter is compatible with the newer models.

Setting to { "type" : "json_object" } will guarantee a valid JSON object.

n

Integer

Optional

It indicates the number of chat completions to generate.

Default value: 1

logprobs

Integer

Optional

This value decides whether to return the log probabilities of output tokens. This option is not available on the gpt-4-vision-preview model.

Default value: false

stop

String/array

Optional

This allows you to provide four sequences where the API will stop generating further tokens.

Default value: null

presence_penalty

Float

Optional

A value range between -2.0 to 2.0. The positive value penalizes the new tokens if they exist in the text, increasing the possibility of generating new things.

Default value: 0

frequency_penalty

Float

Optional

A value range between -2.0 to 2.0. The positive value penalizes the new tokens concerning their existing frequency in the text, decreasing the possibility of repeating the same words.

Default value: 0

Note: You can learn more about the model argument in this lesson.

Response fields

The response is a JSON object. Some essential attributes are given below:

Fields

Format

Description

id

String

This is a unique ID for the chat completion.

choices

Array

This is an array of objects. Every object contains valuable information about the response. The size of the array will be equal to the n parameter that we provided in the request parameters.

model

String

This is the Unix timestamp (in seconds) of when the chat completions was created.

usage

Object

These usage statistics specify the prompt_tokens, completion_tokens, and total_tokens for the chat completion requests.

From these response parameters, the parameter choices contains the output response from the API. Let's look at the choices array in more detail.

Fields

Format

Description

choices[i].finish_reason

String

This provides the reason the model stopped generating tokens. Here are the reasons sent:

stop: This is a natural stop point.

length: The number of tokens specified in the request has been reached.

content_filter: Content is omitted due to a flag generated by a filter.

tool_calls: The model calls a tool.

choices[i].index

Integer

This gives the index of the choice in a list of choices.

choices[i].message

Object

This is the chat completion response message generated by the model.

choices[i].logprobs

Object

This is the log probability of the response choice.

The input to this endpoint is an array that contains the request text message and role for the system. The output is also an array that contains a role and the text message response.

Completions: input and output model
Completions: input and output model

Prompt design

The chat completions endpoint is an array-in and array-out endpoint. We can instruct it on what to do by providing the relevant texts in the array. A well-written prompt will result in good output.

Prompt design
Prompt design

Basics of prompt design

OpenAI can perform many simple and complex tasks in text analysis. To get the most out of it, we have to be very specific about our prompt. We must take particular care about the following points:

  • Proofread the prompt for spelling and grammatical mistakes because that can affect the output.

  • Check for setting parameters such as temperature and top_p.

  • Ensure clarity about the intended generation of the text.

  • Provide one or more examples. (We’ll see this prompt design in the next lessons.)

Let’s look at a few examples by executing various prompts.

Using the chat completions endpoint

Let’s use the chat completions endpoint to write something about artificial intelligence. In the code widget below, we use the GPT-3.5-Turbo model for the chat completions task. The temperature value used is 0.9. Make sure you’ve added your SECRET_KEY. Press the "Run" button to see the response for our prompt.

The following prompt exhibits the generation use case by generating some text corresponding to the provided prompt:

Write a tagline about artificial intelligence.

We may get the following output:

Unlocking the potential of artificial intelligence, one innovation at a time.

Note: The output can be different for each run.

Let’s try out the prompt provided above.

Javascript (babel-node)
// Define endpoint URL here
const endpointUrl = "https://api.openai.com/v1/chat/completions";
// Define Header Parameters here
const headerParameters = {
"Authorization": "Bearer {{SECRET_KEY}}",
"Content-Type": "application/json"
};
// Body Parameters
const bodyParameters = JSON.stringify({
model: "gpt-3.5-turbo",
messages: [{role:"system",content:"You are a helpful assistant"},
{role:"user",content:"Write a tagline about artificial intelligence."}],
temperature: 0.9
});
// Setting API call options
const options = {
method: "POST",
headers: headerParameters,
body: bodyParameters
};
// Function to make API call
async function createCompletion() {
try {
const response = await fetch(`${endpointUrl}`, options);
// Printing response
printResponse(response);
} catch (error) {
// Printing error message
printError(error);
}
}
// Calling function to make API call
createCompletion();

We have imported the node-fetch library to make an API call in the above code. Let’s see some code details:

  • Line 2: We define the endpoint URL.

  • Line 5–8: We define the header, which includes the authorization token and content type.

  • Line 11–15: We define the request parameters required to make the API call.

  • Line 19–23: We set options by specifying the header, body parameters, and the request method as POST.

  • Line 26–35: We create a function createCompletion to make an API call using fetch and handle any exception if it occurs. The printResponse and printError are the custom functions to print the respective objects.

  • Line 37: We invoke the createCompletion function.

Try out different prompts and examine the varying responses. Here are some examples:

"Suggest one name for a cat."
"Suggest two names for a cat."
"Suggest names for a cat."

In the first example, the API will return only one name. In the second example, it will return two names. For the third example, it will return multiple names. We’ll see that the model is great at understanding the context of the sentences, and the messages input plays an important role in this regard. It understands what and how many names are required.