POST
/v1/completions

Completions

Generate text completions for prompts using the legacy completions format

Request

Example Requestbash
curl https://api.parrotrouter.com/v1/completions \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-3.5-turbo-instruct",
    "prompt": "Write a tagline for an ice cream shop:",
    "temperature": 0.7,
    "max_tokens": 50,
    "n": 3
  }'

Parameters

model
required
string

ID of the model to use. Must be a model that supports the completions endpoint.

prompt
required
string or array

The prompt(s) to generate completions for. Can be a string or an array of strings.

// Single prompt
"prompt": "Once upon a time"

// Multiple prompts
"prompt": [
  "Write a poem about AI",
  "Explain quantum physics",
  "Recipe for chocolate cake"
]
suffix
optional
string

The suffix that comes after a completion of inserted text. Used for inserting completions within text.

{
  "prompt": "The quick brown",
  "suffix": "jumps over the lazy dog",
  "max_tokens": 10
}
echo
optional
boolean

Echo back the prompt in addition to the completion. Default: false

best_of
optional
integer

Generates best_of completions server-side and returns the "best" (with lowest log probability per token). Results cannot be streamed. Must be greater than n. Default: 1

logprobs
optional
integer

Include the log probabilities on the logprobs most likely tokens. Maximum value: 5

Response

Successful Responsejson
{
  "id": "cmpl-abc123",
  "object": "text_completion",
  "created": 1699000000,
  "model": "gpt-3.5-turbo-instruct",
  "choices": [
    {
      "text": "\n\nSweet dreams are made of cream!",
      "index": 0,
      "logprobs": null,
      "finish_reason": "stop"
    },
    {
      "text": "\n\nChill out with our cool treats!",
      "index": 1,
      "logprobs": null,
      "finish_reason": "stop"
    },
    {
      "text": "\n\nLife is short, eat dessert first!",
      "index": 2,
      "logprobs": null,
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 9,
    "completion_tokens": 36,
    "total_tokens": 45
  }
}

Response Fields

  • id - Unique identifier for the completion
  • object - Always "text_completion"
  • created - Unix timestamp of when the completion was created
  • model - The model used for the completion
  • choices - Array of completion choices
  • usage - Token usage statistics

Streaming Response

When stream: true, the response is sent as Server-Sent Events:

Stream Responsetext
data: {"id":"cmpl-abc123","object":"text_completion","created":1699000000,"choices":[{"text":" Sweet","index":0,"logprobs":null,"finish_reason":null}],"model":"gpt-3.5-turbo-instruct"}

data: {"id":"cmpl-abc123","object":"text_completion","created":1699000000,"choices":[{"text":" dreams","index":0,"logprobs":null,"finish_reason":null}],"model":"gpt-3.5-turbo-instruct"}

data: {"id":"cmpl-abc123","object":"text_completion","created":1699000000,"choices":[{"text":" are","index":0,"logprobs":null,"finish_reason":null}],"model":"gpt-3.5-turbo-instruct"}

data: [DONE]

Code Examples

Pythonpython
from openai import OpenAI

client = OpenAI(
    base_url="https://api.parrotrouter.com/v1",
    api_key="YOUR_API_KEY"
)

response = client.completions.create(
    model="gpt-3.5-turbo-instruct",
    prompt="Write a tagline for an ice cream shop:",
    temperature=0.7,
    max_tokens=50,
    n=3
)

for choice in response.choices:
    print(choice.text)
TypeScripttypescript
import OpenAI from 'openai';

const client = new OpenAI({
  baseURL: 'https://api.parrotrouter.com/v1',
  apiKey: 'YOUR_API_KEY',
});

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

response.choices.forEach(choice => {
  console.log(choice.text);
});
Streaming with cURLbash
curl https://api.parrotrouter.com/v1/completions \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-3.5-turbo-instruct",
    "prompt": "Tell me a story about a robot",
    "max_tokens": 200,
    "stream": true
  }'

Use Cases

Text Generation

{
  "model": "gpt-3.5-turbo-instruct",
  "prompt": "Write a product description for wireless headphones:",
  "max_tokens": 150,
  "temperature": 0.8
}

Code Completion

{
  "model": "gpt-3.5-turbo-instruct",
  "prompt": "def fibonacci(n):\n    # Calculate the nth Fibonacci number\n",
  "max_tokens": 100,
  "temperature": 0.2,
  "stop": ["\n\n"]
}

Text Insertion

{
  "model": "gpt-3.5-turbo-instruct",
  "prompt": "The weather today is",
  "suffix": "so I decided to stay indoors.",
  "max_tokens": 20,
  "temperature": 0.7
}

Error Handling

Common errors and how to handle them:

Model Not Foundjson
{
  "error": {
    "message": "The model 'invalid-model' does not exist or you do not have access to it.",
    "type": "invalid_request_error",
    "code": "model_not_found"
  }
}
Invalid Promptjson
{
  "error": {
    "message": "You must provide a prompt",
    "type": "invalid_request_error",
    "code": "invalid_prompt"
  }
}

Migration Guide

Legacy Completion

{
  "model": "gpt-3.5-turbo-instruct",
  "prompt": "Translate to French: Hello",
  "max_tokens": 50
}

Chat Completion

{
  "model": "gpt-4",
  "messages": [
    {
      "role": "user",
      "content": "Translate to French: Hello"
    }
  ],
  "max_tokens": 50
}

Related