Skip to main content
The Completions API is a legacy endpoint for generating text completions. For new projects, we recommend using the Chat Completions API instead.
This is a legacy endpoint. We recommend using the Chat Completions API for new projects, which provides more capabilities and better performance.

Create a completion

Creates a completion for the provided prompt and parameters.
client.completions.create(params)
model
String
required
ID of the model to use. You can use the List models API to see available models.
prompt
String | Array
required
The prompt(s) to generate completions for, encoded as a string, array of strings, array of tokens, or array of token arrays.
max_tokens
Integer
default:"16"
The maximum number of tokens that can be generated in the completion.
temperature
Float
default:"1.0"
Sampling temperature between 0 and 2. Higher values make output more random, lower values make it more focused.
top_p
Float
default:"1.0"
An alternative to sampling with temperature, called nucleus sampling. The model considers the results of the tokens with top_p probability mass.
n
Integer
default:"1"
How many completions to generate for each prompt.
stream
Boolean
default:"false"
Whether to stream back partial progress. For streaming, use create_streaming method instead.
logprobs
Integer
Include the log probabilities on the logprobs most likely output tokens, as well the chosen tokens.
echo
Boolean
default:"false"
Echo back the prompt in addition to the completion.
stop
String | Array<String>
Up to 4 sequences where the API will stop generating further tokens.
presence_penalty
Float
default:"0"
Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far.
frequency_penalty
Float
default:"0"
Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text.
best_of
Integer
Generates best_of completions server-side and returns the “best” (the one with the highest log probability per token).
logit_bias
Hash
Modify the likelihood of specified tokens appearing in the completion. Maps token IDs to bias values from -100 to 100.
suffix
String
The suffix that comes after a completion of inserted text.
seed
Integer
If specified, the system will make a best effort to sample deterministically for improved reproducibility.
user
String
A unique identifier representing your end-user, which can help OpenAI monitor and detect abuse.

Response

Returns a Completion object.
id
String
Unique identifier for the completion.
object
String
The object type, always text_completion.
created
Integer
Unix timestamp of when the completion was created.
model
String
The model used for completion.
choices
Array
A list of completion choices.
usage
Object
Token usage statistics.

Examples

Basic completion

require "openai"

client = OpenAI::Client.new

completion = client.completions.create(
  model: "gpt-3.5-turbo-instruct",
  prompt: "Once upon a time",
  max_tokens: 100,
  temperature: 0.7
)

puts completion.choices.first.text

Streaming completion

Use create_streaming for Server-Sent Events streaming:
require "openai"

client = OpenAI::Client.new

stream = client.completions.create_streaming(
  model: "gpt-3.5-turbo-instruct",
  prompt: "Write a short poem about Ruby:",
  max_tokens: 100
)

stream.each do |chunk|
  print chunk.choices.first&.text
end
puts

Multiple completions

completion = client.completions.create(
  model: "gpt-3.5-turbo-instruct",
  prompt: "Suggest a creative project name:",
  n: 3,
  max_tokens: 10
)

completion.choices.each do |choice|
  puts "Option #{choice.index + 1}: #{choice.text}"
end

With stop sequences

completion = client.completions.create(
  model: "gpt-3.5-turbo-instruct",
  prompt: "List of programming languages:\n1.",
  stop: ["\n\n", "10."],
  max_tokens: 100
)

puts completion.choices.first.text

Migration to Chat Completions

For new projects, we recommend using the Chat Completions API. Here’s how to migrate:
completion = client.completions.create(
  model: "gpt-3.5-turbo-instruct",
  prompt: "Say this is a test"
)

puts completion.choices.first.text

Build docs developers (and LLMs) love