AI Features

Chat Completions

Learn how to generate or manipulate text using the completions endpoint of OpenAI API.

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

Completions API call

In the code segment below, we use the openai library in Python. The following function can be used to call the completions endpoint:

from openai import OpenAI
client = OpenAI(api_key="{{YOUR_SECTEY_KEY}}")
response = client.chat.completions.create(
model="model_id",
messages=[
{"role": "system", "content": "..."},
{"role": "user", "content": "..."}
],
)

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 ...