SPB Git

Repository Public

create.md 14.1 KB 435 lines markdown

1> For the complete documentation index, see [llms.txt](/llms.txt). Markdown versions of documentation pages are available by appending `.md` to the page URL.23## Create completion45**post** `/completions`67Creates a completion for the provided prompt and parameters.89Returns a completion object, or a sequence of completion objects if the request is streamed.1011### Body Parameters1213- `model: string or "gpt-3.5-turbo-instruct" or "davinci-002" or "babbage-002"`1415  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.1617  - `string`1819  - `"gpt-3.5-turbo-instruct" or "davinci-002" or "babbage-002"`2021    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.2223    - `"gpt-3.5-turbo-instruct"`2425    - `"davinci-002"`2627    - `"babbage-002"`2829- `prompt: string or array of string or array of number or array of array of number or null`3031  The prompt(s) to generate completions for, encoded as a string, array of strings, array of tokens, or array of token arrays.3233  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.3435  - `string`3637  - `array of string`3839  - `array of number`4041  - `array of array of number`4243- `best_of: optional number or null`4445  Generates `best_of` completions server-side and returns the "best" (the one with the highest log probability per token). Results cannot be streamed.4647  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`.4849  **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`.5051- `echo: optional boolean or null`5253  Echo back the prompt in addition to the completion5455- `frequency_penalty: optional number or null`5657  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.5859  [See more information about frequency and presence penalties.](/api/docs/guides/text)6061- `logit_bias: optional map[number] or null`6263  Modify the likelihood of specified tokens appearing in the completion.6465  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.6667  As an example, you can pass `{"50256": -100}` to prevent the <|endoftext|> token from being generated.6869- `logprobs: optional number or null`7071  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.7273  The maximum value for `logprobs` is 5.7475- `max_tokens: optional number or null`7677  The maximum number of [tokens](https://platform.openai.com/tokenizer) that can be generated in the completion.7879  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.8081- `n: optional number or null`8283  How many completions to generate for each prompt.8485  **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`.8687- `presence_penalty: optional number or null`8889  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.9091  [See more information about frequency and presence penalties.](/api/docs/guides/text)9293- `seed: optional number or null`9495  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.9697  Determinism is not guaranteed, and you should refer to the `system_fingerprint` response parameter to monitor changes in the backend.9899- `stop: optional string or array of string or null`100101  Not supported with latest reasoning models `o3` and `o4-mini`.102103  Up to 4 sequences where the API will stop generating further tokens. The104  returned text will not contain the stop sequence.105106  - `string`107108  - `array of string`109110- `stream: optional boolean or null`111112  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).113114- `stream_options: optional ChatCompletionStreamOptions or null`115116  Options for streaming response. Only set this when you set `stream: true`.117118  - `include_obfuscation: optional boolean`119120    When true, stream obfuscation will be enabled. Stream obfuscation adds121    random characters to an `obfuscation` field on streaming delta events to122    normalize payload sizes as a mitigation to certain side-channel attacks.123    These obfuscation fields are included by default, but add a small amount124    of overhead to the data stream. You can set `include_obfuscation` to125    false to optimize for bandwidth if you trust the network links between126    your application and the OpenAI API.127128  - `include_usage: optional boolean`129130    If set, an additional chunk will be streamed before the `data: [DONE]`131    message. The `usage` field on this chunk shows the token usage statistics132    for the entire request, and the `choices` field will always be an empty133    array.134135    All other chunks will also include a `usage` field, but with a null136    value. **NOTE:** If the stream is interrupted, you may not receive the137    final usage chunk which contains the total token usage for the request.138139- `suffix: optional string or null`140141  The suffix that comes after a completion of inserted text.142143  This parameter is only supported for `gpt-3.5-turbo-instruct`.144145- `temperature: optional number or null`146147  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.148149  We generally recommend altering this or `top_p` but not both.150151- `top_p: optional number or null`152153  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.154155  We generally recommend altering this or `temperature` but not both.156157- `user: optional string`158159  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).160161### Returns162163- `Completion object { id, choices, created, 4 more }`164165  Represents a completion response from the API. Note: both the streamed and non-streamed response objects share the same shape (unlike the chat endpoint).166167  - `id: string`168169    A unique identifier for the completion.170171  - `choices: array of CompletionChoice`172173    The list of completion choices the model generated for the input prompt.174175    - `finish_reason: "stop" or "length" or "content_filter"`176177      The reason the model stopped generating tokens. This will be `stop` if the model hit a natural stop point or a provided stop sequence,178      `length` if the maximum number of tokens specified in the request was reached,179      or `content_filter` if content was omitted due to a flag from our content filters.180181      - `"stop"`182183      - `"length"`184185      - `"content_filter"`186187    - `index: number`188189    - `logprobs: object { text_offset, token_logprobs, tokens, top_logprobs }  or null`190191      - `text_offset: optional array of number`192193      - `token_logprobs: optional array of number`194195      - `tokens: optional array of string`196197      - `top_logprobs: optional array of map[number]`198199    - `text: string`200201  - `created: number`202203    The Unix timestamp (in seconds) of when the completion was created.204205  - `model: string`206207    The model used for completion.208209  - `object: "text_completion"`210211    The object type, which is always "text_completion"212213    - `"text_completion"`214215  - `system_fingerprint: optional string`216217    This fingerprint represents the backend configuration that the model runs with.218219    Can be used in conjunction with the `seed` request parameter to understand when backend changes have been made that might impact determinism.220221  - `usage: optional CompletionUsage`222223    Usage statistics for the completion request.224225    - `completion_tokens: number`226227      Number of tokens in the generated completion.228229    - `prompt_tokens: number`230231      Number of tokens in the prompt.232233    - `total_tokens: number`234235      Total number of tokens used in the request (prompt + completion).236237    - `completion_tokens_details: optional object { accepted_prediction_tokens, audio_tokens, reasoning_tokens, 2 more }`238239      Breakdown of tokens used in a completion.240241      - `accepted_prediction_tokens: optional number`242243        When using Predicted Outputs, the number of tokens in the244        prediction that appeared in the completion.245246      - `audio_tokens: optional number`247248        Audio input tokens generated by the model.249250      - `reasoning_tokens: optional number`251252        Tokens generated by the model for reasoning.253254      - `rejected_prediction_tokens: optional number`255256        When using Predicted Outputs, the number of tokens in the257        prediction that did not appear in the completion. However, like258        reasoning tokens, these tokens are still counted in the total259        completion tokens for purposes of billing, output, and context window260        limits.261262      - `text_tokens: optional number`263264        Text output tokens generated by the model.265266    - `prompt_tokens_details: optional object { audio_tokens, cache_write_tokens, cached_tokens, 2 more }`267268      Breakdown of tokens used in the prompt.269270      - `audio_tokens: optional number`271272        Audio input tokens present in the prompt.273274      - `cache_write_tokens: optional number`275276        The unadjusted number of prompt tokens written to cache.277278      - `cached_tokens: optional number`279280        Cached tokens present in the prompt.281282      - `image_tokens: optional number`283284        Image input tokens present in the prompt.285286      - `text_tokens: optional number`287288        Text input tokens present in the prompt.289290### Example291292```http293curl https://api.openai.com/v1/completions \294    -H 'Content-Type: application/json' \295    -H "Authorization: Bearer $OPENAI_API_KEY" \296    -d '{297          "model": "gpt-3.5-turbo-instruct",298          "prompt": "This is a test.",299          "max_tokens": 16,300          "n": 1,301          "suffix": "test.",302          "temperature": 1,303          "top_p": 1,304          "user": "user-1234"305        }'306```307308#### Response309310```json311{312  "id": "id",313  "choices": [314    {315      "finish_reason": "stop",316      "index": 0,317      "logprobs": {318        "text_offset": [319          0320        ],321        "token_logprobs": [322          0323        ],324        "tokens": [325          "string"326        ],327        "top_logprobs": [328          {329            "foo": 0330          }331        ]332      },333      "text": "text"334    }335  ],336  "created": 0,337  "model": "model",338  "object": "text_completion",339  "system_fingerprint": "system_fingerprint",340  "usage": {341    "completion_tokens": 0,342    "prompt_tokens": 0,343    "total_tokens": 0,344    "completion_tokens_details": {345      "accepted_prediction_tokens": 0,346      "audio_tokens": 0,347      "reasoning_tokens": 0,348      "rejected_prediction_tokens": 0,349      "text_tokens": 0350    },351    "prompt_tokens_details": {352      "audio_tokens": 0,353      "cache_write_tokens": 0,354      "cached_tokens": 0,355      "image_tokens": 0,356      "text_tokens": 0357    }358  }359}360```361362### No streaming363364```http365curl https://api.openai.com/v1/completions \366  -H "Content-Type: application/json" \367  -H "Authorization: Bearer $OPENAI_API_KEY" \368  -d '{369    "model": "gpt-3.5-turbo-instruct",370    "prompt": "Say this is a test",371    "max_tokens": 7,372    "temperature": 0373  }'374```375376#### Response377378```json379{380  "id": "cmpl-uqkvlQyYK7bGYrRHQ0eXlWi7",381  "object": "text_completion",382  "created": 1589478378,383  "model": "gpt-3.5-turbo-instruct",384  "system_fingerprint": "fp_44709d6fcb",385  "choices": [386    {387      "text": "\n\nThis is indeed a test",388      "index": 0,389      "logprobs": null,390      "finish_reason": "length"391    }392  ],393  "usage": {394    "prompt_tokens": 5,395    "completion_tokens": 7,396    "total_tokens": 12397  }398}399```400401### Streaming402403```http404curl https://api.openai.com/v1/completions \405  -H "Content-Type: application/json" \406  -H "Authorization: Bearer $OPENAI_API_KEY" \407  -d '{408    "model": "gpt-3.5-turbo-instruct",409    "prompt": "Say this is a test",410    "max_tokens": 7,411    "temperature": 0,412    "stream": true413  }'414```415416#### Response417418```json419{420  "id": "cmpl-7iA7iJjj8V2zOkCGvWF2hAkDWBQZe",421  "object": "text_completion",422  "created": 1690759702,423  "choices": [424    {425      "text": "This",426      "index": 0,427      "logprobs": null,428      "finish_reason": null429    }430  ],431  "model": "gpt-3.5-turbo-instruct"432  "system_fingerprint": "fp_44709d6fcb",433}434```435
Files main
Menu

Theme