openai/openai-java
Publicmirrored from https://github.com/openai/openai-javaAvailable
README.md
971lines · modeblame
6eb3f62eStainless Bot3 years ago | 1 | # OpenAI Java API Library |
| 2 | | |
11c62f22Stainless Bot1 years ago | 3 | <!-- x-release-please-start-version --> |
| 4 | | |
de59688dstainless-app[bot]1 years ago | 5 | [](https://central.sonatype.com/artifact/com.openai/openai-java/1.4.0) |
| 6 | [](https://javadoc.io/doc/com.openai/openai-java/1.4.0) | |
6eb3f62eStainless Bot3 years ago | 7 | |
11c62f22Stainless Bot1 years ago | 8 | <!-- x-release-please-end --> |
| 9 | | |
d6a37429stainless-app[bot]1 years ago | 10 | The OpenAI Java SDK provides convenient access to the [OpenAI REST API](https://platform.openai.com/docs) from applications written in Java. |
6eb3f62eStainless Bot3 years ago | 11 | |
22a4cb9astainless-app[bot]1 years ago | 12 | <!-- x-release-please-start-version --> |
| 13 | | |
de59688dstainless-app[bot]1 years ago | 14 | The REST API documentation can be found on [platform.openai.com](https://platform.openai.com/docs). Javadocs are available on [javadoc.io](https://javadoc.io/doc/com.openai/openai-java/1.4.0). |
6eb3f62eStainless Bot3 years ago | 15 | |
22a4cb9astainless-app[bot]1 years ago | 16 | <!-- x-release-please-end --> |
| 17 | | |
1c7fc105stainless-app[bot]1 years ago | 18 | ## Installation |
6eb3f62eStainless Bot3 years ago | 19 | |
11c62f22Stainless Bot1 years ago | 20 | <!-- x-release-please-start-version --> |
| 21 | | |
1c7fc105stainless-app[bot]1 years ago | 22 | ### Gradle |
| 23 | | |
6eb3f62eStainless Bot3 years ago | 24 | ```kotlin |
de59688dstainless-app[bot]1 years ago | 25 | implementation("com.openai:openai-java:1.4.0") |
6eb3f62eStainless Bot3 years ago | 26 | ``` |
| 27 | | |
1c7fc105stainless-app[bot]1 years ago | 28 | ### Maven |
6eb3f62eStainless Bot3 years ago | 29 | |
| 30 | ```xml | |
| 31 | <dependency> | |
d6a37429stainless-app[bot]1 years ago | 32 | <groupId>com.openai</groupId> |
| 33 | <artifactId>openai-java</artifactId> | |
de59688dstainless-app[bot]1 years ago | 34 | <version>1.4.0</version> |
6eb3f62eStainless Bot3 years ago | 35 | </dependency> |
| 36 | ``` | |
| 37 | | |
11c62f22Stainless Bot1 years ago | 38 | <!-- x-release-please-end --> |
| 39 | | |
47ff5b11stainless-app[bot]1 years ago | 40 | ## Requirements |
| 41 | | |
| 42 | This library requires Java 8 or later. | |
| 43 | | |
1c7fc105stainless-app[bot]1 years ago | 44 | ## Usage |
| 45 | | |
8ec229bbTomer Aberbach1 years ago | 46 | See the [`openai-java-example`](openai-java-example/src/main/java/com/openai/example) directory for complete and runnable examples. |
| 47 | | |
589ea32fTomer Aberbach1 years ago | 48 | The primary API for interacting with OpenAI models is the [Responses API](https://platform.openai.com/docs/api-reference/responses). You can generate text from the model with the code below. |
| 49 | | |
| 50 | ```java | |
| 51 | import com.openai.client.OpenAIClient; | |
| 52 | import com.openai.client.okhttp.OpenAIOkHttpClient; | |
| 53 | import com.openai.models.ChatModel; | |
| 54 | import com.openai.models.responses.Response; | |
| 55 | import com.openai.models.responses.ResponseCreateParams; | |
| 56 | | |
| 57 | // Configures using the `OPENAI_API_KEY`, `OPENAI_ORG_ID` and `OPENAI_PROJECT_ID` environment variables | |
| 58 | OpenAIClient client = OpenAIOkHttpClient.fromEnv(); | |
| 59 | | |
| 60 | ResponseCreateParams params = ResponseCreateParams.builder() | |
| 61 | .input("Say this is a test") | |
de59688dstainless-app[bot]1 years ago | 62 | .model(ChatModel.GPT_4_1) |
589ea32fTomer Aberbach1 years ago | 63 | .build(); |
| 64 | Response response = client.responses().create(params); | |
| 65 | ``` | |
| 66 | | |
| 67 | The previous standard (supported indefinitely) for generating text is the [Chat Completions API](https://platform.openai.com/docs/api-reference/chat). You can use that API to generate text from the model with the code below. | |
| 68 | | |
3e7910c3stainless-app[bot]1 years ago | 69 | ```java |
| 70 | import com.openai.client.OpenAIClient; | |
| 71 | import com.openai.client.okhttp.OpenAIOkHttpClient; | |
| 72 | import com.openai.models.ChatModel; | |
1a65445fstainless-app[bot]1 years ago | 73 | import com.openai.models.chat.completions.ChatCompletion; |
| 74 | import com.openai.models.chat.completions.ChatCompletionCreateParams; | |
3e7910c3stainless-app[bot]1 years ago | 75 | |
b8fe68a5stainless-app[bot]1 years ago | 76 | // Configures using the `OPENAI_API_KEY`, `OPENAI_ORG_ID`, `OPENAI_PROJECT_ID` and `OPENAI_BASE_URL` environment variables |
3e7910c3stainless-app[bot]1 years ago | 77 | OpenAIClient client = OpenAIOkHttpClient.fromEnv(); |
| 78 | | |
| 79 | ChatCompletionCreateParams params = ChatCompletionCreateParams.builder() | |
| 80 | .addUserMessage("Say this is a test") | |
2912dacdstainless-app[bot]1 years ago | 81 | .model(ChatModel.GPT_4_1) |
3e7910c3stainless-app[bot]1 years ago | 82 | .build(); |
| 83 | ChatCompletion chatCompletion = client.chat().completions().create(params); | |
| 84 | ``` | |
| 85 | | |
| 86 | ## Client configuration | |
| 87 | | |
| 88 | Configure the client using environment variables: | |
| 89 | | |
| 90 | ```java | |
| 91 | import com.openai.client.OpenAIClient; | |
| 92 | import com.openai.client.okhttp.OpenAIOkHttpClient; | |
6eb3f62eStainless Bot3 years ago | 93 | |
b8fe68a5stainless-app[bot]1 years ago | 94 | // Configures using the `OPENAI_API_KEY`, `OPENAI_ORG_ID`, `OPENAI_PROJECT_ID` and `OPENAI_BASE_URL` environment variables |
3e7910c3stainless-app[bot]1 years ago | 95 | OpenAIClient client = OpenAIOkHttpClient.fromEnv(); |
| 96 | ``` | |
| 97 | | |
| 98 | Or manually: | |
6eb3f62eStainless Bot3 years ago | 99 | |
| 100 | ```java | |
af9f9a27Robert Craigie2 years ago | 101 | import com.openai.client.OpenAIClient; |
| 102 | import com.openai.client.okhttp.OpenAIOkHttpClient; | |
6eb3f62eStainless Bot3 years ago | 103 | |
af9f9a27Robert Craigie2 years ago | 104 | OpenAIClient client = OpenAIOkHttpClient.builder() |
| 105 | .apiKey("My API Key") | |
6eb3f62eStainless Bot3 years ago | 106 | .build(); |
| 107 | ``` | |
| 108 | | |
3e7910c3stainless-app[bot]1 years ago | 109 | Or using a combination of the two approaches: |
6eb3f62eStainless Bot3 years ago | 110 | |
| 111 | ```java | |
a1977ca8stainless-app[bot]1 years ago | 112 | import com.openai.client.OpenAIClient; |
| 113 | import com.openai.client.okhttp.OpenAIOkHttpClient; | |
| 114 | | |
af9f9a27Robert Craigie2 years ago | 115 | OpenAIClient client = OpenAIOkHttpClient.builder() |
b8fe68a5stainless-app[bot]1 years ago | 116 | // Configures using the `OPENAI_API_KEY`, `OPENAI_ORG_ID`, `OPENAI_PROJECT_ID` and `OPENAI_BASE_URL` environment variables |
af9f9a27Robert Craigie2 years ago | 117 | .fromEnv() |
3e7910c3stainless-app[bot]1 years ago | 118 | .apiKey("My API Key") |
af9f9a27Robert Craigie2 years ago | 119 | .build(); |
6eb3f62eStainless Bot3 years ago | 120 | ``` |
| 121 | | |
3e7910c3stainless-app[bot]1 years ago | 122 | See this table for the available options: |
af9f9a27Robert Craigie2 years ago | 123 | |
b8fe68a5stainless-app[bot]1 years ago | 124 | | Setter | Environment variable | Required | Default value | |
| 125 | | -------------- | -------------------- | -------- | ----------------------------- | | |
| 126 | | `apiKey` | `OPENAI_API_KEY` | true | - | | |
| 127 | | `organization` | `OPENAI_ORG_ID` | false | - | | |
| 128 | | `project` | `OPENAI_PROJECT_ID` | false | - | | |
| 129 | | `baseUrl` | `OPENAI_BASE_URL` | true | `"https://api.openai.com/v1"` | | |
6eb3f62eStainless Bot3 years ago | 130 | |
3e7910c3stainless-app[bot]1 years ago | 131 | > [!TIP] |
| 132 | > Don't create more than one client in the same application. Each client has a connection pool and | |
| 133 | > thread pools, which are more efficient to share between requests. | |
6eb3f62eStainless Bot3 years ago | 134 | |
3e7910c3stainless-app[bot]1 years ago | 135 | ## Requests and responses |
6eb3f62eStainless Bot3 years ago | 136 | |
3e7910c3stainless-app[bot]1 years ago | 137 | To send a request to the OpenAI API, build an instance of some `Params` class and pass it to the corresponding client method. When the response is received, it will be deserialized into an instance of a Java class. |
| 138 | | |
| 139 | For example, `client.chat().completions().create(...)` should be called with an instance of `ChatCompletionCreateParams`, and it will return an instance of `ChatCompletion`. | |
| 140 | | |
088a8980stainless-app[bot]1 years ago | 141 | ## Immutability |
| 142 | | |
| 143 | Each class in the SDK has an associated [builder](https://blogs.oracle.com/javamagazine/post/exploring-joshua-blochs-builder-design-pattern-in-java) or factory method for constructing it. | |
| 144 | | |
| 145 | Each class is [immutable](https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html) once constructed. If the class has an associated builder, then it has a `toBuilder()` method, which can be used to convert it back to a builder for making a modified copy. | |
| 146 | | |
| 147 | Because each class is immutable, builder modification will _never_ affect already built class instances. | |
| 148 | | |
3e7910c3stainless-app[bot]1 years ago | 149 | ## Asynchronous execution |
| 150 | | |
| 151 | The default client is synchronous. To switch to asynchronous execution, call the `async()` method: | |
6eb3f62eStainless Bot3 years ago | 152 | |
| 153 | ```java | |
3e7910c3stainless-app[bot]1 years ago | 154 | import com.openai.client.OpenAIClient; |
| 155 | import com.openai.client.okhttp.OpenAIOkHttpClient; | |
a1977ca8stainless-app[bot]1 years ago | 156 | import com.openai.models.ChatModel; |
1a65445fstainless-app[bot]1 years ago | 157 | import com.openai.models.chat.completions.ChatCompletion; |
| 158 | import com.openai.models.chat.completions.ChatCompletionCreateParams; | |
3e7910c3stainless-app[bot]1 years ago | 159 | import java.util.concurrent.CompletableFuture; |
| 160 | | |
b8fe68a5stainless-app[bot]1 years ago | 161 | // Configures using the `OPENAI_API_KEY`, `OPENAI_ORG_ID`, `OPENAI_PROJECT_ID` and `OPENAI_BASE_URL` environment variables |
3e7910c3stainless-app[bot]1 years ago | 162 | OpenAIClient client = OpenAIOkHttpClient.fromEnv(); |
af9f9a27Robert Craigie2 years ago | 163 | |
| 164 | ChatCompletionCreateParams params = ChatCompletionCreateParams.builder() | |
e4537896stainless-app[bot]1 years ago | 165 | .addUserMessage("Say this is a test") |
2912dacdstainless-app[bot]1 years ago | 166 | .model(ChatModel.GPT_4_1) |
6eb3f62eStainless Bot3 years ago | 167 | .build(); |
3e7910c3stainless-app[bot]1 years ago | 168 | CompletableFuture<ChatCompletion> chatCompletion = client.async().chat().completions().create(params); |
6eb3f62eStainless Bot3 years ago | 169 | ``` |
| 170 | | |
3e7910c3stainless-app[bot]1 years ago | 171 | Or create an asynchronous client from the beginning: |
303b5f25Stainless Bot2 years ago | 172 | |
| 173 | ```java | |
3e7910c3stainless-app[bot]1 years ago | 174 | import com.openai.client.OpenAIClientAsync; |
| 175 | import com.openai.client.okhttp.OpenAIOkHttpClientAsync; | |
| 176 | import com.openai.models.ChatModel; | |
1a65445fstainless-app[bot]1 years ago | 177 | import com.openai.models.chat.completions.ChatCompletion; |
| 178 | import com.openai.models.chat.completions.ChatCompletionCreateParams; | |
3e7910c3stainless-app[bot]1 years ago | 179 | import java.util.concurrent.CompletableFuture; |
303b5f25Stainless Bot2 years ago | 180 | |
b8fe68a5stainless-app[bot]1 years ago | 181 | // Configures using the `OPENAI_API_KEY`, `OPENAI_ORG_ID`, `OPENAI_PROJECT_ID` and `OPENAI_BASE_URL` environment variables |
3e7910c3stainless-app[bot]1 years ago | 182 | OpenAIClientAsync client = OpenAIOkHttpClientAsync.fromEnv(); |
| 183 | | |
| 184 | ChatCompletionCreateParams params = ChatCompletionCreateParams.builder() | |
| 185 | .addUserMessage("Say this is a test") | |
2912dacdstainless-app[bot]1 years ago | 186 | .model(ChatModel.GPT_4_1) |
3e7910c3stainless-app[bot]1 years ago | 187 | .build(); |
| 188 | CompletableFuture<ChatCompletion> chatCompletion = client.chat().completions().create(params); | |
303b5f25Stainless Bot2 years ago | 189 | ``` |
| 190 | | |
3e7910c3stainless-app[bot]1 years ago | 191 | The asynchronous client supports the same options as the synchronous one, except most methods return `CompletableFuture`s. |
303b5f25Stainless Bot2 years ago | 192 | |
3e7910c3stainless-app[bot]1 years ago | 193 | ## Streaming |
a1977ca8stainless-app[bot]1 years ago | 194 | |
3e7910c3stainless-app[bot]1 years ago | 195 | The SDK defines methods that return response "chunk" streams, where each chunk can be individually processed as soon as it arrives instead of waiting on the full response. Streaming methods generally correspond to [SSE](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events) or [JSONL](https://jsonlines.org) responses. |
303b5f25Stainless Bot2 years ago | 196 | |
3e7910c3stainless-app[bot]1 years ago | 197 | Some of these methods may have streaming and non-streaming variants, but a streaming method will always have a `Streaming` suffix in its name, even if it doesn't have a non-streaming variant. |
303b5f25Stainless Bot2 years ago | 198 | |
6633c380stainless-app[bot]1 years ago | 199 | These streaming methods return [`StreamResponse`](openai-java-core/src/main/kotlin/com/openai/core/http/StreamResponse.kt) for synchronous clients: |
3e7910c3stainless-app[bot]1 years ago | 200 | |
| 201 | ```java | |
| 202 | import com.openai.core.http.StreamResponse; | |
1a65445fstainless-app[bot]1 years ago | 203 | import com.openai.models.chat.completions.ChatCompletionChunk; |
3e7910c3stainless-app[bot]1 years ago | 204 | |
| 205 | try (StreamResponse<ChatCompletionChunk> streamResponse = client.chat().completions().createStreaming(params)) { | |
| 206 | streamResponse.stream().forEach(chunk -> { | |
| 207 | System.out.println(chunk); | |
| 208 | }); | |
| 209 | System.out.println("No more chunks!"); | |
| 210 | } | |
303b5f25Stainless Bot2 years ago | 211 | ``` |
| 212 | | |
6633c380stainless-app[bot]1 years ago | 213 | Or [`AsyncStreamResponse`](openai-java-core/src/main/kotlin/com/openai/core/http/AsyncStreamResponse.kt) for asynchronous clients: |
303b5f25Stainless Bot2 years ago | 214 | |
3e7910c3stainless-app[bot]1 years ago | 215 | ```java |
| 216 | import com.openai.core.http.AsyncStreamResponse; | |
1a65445fstainless-app[bot]1 years ago | 217 | import com.openai.models.chat.completions.ChatCompletionChunk; |
3e7910c3stainless-app[bot]1 years ago | 218 | import java.util.Optional; |
6eb3f62eStainless Bot3 years ago | 219 | |
3e7910c3stainless-app[bot]1 years ago | 220 | client.async().chat().completions().createStreaming(params).subscribe(chunk -> { |
| 221 | System.out.println(chunk); | |
| 222 | }); | |
| 223 | | |
| 224 | // If you need to handle errors or completion of the stream | |
| 225 | client.async().chat().completions().createStreaming(params).subscribe(new AsyncStreamResponse.Handler<>() { | |
| 226 | @Override | |
| 227 | public void onNext(ChatCompletionChunk chunk) { | |
| 228 | System.out.println(chunk); | |
| 229 | } | |
6eb3f62eStainless Bot3 years ago | 230 | |
3e7910c3stainless-app[bot]1 years ago | 231 | @Override |
| 232 | public void onComplete(Optional<Throwable> error) { | |
| 233 | if (error.isPresent()) { | |
| 234 | System.out.println("Something went wrong!"); | |
| 235 | throw new RuntimeException(error.get()); | |
| 236 | } else { | |
f35de10estainless-app[bot]1 years ago | 237 | System.out.println("No more chunks!"); |
3e7910c3stainless-app[bot]1 years ago | 238 | } |
| 239 | } | |
| 240 | }); | |
f35de10estainless-app[bot]1 years ago | 241 | |
| 242 | // Or use futures | |
| 243 | client.async().chat().completions().createStreaming(params) | |
| 244 | .subscribe(chunk -> { | |
| 245 | System.out.println(chunk); | |
| 246 | }) | |
| 247 | .onCompleteFuture(); | |
| 248 | .whenComplete((unused, error) -> { | |
| 249 | if (error != null) { | |
| 250 | System.out.println("Something went wrong!"); | |
| 251 | throw new RuntimeException(error); | |
| 252 | } else { | |
| 253 | System.out.println("No more chunks!"); | |
| 254 | } | |
| 255 | }); | |
3e7910c3stainless-app[bot]1 years ago | 256 | ``` |
6eb3f62eStainless Bot3 years ago | 257 | |
6633c380stainless-app[bot]1 years ago | 258 | Async streaming uses a dedicated per-client cached thread pool [`Executor`](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Executor.html) to stream without blocking the current thread. This default is suitable for most purposes. |
6eb3f62eStainless Bot3 years ago | 259 | |
3e7910c3stainless-app[bot]1 years ago | 260 | To use a different `Executor`, configure the subscription using the `executor` parameter: |
6eb3f62eStainless Bot3 years ago | 261 | |
3e7910c3stainless-app[bot]1 years ago | 262 | ```java |
| 263 | import java.util.concurrent.Executor; | |
| 264 | import java.util.concurrent.Executors; | |
6eb3f62eStainless Bot3 years ago | 265 | |
3e7910c3stainless-app[bot]1 years ago | 266 | Executor executor = Executors.newFixedThreadPool(4); |
| 267 | client.async().chat().completions().createStreaming(params).subscribe( | |
| 268 | chunk -> System.out.println(chunk), executor | |
| 269 | ); | |
| 270 | ``` | |
6eb3f62eStainless Bot3 years ago | 271 | |
3e7910c3stainless-app[bot]1 years ago | 272 | Or configure the client globally using the `streamHandlerExecutor` method: |
6eb3f62eStainless Bot3 years ago | 273 | |
| 274 | ```java | |
3e7910c3stainless-app[bot]1 years ago | 275 | import com.openai.client.OpenAIClient; |
| 276 | import com.openai.client.okhttp.OpenAIOkHttpClient; | |
| 277 | import java.util.concurrent.Executors; | |
a1977ca8stainless-app[bot]1 years ago | 278 | |
3e7910c3stainless-app[bot]1 years ago | 279 | OpenAIClient client = OpenAIOkHttpClient.builder() |
| 280 | .fromEnv() | |
| 281 | .streamHandlerExecutor(Executors.newFixedThreadPool(4)) | |
| 282 | .build(); | |
6eb3f62eStainless Bot3 years ago | 283 | ``` |
| 284 | | |
25f6953bstainless-app[bot]1 years ago | 285 | ### Streaming helpers |
| 286 | | |
| 287 | The SDK provides conveniences for streamed chat completions. A | |
| 288 | [`ChatCompletionAccumulator`](openai-java-core/src/main/kotlin/com/openai/helpers/ChatCompletionAccumulator.kt) | |
| 289 | can record the stream of chat completion chunks in the response as they are processed and accumulate | |
| 290 | a [`ChatCompletion`](openai-java-core/src/main/kotlin/com/openai/models/chat/completions/ChatCompletion.kt) | |
| 291 | object similar to that which would have been returned by the non-streaming API. | |
| 292 | | |
| 293 | For a synchronous response add a | |
| 294 | [`Stream.peek()`](https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#peek-java.util.function.Consumer-) | |
| 295 | call to the stream pipeline to accumulate each chunk: | |
| 296 | | |
| 297 | ```java | |
| 298 | import com.openai.core.http.StreamResponse; | |
| 299 | import com.openai.helpers.ChatCompletionAccumulator; | |
| 300 | import com.openai.models.chat.completions.ChatCompletion; | |
| 301 | import com.openai.models.chat.completions.ChatCompletionChunk; | |
| 302 | | |
| 303 | ChatCompletionAccumulator chatCompletionAccumulator = ChatCompletionAccumulator.create(); | |
| 304 | | |
| 305 | try (StreamResponse<ChatCompletionChunk> streamResponse = | |
| 306 | client.chat().completions().createStreaming(createParams)) { | |
| 307 | streamResponse.stream() | |
| 308 | .peek(chatCompletionAccumulator::accumulate) | |
| 309 | .flatMap(completion -> completion.choices().stream()) | |
| 310 | .flatMap(choice -> choice.delta().content().stream()) | |
| 311 | .forEach(System.out::print); | |
| 312 | } | |
| 313 | | |
| 314 | ChatCompletion chatCompletion = chatCompletionAccumulator.chatCompletion(); | |
| 315 | ``` | |
| 316 | | |
| 317 | For an asynchronous response, add the `ChatCompletionAccumulator` to the `subscribe()` call: | |
| 318 | | |
| 319 | ```java | |
| 320 | import com.openai.helpers.ChatCompletionAccumulator; | |
| 321 | import com.openai.models.chat.completions.ChatCompletion; | |
| 322 | | |
| 323 | ChatCompletionAccumulator chatCompletionAccumulator = ChatCompletionAccumulator.create(); | |
| 324 | | |
| 325 | client.chat() | |
| 326 | .completions() | |
| 327 | .createStreaming(createParams) | |
| 328 | .subscribe(chunk -> chatCompletionAccumulator.accumulate(chunk).choices().stream() | |
| 329 | .flatMap(choice -> choice.delta().content().stream()) | |
| 330 | .forEach(System.out::print)) | |
| 331 | .onCompleteFuture() | |
| 332 | .join(); | |
| 333 | | |
| 334 | ChatCompletion chatCompletion = chatCompletionAccumulator.chatCompletion(); | |
| 335 | ``` | |
| 336 | | |
a9782b0cstainless-app[bot]1 years ago | 337 | ## File uploads |
| 338 | | |
| 339 | The SDK defines methods that accept files. | |
| 340 | | |
| 341 | To upload a file, pass a [`Path`](https://docs.oracle.com/javase/8/docs/api/java/nio/file/Path.html): | |
| 342 | | |
| 343 | ```java | |
1a65445fstainless-app[bot]1 years ago | 344 | import com.openai.models.files.FileCreateParams; |
| 345 | import com.openai.models.files.FileObject; | |
| 346 | import com.openai.models.files.FilePurpose; | |
a9782b0cstainless-app[bot]1 years ago | 347 | import java.nio.file.Paths; |
| 348 | | |
| 349 | FileCreateParams params = FileCreateParams.builder() | |
| 350 | .purpose(FilePurpose.FINE_TUNE) | |
| 351 | .file(Paths.get("input.jsonl")) | |
| 352 | .build(); | |
| 353 | FileObject fileObject = client.files().create(params); | |
| 354 | ``` | |
| 355 | | |
| 356 | Or an arbitrary [`InputStream`](https://docs.oracle.com/javase/8/docs/api/java/io/InputStream.html): | |
| 357 | | |
| 358 | ```java | |
1a65445fstainless-app[bot]1 years ago | 359 | import com.openai.models.files.FileCreateParams; |
| 360 | import com.openai.models.files.FileObject; | |
| 361 | import com.openai.models.files.FilePurpose; | |
a9782b0cstainless-app[bot]1 years ago | 362 | import java.net.URL; |
| 363 | | |
| 364 | FileCreateParams params = FileCreateParams.builder() | |
| 365 | .purpose(FilePurpose.FINE_TUNE) | |
88df0366stainless-app[bot]1 years ago | 366 | .file(new URL("https://example.com/input.jsonl").openStream()) |
a9782b0cstainless-app[bot]1 years ago | 367 | .build(); |
| 368 | FileObject fileObject = client.files().create(params); | |
| 369 | ``` | |
| 370 | | |
| 371 | Or a `byte[]` array: | |
| 372 | | |
| 373 | ```java | |
1a65445fstainless-app[bot]1 years ago | 374 | import com.openai.models.files.FileCreateParams; |
| 375 | import com.openai.models.files.FileObject; | |
| 376 | import com.openai.models.files.FilePurpose; | |
a9782b0cstainless-app[bot]1 years ago | 377 | |
| 378 | FileCreateParams params = FileCreateParams.builder() | |
| 379 | .purpose(FilePurpose.FINE_TUNE) | |
| 380 | .file("content".getBytes()) | |
| 381 | .build(); | |
| 382 | FileObject fileObject = client.files().create(params); | |
| 383 | ``` | |
| 384 | | |
4453173cstainless-app[bot]1 years ago | 385 | Note that when passing a non-`Path` its filename is unknown so it will not be included in the request. To manually set a filename, pass a [`MultipartField`](openai-java-core/src/main/kotlin/com/openai/core/Values.kt): |
a9782b0cstainless-app[bot]1 years ago | 386 | |
| 387 | ```java | |
| 388 | import com.openai.core.MultipartField; | |
1a65445fstainless-app[bot]1 years ago | 389 | import com.openai.models.files.FileCreateParams; |
| 390 | import com.openai.models.files.FileObject; | |
| 391 | import com.openai.models.files.FilePurpose; | |
a9782b0cstainless-app[bot]1 years ago | 392 | import java.io.InputStream; |
| 393 | import java.net.URL; | |
| 394 | | |
| 395 | FileCreateParams params = FileCreateParams.builder() | |
| 396 | .purpose(FilePurpose.FINE_TUNE) | |
| 397 | .file(MultipartField.<InputStream>builder() | |
88df0366stainless-app[bot]1 years ago | 398 | .value(new URL("https://example.com/input.jsonl").openStream()) |
a9782b0cstainless-app[bot]1 years ago | 399 | .filename("input.jsonl") |
| 400 | .build()) | |
| 401 | .build(); | |
| 402 | FileObject fileObject = client.files().create(params); | |
| 403 | ``` | |
| 404 | | |
3e7910c3stainless-app[bot]1 years ago | 405 | ## Binary responses |
6eb3f62eStainless Bot3 years ago | 406 | |
3e7910c3stainless-app[bot]1 years ago | 407 | The SDK defines methods that return binary responses, which are used for API responses that shouldn't necessarily be parsed, like non-JSON data. |
| 408 | | |
6633c380stainless-app[bot]1 years ago | 409 | These methods return [`HttpResponse`](openai-java-core/src/main/kotlin/com/openai/core/http/HttpResponse.kt): |
6eb3f62eStainless Bot3 years ago | 410 | |
| 411 | ```java | |
3e7910c3stainless-app[bot]1 years ago | 412 | import com.openai.core.http.HttpResponse; |
1a65445fstainless-app[bot]1 years ago | 413 | import com.openai.models.files.FileContentParams; |
a1977ca8stainless-app[bot]1 years ago | 414 | |
3e7910c3stainless-app[bot]1 years ago | 415 | FileContentParams params = FileContentParams.builder() |
| 416 | .fileId("file_id") | |
| 417 | .build(); | |
| 418 | HttpResponse response = client.files().content(params); | |
| 419 | ``` | |
6eb3f62eStainless Bot3 years ago | 420 | |
6633c380stainless-app[bot]1 years ago | 421 | To save the response content to a file, use the [`Files.copy(...)`](https://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#copy-java.io.InputStream-java.nio.file.Path-java.nio.file.CopyOption...-) method: |
6eb3f62eStainless Bot3 years ago | 422 | |
3e7910c3stainless-app[bot]1 years ago | 423 | ```java |
| 424 | import com.openai.core.http.HttpResponse; | |
| 425 | import java.nio.file.Files; | |
| 426 | import java.nio.file.Paths; | |
| 427 | import java.nio.file.StandardCopyOption; | |
| 428 | | |
| 429 | try (HttpResponse response = client.files().content(params)) { | |
| 430 | Files.copy( | |
| 431 | response.body(), | |
| 432 | Paths.get(path), | |
| 433 | StandardCopyOption.REPLACE_EXISTING | |
| 434 | ); | |
| 435 | } catch (Exception e) { | |
| 436 | System.out.println("Something went wrong!"); | |
| 437 | throw new RuntimeException(e); | |
6eb3f62eStainless Bot3 years ago | 438 | } |
| 439 | ``` | |
| 440 | | |
6633c380stainless-app[bot]1 years ago | 441 | Or transfer the response content to any [`OutputStream`](https://docs.oracle.com/javase/8/docs/api/java/io/OutputStream.html): |
6eb3f62eStainless Bot3 years ago | 442 | |
| 443 | ```java | |
3e7910c3stainless-app[bot]1 years ago | 444 | import com.openai.core.http.HttpResponse; |
| 445 | import java.nio.file.Files; | |
| 446 | import java.nio.file.Paths; | |
| 447 | | |
| 448 | try (HttpResponse response = client.files().content(params)) { | |
| 449 | response.body().transferTo(Files.newOutputStream(Paths.get(path))); | |
| 450 | } catch (Exception e) { | |
| 451 | System.out.println("Something went wrong!"); | |
| 452 | throw new RuntimeException(e); | |
| 453 | } | |
6eb3f62eStainless Bot3 years ago | 454 | ``` |
| 455 | | |
cdf6cd11stainless-app[bot]1 years ago | 456 | ## Raw responses |
| 457 | | |
| 458 | The SDK defines methods that deserialize responses into instances of Java classes. However, these methods don't provide access to the response headers, status code, or the raw response body. | |
| 459 | | |
| 460 | To access this data, prefix any HTTP method call on a client or service with `withRawResponse()`: | |
| 461 | | |
| 462 | ```java | |
| 463 | import com.openai.core.http.Headers; | |
| 464 | import com.openai.core.http.HttpResponseFor; | |
| 465 | import com.openai.models.ChatModel; | |
1a65445fstainless-app[bot]1 years ago | 466 | import com.openai.models.chat.completions.ChatCompletion; |
| 467 | import com.openai.models.chat.completions.ChatCompletionCreateParams; | |
cdf6cd11stainless-app[bot]1 years ago | 468 | |
| 469 | ChatCompletionCreateParams params = ChatCompletionCreateParams.builder() | |
| 470 | .addUserMessage("Say this is a test") | |
2912dacdstainless-app[bot]1 years ago | 471 | .model(ChatModel.GPT_4_1) |
cdf6cd11stainless-app[bot]1 years ago | 472 | .build(); |
| 473 | HttpResponseFor<ChatCompletion> chatCompletion = client.chat().completions().withRawResponse().create(params); | |
| 474 | | |
| 475 | int statusCode = chatCompletion.statusCode(); | |
| 476 | Headers headers = chatCompletion.headers(); | |
| 477 | ``` | |
| 478 | | |
| 479 | You can still deserialize the response into an instance of a Java class if needed: | |
| 480 | | |
| 481 | ```java | |
1a65445fstainless-app[bot]1 years ago | 482 | import com.openai.models.chat.completions.ChatCompletion; |
cdf6cd11stainless-app[bot]1 years ago | 483 | |
| 484 | ChatCompletion parsedChatCompletion = chatCompletion.parse(); | |
| 485 | ``` | |
| 486 | | |
5d89bd2astainless-app[bot]1 years ago | 487 | ### Request IDs |
| 488 | | |
| 489 | > For more information on debugging requests, see [the API docs](https://platform.openai.com/docs/api-reference/debugging-requests). | |
| 490 | | |
| 491 | When using raw responses, you can access the `x-request-id` response header using the `requestId()` method: | |
| 492 | | |
| 493 | ```java | |
| 494 | import com.openai.core.http.HttpResponseFor; | |
| 495 | import com.openai.models.chat.completions.ChatCompletion; | |
| 496 | import java.util.Optional; | |
| 497 | | |
| 498 | HttpResponseFor<ChatCompletion> chatCompletion = client.chat().completions().withRawResponse().create(params); | |
| 499 | Optional<String> requestId = chatCompletion.requestId(); | |
| 500 | ``` | |
| 501 | | |
| 502 | This can be used to quickly log failing requests and report them back to OpenAI. | |
| 503 | | |
3e7910c3stainless-app[bot]1 years ago | 504 | ## Error handling |
| 505 | | |
| 506 | The SDK throws custom unchecked exception types: | |
| 507 | | |
6633c380stainless-app[bot]1 years ago | 508 | - [`OpenAIServiceException`](openai-java-core/src/main/kotlin/com/openai/errors/OpenAIServiceException.kt): Base class for HTTP errors. See this table for which exception subclass is thrown for each HTTP status code: |
3e7910c3stainless-app[bot]1 years ago | 509 | |
d6a37429stainless-app[bot]1 years ago | 510 | | Status | Exception | |
| 511 | | ------ | ---------------------------------------------------------------------------------------------------------------------- | | |
| 512 | | 400 | [`BadRequestException`](openai-java-core/src/main/kotlin/com/openai/errors/BadRequestException.kt) | | |
| 513 | | 401 | [`UnauthorizedException`](openai-java-core/src/main/kotlin/com/openai/errors/UnauthorizedException.kt) | | |
| 514 | | 403 | [`PermissionDeniedException`](openai-java-core/src/main/kotlin/com/openai/errors/PermissionDeniedException.kt) | | |
| 515 | | 404 | [`NotFoundException`](openai-java-core/src/main/kotlin/com/openai/errors/NotFoundException.kt) | | |
| 516 | | 422 | [`UnprocessableEntityException`](openai-java-core/src/main/kotlin/com/openai/errors/UnprocessableEntityException.kt) | | |
| 517 | | 429 | [`RateLimitException`](openai-java-core/src/main/kotlin/com/openai/errors/RateLimitException.kt) | | |
| 518 | | 5xx | [`InternalServerException`](openai-java-core/src/main/kotlin/com/openai/errors/InternalServerException.kt) | | |
| 519 | | others | [`UnexpectedStatusCodeException`](openai-java-core/src/main/kotlin/com/openai/errors/UnexpectedStatusCodeException.kt) | | |
| 520 | | |
| 521 | [`SseException`](openai-java-core/src/main/kotlin/com/openai/errors/SseException.kt) is thrown for errors encountered during [SSE streaming](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events) after a successful initial HTTP response. | |
3e7910c3stainless-app[bot]1 years ago | 522 | |
6633c380stainless-app[bot]1 years ago | 523 | - [`OpenAIIoException`](openai-java-core/src/main/kotlin/com/openai/errors/OpenAIIoException.kt): I/O networking errors. |
3e7910c3stainless-app[bot]1 years ago | 524 | |
6633c380stainless-app[bot]1 years ago | 525 | - [`OpenAIInvalidDataException`](openai-java-core/src/main/kotlin/com/openai/errors/OpenAIInvalidDataException.kt): Failure to interpret successfully parsed data. For example, when accessing a property that's supposed to be required, but the API unexpectedly omitted it from the response. |
3e7910c3stainless-app[bot]1 years ago | 526 | |
6633c380stainless-app[bot]1 years ago | 527 | - [`OpenAIException`](openai-java-core/src/main/kotlin/com/openai/errors/OpenAIException.kt): Base class for all exceptions. Most errors will result in one of the previously mentioned ones, but completely generic errors may be thrown using the base class. |
6eb3f62eStainless Bot3 years ago | 528 | |
303b5f25Stainless Bot2 years ago | 529 | ## Pagination |
| 530 | | |
a1977ca8stainless-app[bot]1 years ago | 531 | For methods that return a paginated list of results, this library provides convenient ways access the results either one page at a time, or item-by-item across all pages. |
303b5f25Stainless Bot2 years ago | 532 | |
| 533 | ### Auto-pagination | |
| 534 | | |
a1977ca8stainless-app[bot]1 years ago | 535 | To iterate through all results across all pages, you can use `autoPager`, which automatically handles fetching more pages for you: |
303b5f25Stainless Bot2 years ago | 536 | |
| 537 | ### Synchronous | |
| 538 | | |
| 539 | ```java | |
1a65445fstainless-app[bot]1 years ago | 540 | import com.openai.models.finetuning.jobs.FineTuningJob; |
| 541 | import com.openai.models.finetuning.jobs.JobListPage; | |
a1977ca8stainless-app[bot]1 years ago | 542 | |
303b5f25Stainless Bot2 years ago | 543 | // As an Iterable: |
1a65445fstainless-app[bot]1 years ago | 544 | JobListPage page = client.fineTuning().jobs().list(params); |
303b5f25Stainless Bot2 years ago | 545 | for (FineTuningJob job : page.autoPager()) { |
| 546 | System.out.println(job); | |
| 547 | }; | |
| 548 | | |
| 549 | // As a Stream: | |
| 550 | client.fineTuning().jobs().list(params).autoPager().stream() | |
| 551 | .limit(50) | |
| 552 | .forEach(job -> System.out.println(job)); | |
| 553 | ``` | |
| 554 | | |
| 555 | ### Asynchronous | |
| 556 | | |
| 557 | ```java | |
| 558 | // Using forEach, which returns CompletableFuture<Void>: | |
| 559 | asyncClient.fineTuning().jobs().list(params).autoPager() | |
| 560 | .forEach(job -> System.out.println(job), executor); | |
| 561 | ``` | |
| 562 | | |
| 563 | ### Manual pagination | |
| 564 | | |
a1977ca8stainless-app[bot]1 years ago | 565 | If none of the above helpers meet your needs, you can also manually request pages one-by-one. A page of results has a `data()` method to fetch the list of objects, as well as top-level `response` and other methods to fetch top-level data about the page. It also has methods `hasNextPage`, `getNextPage`, and `getNextPageParams` methods to help with pagination. |
303b5f25Stainless Bot2 years ago | 566 | |
| 567 | ```java | |
1a65445fstainless-app[bot]1 years ago | 568 | import com.openai.models.finetuning.jobs.FineTuningJob; |
| 569 | import com.openai.models.finetuning.jobs.JobListPage; | |
a1977ca8stainless-app[bot]1 years ago | 570 | |
1a65445fstainless-app[bot]1 years ago | 571 | JobListPage page = client.fineTuning().jobs().list(params); |
303b5f25Stainless Bot2 years ago | 572 | while (page != null) { |
| 573 | for (FineTuningJob job : page.data()) { | |
| 574 | System.out.println(job); | |
| 575 | } | |
| 576 | | |
| 577 | page = page.getNextPage().orElse(null); | |
| 578 | } | |
| 579 | ``` | |
| 580 | | |
3e7910c3stainless-app[bot]1 years ago | 581 | ## Logging |
6eb3f62eStainless Bot3 years ago | 582 | |
3e7910c3stainless-app[bot]1 years ago | 583 | The SDK uses the standard [OkHttp logging interceptor](https://github.com/square/okhttp/tree/master/okhttp-logging-interceptor). |
6eb3f62eStainless Bot3 years ago | 584 | |
3e7910c3stainless-app[bot]1 years ago | 585 | Enable logging by setting the `OPENAI_LOG` environment variable to `info`: |
6eb3f62eStainless Bot3 years ago | 586 | |
3e7910c3stainless-app[bot]1 years ago | 587 | ```sh |
| 588 | $ export OPENAI_LOG=info | |
| 589 | ``` | |
6eb3f62eStainless Bot3 years ago | 590 | |
3e7910c3stainless-app[bot]1 years ago | 591 | Or to `debug` for more verbose logging: |
6eb3f62eStainless Bot3 years ago | 592 | |
3e7910c3stainless-app[bot]1 years ago | 593 | ```sh |
| 594 | $ export OPENAI_LOG=debug | |
| 595 | ``` | |
6eb3f62eStainless Bot3 years ago | 596 | |
de59688dstainless-app[bot]1 years ago | 597 | ## Jackson |
| 598 | | |
| 599 | The SDK depends on [Jackson](https://github.com/FasterXML/jackson) for JSON serialization/deserialization. It is compatible with version 2.13.4 or higher, but depends on version 2.18.2 by default. | |
| 600 | | |
| 601 | The SDK throws an exception if it detects an incompatible Jackson version at runtime (e.g. if the default version was overridden in your Maven or Gradle config). | |
| 602 | | |
| 603 | If the SDK threw an exception, but you're _certain_ the version is compatible, then disable the version check using the `checkJacksonVersionCompatibility` on [`OpenAIOkHttpClient`](openai-java-client-okhttp/src/main/kotlin/com/openai/client/okhttp/OpenAIOkHttpClient.kt) or [`OpenAIOkHttpClientAsync`](openai-java-client-okhttp/src/main/kotlin/com/openai/client/okhttp/OpenAIOkHttpClientAsync.kt). | |
| 604 | | |
| 605 | > [!CAUTION] | |
| 606 | > We make no guarantee that the SDK works correctly when the Jackson version check is disabled. | |
| 607 | | |
3e7910c3stainless-app[bot]1 years ago | 608 | ## Microsoft Azure |
f3c75d75Shawn Fang1 years ago | 609 | |
| 610 | To use this library with [Azure OpenAI](https://learn.microsoft.com/azure/ai-services/openai/overview), use the same | |
8ec229bbTomer Aberbach1 years ago | 611 | OpenAI client builder but with the Azure-specific configuration. |
f3c75d75Shawn Fang1 years ago | 612 | |
| 613 | ```java | |
8ec229bbTomer Aberbach1 years ago | 614 | OpenAIClient client = OpenAIOkHttpClient.builder() |
b8fe68a5stainless-app[bot]1 years ago | 615 | // Gets the API key and endpoint from the `AZURE_OPENAI_KEY` and `OPENAI_BASE_URL` environment variables, respectively |
8ec229bbTomer Aberbach1 years ago | 616 | .fromEnv() |
| 617 | // Set the Azure Entra ID | |
| 618 | .credential(BearerTokenCredential.create(AuthenticationUtil.getBearerTokenSupplier( | |
| 619 | new DefaultAzureCredentialBuilder().build(), "https://cognitiveservices.azure.com/.default"))) | |
| 620 | .build(); | |
f3c75d75Shawn Fang1 years ago | 621 | ``` |
| 622 | | |
8ec229bbTomer Aberbach1 years ago | 623 | See the complete Azure OpenAI example in the [`openai-java-example`](openai-java-example/src/main/java/com/openai/example/AzureEntraIdExample.java) directory. The other examples in the directory also work with Azure as long as the client is configured to use it. |
f3c75d75Shawn Fang1 years ago | 624 | |
6eb3f62eStainless Bot3 years ago | 625 | ## Network options |
| 626 | | |
| 627 | ### Retries | |
| 628 | | |
3e7910c3stainless-app[bot]1 years ago | 629 | The SDK automatically retries 2 times by default, with a short exponential backoff. |
| 630 | | |
| 631 | Only the following error types are retried: | |
| 632 | | |
| 633 | - Connection errors (for example, due to a network connectivity problem) | |
| 634 | - 408 Request Timeout | |
| 635 | - 409 Conflict | |
| 636 | - 429 Rate Limit | |
| 637 | - 5xx Internal | |
| 638 | | |
| 639 | The API may also explicitly instruct the SDK to retry or not retry a response. | |
| 640 | | |
| 641 | To set a custom number of retries, configure the client using the `maxRetries` method: | |
6eb3f62eStainless Bot3 years ago | 642 | |
| 643 | ```java | |
a1977ca8stainless-app[bot]1 years ago | 644 | import com.openai.client.OpenAIClient; |
| 645 | import com.openai.client.okhttp.OpenAIOkHttpClient; | |
| 646 | | |
af9f9a27Robert Craigie2 years ago | 647 | OpenAIClient client = OpenAIOkHttpClient.builder() |
6eb3f62eStainless Bot3 years ago | 648 | .fromEnv() |
| 649 | .maxRetries(4) | |
| 650 | .build(); | |
| 651 | ``` | |
| 652 | | |
| 653 | ### Timeouts | |
| 654 | | |
3e7910c3stainless-app[bot]1 years ago | 655 | Requests time out after 10 minutes by default. |
| 656 | | |
| 657 | To set a custom timeout, configure the method call using the `timeout` method: | |
| 658 | | |
| 659 | ```java | |
| 660 | import com.openai.models.ChatModel; | |
1a65445fstainless-app[bot]1 years ago | 661 | import com.openai.models.chat.completions.ChatCompletion; |
| 662 | import com.openai.models.chat.completions.ChatCompletionCreateParams; | |
3e7910c3stainless-app[bot]1 years ago | 663 | |
| 664 | ChatCompletion chatCompletion = client.chat().completions().create( | |
| 665 | params, RequestOptions.builder().timeout(Duration.ofSeconds(30)).build() | |
| 666 | ); | |
| 667 | ``` | |
| 668 | | |
| 669 | Or configure the default for all method calls at the client level: | |
6eb3f62eStainless Bot3 years ago | 670 | |
| 671 | ```java | |
a1977ca8stainless-app[bot]1 years ago | 672 | import com.openai.client.OpenAIClient; |
| 673 | import com.openai.client.okhttp.OpenAIOkHttpClient; | |
| 674 | import java.time.Duration; | |
| 675 | | |
af9f9a27Robert Craigie2 years ago | 676 | OpenAIClient client = OpenAIOkHttpClient.builder() |
6eb3f62eStainless Bot3 years ago | 677 | .fromEnv() |
| 678 | .timeout(Duration.ofSeconds(30)) | |
| 679 | .build(); | |
| 680 | ``` | |
| 681 | | |
| 682 | ### Proxies | |
| 683 | | |
3e7910c3stainless-app[bot]1 years ago | 684 | To route requests through a proxy, configure the client using the `proxy` method: |
6eb3f62eStainless Bot3 years ago | 685 | |
| 686 | ```java | |
a1977ca8stainless-app[bot]1 years ago | 687 | import com.openai.client.OpenAIClient; |
| 688 | import com.openai.client.okhttp.OpenAIOkHttpClient; | |
| 689 | import java.net.InetSocketAddress; | |
| 690 | import java.net.Proxy; | |
| 691 | | |
af9f9a27Robert Craigie2 years ago | 692 | OpenAIClient client = OpenAIOkHttpClient.builder() |
6eb3f62eStainless Bot3 years ago | 693 | .fromEnv() |
3e7910c3stainless-app[bot]1 years ago | 694 | .proxy(new Proxy( |
| 695 | Proxy.Type.HTTP, new InetSocketAddress( | |
| 696 | "https://example.com", 8080 | |
| 697 | ) | |
| 698 | )) | |
6eb3f62eStainless Bot3 years ago | 699 | .build(); |
af9f9a27Robert Craigie2 years ago | 700 | ``` |
| 701 | | |
de59688dstainless-app[bot]1 years ago | 702 | ### Custom HTTP client |
| 703 | | |
| 704 | The SDK consists of three artifacts: | |
| 705 | | |
| 706 | - `openai-java-core` | |
| 707 | - Contains core SDK logic | |
| 708 | - Does not depend on [OkHttp](https://square.github.io/okhttp) | |
| 709 | - Exposes [`OpenAIClient`](openai-java-core/src/main/kotlin/com/openai/client/OpenAIClient.kt), [`OpenAIClientAsync`](openai-java-core/src/main/kotlin/com/openai/client/OpenAIClientAsync.kt), [`OpenAIClientImpl`](openai-java-core/src/main/kotlin/com/openai/client/OpenAIClientImpl.kt), and [`OpenAIClientAsyncImpl`](openai-java-core/src/main/kotlin/com/openai/client/OpenAIClientAsyncImpl.kt), all of which can work with any HTTP client | |
| 710 | - `openai-java-client-okhttp` | |
| 711 | - Depends on [OkHttp](https://square.github.io/okhttp) | |
| 712 | - Exposes [`OpenAIOkHttpClient`](openai-java-client-okhttp/src/main/kotlin/com/openai/client/okhttp/OpenAIOkHttpClient.kt) and [`OpenAIOkHttpClientAsync`](openai-java-client-okhttp/src/main/kotlin/com/openai/client/okhttp/OpenAIOkHttpClientAsync.kt), which provide a way to construct [`OpenAIClientImpl`](openai-java-core/src/main/kotlin/com/openai/client/OpenAIClientImpl.kt) and [`OpenAIClientAsyncImpl`](openai-java-core/src/main/kotlin/com/openai/client/OpenAIClientAsyncImpl.kt), respectively, using OkHttp | |
| 713 | - `openai-java` | |
| 714 | - Depends on and exposes the APIs of both `openai-java-core` and `openai-java-client-okhttp` | |
| 715 | - Does not have its own logic | |
| 716 | | |
| 717 | This structure allows replacing the SDK's default HTTP client without pulling in unnecessary dependencies. | |
| 718 | | |
| 719 | #### Customized [`OkHttpClient`](https://square.github.io/okhttp/3.x/okhttp/okhttp3/OkHttpClient.html) | |
| 720 | | |
| 721 | > [!TIP] | |
| 722 | > Try the available [network options](#network-options) before replacing the default client. | |
| 723 | | |
| 724 | To use a customized `OkHttpClient`: | |
| 725 | | |
| 726 | 1. Replace your [`openai-java` dependency](#installation) with `openai-java-core` | |
| 727 | 2. Copy `openai-java-client-okhttp`'s [`OkHttpClient`](openai-java-client-okhttp/src/main/kotlin/com/openai/client/okhttp/OkHttpClient.kt) class into your code and customize it | |
| 728 | 3. Construct [`OpenAIClientImpl`](openai-java-core/src/main/kotlin/com/openai/client/OpenAIClientImpl.kt) or [`OpenAIClientAsyncImpl`](openai-java-core/src/main/kotlin/com/openai/client/OpenAIClientAsyncImpl.kt), similarly to [`OpenAIOkHttpClient`](openai-java-client-okhttp/src/main/kotlin/com/openai/client/okhttp/OpenAIOkHttpClient.kt) or [`OpenAIOkHttpClientAsync`](openai-java-client-okhttp/src/main/kotlin/com/openai/client/okhttp/OpenAIOkHttpClientAsync.kt), using your customized client | |
| 729 | | |
| 730 | ### Completely custom HTTP client | |
| 731 | | |
| 732 | To use a completely custom HTTP client: | |
| 733 | | |
| 734 | 1. Replace your [`openai-java` dependency](#installation) with `openai-java-core` | |
| 735 | 2. Write a class that implements the [`HttpClient`](openai-java-core/src/main/kotlin/com/openai/core/http/HttpClient.kt) interface | |
| 736 | 3. Construct [`OpenAIClientImpl`](openai-java-core/src/main/kotlin/com/openai/client/OpenAIClientImpl.kt) or [`OpenAIClientAsyncImpl`](openai-java-core/src/main/kotlin/com/openai/client/OpenAIClientAsyncImpl.kt), similarly to [`OpenAIOkHttpClient`](openai-java-client-okhttp/src/main/kotlin/com/openai/client/okhttp/OpenAIOkHttpClient.kt) or [`OpenAIOkHttpClientAsync`](openai-java-client-okhttp/src/main/kotlin/com/openai/client/okhttp/OpenAIOkHttpClientAsync.kt), using your new client class | |
| 737 | | |
3e7910c3stainless-app[bot]1 years ago | 738 | ## Undocumented API functionality |
af9f9a27Robert Craigie2 years ago | 739 | |
3e7910c3stainless-app[bot]1 years ago | 740 | The SDK is typed for convenient usage of the documented API. However, it also supports working with undocumented or not yet supported parts of the API. |
af9f9a27Robert Craigie2 years ago | 741 | |
3e7910c3stainless-app[bot]1 years ago | 742 | ### Parameters |
9d90f9ebstainless-app[bot]1 years ago | 743 | |
3e7910c3stainless-app[bot]1 years ago | 744 | To set undocumented parameters, call the `putAdditionalHeader`, `putAdditionalQueryParam`, or `putAdditionalBodyProperty` methods on any `Params` class: |
af9f9a27Robert Craigie2 years ago | 745 | |
5241c81estainless-app[bot]1 years ago | 746 | ```java |
9d90f9ebstainless-app[bot]1 years ago | 747 | import com.openai.core.JsonValue; |
1a65445fstainless-app[bot]1 years ago | 748 | import com.openai.models.chat.completions.ChatCompletionCreateParams; |
9d90f9ebstainless-app[bot]1 years ago | 749 | |
| 750 | ChatCompletionCreateParams params = ChatCompletionCreateParams.builder() | |
| 751 | .putAdditionalHeader("Secret-Header", "42") | |
| 752 | .putAdditionalQueryParam("secret_query_param", "42") | |
| 753 | .putAdditionalBodyProperty("secretProperty", JsonValue.from("42")) | |
af9f9a27Robert Craigie2 years ago | 754 | .build(); |
| 755 | ``` | |
| 756 | | |
d3df21f3stainless-app[bot]1 years ago | 757 | These can be accessed on the built object later using the `_additionalHeaders()`, `_additionalQueryParams()`, and `_additionalBodyProperties()` methods. |
| 758 | | |
| 759 | To set undocumented parameters on _nested_ headers, query params, or body classes, call the `putAdditionalProperty` method on the nested class: | |
| 760 | | |
| 761 | ```java | |
| 762 | import com.openai.core.JsonValue; | |
1a65445fstainless-app[bot]1 years ago | 763 | import com.openai.models.chat.completions.ChatCompletionCreateParams; |
d3df21f3stainless-app[bot]1 years ago | 764 | |
| 765 | ChatCompletionCreateParams params = ChatCompletionCreateParams.builder() | |
| 766 | .responseFormat(ChatCompletionCreateParams.ResponseFormat.builder() | |
| 767 | .putAdditionalProperty("secretProperty", JsonValue.from("42")) | |
| 768 | .build()) | |
| 769 | .build(); | |
| 770 | ``` | |
| 771 | | |
| 772 | These properties can be accessed on the nested built object later using the `_additionalProperties()` method. | |
9d90f9ebstainless-app[bot]1 years ago | 773 | |
4453173cstainless-app[bot]1 years ago | 774 | To set a documented parameter or property to an undocumented or not yet supported _value_, pass a [`JsonValue`](openai-java-core/src/main/kotlin/com/openai/core/Values.kt) object to its setter: |
af9f9a27Robert Craigie2 years ago | 775 | |
3e7910c3stainless-app[bot]1 years ago | 776 | ```java |
| 777 | import com.openai.core.JsonValue; | |
1a65445fstainless-app[bot]1 years ago | 778 | import com.openai.models.chat.completions.ChatCompletionCreateParams; |
af9f9a27Robert Craigie2 years ago | 779 | |
3e7910c3stainless-app[bot]1 years ago | 780 | ChatCompletionCreateParams params = ChatCompletionCreateParams.builder() |
| 781 | .addUserMessage("Say this is a test") | |
| 782 | .model(JsonValue.from(42)) | |
| 783 | .build(); | |
| 784 | ``` | |
7944e86cstainless-app[bot]1 years ago | 785 | |
4453173cstainless-app[bot]1 years ago | 786 | The most straightforward way to create a [`JsonValue`](openai-java-core/src/main/kotlin/com/openai/core/Values.kt) is using its `from(...)` method: |
| 787 | | |
| 788 | ```java | |
| 789 | import com.openai.core.JsonValue; | |
| 790 | import java.util.List; | |
| 791 | import java.util.Map; | |
| 792 | | |
| 793 | // Create primitive JSON values | |
| 794 | JsonValue nullValue = JsonValue.from(null); | |
| 795 | JsonValue booleanValue = JsonValue.from(true); | |
| 796 | JsonValue numberValue = JsonValue.from(42); | |
| 797 | JsonValue stringValue = JsonValue.from("Hello World!"); | |
| 798 | | |
| 799 | // Create a JSON array value equivalent to `["Hello", "World"]` | |
| 800 | JsonValue arrayValue = JsonValue.from(List.of( | |
| 801 | "Hello", "World" | |
| 802 | )); | |
| 803 | | |
| 804 | // Create a JSON object value equivalent to `{ "a": 1, "b": 2 }` | |
| 805 | JsonValue objectValue = JsonValue.from(Map.of( | |
| 806 | "a", 1, | |
| 807 | "b", 2 | |
| 808 | )); | |
| 809 | | |
| 810 | // Create an arbitrarily nested JSON equivalent to: | |
| 811 | // { | |
| 812 | // "a": [1, 2], | |
| 813 | // "b": [3, 4] | |
| 814 | // } | |
| 815 | JsonValue complexValue = JsonValue.from(Map.of( | |
| 816 | "a", List.of( | |
| 817 | 1, 2 | |
| 818 | ), | |
| 819 | "b", List.of( | |
| 820 | 3, 4 | |
| 821 | ) | |
| 822 | )); | |
| 823 | ``` | |
| 824 | | |
7f36e508stainless-app[bot]1 years ago | 825 | Normally a `Builder` class's `build` method will throw [`IllegalStateException`](https://docs.oracle.com/javase/8/docs/api/java/lang/IllegalStateException.html) if any required parameter or property is unset. |
| 826 | | |
| 827 | To forcibly omit a required parameter or property, pass [`JsonMissing`](openai-java-core/src/main/kotlin/com/openai/core/Values.kt): | |
| 828 | | |
| 829 | ```java | |
| 830 | import com.openai.core.JsonMissing; | |
| 831 | import com.openai.models.ChatModel; | |
| 832 | import com.openai.models.chat.completions.ChatCompletionCreateParams; | |
| 833 | | |
| 834 | ChatCompletionCreateParams params = ChatCompletionCreateParams.builder() | |
2912dacdstainless-app[bot]1 years ago | 835 | .model(ChatModel.GPT_4_1) |
7f36e508stainless-app[bot]1 years ago | 836 | .messages(JsonMissing.of()) |
| 837 | .build(); | |
| 838 | ``` | |
| 839 | | |
3e7910c3stainless-app[bot]1 years ago | 840 | ### Response properties |
7944e86cstainless-app[bot]1 years ago | 841 | |
3e7910c3stainless-app[bot]1 years ago | 842 | To access undocumented response properties, call the `_additionalProperties()` method: |
7944e86cstainless-app[bot]1 years ago | 843 | |
3e7910c3stainless-app[bot]1 years ago | 844 | ```java |
| 845 | import com.openai.core.JsonValue; | |
| 846 | import java.util.Map; | |
| 847 | | |
| 848 | Map<String, JsonValue> additionalProperties = client.chat().completions().create(params)._additionalProperties(); | |
| 849 | JsonValue secretPropertyValue = additionalProperties.get("secretProperty"); | |
| 850 | | |
| 851 | String result = secretPropertyValue.accept(new JsonValue.Visitor<>() { | |
| 852 | @Override | |
| 853 | public String visitNull() { | |
| 854 | return "It's null!"; | |
| 855 | } | |
| 856 | | |
| 857 | @Override | |
| 858 | public String visitBoolean(boolean value) { | |
| 859 | return "It's a boolean!"; | |
| 860 | } | |
| 861 | | |
| 862 | @Override | |
| 863 | public String visitNumber(Number value) { | |
| 864 | return "It's a number!"; | |
| 865 | } | |
| 866 | | |
| 867 | // Other methods include `visitMissing`, `visitString`, `visitArray`, and `visitObject` | |
| 868 | // The default implementation of each unimplemented method delegates to `visitDefault`, which throws by default, but can also be overridden | |
| 869 | }); | |
7944e86cstainless-app[bot]1 years ago | 870 | ``` |
| 871 | | |
3e7910c3stainless-app[bot]1 years ago | 872 | To access a property's raw JSON value, which may be undocumented, call its `_` prefixed method: |
7944e86cstainless-app[bot]1 years ago | 873 | |
3e7910c3stainless-app[bot]1 years ago | 874 | ```java |
| 875 | import com.openai.core.JsonField; | |
1a65445fstainless-app[bot]1 years ago | 876 | import com.openai.models.chat.completions.ChatCompletionMessageParam; |
3e7910c3stainless-app[bot]1 years ago | 877 | import java.util.Optional; |
| 878 | | |
| 879 | JsonField<List<ChatCompletionMessageParam>> messages = client.chat().completions().create(params)._messages(); | |
| 880 | | |
| 881 | if (messages.isMissing()) { | |
| 882 | // The property is absent from the JSON response | |
| 883 | } else if (messages.isNull()) { | |
| 884 | // The property was set to literal null | |
| 885 | } else { | |
| 886 | // Check if value was provided as a string | |
| 887 | // Other methods include `asNumber()`, `asBoolean()`, etc. | |
| 888 | Optional<String> jsonString = messages.asString(); | |
| 889 | | |
| 890 | // Try to deserialize into a custom type | |
| 891 | MyClass myObject = messages.asUnknown().orElseThrow().convert(MyClass.class); | |
| 892 | } | |
| 893 | ``` | |
| 894 | | |
| 895 | ### Response validation | |
| 896 | | |
| 897 | In rare cases, the API may return a response that doesn't match the expected type. For example, the SDK may expect a property to contain a `String`, but the API could return something else. | |
| 898 | | |
6633c380stainless-app[bot]1 years ago | 899 | By default, the SDK will not throw an exception in this case. It will throw [`OpenAIInvalidDataException`](openai-java-core/src/main/kotlin/com/openai/errors/OpenAIInvalidDataException.kt) only if you directly access the property. |
3e7910c3stainless-app[bot]1 years ago | 900 | |
| 901 | If you would prefer to check that the response is completely well-typed upfront, then either call `validate()`: | |
| 902 | | |
| 903 | ```java | |
1a65445fstainless-app[bot]1 years ago | 904 | import com.openai.models.chat.completions.ChatCompletion; |
3e7910c3stainless-app[bot]1 years ago | 905 | |
| 906 | ChatCompletion chatCompletion = client.chat().completions().create(params).validate(); | |
| 907 | ``` | |
| 908 | | |
| 909 | Or configure the method call to validate the response using the `responseValidation` method: | |
| 910 | | |
| 911 | ```java | |
| 912 | import com.openai.models.ChatModel; | |
1a65445fstainless-app[bot]1 years ago | 913 | import com.openai.models.chat.completions.ChatCompletion; |
| 914 | import com.openai.models.chat.completions.ChatCompletionCreateParams; | |
3e7910c3stainless-app[bot]1 years ago | 915 | |
| 916 | ChatCompletion chatCompletion = client.chat().completions().create( | |
| 917 | params, RequestOptions.builder().responseValidation(true).build() | |
| 918 | ); | |
| 919 | ``` | |
| 920 | | |
| 921 | Or configure the default for all method calls at the client level: | |
| 922 | | |
| 923 | ```java | |
| 924 | import com.openai.client.OpenAIClient; | |
| 925 | import com.openai.client.okhttp.OpenAIOkHttpClient; | |
| 926 | | |
| 927 | OpenAIClient client = OpenAIOkHttpClient.builder() | |
| 928 | .fromEnv() | |
| 929 | .responseValidation(true) | |
| 930 | .build(); | |
7944e86cstainless-app[bot]1 years ago | 931 | ``` |
| 932 | | |
f92bf999stainless-app[bot]1 years ago | 933 | ## FAQ |
| 934 | | |
| 935 | ### Why don't you use plain `enum` classes? | |
| 936 | | |
| 937 | Java `enum` classes are not trivially [forwards compatible](https://www.stainless.com/blog/making-java-enums-forwards-compatible). Using them in the SDK could cause runtime exceptions if the API is updated to respond with a new enum value. | |
| 938 | | |
| 939 | ### Why do you represent fields using `JsonField<T>` instead of just plain `T`? | |
| 940 | | |
| 941 | Using `JsonField<T>` enables a few features: | |
| 942 | | |
| 943 | - Allowing usage of [undocumented API functionality](#undocumented-api-functionality) | |
| 944 | - Lazily [validating the API response against the expected shape](#response-validation) | |
| 945 | - Representing absent vs explicitly null values | |
| 946 | | |
| 947 | ### Why don't you use [`data` classes](https://kotlinlang.org/docs/data-classes.html)? | |
| 948 | | |
| 949 | It is not [backwards compatible to add new fields to a data class](https://kotlinlang.org/docs/api-guidelines-backward-compatibility.html#avoid-using-data-classes-in-your-api) and we don't want to introduce a breaking change every time we add a field to a class. | |
| 950 | | |
| 951 | ### Why don't you use checked exceptions? | |
| 952 | | |
| 953 | Checked exceptions are widely considered a mistake in the Java programming language. In fact, they were omitted from Kotlin for this reason. | |
| 954 | | |
| 955 | Checked exceptions: | |
| 956 | | |
| 957 | - Are verbose to handle | |
| 958 | - Encourage error handling at the wrong level of abstraction, where nothing can be done about the error | |
| 959 | - Are tedious to propagate due to the [function coloring problem](https://journal.stuffwithstuff.com/2015/02/01/what-color-is-your-function) | |
| 960 | - Don't play well with lambdas (also due to the function coloring problem) | |
| 961 | | |
af9f9a27Robert Craigie2 years ago | 962 | ## Semantic versioning |
| 963 | | |
| 964 | This package generally follows [SemVer](https://semver.org/spec/v2.0.0.html) conventions, though certain backwards-incompatible changes may be released as minor versions: | |
| 965 | | |
ce5c7803stainless-app[bot]1 years ago | 966 | 1. Changes to library internals which are technically public but not intended or documented for external use. _(Please open a GitHub issue to let us know if you are relying on such internals.)_ |
af9f9a27Robert Craigie2 years ago | 967 | 2. Changes that we do not expect to impact the vast majority of users in practice. |
| 968 | | |
| 969 | We take backwards-compatibility seriously and work hard to ensure you can rely on a smooth upgrade experience. | |
| 970 | | |
11c62f22Stainless Bot1 years ago | 971 | We are keen for your feedback; please open an [issue](https://www.github.com/openai/openai-java/issues) with questions, bugs, or suggestions. |