OpenAI Completions API [ 2023 ]
OpenAI Completions API
The OpenAI Completions API is a RESTful API that allows developers to send prompts
to the model and receive text completions. With this API, developers can customize the model’s behavior, such as picking an AI model (GPT-3.5, GPT-4), setting the maximum length of the generated text, etc. The API also allows the customization and fine-tuning of model parameters like temperature and top_p.
We can use the OpenAI platform playground to test the API visually. Follow the steps to test the API:
Login to the OpenAI Platform.
Click Playground >> Completions left nav option.
https://platform.openai.com/playground/complete
Enter the prompt and pick an AI model from the Model drop-down.
Adjust the model settings.
Click on the Submit button to get the model response.
OpenAI Completions API
Send a POST request to the following API endpoint with your prompt and parameters in the request body. The API splits text into tokens. The model processes your prompt and returns a response containing the generated text.
POST /v1/completions
Example
Let’s learn how to invoke the API using Python code. This example requires OpenAI Python SDK setup and API Keys to interact with the AI model.
In this API call, we will pick the gpt-3.5-turbo model. The temperature setting is 1.0,
max_tokens is 256 and the top_p setting is 1.0.
Python Code
“””
OpenAI Completions API Request
— Python OpenAI SDK
— OpenAI Tutorials — www.TestingDocs.com
“””
from openai import OpenAI
client = OpenAI()
response = client.completions.create(
model=”gpt-3.5-turbo-instruct”,
prompt=”TestingDocs prompt”,
temperature=1,
max_tokens=256,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
Output
Run the code to view the model output.
Stop sequence:
If the model predicts a completion that begins with a stop sequence, the model will have no output response.
Completions API is legacy and is replaced by Chat Completions API.
—
OpenAI API Tutorials
OpenAI tutorials on this website can be found at:
OpenAI website