AI

OpenAI Chat Completions API

OpenAI Chat Completions API

The OpenAI Chat Completions API is a powerful tool for developers to integrate the capabilities of OpenAI’s advanced language models(LLMs like GPT-4) into their applications. This API is designed to respond to text inputs.

To understand the API, use the OpenAI platform playground to interact with the model.

Login to the Open AI platform playground.

Choose Chat from the drop-down.

Pick an AI model and adjust the settings.

Submit a user prompt for the model.

 

OpenAI Chat Playground Demo

Chat Completions API

Send a POST request to the following API endpoint.

POST /v1/chat/completions

Prerequisites

Code

Let’s write a simple Python program to invoke the API. The user prompt is a simple math equation to be solved.

“””
OpenAI Chat Completions API Demo
— Python OpenAI SDK
— OpenAI Tutorials — www.TestingDocs.com
“””

from openai import OpenAI
client = OpenAI()

prompt = “3 + 9 = ?”

print(“User Prompt: “,prompt)

# Chat Completions API call
response = client.chat.completions.create(
model=”gpt-4-turbo-preview”,
messages=[
{
“role”: “user”,
“content”: prompt
}
],
temperature=1,
max_tokens=256,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
)

print(“Assistant Response: “,response.choices[0].message.content)

Output

Run the program to invoke the API and see the AI assistant response message.

OpenAI Chat Completions API

That’s it. You successfully ran the API call and got the AI model response.

The Assistants API is the next evolution to this API. However, the Assistants API
is still in the beta phase.