python/resources/completions/index.md +0 −617 deleted
File Deleted View Diff
1# Completions
2
3## Create completion
4
5`completions.create(CompletionCreateParams**kwargs) -> Completion`
6
7**post** `/completions`
8
9Creates a completion for the provided prompt and parameters.
10
11Returns a completion object, or a sequence of completion objects if the request is streamed.
12
13### Parameters
14
15- `model: Union[str, Literal["gpt-3.5-turbo-instruct", "davinci-002", "babbage-002"]]`
16
17 ID of the model to use. You can use the [List models](https://platform.openai.com/docs/api-reference/models/list) API to see all of your available models, or see our [Model overview](https://platform.openai.com/docs/models) for descriptions of them.
18
19 - `str`
20
21 - `Literal["gpt-3.5-turbo-instruct", "davinci-002", "babbage-002"]`
22
23 ID of the model to use. You can use the [List models](https://platform.openai.com/docs/api-reference/models/list) API to see all of your available models, or see our [Model overview](https://platform.openai.com/docs/models) for descriptions of them.
24
25 - `"gpt-3.5-turbo-instruct"`
26
27 - `"davinci-002"`
28
29 - `"babbage-002"`
30
31- `prompt: Union[str, Sequence[str], Iterable[int], 2 more]`
32
33 The prompt(s) to generate completions for, encoded as a string, array of strings, array of tokens, or array of token arrays.
34
35 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.
36
37 - `str`
38
39 - `Sequence[str]`
40
41 - `Iterable[int]`
42
43 - `Iterable[Iterable[int]]`
44
45- `best_of: Optional[int]`
46
47 Generates `best_of` completions server-side and returns the "best" (the one with the highest log probability per token). Results cannot be streamed.
48
49 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`.
50
51 **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`.
52
53- `echo: Optional[bool]`
54
55 Echo back the prompt in addition to the completion
56
57- `frequency_penalty: Optional[float]`
58
59 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.
60
61 [See more information about frequency and presence penalties.](https://platform.openai.com/docs/guides/text-generation)
62
63- `logit_bias: Optional[Dict[str, int]]`
64
65 Modify the likelihood of specified tokens appearing in the completion.
66
67 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](/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.
68
69 As an example, you can pass `{"50256": -100}` to prevent the <|endoftext|> token from being generated.
70
71- `logprobs: Optional[int]`
72
73 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.
74
75 The maximum value for `logprobs` is 5.
76
77- `max_tokens: Optional[int]`
78
79 The maximum number of [tokens](/tokenizer) that can be generated in the completion.
80
81 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.
82
83- `n: Optional[int]`
84
85 How many completions to generate for each prompt.
86
87 **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`.
88
89- `presence_penalty: Optional[float]`
90
91 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.
92
93 [See more information about frequency and presence penalties.](https://platform.openai.com/docs/guides/text-generation)
94
95- `seed: Optional[int]`
96
97 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.
98
99 Determinism is not guaranteed, and you should refer to the `system_fingerprint` response parameter to monitor changes in the backend.
100
101- `stop: Optional[Union[Optional[str], Sequence[str], null]]`
102
103 Not supported with latest reasoning models `o3` and `o4-mini`.
104
105 Up to 4 sequences where the API will stop generating further tokens. The
106 returned text will not contain the stop sequence.
107
108 - `Optional[str]`
109
110 - `Sequence[str]`
111
112- `stream: Optional[Literal[false]]`
113
114 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).
115
116 - `false`
117
118- `stream_options: Optional[ChatCompletionStreamOptionsParam]`
119
120 Options for streaming response. Only set this when you set `stream: true`.
121
122 - `include_obfuscation: Optional[bool]`
123
124 When true, stream obfuscation will be enabled. Stream obfuscation adds
125 random characters to an `obfuscation` field on streaming delta events to
126 normalize payload sizes as a mitigation to certain side-channel attacks.
127 These obfuscation fields are included by default, but add a small amount
128 of overhead to the data stream. You can set `include_obfuscation` to
129 false to optimize for bandwidth if you trust the network links between
130 your application and the OpenAI API.
131
132 - `include_usage: Optional[bool]`
133
134 If set, an additional chunk will be streamed before the `data: [DONE]`
135 message. The `usage` field on this chunk shows the token usage statistics
136 for the entire request, and the `choices` field will always be an empty
137 array.
138
139 All other chunks will also include a `usage` field, but with a null
140 value. **NOTE:** If the stream is interrupted, you may not receive the
141 final usage chunk which contains the total token usage for the request.
142
143- `suffix: Optional[str]`
144
145 The suffix that comes after a completion of inserted text.
146
147 This parameter is only supported for `gpt-3.5-turbo-instruct`.
148
149- `temperature: Optional[float]`
150
151 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.
152
153 We generally recommend altering this or `top_p` but not both.
154
155- `top_p: Optional[float]`
156
157 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.
158
159 We generally recommend altering this or `temperature` but not both.
160
161- `user: Optional[str]`
162
163 A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. [Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
164
165### Returns
166
167- `class Completion: …`
168
169 Represents a completion response from the API. Note: both the streamed and non-streamed response objects share the same shape (unlike the chat endpoint).
170
171 - `id: str`
172
173 A unique identifier for the completion.
174
175 - `choices: List[CompletionChoice]`
176
177 The list of completion choices the model generated for the input prompt.
178
179 - `finish_reason: Literal["stop", "length", "content_filter"]`
180
181 The reason the model stopped generating tokens. This will be `stop` if the model hit a natural stop point or a provided stop sequence,
182 `length` if the maximum number of tokens specified in the request was reached,
183 or `content_filter` if content was omitted due to a flag from our content filters.
184
185 - `"stop"`
186
187 - `"length"`
188
189 - `"content_filter"`
190
191 - `index: int`
192
193 - `logprobs: Optional[Logprobs]`
194
195 - `text_offset: Optional[List[int]]`
196
197 - `token_logprobs: Optional[List[float]]`
198
199 - `tokens: Optional[List[str]]`
200
201 - `top_logprobs: Optional[List[Dict[str, float]]]`
202
203 - `text: str`
204
205 - `created: int`
206
207 The Unix timestamp (in seconds) of when the completion was created.
208
209 - `model: str`
210
211 The model used for completion.
212
213 - `object: Literal["text_completion"]`
214
215 The object type, which is always "text_completion"
216
217 - `"text_completion"`
218
219 - `system_fingerprint: Optional[str]`
220
221 This fingerprint represents the backend configuration that the model runs with.
222
223 Can be used in conjunction with the `seed` request parameter to understand when backend changes have been made that might impact determinism.
224
225 - `usage: Optional[CompletionUsage]`
226
227 Usage statistics for the completion request.
228
229 - `completion_tokens: int`
230
231 Number of tokens in the generated completion.
232
233 - `prompt_tokens: int`
234
235 Number of tokens in the prompt.
236
237 - `total_tokens: int`
238
239 Total number of tokens used in the request (prompt + completion).
240
241 - `completion_tokens_details: Optional[CompletionTokensDetails]`
242
243 Breakdown of tokens used in a completion.
244
245 - `accepted_prediction_tokens: Optional[int]`
246
247 When using Predicted Outputs, the number of tokens in the
248 prediction that appeared in the completion.
249
250 - `audio_tokens: Optional[int]`
251
252 Audio input tokens generated by the model.
253
254 - `reasoning_tokens: Optional[int]`
255
256 Tokens generated by the model for reasoning.
257
258 - `rejected_prediction_tokens: Optional[int]`
259
260 When using Predicted Outputs, the number of tokens in the
261 prediction that did not appear in the completion. However, like
262 reasoning tokens, these tokens are still counted in the total
263 completion tokens for purposes of billing, output, and context window
264 limits.
265
266 - `prompt_tokens_details: Optional[PromptTokensDetails]`
267
268 Breakdown of tokens used in the prompt.
269
270 - `audio_tokens: Optional[int]`
271
272 Audio input tokens present in the prompt.
273
274 - `cached_tokens: Optional[int]`
275
276 Cached tokens present in the prompt.
277
278### Example
279
280```python
281import os
282from openai import OpenAI
283
284client = OpenAI(
285 api_key=os.environ.get("OPENAI_API_KEY"), # This is the default and can be omitted
286)
287for completion in client.completions.create(
288 model="string",
289 prompt="This is a test.",
290):
291 print(completion)
292```
293
294#### Response
295
296```json
297{
298 "id": "id",
299 "choices": [
300 {
301 "finish_reason": "stop",
302 "index": 0,
303 "logprobs": {
304 "text_offset": [
305 0
306 ],
307 "token_logprobs": [
308 0
309 ],
310 "tokens": [
311 "string"
312 ],
313 "top_logprobs": [
314 {
315 "foo": 0
316 }
317 ]
318 },
319 "text": "text"
320 }
321 ],
322 "created": 0,
323 "model": "model",
324 "object": "text_completion",
325 "system_fingerprint": "system_fingerprint",
326 "usage": {
327 "completion_tokens": 0,
328 "prompt_tokens": 0,
329 "total_tokens": 0,
330 "completion_tokens_details": {
331 "accepted_prediction_tokens": 0,
332 "audio_tokens": 0,
333 "reasoning_tokens": 0,
334 "rejected_prediction_tokens": 0
335 },
336 "prompt_tokens_details": {
337 "audio_tokens": 0,
338 "cached_tokens": 0
339 }
340 }
341}
342```
343
344### No streaming
345
346```python
347from openai import OpenAI
348client = OpenAI()
349
350client.completions.create(
351 model="VAR_completion_model_id",
352 prompt="Say this is a test",
353 max_tokens=7,
354 temperature=0
355)
356```
357
358#### Response
359
360```json
361{
362 "id": "cmpl-uqkvlQyYK7bGYrRHQ0eXlWi7",
363 "object": "text_completion",
364 "created": 1589478378,
365 "model": "VAR_completion_model_id",
366 "system_fingerprint": "fp_44709d6fcb",
367 "choices": [
368 {
369 "text": "\n\nThis is indeed a test",
370 "index": 0,
371 "logprobs": null,
372 "finish_reason": "length"
373 }
374 ],
375 "usage": {
376 "prompt_tokens": 5,
377 "completion_tokens": 7,
378 "total_tokens": 12
379 }
380}
381```
382
383### Streaming
384
385```python
386from openai import OpenAI
387client = OpenAI()
388
389for chunk in client.completions.create(
390 model="VAR_completion_model_id",
391 prompt="Say this is a test",
392 max_tokens=7,
393 temperature=0,
394 stream=True
395):
396 print(chunk.choices[0].text)
397```
398
399#### Response
400
401```json
402{
403 "id": "cmpl-7iA7iJjj8V2zOkCGvWF2hAkDWBQZe",
404 "object": "text_completion",
405 "created": 1690759702,
406 "choices": [
407 {
408 "text": "This",
409 "index": 0,
410 "logprobs": null,
411 "finish_reason": null
412 }
413 ],
414 "model": "gpt-3.5-turbo-instruct"
415 "system_fingerprint": "fp_44709d6fcb",
416}
417```
418
419## Domain Types
420
421### Completion
422
423- `class Completion: …`
424
425 Represents a completion response from the API. Note: both the streamed and non-streamed response objects share the same shape (unlike the chat endpoint).
426
427 - `id: str`
428
429 A unique identifier for the completion.
430
431 - `choices: List[CompletionChoice]`
432
433 The list of completion choices the model generated for the input prompt.
434
435 - `finish_reason: Literal["stop", "length", "content_filter"]`
436
437 The reason the model stopped generating tokens. This will be `stop` if the model hit a natural stop point or a provided stop sequence,
438 `length` if the maximum number of tokens specified in the request was reached,
439 or `content_filter` if content was omitted due to a flag from our content filters.
440
441 - `"stop"`
442
443 - `"length"`
444
445 - `"content_filter"`
446
447 - `index: int`
448
449 - `logprobs: Optional[Logprobs]`
450
451 - `text_offset: Optional[List[int]]`
452
453 - `token_logprobs: Optional[List[float]]`
454
455 - `tokens: Optional[List[str]]`
456
457 - `top_logprobs: Optional[List[Dict[str, float]]]`
458
459 - `text: str`
460
461 - `created: int`
462
463 The Unix timestamp (in seconds) of when the completion was created.
464
465 - `model: str`
466
467 The model used for completion.
468
469 - `object: Literal["text_completion"]`
470
471 The object type, which is always "text_completion"
472
473 - `"text_completion"`
474
475 - `system_fingerprint: Optional[str]`
476
477 This fingerprint represents the backend configuration that the model runs with.
478
479 Can be used in conjunction with the `seed` request parameter to understand when backend changes have been made that might impact determinism.
480
481 - `usage: Optional[CompletionUsage]`
482
483 Usage statistics for the completion request.
484
485 - `completion_tokens: int`
486
487 Number of tokens in the generated completion.
488
489 - `prompt_tokens: int`
490
491 Number of tokens in the prompt.
492
493 - `total_tokens: int`
494
495 Total number of tokens used in the request (prompt + completion).
496
497 - `completion_tokens_details: Optional[CompletionTokensDetails]`
498
499 Breakdown of tokens used in a completion.
500
501 - `accepted_prediction_tokens: Optional[int]`
502
503 When using Predicted Outputs, the number of tokens in the
504 prediction that appeared in the completion.
505
506 - `audio_tokens: Optional[int]`
507
508 Audio input tokens generated by the model.
509
510 - `reasoning_tokens: Optional[int]`
511
512 Tokens generated by the model for reasoning.
513
514 - `rejected_prediction_tokens: Optional[int]`
515
516 When using Predicted Outputs, the number of tokens in the
517 prediction that did not appear in the completion. However, like
518 reasoning tokens, these tokens are still counted in the total
519 completion tokens for purposes of billing, output, and context window
520 limits.
521
522 - `prompt_tokens_details: Optional[PromptTokensDetails]`
523
524 Breakdown of tokens used in the prompt.
525
526 - `audio_tokens: Optional[int]`
527
528 Audio input tokens present in the prompt.
529
530 - `cached_tokens: Optional[int]`
531
532 Cached tokens present in the prompt.
533
534### Completion Choice
535
536- `class CompletionChoice: …`
537
538 - `finish_reason: Literal["stop", "length", "content_filter"]`
539
540 The reason the model stopped generating tokens. This will be `stop` if the model hit a natural stop point or a provided stop sequence,
541 `length` if the maximum number of tokens specified in the request was reached,
542 or `content_filter` if content was omitted due to a flag from our content filters.
543
544 - `"stop"`
545
546 - `"length"`
547
548 - `"content_filter"`
549
550 - `index: int`
551
552 - `logprobs: Optional[Logprobs]`
553
554 - `text_offset: Optional[List[int]]`
555
556 - `token_logprobs: Optional[List[float]]`
557
558 - `tokens: Optional[List[str]]`
559
560 - `top_logprobs: Optional[List[Dict[str, float]]]`
561
562 - `text: str`
563
564### Completion Usage
565
566- `class CompletionUsage: …`
567
568 Usage statistics for the completion request.
569
570 - `completion_tokens: int`
571
572 Number of tokens in the generated completion.
573
574 - `prompt_tokens: int`
575
576 Number of tokens in the prompt.
577
578 - `total_tokens: int`
579
580 Total number of tokens used in the request (prompt + completion).
581
582 - `completion_tokens_details: Optional[CompletionTokensDetails]`
583
584 Breakdown of tokens used in a completion.
585
586 - `accepted_prediction_tokens: Optional[int]`
587
588 When using Predicted Outputs, the number of tokens in the
589 prediction that appeared in the completion.
590
591 - `audio_tokens: Optional[int]`
592
593 Audio input tokens generated by the model.
594
595 - `reasoning_tokens: Optional[int]`
596
597 Tokens generated by the model for reasoning.
598
599 - `rejected_prediction_tokens: Optional[int]`
600
601 When using Predicted Outputs, the number of tokens in the
602 prediction that did not appear in the completion. However, like
603 reasoning tokens, these tokens are still counted in the total
604 completion tokens for purposes of billing, output, and context window
605 limits.
606
607 - `prompt_tokens_details: Optional[PromptTokensDetails]`
608
609 Breakdown of tokens used in the prompt.
610
611 - `audio_tokens: Optional[int]`
612
613 Audio input tokens present in the prompt.
614
615 - `cached_tokens: Optional[int]`
616
617 Cached tokens present in the prompt.