Endpoint
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
Your API key for the specified provider
Body Parameters
The model to use for completion (e.g., gpt-3.5-turbo-instruct, text-davinci-003)
The prompt(s) to generate completions for
Maximum number of tokens to generate
Sampling temperature between 0 and 2
Nucleus sampling parameter
Number of completions to generate
Whether to stream the response
Up to 4 sequences where the API will stop generating
Penalty for token presence (-2.0 to 2.0)
Penalty for token frequency (-2.0 to 2.0)
Text to append after the completion
Echo back the prompt in addition to the completion
Generate multiple completions server-side and return the best
Include log probabilities on the most likely tokens
Unique identifier for the end-user
Response
Unique identifier for the completion
Object type, always text_completion
Unix timestamp of creation
The model used for completion
Array of completion choicesLog probability information (if requested)
Reason for completion: stop, length, or content_filter
Token usage informationNumber of tokens in the prompt
Number of tokens in the completion
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."]
}'