Skip to main content

Endpoint

POST /v1/completions
Creates a completion for the provided prompt using the specified model.
The completions endpoint is considered legacy. Use the Chat Completions endpoint for new applications.

Request

Headers

Content-Type
string
required
Must be application/json
x-portkey-provider
string
required
The AI provider to use
x-portkey-api-key
string
required
Your API key for the specified provider

Body Parameters

model
string
required
The model to use for completion (e.g., gpt-3.5-turbo-instruct, text-davinci-003)
prompt
string | array
required
The prompt(s) to generate completions for
max_tokens
integer
default:16
Maximum number of tokens to generate
temperature
number
default:1
Sampling temperature between 0 and 2
top_p
number
default:1
Nucleus sampling parameter
n
integer
default:1
Number of completions to generate
stream
boolean
default:false
Whether to stream the response
stop
string | array
Up to 4 sequences where the API will stop generating
presence_penalty
number
default:0
Penalty for token presence (-2.0 to 2.0)
frequency_penalty
number
default:0
Penalty for token frequency (-2.0 to 2.0)
suffix
string
Text to append after the completion
echo
boolean
default:false
Echo back the prompt in addition to the completion
best_of
integer
default:1
Generate multiple completions server-side and return the best
logprobs
integer
Include log probabilities on the most likely tokens
user
string
Unique identifier for the end-user

Response

id
string
Unique identifier for the completion
object
string
Object type, always text_completion
created
integer
Unix timestamp of creation
model
string
The model used for completion
choices
array
Array of completion choices
text
string
The generated text
index
integer
Choice index
logprobs
object
Log probability information (if requested)
finish_reason
string
Reason for completion: stop, length, or content_filter
usage
object
Token usage information
prompt_tokens
integer
Number of tokens in the prompt
completion_tokens
integer
Number of tokens in the completion
total_tokens
integer
Total tokens used

Examples

Basic Completion

curl http://localhost:8787/v1/completions \
  -H "Content-Type: application/json" \
  -H "x-portkey-provider: openai" \
  -H "x-portkey-api-key: sk-..." \
  -d '{
    "model": "gpt-3.5-turbo-instruct",
    "prompt": "Write a tagline for an ice cream shop.",
    "max_tokens": 20
  }'

Response

{
  "id": "cmpl-123",
  "object": "text_completion",
  "created": 1677652288,
  "model": "gpt-3.5-turbo-instruct",
  "choices": [{
    "text": " Sweet treats for every occasion!",
    "index": 0,
    "logprobs": null,
    "finish_reason": "stop"
  }],
  "usage": {
    "prompt_tokens": 10,
    "completion_tokens": 7,
    "total_tokens": 17
  }
}

Python SDK

from portkey_ai import Portkey

client = Portkey(
    provider="openai",
    Authorization="sk-..."
)

response = client.completions.create(
    model="gpt-3.5-turbo-instruct",
    prompt="Write a tagline for an ice cream shop.",
    max_tokens=20
)

print(response.choices[0].text)

JavaScript SDK

import Portkey from 'portkey-ai';

const client = new Portkey({
  provider: 'openai',
  Authorization: 'sk-...'
});

const response = await client.completions.create({
  model: 'gpt-3.5-turbo-instruct',
  prompt: 'Write a tagline for an ice cream shop.',
  max_tokens: 20
});

console.log(response.choices[0].text);

Multiple Completions

curl http://localhost:8787/v1/completions \
  -H "Content-Type: application/json" \
  -H "x-portkey-provider: openai" \
  -H "x-portkey-api-key: sk-..." \
  -d '{
    "model": "gpt-3.5-turbo-instruct",
    "prompt": "Complete this sentence: The future of AI is",
    "max_tokens": 30,
    "n": 3
  }'

Streaming Completion

from portkey_ai import Portkey

client = Portkey(
    provider="openai",
    Authorization="sk-..."
)

stream = client.completions.create(
    model="gpt-3.5-turbo-instruct",
    prompt="Write a short poem about coding",
    max_tokens=50,
    stream=True
)

for chunk in stream:
    if chunk.choices[0].text:
        print(chunk.choices[0].text, end="", flush=True)

With Stop Sequences

curl http://localhost:8787/v1/completions \
  -H "Content-Type: application/json" \
  -H "x-portkey-provider: openai" \
  -H "x-portkey-api-key: sk-..." \
  -d '{
    "model": "gpt-3.5-turbo-instruct",
    "prompt": "List three programming languages:\n1.",
    "max_tokens": 50,
    "stop": ["\n4."]
  }'

Build docs developers (and LLMs) love