ruby/resources/completions/index.md +0 −536 deleted
File Deleted View Diff
1# Completions
2
3## Create completion
4
5`completions.create(**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: String | :"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 - `String = String`
20
21 - `Model = :"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: String | Array[String] | Array[Integer] | Array[Array[Integer]]`
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 - `String = String`
38
39 - `ArrayOfStrings = Array[String]`
40
41 - `ArrayOfTokens = Array[Integer]`
42
43 - `ArrayOfTokenArrays = Array[Array[Integer]]`
44
45- `best_of: Integer`
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: bool`
54
55 Echo back the prompt in addition to the completion
56
57- `frequency_penalty: 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: Hash[Symbol, Integer]`
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: Integer`
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: Integer`
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: Integer`
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: 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: Integer`
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: String | Array[String]`
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 - `String = String`
109
110 - `UnionMember1 = Array[String]`
111
112- `stream: bool`
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- `stream_options: ChatCompletionStreamOptions`
117
118 Options for streaming response. Only set this when you set `stream: true`.
119
120 - `include_obfuscation: bool`
121
122 When true, stream obfuscation will be enabled. Stream obfuscation adds
123 random characters to an `obfuscation` field on streaming delta events to
124 normalize payload sizes as a mitigation to certain side-channel attacks.
125 These obfuscation fields are included by default, but add a small amount
126 of overhead to the data stream. You can set `include_obfuscation` to
127 false to optimize for bandwidth if you trust the network links between
128 your application and the OpenAI API.
129
130 - `include_usage: bool`
131
132 If set, an additional chunk will be streamed before the `data: [DONE]`
133 message. The `usage` field on this chunk shows the token usage statistics
134 for the entire request, and the `choices` field will always be an empty
135 array.
136
137 All other chunks will also include a `usage` field, but with a null
138 value. **NOTE:** If the stream is interrupted, you may not receive the
139 final usage chunk which contains the total token usage for the request.
140
141- `suffix: String`
142
143 The suffix that comes after a completion of inserted text.
144
145 This parameter is only supported for `gpt-3.5-turbo-instruct`.
146
147- `temperature: Float`
148
149 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.
150
151 We generally recommend altering this or `top_p` but not both.
152
153- `top_p: Float`
154
155 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.
156
157 We generally recommend altering this or `temperature` but not both.
158
159- `user: String`
160
161 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).
162
163### Returns
164
165- `class Completion`
166
167 Represents a completion response from the API. Note: both the streamed and non-streamed response objects share the same shape (unlike the chat endpoint).
168
169 - `id: String`
170
171 A unique identifier for the completion.
172
173 - `choices: Array[CompletionChoice]`
174
175 The list of completion choices the model generated for the input prompt.
176
177 - `finish_reason: :stop | :length | :content_filter`
178
179 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.
182
183 - `:stop`
184
185 - `:length`
186
187 - `:content_filter`
188
189 - `index: Integer`
190
191 - `logprobs: Logprobs{ text_offset, token_logprobs, tokens, top_logprobs}`
192
193 - `text_offset: Array[Integer]`
194
195 - `token_logprobs: Array[Float]`
196
197 - `tokens: Array[String]`
198
199 - `top_logprobs: Array[Hash[Symbol, Float]]`
200
201 - `text: String`
202
203 - `created: Integer`
204
205 The Unix timestamp (in seconds) of when the completion was created.
206
207 - `model: String`
208
209 The model used for completion.
210
211 - `object: :text_completion`
212
213 The object type, which is always "text_completion"
214
215 - `:text_completion`
216
217 - `system_fingerprint: String`
218
219 This fingerprint represents the backend configuration that the model runs with.
220
221 Can be used in conjunction with the `seed` request parameter to understand when backend changes have been made that might impact determinism.
222
223 - `usage: CompletionUsage`
224
225 Usage statistics for the completion request.
226
227 - `completion_tokens: Integer`
228
229 Number of tokens in the generated completion.
230
231 - `prompt_tokens: Integer`
232
233 Number of tokens in the prompt.
234
235 - `total_tokens: Integer`
236
237 Total number of tokens used in the request (prompt + completion).
238
239 - `completion_tokens_details: CompletionTokensDetails{ accepted_prediction_tokens, audio_tokens, reasoning_tokens, rejected_prediction_tokens}`
240
241 Breakdown of tokens used in a completion.
242
243 - `accepted_prediction_tokens: Integer`
244
245 When using Predicted Outputs, the number of tokens in the
246 prediction that appeared in the completion.
247
248 - `audio_tokens: Integer`
249
250 Audio input tokens generated by the model.
251
252 - `reasoning_tokens: Integer`
253
254 Tokens generated by the model for reasoning.
255
256 - `rejected_prediction_tokens: Integer`
257
258 When using Predicted Outputs, the number of tokens in the
259 prediction that did not appear in the completion. However, like
260 reasoning tokens, these tokens are still counted in the total
261 completion tokens for purposes of billing, output, and context window
262 limits.
263
264 - `prompt_tokens_details: PromptTokensDetails{ audio_tokens, cached_tokens}`
265
266 Breakdown of tokens used in the prompt.
267
268 - `audio_tokens: Integer`
269
270 Audio input tokens present in the prompt.
271
272 - `cached_tokens: Integer`
273
274 Cached tokens present in the prompt.
275
276### Example
277
278```ruby
279require "openai"
280
281openai = OpenAI::Client.new(api_key: "My API Key")
282
283completion = openai.completions.create(model: :"gpt-3.5-turbo-instruct", prompt: "This is a test.")
284
285puts(completion)
286```
287
288#### Response
289
290```json
291{
292 "id": "id",
293 "choices": [
294 {
295 "finish_reason": "stop",
296 "index": 0,
297 "logprobs": {
298 "text_offset": [
299 0
300 ],
301 "token_logprobs": [
302 0
303 ],
304 "tokens": [
305 "string"
306 ],
307 "top_logprobs": [
308 {
309 "foo": 0
310 }
311 ]
312 },
313 "text": "text"
314 }
315 ],
316 "created": 0,
317 "model": "model",
318 "object": "text_completion",
319 "system_fingerprint": "system_fingerprint",
320 "usage": {
321 "completion_tokens": 0,
322 "prompt_tokens": 0,
323 "total_tokens": 0,
324 "completion_tokens_details": {
325 "accepted_prediction_tokens": 0,
326 "audio_tokens": 0,
327 "reasoning_tokens": 0,
328 "rejected_prediction_tokens": 0
329 },
330 "prompt_tokens_details": {
331 "audio_tokens": 0,
332 "cached_tokens": 0
333 }
334 }
335}
336```
337
338## Domain Types
339
340### Completion
341
342- `class Completion`
343
344 Represents a completion response from the API. Note: both the streamed and non-streamed response objects share the same shape (unlike the chat endpoint).
345
346 - `id: String`
347
348 A unique identifier for the completion.
349
350 - `choices: Array[CompletionChoice]`
351
352 The list of completion choices the model generated for the input prompt.
353
354 - `finish_reason: :stop | :length | :content_filter`
355
356 The reason the model stopped generating tokens. This will be `stop` if the model hit a natural stop point or a provided stop sequence,
357 `length` if the maximum number of tokens specified in the request was reached,
358 or `content_filter` if content was omitted due to a flag from our content filters.
359
360 - `:stop`
361
362 - `:length`
363
364 - `:content_filter`
365
366 - `index: Integer`
367
368 - `logprobs: Logprobs{ text_offset, token_logprobs, tokens, top_logprobs}`
369
370 - `text_offset: Array[Integer]`
371
372 - `token_logprobs: Array[Float]`
373
374 - `tokens: Array[String]`
375
376 - `top_logprobs: Array[Hash[Symbol, Float]]`
377
378 - `text: String`
379
380 - `created: Integer`
381
382 The Unix timestamp (in seconds) of when the completion was created.
383
384 - `model: String`
385
386 The model used for completion.
387
388 - `object: :text_completion`
389
390 The object type, which is always "text_completion"
391
392 - `:text_completion`
393
394 - `system_fingerprint: String`
395
396 This fingerprint represents the backend configuration that the model runs with.
397
398 Can be used in conjunction with the `seed` request parameter to understand when backend changes have been made that might impact determinism.
399
400 - `usage: CompletionUsage`
401
402 Usage statistics for the completion request.
403
404 - `completion_tokens: Integer`
405
406 Number of tokens in the generated completion.
407
408 - `prompt_tokens: Integer`
409
410 Number of tokens in the prompt.
411
412 - `total_tokens: Integer`
413
414 Total number of tokens used in the request (prompt + completion).
415
416 - `completion_tokens_details: CompletionTokensDetails{ accepted_prediction_tokens, audio_tokens, reasoning_tokens, rejected_prediction_tokens}`
417
418 Breakdown of tokens used in a completion.
419
420 - `accepted_prediction_tokens: Integer`
421
422 When using Predicted Outputs, the number of tokens in the
423 prediction that appeared in the completion.
424
425 - `audio_tokens: Integer`
426
427 Audio input tokens generated by the model.
428
429 - `reasoning_tokens: Integer`
430
431 Tokens generated by the model for reasoning.
432
433 - `rejected_prediction_tokens: Integer`
434
435 When using Predicted Outputs, the number of tokens in the
436 prediction that did not appear in the completion. However, like
437 reasoning tokens, these tokens are still counted in the total
438 completion tokens for purposes of billing, output, and context window
439 limits.
440
441 - `prompt_tokens_details: PromptTokensDetails{ audio_tokens, cached_tokens}`
442
443 Breakdown of tokens used in the prompt.
444
445 - `audio_tokens: Integer`
446
447 Audio input tokens present in the prompt.
448
449 - `cached_tokens: Integer`
450
451 Cached tokens present in the prompt.
452
453### Completion Choice
454
455- `class CompletionChoice`
456
457 - `finish_reason: :stop | :length | :content_filter`
458
459 The reason the model stopped generating tokens. This will be `stop` if the model hit a natural stop point or a provided stop sequence,
460 `length` if the maximum number of tokens specified in the request was reached,
461 or `content_filter` if content was omitted due to a flag from our content filters.
462
463 - `:stop`
464
465 - `:length`
466
467 - `:content_filter`
468
469 - `index: Integer`
470
471 - `logprobs: Logprobs{ text_offset, token_logprobs, tokens, top_logprobs}`
472
473 - `text_offset: Array[Integer]`
474
475 - `token_logprobs: Array[Float]`
476
477 - `tokens: Array[String]`
478
479 - `top_logprobs: Array[Hash[Symbol, Float]]`
480
481 - `text: String`
482
483### Completion Usage
484
485- `class CompletionUsage`
486
487 Usage statistics for the completion request.
488
489 - `completion_tokens: Integer`
490
491 Number of tokens in the generated completion.
492
493 - `prompt_tokens: Integer`
494
495 Number of tokens in the prompt.
496
497 - `total_tokens: Integer`
498
499 Total number of tokens used in the request (prompt + completion).
500
501 - `completion_tokens_details: CompletionTokensDetails{ accepted_prediction_tokens, audio_tokens, reasoning_tokens, rejected_prediction_tokens}`
502
503 Breakdown of tokens used in a completion.
504
505 - `accepted_prediction_tokens: Integer`
506
507 When using Predicted Outputs, the number of tokens in the
508 prediction that appeared in the completion.
509
510 - `audio_tokens: Integer`
511
512 Audio input tokens generated by the model.
513
514 - `reasoning_tokens: Integer`
515
516 Tokens generated by the model for reasoning.
517
518 - `rejected_prediction_tokens: Integer`
519
520 When using Predicted Outputs, the number of tokens in the
521 prediction that did not appear in the completion. However, like
522 reasoning tokens, these tokens are still counted in the total
523 completion tokens for purposes of billing, output, and context window
524 limits.
525
526 - `prompt_tokens_details: PromptTokensDetails{ audio_tokens, cached_tokens}`
527
528 Breakdown of tokens used in the prompt.
529
530 - `audio_tokens: Integer`
531
532 Audio input tokens present in the prompt.
533
534 - `cached_tokens: Integer`
535
536 Cached tokens present in the prompt.