SPB Git

Repository Public

completions.md 20.5 KB 669 lines markdown

1# Completions23> For the complete documentation index, see [llms.txt](/llms.txt). Markdown versions of documentation pages are available by appending `.md` to the page URL.45## Create completion67**post** `/completions`89Creates a completion for the provided prompt and parameters.1011Returns a completion object, or a sequence of completion objects if the request is streamed.1213### Body Parameters1415- `model: string or "gpt-3.5-turbo-instruct" or "davinci-002" or "babbage-002"`1617  ID of the model to use. You can use the [List models](/api/reference/resources/models/methods/list) API to see all of your available models, or see our [Model overview](/api/docs/models) for descriptions of them.1819  - `string`2021  - `"gpt-3.5-turbo-instruct" or "davinci-002" or "babbage-002"`2223    ID of the model to use. You can use the [List models](/api/reference/resources/models/methods/list) API to see all of your available models, or see our [Model overview](/api/docs/models) for descriptions of them.2425    - `"gpt-3.5-turbo-instruct"`2627    - `"davinci-002"`2829    - `"babbage-002"`3031- `prompt: string or array of string or array of number or array of array of number or null`3233  The prompt(s) to generate completions for, encoded as a string, array of strings, array of tokens, or array of token arrays.3435  Note that <|endoftext|> is the document separator that the model sees during training, so if a prompt is not specified the model will generate as if from the beginning of a new document.3637  - `string`3839  - `array of string`4041  - `array of number`4243  - `array of array of number`4445- `best_of: optional number or null`4647  Generates `best_of` completions server-side and returns the "best" (the one with the highest log probability per token). Results cannot be streamed.4849  When used with `n`, `best_of` controls the number of candidate completions and `n` specifies how many to return – `best_of` must be greater than `n`.5051  **Note:** Because this parameter generates many completions, it can quickly consume your token quota. Use carefully and ensure that you have reasonable settings for `max_tokens` and `stop`.5253- `echo: optional boolean or null`5455  Echo back the prompt in addition to the completion5657- `frequency_penalty: optional number or null`5859  Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim.6061  [See more information about frequency and presence penalties.](/api/docs/guides/text)6263- `logit_bias: optional map[number] or null`6465  Modify the likelihood of specified tokens appearing in the completion.6667  Accepts a JSON object that maps tokens (specified by their token ID in the GPT tokenizer) to an associated bias value from -100 to 100. You can use this [tokenizer tool](https://platform.openai.com/tokenizer?view=bpe) to convert text to token IDs. Mathematically, the bias is added to the logits generated by the model prior to sampling. The exact effect will vary per model, but values between -1 and 1 should decrease or increase likelihood of selection; values like -100 or 100 should result in a ban or exclusive selection of the relevant token.6869  As an example, you can pass `{"50256": -100}` to prevent the <|endoftext|> token from being generated.7071- `logprobs: optional number or null`7273  Include the log probabilities on the `logprobs` most likely output tokens, as well the chosen tokens. For example, if `logprobs` is 5, the API will return a list of the 5 most likely tokens. The API will always return the `logprob` of the sampled token, so there may be up to `logprobs+1` elements in the response.7475  The maximum value for `logprobs` is 5.7677- `max_tokens: optional number or null`7879  The maximum number of [tokens](https://platform.openai.com/tokenizer) that can be generated in the completion.8081  The token count of your prompt plus `max_tokens` cannot exceed the model's context length. [Example Python code](https://cookbook.openai.com/examples/how_to_count_tokens_with_tiktoken) for counting tokens.8283- `n: optional number or null`8485  How many completions to generate for each prompt.8687  **Note:** Because this parameter generates many completions, it can quickly consume your token quota. Use carefully and ensure that you have reasonable settings for `max_tokens` and `stop`.8889- `presence_penalty: optional number or null`9091  Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics.9293  [See more information about frequency and presence penalties.](/api/docs/guides/text)9495- `seed: optional number or null`9697  If specified, our system will make a best effort to sample deterministically, such that repeated requests with the same `seed` and parameters should return the same result.9899  Determinism is not guaranteed, and you should refer to the `system_fingerprint` response parameter to monitor changes in the backend.100101- `stop: optional string or array of string or null`102103  Not supported with latest reasoning models `o3` and `o4-mini`.104105  Up to 4 sequences where the API will stop generating further tokens. The106  returned text will not contain the stop sequence.107108  - `string`109110  - `array of string`111112- `stream: optional boolean or null`113114  Whether to stream back partial progress. If set, tokens will be sent as data-only [server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format) as they become available, with the stream terminated by a `data: [DONE]` message. [Example Python code](https://cookbook.openai.com/examples/how_to_stream_completions).115116- `stream_options: optional ChatCompletionStreamOptions or null`117118  Options for streaming response. Only set this when you set `stream: true`.119120  - `include_obfuscation: optional boolean`121122    When true, stream obfuscation will be enabled. Stream obfuscation adds123    random characters to an `obfuscation` field on streaming delta events to124    normalize payload sizes as a mitigation to certain side-channel attacks.125    These obfuscation fields are included by default, but add a small amount126    of overhead to the data stream. You can set `include_obfuscation` to127    false to optimize for bandwidth if you trust the network links between128    your application and the OpenAI API.129130  - `include_usage: optional boolean`131132    If set, an additional chunk will be streamed before the `data: [DONE]`133    message. The `usage` field on this chunk shows the token usage statistics134    for the entire request, and the `choices` field will always be an empty135    array.136137    All other chunks will also include a `usage` field, but with a null138    value. **NOTE:** If the stream is interrupted, you may not receive the139    final usage chunk which contains the total token usage for the request.140141- `suffix: optional string or null`142143  The suffix that comes after a completion of inserted text.144145  This parameter is only supported for `gpt-3.5-turbo-instruct`.146147- `temperature: optional number or null`148149  What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.150151  We generally recommend altering this or `top_p` but not both.152153- `top_p: optional number or null`154155  An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered.156157  We generally recommend altering this or `temperature` but not both.158159- `user: optional string`160161  A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. [Learn more](/api/docs/guides/safety-best-practices#implement-safety-identifiers).162163### Returns164165- `Completion object { id, choices, created, 4 more }`166167  Represents a completion response from the API. Note: both the streamed and non-streamed response objects share the same shape (unlike the chat endpoint).168169  - `id: string`170171    A unique identifier for the completion.172173  - `choices: array of CompletionChoice`174175    The list of completion choices the model generated for the input prompt.176177    - `finish_reason: "stop" or "length" or "content_filter"`178179      The reason the model stopped generating tokens. This will be `stop` if the model hit a natural stop point or a provided stop sequence,180      `length` if the maximum number of tokens specified in the request was reached,181      or `content_filter` if content was omitted due to a flag from our content filters.182183      - `"stop"`184185      - `"length"`186187      - `"content_filter"`188189    - `index: number`190191    - `logprobs: object { text_offset, token_logprobs, tokens, top_logprobs }  or null`192193      - `text_offset: optional array of number`194195      - `token_logprobs: optional array of number`196197      - `tokens: optional array of string`198199      - `top_logprobs: optional array of map[number]`200201    - `text: string`202203  - `created: number`204205    The Unix timestamp (in seconds) of when the completion was created.206207  - `model: string`208209    The model used for completion.210211  - `object: "text_completion"`212213    The object type, which is always "text_completion"214215    - `"text_completion"`216217  - `system_fingerprint: optional string`218219    This fingerprint represents the backend configuration that the model runs with.220221    Can be used in conjunction with the `seed` request parameter to understand when backend changes have been made that might impact determinism.222223  - `usage: optional CompletionUsage`224225    Usage statistics for the completion request.226227    - `completion_tokens: number`228229      Number of tokens in the generated completion.230231    - `prompt_tokens: number`232233      Number of tokens in the prompt.234235    - `total_tokens: number`236237      Total number of tokens used in the request (prompt + completion).238239    - `completion_tokens_details: optional object { accepted_prediction_tokens, audio_tokens, reasoning_tokens, 2 more }`240241      Breakdown of tokens used in a completion.242243      - `accepted_prediction_tokens: optional number`244245        When using Predicted Outputs, the number of tokens in the246        prediction that appeared in the completion.247248      - `audio_tokens: optional number`249250        Audio input tokens generated by the model.251252      - `reasoning_tokens: optional number`253254        Tokens generated by the model for reasoning.255256      - `rejected_prediction_tokens: optional number`257258        When using Predicted Outputs, the number of tokens in the259        prediction that did not appear in the completion. However, like260        reasoning tokens, these tokens are still counted in the total261        completion tokens for purposes of billing, output, and context window262        limits.263264      - `text_tokens: optional number`265266        Text output tokens generated by the model.267268    - `prompt_tokens_details: optional object { audio_tokens, cache_write_tokens, cached_tokens, 2 more }`269270      Breakdown of tokens used in the prompt.271272      - `audio_tokens: optional number`273274        Audio input tokens present in the prompt.275276      - `cache_write_tokens: optional number`277278        The unadjusted number of prompt tokens written to cache.279280      - `cached_tokens: optional number`281282        Cached tokens present in the prompt.283284      - `image_tokens: optional number`285286        Image input tokens present in the prompt.287288      - `text_tokens: optional number`289290        Text input tokens present in the prompt.291292### Example293294```http295curl https://api.openai.com/v1/completions \296    -H 'Content-Type: application/json' \297    -H "Authorization: Bearer $OPENAI_API_KEY" \298    -d '{299          "model": "gpt-3.5-turbo-instruct",300          "prompt": "This is a test.",301          "max_tokens": 16,302          "n": 1,303          "suffix": "test.",304          "temperature": 1,305          "top_p": 1,306          "user": "user-1234"307        }'308```309310#### Response311312```json313{314  "id": "id",315  "choices": [316    {317      "finish_reason": "stop",318      "index": 0,319      "logprobs": {320        "text_offset": [321          0322        ],323        "token_logprobs": [324          0325        ],326        "tokens": [327          "string"328        ],329        "top_logprobs": [330          {331            "foo": 0332          }333        ]334      },335      "text": "text"336    }337  ],338  "created": 0,339  "model": "model",340  "object": "text_completion",341  "system_fingerprint": "system_fingerprint",342  "usage": {343    "completion_tokens": 0,344    "prompt_tokens": 0,345    "total_tokens": 0,346    "completion_tokens_details": {347      "accepted_prediction_tokens": 0,348      "audio_tokens": 0,349      "reasoning_tokens": 0,350      "rejected_prediction_tokens": 0,351      "text_tokens": 0352    },353    "prompt_tokens_details": {354      "audio_tokens": 0,355      "cache_write_tokens": 0,356      "cached_tokens": 0,357      "image_tokens": 0,358      "text_tokens": 0359    }360  }361}362```363364### No streaming365366```http367curl https://api.openai.com/v1/completions \368  -H "Content-Type: application/json" \369  -H "Authorization: Bearer $OPENAI_API_KEY" \370  -d '{371    "model": "gpt-3.5-turbo-instruct",372    "prompt": "Say this is a test",373    "max_tokens": 7,374    "temperature": 0375  }'376```377378#### Response379380```json381{382  "id": "cmpl-uqkvlQyYK7bGYrRHQ0eXlWi7",383  "object": "text_completion",384  "created": 1589478378,385  "model": "gpt-3.5-turbo-instruct",386  "system_fingerprint": "fp_44709d6fcb",387  "choices": [388    {389      "text": "\n\nThis is indeed a test",390      "index": 0,391      "logprobs": null,392      "finish_reason": "length"393    }394  ],395  "usage": {396    "prompt_tokens": 5,397    "completion_tokens": 7,398    "total_tokens": 12399  }400}401```402403### Streaming404405```http406curl https://api.openai.com/v1/completions \407  -H "Content-Type: application/json" \408  -H "Authorization: Bearer $OPENAI_API_KEY" \409  -d '{410    "model": "gpt-3.5-turbo-instruct",411    "prompt": "Say this is a test",412    "max_tokens": 7,413    "temperature": 0,414    "stream": true415  }'416```417418#### Response419420```json421{422  "id": "cmpl-7iA7iJjj8V2zOkCGvWF2hAkDWBQZe",423  "object": "text_completion",424  "created": 1690759702,425  "choices": [426    {427      "text": "This",428      "index": 0,429      "logprobs": null,430      "finish_reason": null431    }432  ],433  "model": "gpt-3.5-turbo-instruct"434  "system_fingerprint": "fp_44709d6fcb",435}436```437438## Domain Types439440### Completion441442- `Completion object { id, choices, created, 4 more }`443444  Represents a completion response from the API. Note: both the streamed and non-streamed response objects share the same shape (unlike the chat endpoint).445446  - `id: string`447448    A unique identifier for the completion.449450  - `choices: array of CompletionChoice`451452    The list of completion choices the model generated for the input prompt.453454    - `finish_reason: "stop" or "length" or "content_filter"`455456      The reason the model stopped generating tokens. This will be `stop` if the model hit a natural stop point or a provided stop sequence,457      `length` if the maximum number of tokens specified in the request was reached,458      or `content_filter` if content was omitted due to a flag from our content filters.459460      - `"stop"`461462      - `"length"`463464      - `"content_filter"`465466    - `index: number`467468    - `logprobs: object { text_offset, token_logprobs, tokens, top_logprobs }  or null`469470      - `text_offset: optional array of number`471472      - `token_logprobs: optional array of number`473474      - `tokens: optional array of string`475476      - `top_logprobs: optional array of map[number]`477478    - `text: string`479480  - `created: number`481482    The Unix timestamp (in seconds) of when the completion was created.483484  - `model: string`485486    The model used for completion.487488  - `object: "text_completion"`489490    The object type, which is always "text_completion"491492    - `"text_completion"`493494  - `system_fingerprint: optional string`495496    This fingerprint represents the backend configuration that the model runs with.497498    Can be used in conjunction with the `seed` request parameter to understand when backend changes have been made that might impact determinism.499500  - `usage: optional CompletionUsage`501502    Usage statistics for the completion request.503504    - `completion_tokens: number`505506      Number of tokens in the generated completion.507508    - `prompt_tokens: number`509510      Number of tokens in the prompt.511512    - `total_tokens: number`513514      Total number of tokens used in the request (prompt + completion).515516    - `completion_tokens_details: optional object { accepted_prediction_tokens, audio_tokens, reasoning_tokens, 2 more }`517518      Breakdown of tokens used in a completion.519520      - `accepted_prediction_tokens: optional number`521522        When using Predicted Outputs, the number of tokens in the523        prediction that appeared in the completion.524525      - `audio_tokens: optional number`526527        Audio input tokens generated by the model.528529      - `reasoning_tokens: optional number`530531        Tokens generated by the model for reasoning.532533      - `rejected_prediction_tokens: optional number`534535        When using Predicted Outputs, the number of tokens in the536        prediction that did not appear in the completion. However, like537        reasoning tokens, these tokens are still counted in the total538        completion tokens for purposes of billing, output, and context window539        limits.540541      - `text_tokens: optional number`542543        Text output tokens generated by the model.544545    - `prompt_tokens_details: optional object { audio_tokens, cache_write_tokens, cached_tokens, 2 more }`546547      Breakdown of tokens used in the prompt.548549      - `audio_tokens: optional number`550551        Audio input tokens present in the prompt.552553      - `cache_write_tokens: optional number`554555        The unadjusted number of prompt tokens written to cache.556557      - `cached_tokens: optional number`558559        Cached tokens present in the prompt.560561      - `image_tokens: optional number`562563        Image input tokens present in the prompt.564565      - `text_tokens: optional number`566567        Text input tokens present in the prompt.568569### Completion Choice570571- `CompletionChoice object { finish_reason, index, logprobs, text }`572573  - `finish_reason: "stop" or "length" or "content_filter"`574575    The reason the model stopped generating tokens. This will be `stop` if the model hit a natural stop point or a provided stop sequence,576    `length` if the maximum number of tokens specified in the request was reached,577    or `content_filter` if content was omitted due to a flag from our content filters.578579    - `"stop"`580581    - `"length"`582583    - `"content_filter"`584585  - `index: number`586587  - `logprobs: object { text_offset, token_logprobs, tokens, top_logprobs }  or null`588589    - `text_offset: optional array of number`590591    - `token_logprobs: optional array of number`592593    - `tokens: optional array of string`594595    - `top_logprobs: optional array of map[number]`596597  - `text: string`598599### Completion Usage600601- `CompletionUsage object { completion_tokens, prompt_tokens, total_tokens, 2 more }`602603  Usage statistics for the completion request.604605  - `completion_tokens: number`606607    Number of tokens in the generated completion.608609  - `prompt_tokens: number`610611    Number of tokens in the prompt.612613  - `total_tokens: number`614615    Total number of tokens used in the request (prompt + completion).616617  - `completion_tokens_details: optional object { accepted_prediction_tokens, audio_tokens, reasoning_tokens, 2 more }`618619    Breakdown of tokens used in a completion.620621    - `accepted_prediction_tokens: optional number`622623      When using Predicted Outputs, the number of tokens in the624      prediction that appeared in the completion.625626    - `audio_tokens: optional number`627628      Audio input tokens generated by the model.629630    - `reasoning_tokens: optional number`631632      Tokens generated by the model for reasoning.633634    - `rejected_prediction_tokens: optional number`635636      When using Predicted Outputs, the number of tokens in the637      prediction that did not appear in the completion. However, like638      reasoning tokens, these tokens are still counted in the total639      completion tokens for purposes of billing, output, and context window640      limits.641642    - `text_tokens: optional number`643644      Text output tokens generated by the model.645646  - `prompt_tokens_details: optional object { audio_tokens, cache_write_tokens, cached_tokens, 2 more }`647648    Breakdown of tokens used in the prompt.649650    - `audio_tokens: optional number`651652      Audio input tokens present in the prompt.653654    - `cache_write_tokens: optional number`655656      The unadjusted number of prompt tokens written to cache.657658    - `cached_tokens: optional number`659660      Cached tokens present in the prompt.661662    - `image_tokens: optional number`663664      Image input tokens present in the prompt.665666    - `text_tokens: optional number`667668      Text input tokens present in the prompt.669
Files main
Menu

Theme