ruby/resources/completions/methods/create/index.md +0 −334 deleted
File Deleted View Diff
1## Create completion
2
3`completions.create(**kwargs) -> Completion`
4
5**post** `/completions`
6
7Creates a completion for the provided prompt and parameters.
8
9Returns a completion object, or a sequence of completion objects if the request is streamed.
10
11### Parameters
12
13- `model: String | :"gpt-3.5-turbo-instruct" | :"davinci-002" | :"babbage-002"`
14
15 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.
16
17 - `String = String`
18
19 - `Model = :"gpt-3.5-turbo-instruct" | :"davinci-002" | :"babbage-002"`
20
21 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.
22
23 - `:"gpt-3.5-turbo-instruct"`
24
25 - `:"davinci-002"`
26
27 - `:"babbage-002"`
28
29- `prompt: String | Array[String] | Array[Integer] | Array[Array[Integer]]`
30
31 The prompt(s) to generate completions for, encoded as a string, array of strings, array of tokens, or array of token arrays.
32
33 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.
34
35 - `String = String`
36
37 - `ArrayOfStrings = Array[String]`
38
39 - `ArrayOfTokens = Array[Integer]`
40
41 - `ArrayOfTokenArrays = Array[Array[Integer]]`
42
43- `best_of: Integer`
44
45 Generates `best_of` completions server-side and returns the "best" (the one with the highest log probability per token). Results cannot be streamed.
46
47 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`.
48
49 **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`.
50
51- `echo: bool`
52
53 Echo back the prompt in addition to the completion
54
55- `frequency_penalty: Float`
56
57 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.
58
59 [See more information about frequency and presence penalties.](https://platform.openai.com/docs/guides/text-generation)
60
61- `logit_bias: Hash[Symbol, Integer]`
62
63 Modify the likelihood of specified tokens appearing in the completion.
64
65 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.
66
67 As an example, you can pass `{"50256": -100}` to prevent the <|endoftext|> token from being generated.
68
69- `logprobs: Integer`
70
71 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.
72
73 The maximum value for `logprobs` is 5.
74
75- `max_tokens: Integer`
76
77 The maximum number of [tokens](/tokenizer) that can be generated in the completion.
78
79 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.
80
81- `n: Integer`
82
83 How many completions to generate for each prompt.
84
85 **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`.
86
87- `presence_penalty: Float`
88
89 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.
90
91 [See more information about frequency and presence penalties.](https://platform.openai.com/docs/guides/text-generation)
92
93- `seed: Integer`
94
95 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.
96
97 Determinism is not guaranteed, and you should refer to the `system_fingerprint` response parameter to monitor changes in the backend.
98
99- `stop: String | Array[String]`
100
101 Not supported with latest reasoning models `o3` and `o4-mini`.
102
103 Up to 4 sequences where the API will stop generating further tokens. The
104 returned text will not contain the stop sequence.
105
106 - `String = String`
107
108 - `UnionMember1 = Array[String]`
109
110- `stream: bool`
111
112 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).
113
114- `stream_options: ChatCompletionStreamOptions`
115
116 Options for streaming response. Only set this when you set `stream: true`.
117
118 - `include_obfuscation: bool`
119
120 When true, stream obfuscation will be enabled. Stream obfuscation adds
121 random characters to an `obfuscation` field on streaming delta events to
122 normalize payload sizes as a mitigation to certain side-channel attacks.
123 These obfuscation fields are included by default, but add a small amount
124 of overhead to the data stream. You can set `include_obfuscation` to
125 false to optimize for bandwidth if you trust the network links between
126 your application and the OpenAI API.
127
128 - `include_usage: bool`
129
130 If set, an additional chunk will be streamed before the `data: [DONE]`
131 message. The `usage` field on this chunk shows the token usage statistics
132 for the entire request, and the `choices` field will always be an empty
133 array.
134
135 All other chunks will also include a `usage` field, but with a null
136 value. **NOTE:** If the stream is interrupted, you may not receive the
137 final usage chunk which contains the total token usage for the request.
138
139- `suffix: String`
140
141 The suffix that comes after a completion of inserted text.
142
143 This parameter is only supported for `gpt-3.5-turbo-instruct`.
144
145- `temperature: Float`
146
147 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.
148
149 We generally recommend altering this or `top_p` but not both.
150
151- `top_p: Float`
152
153 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.
154
155 We generally recommend altering this or `temperature` but not both.
156
157- `user: String`
158
159 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).
160
161### Returns
162
163- `class Completion`
164
165 Represents a completion response from the API. Note: both the streamed and non-streamed response objects share the same shape (unlike the chat endpoint).
166
167 - `id: String`
168
169 A unique identifier for the completion.
170
171 - `choices: Array[CompletionChoice]`
172
173 The list of completion choices the model generated for the input prompt.
174
175 - `finish_reason: :stop | :length | :content_filter`
176
177 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.
180
181 - `:stop`
182
183 - `:length`
184
185 - `:content_filter`
186
187 - `index: Integer`
188
189 - `logprobs: Logprobs{ text_offset, token_logprobs, tokens, top_logprobs}`
190
191 - `text_offset: Array[Integer]`
192
193 - `token_logprobs: Array[Float]`
194
195 - `tokens: Array[String]`
196
197 - `top_logprobs: Array[Hash[Symbol, Float]]`
198
199 - `text: String`
200
201 - `created: Integer`
202
203 The Unix timestamp (in seconds) of when the completion was created.
204
205 - `model: String`
206
207 The model used for completion.
208
209 - `object: :text_completion`
210
211 The object type, which is always "text_completion"
212
213 - `:text_completion`
214
215 - `system_fingerprint: String`
216
217 This fingerprint represents the backend configuration that the model runs with.
218
219 Can be used in conjunction with the `seed` request parameter to understand when backend changes have been made that might impact determinism.
220
221 - `usage: CompletionUsage`
222
223 Usage statistics for the completion request.
224
225 - `completion_tokens: Integer`
226
227 Number of tokens in the generated completion.
228
229 - `prompt_tokens: Integer`
230
231 Number of tokens in the prompt.
232
233 - `total_tokens: Integer`
234
235 Total number of tokens used in the request (prompt + completion).
236
237 - `completion_tokens_details: CompletionTokensDetails{ accepted_prediction_tokens, audio_tokens, reasoning_tokens, rejected_prediction_tokens}`
238
239 Breakdown of tokens used in a completion.
240
241 - `accepted_prediction_tokens: Integer`
242
243 When using Predicted Outputs, the number of tokens in the
244 prediction that appeared in the completion.
245
246 - `audio_tokens: Integer`
247
248 Audio input tokens generated by the model.
249
250 - `reasoning_tokens: Integer`
251
252 Tokens generated by the model for reasoning.
253
254 - `rejected_prediction_tokens: Integer`
255
256 When using Predicted Outputs, the number of tokens in the
257 prediction that did not appear in the completion. However, like
258 reasoning tokens, these tokens are still counted in the total
259 completion tokens for purposes of billing, output, and context window
260 limits.
261
262 - `prompt_tokens_details: PromptTokensDetails{ audio_tokens, cached_tokens}`
263
264 Breakdown of tokens used in the prompt.
265
266 - `audio_tokens: Integer`
267
268 Audio input tokens present in the prompt.
269
270 - `cached_tokens: Integer`
271
272 Cached tokens present in the prompt.
273
274### Example
275
276```ruby
277require "openai"
278
279openai = OpenAI::Client.new(api_key: "My API Key")
280
281completion = openai.completions.create(model: :"gpt-3.5-turbo-instruct", prompt: "This is a test.")
282
283puts(completion)
284```
285
286#### Response
287
288```json
289{
290 "id": "id",
291 "choices": [
292 {
293 "finish_reason": "stop",
294 "index": 0,
295 "logprobs": {
296 "text_offset": [
297 0
298 ],
299 "token_logprobs": [
300 0
301 ],
302 "tokens": [
303 "string"
304 ],
305 "top_logprobs": [
306 {
307 "foo": 0
308 }
309 ]
310 },
311 "text": "text"
312 }
313 ],
314 "created": 0,
315 "model": "model",
316 "object": "text_completion",
317 "system_fingerprint": "system_fingerprint",
318 "usage": {
319 "completion_tokens": 0,
320 "prompt_tokens": 0,
321 "total_tokens": 0,
322 "completion_tokens_details": {
323 "accepted_prediction_tokens": 0,
324 "audio_tokens": 0,
325 "reasoning_tokens": 0,
326 "rejected_prediction_tokens": 0
327 },
328 "prompt_tokens_details": {
329 "audio_tokens": 0,
330 "cached_tokens": 0
331 }
332 }
333}
334```