java/resources/completions/methods/create/index.md +0 −318 deleted
File Deleted View Diff
1## Create completion
2
3`Completion completions().create(CompletionCreateParamsparams, RequestOptionsrequestOptions = RequestOptions.none())`
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- `CompletionCreateParams params`
14
15 - `Model model`
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 - `GPT_3_5_TURBO_INSTRUCT("gpt-3.5-turbo-instruct")`
20
21 - `DAVINCI_002("davinci-002")`
22
23 - `BABBAGE_002("babbage-002")`
24
25 - `Optional<Prompt> prompt`
26
27 The prompt(s) to generate completions for, encoded as a string, array of strings, array of tokens, or array of token arrays.
28
29 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.
30
31 - `String`
32
33 - `List<String>`
34
35 - `List<long>`
36
37 - `List<List<long>>`
38
39 - `Optional<Long> bestOf`
40
41 Generates `best_of` completions server-side and returns the "best" (the one with the highest log probability per token). Results cannot be streamed.
42
43 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`.
44
45 **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`.
46
47 - `Optional<Boolean> echo`
48
49 Echo back the prompt in addition to the completion
50
51 - `Optional<Double> frequencyPenalty`
52
53 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.
54
55 [See more information about frequency and presence penalties.](https://platform.openai.com/docs/guides/text-generation)
56
57 - `Optional<LogitBias> logitBias`
58
59 Modify the likelihood of specified tokens appearing in the completion.
60
61 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.
62
63 As an example, you can pass `{"50256": -100}` to prevent the <|endoftext|> token from being generated.
64
65 - `Optional<Long> logprobs`
66
67 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.
68
69 The maximum value for `logprobs` is 5.
70
71 - `Optional<Long> maxTokens`
72
73 The maximum number of [tokens](/tokenizer) that can be generated in the completion.
74
75 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.
76
77 - `Optional<Long> n`
78
79 How many completions to generate for each prompt.
80
81 **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`.
82
83 - `Optional<Double> presencePenalty`
84
85 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.
86
87 [See more information about frequency and presence penalties.](https://platform.openai.com/docs/guides/text-generation)
88
89 - `Optional<Long> seed`
90
91 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.
92
93 Determinism is not guaranteed, and you should refer to the `system_fingerprint` response parameter to monitor changes in the backend.
94
95 - `Optional<Stop> stop`
96
97 Not supported with latest reasoning models `o3` and `o4-mini`.
98
99 Up to 4 sequences where the API will stop generating further tokens. The
100 returned text will not contain the stop sequence.
101
102 - `String`
103
104 - `List<String>`
105
106 - `Optional<ChatCompletionStreamOptions> streamOptions`
107
108 Options for streaming response. Only set this when you set `stream: true`.
109
110 - `Optional<String> suffix`
111
112 The suffix that comes after a completion of inserted text.
113
114 This parameter is only supported for `gpt-3.5-turbo-instruct`.
115
116 - `Optional<Double> temperature`
117
118 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.
119
120 We generally recommend altering this or `top_p` but not both.
121
122 - `Optional<Double> topP`
123
124 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.
125
126 We generally recommend altering this or `temperature` but not both.
127
128 - `Optional<String> user`
129
130 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).
131
132### Returns
133
134- `class Completion:`
135
136 Represents a completion response from the API. Note: both the streamed and non-streamed response objects share the same shape (unlike the chat endpoint).
137
138 - `String id`
139
140 A unique identifier for the completion.
141
142 - `List<CompletionChoice> choices`
143
144 The list of completion choices the model generated for the input prompt.
145
146 - `FinishReason finishReason`
147
148 The reason the model stopped generating tokens. This will be `stop` if the model hit a natural stop point or a provided stop sequence,
149 `length` if the maximum number of tokens specified in the request was reached,
150 or `content_filter` if content was omitted due to a flag from our content filters.
151
152 - `STOP("stop")`
153
154 - `LENGTH("length")`
155
156 - `CONTENT_FILTER("content_filter")`
157
158 - `long index`
159
160 - `Optional<Logprobs> logprobs`
161
162 - `Optional<List<Long>> textOffset`
163
164 - `Optional<List<Double>> tokenLogprobs`
165
166 - `Optional<List<String>> tokens`
167
168 - `Optional<List<TopLogprob>> topLogprobs`
169
170 - `String text`
171
172 - `long created`
173
174 The Unix timestamp (in seconds) of when the completion was created.
175
176 - `String model`
177
178 The model used for completion.
179
180 - `JsonValue; object_ "text_completion"constant`
181
182 The object type, which is always "text_completion"
183
184 - `TEXT_COMPLETION("text_completion")`
185
186 - `Optional<String> systemFingerprint`
187
188 This fingerprint represents the backend configuration that the model runs with.
189
190 Can be used in conjunction with the `seed` request parameter to understand when backend changes have been made that might impact determinism.
191
192 - `Optional<CompletionUsage> usage`
193
194 Usage statistics for the completion request.
195
196 - `long completionTokens`
197
198 Number of tokens in the generated completion.
199
200 - `long promptTokens`
201
202 Number of tokens in the prompt.
203
204 - `long totalTokens`
205
206 Total number of tokens used in the request (prompt + completion).
207
208 - `Optional<CompletionTokensDetails> completionTokensDetails`
209
210 Breakdown of tokens used in a completion.
211
212 - `Optional<Long> acceptedPredictionTokens`
213
214 When using Predicted Outputs, the number of tokens in the
215 prediction that appeared in the completion.
216
217 - `Optional<Long> audioTokens`
218
219 Audio input tokens generated by the model.
220
221 - `Optional<Long> reasoningTokens`
222
223 Tokens generated by the model for reasoning.
224
225 - `Optional<Long> rejectedPredictionTokens`
226
227 When using Predicted Outputs, the number of tokens in the
228 prediction that did not appear in the completion. However, like
229 reasoning tokens, these tokens are still counted in the total
230 completion tokens for purposes of billing, output, and context window
231 limits.
232
233 - `Optional<PromptTokensDetails> promptTokensDetails`
234
235 Breakdown of tokens used in the prompt.
236
237 - `Optional<Long> audioTokens`
238
239 Audio input tokens present in the prompt.
240
241 - `Optional<Long> cachedTokens`
242
243 Cached tokens present in the prompt.
244
245### Example
246
247```java
248package com.openai.example;
249
250import com.openai.client.OpenAIClient;
251import com.openai.client.okhttp.OpenAIOkHttpClient;
252import com.openai.models.completions.Completion;
253import com.openai.models.completions.CompletionCreateParams;
254
255public final class Main {
256 private Main() {}
257
258 public static void main(String[] args) {
259 OpenAIClient client = OpenAIOkHttpClient.fromEnv();
260
261 CompletionCreateParams params = CompletionCreateParams.builder()
262 .model(CompletionCreateParams.Model.GPT_3_5_TURBO_INSTRUCT)
263 .prompt("This is a test.")
264 .build();
265 Completion completion = client.completions().create(params);
266 }
267}
268```
269
270#### Response
271
272```json
273{
274 "id": "id",
275 "choices": [
276 {
277 "finish_reason": "stop",
278 "index": 0,
279 "logprobs": {
280 "text_offset": [
281 0
282 ],
283 "token_logprobs": [
284 0
285 ],
286 "tokens": [
287 "string"
288 ],
289 "top_logprobs": [
290 {
291 "foo": 0
292 }
293 ]
294 },
295 "text": "text"
296 }
297 ],
298 "created": 0,
299 "model": "model",
300 "object": "text_completion",
301 "system_fingerprint": "system_fingerprint",
302 "usage": {
303 "completion_tokens": 0,
304 "prompt_tokens": 0,
305 "total_tokens": 0,
306 "completion_tokens_details": {
307 "accepted_prediction_tokens": 0,
308 "audio_tokens": 0,
309 "reasoning_tokens": 0,
310 "rejected_prediction_tokens": 0
311 },
312 "prompt_tokens_details": {
313 "audio_tokens": 0,
314 "cached_tokens": 0
315 }
316 }
317}
318```