Skip to content

Generate legacy text completion from prompt

client.Completions.New(ctx, body) (*Completion, error)
POST/v5/completions

Generates a legacy text completion from a raw prompt (a string or list of strings).

Use this endpoint for non-chat, prompt-in/text-out inference using the OpenAI text-completion contract; use /v5/chat/completions when you have a structured messages array, /v5/responses for the OpenAI Responses API, and /v5/inference for payloads that follow no OpenAI schema. The model is selected from model given as vendor/name and routed to the matching per-vendor gateway. When stream is set the response is delivered as server-sent events; otherwise a single text_completion object is returned. Token usage is recorded for the account, read from the final chunk on streaming responses.

ParametersExpand Collapse
body CompletionNewParams
Model param.Field[string]

model specified as model_vendor/model, for example openai/gpt-4o

Prompt param.Field[CompletionNewParamsPromptUnion]

The prompt to generate completions for, encoded as a string

string
[]string
BestOf param.Field[int64]Optional

Generates best_of completions server-side and returns the best one. Must be greater than n when used together.

Echo param.Field[bool]Optional

Echo back the prompt in addition to the completion

FrequencyPenalty param.Field[float64]Optional

Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text.

LogitBias param.Field[map[string, int64]]Optional

Modify the likelihood of specified tokens appearing in the completion. Maps tokens to bias values from -100 to 100.

Logprobs param.Field[int64]Optional

Include log probabilities of the most likely tokens. Maximum value is 5.

MaxTokens param.Field[int64]Optional

The maximum number of tokens that can be generated in the completion.

N param.Field[int64]Optional

How many completions to generate for each prompt.

PresencePenalty param.Field[float64]Optional

Number between -2.0 and 2.0. Positive values penalize new tokens based on their presence in the text so far.

Seed param.Field[int64]Optional

If specified, attempts to generate deterministic samples. Determinism is not guaranteed.

Stop param.Field[CompletionNewParamsStopUnion]Optional

Up to 4 sequences where the API will stop generating further tokens.

string
[]string
StreamOptions param.Field[map[string, any]]Optional

Options for streaming response. Only set this when stream is True.

Suffix param.Field[string]Optional

The suffix that comes after a completion of inserted text. Only supported for gpt-3.5-turbo-instruct.

Temperature param.Field[float64]Optional

Sampling temperature between 0 and 2. Higher values make output more random, lower more focused.

TopP param.Field[float64]Optional

Alternative to temperature. Consider only tokens with top_p probability mass. Range 0-1.

User param.Field[string]Optional

A unique identifier representing your end-user, which can help OpenAI monitor and detect abuse.

ReturnsExpand Collapse
type Completion struct{…}
ID string
Choices []CompletionChoice
FinishReason string
One of the following:
const CompletionChoiceFinishReasonStop CompletionChoiceFinishReason = "stop"
const CompletionChoiceFinishReasonLength CompletionChoiceFinishReason = "length"
const CompletionChoiceFinishReasonContentFilter CompletionChoiceFinishReason = "content_filter"
Index int64
Text string
Logprobs CompletionChoiceLogprobsOptional
TextOffset []int64Optional
TokenLogprobs []float64Optional
Tokens []stringOptional
TopLogprobs []map[string, float64]Optional
Created int64
Model string
Object CompletionObjectOptional
SystemFingerprint stringOptional
Usage CompletionUsageOptional

Usage statistics for the completion request.

CompletionTokens int64
PromptTokens int64
TotalTokens int64
CompletionTokensDetails CompletionUsageCompletionTokensDetailsOptional

Breakdown of tokens used in a completion.

AcceptedPredictionTokens int64Optional
AudioTokens int64Optional
ReasoningTokens int64Optional
RejectedPredictionTokens int64Optional
PromptTokensDetails CompletionUsagePromptTokensDetailsOptional

Breakdown of tokens used in the prompt.

AudioTokens int64Optional
CachedTokens int64Optional
type Completion struct{…}
ID string
Choices []CompletionChoice
FinishReason string
One of the following:
const CompletionChoiceFinishReasonStop CompletionChoiceFinishReason = "stop"
const CompletionChoiceFinishReasonLength CompletionChoiceFinishReason = "length"
const CompletionChoiceFinishReasonContentFilter CompletionChoiceFinishReason = "content_filter"
Index int64
Text string
Logprobs CompletionChoiceLogprobsOptional
TextOffset []int64Optional
TokenLogprobs []float64Optional
Tokens []stringOptional
TopLogprobs []map[string, float64]Optional
Created int64
Model string
Object CompletionObjectOptional
SystemFingerprint stringOptional
Usage CompletionUsageOptional

Usage statistics for the completion request.

CompletionTokens int64
PromptTokens int64
TotalTokens int64
CompletionTokensDetails CompletionUsageCompletionTokensDetailsOptional

Breakdown of tokens used in a completion.

AcceptedPredictionTokens int64Optional
AudioTokens int64Optional
ReasoningTokens int64Optional
RejectedPredictionTokens int64Optional
PromptTokensDetails CompletionUsagePromptTokensDetailsOptional

Breakdown of tokens used in the prompt.

AudioTokens int64Optional
CachedTokens int64Optional

Generate legacy text completion from prompt

package main

import (
  "context"
  "fmt"

  "github.com/scaleapi/sgp-dev-go"
  "github.com/scaleapi/sgp-dev-go/option"
)

func main() {
  client := sgpdev.NewClient(
    option.WithAPIKey("My API Key"),
    option.WithAccountID("My Account ID"),
  )
  completion, err := client.Completions.New(context.TODO(), sgpdev.CompletionNewParams{
    Model: "model",
    Prompt: sgpdev.CompletionNewParamsPromptUnion{
      OfString: sgpdev.String("string"),
    },
  })
  if err != nil {
    panic(err.Error())
  }
  fmt.Printf("%+v\n", completion.ID)
}
{
  "id": "id",
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "text": "text",
      "logprobs": {
        "text_offset": [
          0
        ],
        "token_logprobs": [
          0
        ],
        "tokens": [
          "string"
        ],
        "top_logprobs": [
          {
            "foo": 0
          }
        ]
      }
    }
  ],
  "created": 0,
  "model": "model",
  "object": "text_completion",
  "system_fingerprint": "system_fingerprint",
  "usage": {
    "completion_tokens": 0,
    "prompt_tokens": 0,
    "total_tokens": 0,
    "completion_tokens_details": {
      "accepted_prediction_tokens": 0,
      "audio_tokens": 0,
      "reasoning_tokens": 0,
      "rejected_prediction_tokens": 0
    },
    "prompt_tokens_details": {
      "audio_tokens": 0,
      "cached_tokens": 0
    }
  }
}
Returns Examples
{
  "id": "id",
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "text": "text",
      "logprobs": {
        "text_offset": [
          0
        ],
        "token_logprobs": [
          0
        ],
        "tokens": [
          "string"
        ],
        "top_logprobs": [
          {
            "foo": 0
          }
        ]
      }
    }
  ],
  "created": 0,
  "model": "model",
  "object": "text_completion",
  "system_fingerprint": "system_fingerprint",
  "usage": {
    "completion_tokens": 0,
    "prompt_tokens": 0,
    "total_tokens": 0,
    "completion_tokens_details": {
      "accepted_prediction_tokens": 0,
      "audio_tokens": 0,
      "reasoning_tokens": 0,
      "rejected_prediction_tokens": 0
    },
    "prompt_tokens_details": {
      "audio_tokens": 0,
      "cached_tokens": 0
    }
  }
}