You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Alternately, set the environment with `OPENAI_API_KEY`, `OPENAI_ORG_ID` or `OPENAI_PROJECT_ID`, and use `OpenAIOkHttpClient.fromEnv()` to read from the environment.
// Note: you can also call fromEnv() from the client builder, for example if you need to set additional properties
@@ -82,12 +85,14 @@ Read the documentation for more configuration options.
82
85
83
86
### Example: creating a resource
84
87
85
-
To create a new chat completion, first use the `ChatCompletionCreateParams` builder to specify attributes,
86
-
then pass that to the `create` method of the `completions` service.
88
+
To create a new chat completion, first use the `ChatCompletionCreateParams` builder to specify attributes, then pass that to the `create` method of the `completions` service.
@@ -143,14 +150,14 @@ See [Pagination](#pagination) below for more information on transparently workin
143
150
144
151
To make a request to the OpenAI API, you generally build an instance of the appropriate `Params` class.
145
152
146
-
In [Example: creating a resource](#example-creating-a-resource) above, we used the `ChatCompletionCreateParams.builder()` to pass to
147
-
the `create` method of the `completions` service.
153
+
In [Example: creating a resource](#example-creating-a-resource) above, we used the `ChatCompletionCreateParams.builder()` to pass to the `create` method of the `completions` service.
148
154
149
-
Sometimes, the API may support other properties that are not yet supported in the Java SDK types. In that case,
150
-
you can attach them using the `putAdditionalProperty` method.
155
+
Sometimes, the API may support other properties that are not yet supported in the Java SDK types. In that case, you can attach them using the `putAdditionalProperty` method.
When receiving a response, the OpenAI Java SDK will deserialize it into instances of the typed model classes. In rare cases, the API may return a response property that doesn't match the expected Java type. If you directly access the mistaken property, the SDK will throw an unchecked `OpenAIInvalidDataException` at runtime. If you would prefer to check in advance that that response is completely well-typed, call `.validate()` on the returned model.
In rare cases, you may want to access the underlying JSON value for a response property rather than using the typed version provided by
173
-
this SDK. Each model property has a corresponding JSON version, with an underscore before the method name, which returns a `JsonField` value.
181
+
In rare cases, you may want to access the underlying JSON value for a response property rather than using the typed version provided by this SDK. Each model property has a corresponding JSON version, with an underscore before the method name, which returns a `JsonField` value.
174
182
175
183
```java
184
+
importcom.openai.core.JsonField;
185
+
importjava.util.Optional;
186
+
176
187
JsonField field = responseObj._field();
177
188
178
189
if (field.isMissing()) {
@@ -194,24 +205,27 @@ if (field.isMissing()) {
194
205
Sometimes, the server response may include additional properties that are not yet available in this library's types. You can access them using the model's `_additionalProperties` method:
For methods that return a paginated list of results, this library provides convenient ways access
205
-
the results either one page at a time, or item-by-item across all pages.
217
+
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.
206
218
207
219
### Auto-pagination
208
220
209
-
To iterate through all results across all pages, you can use `autoPager`,
210
-
which automatically handles fetching more pages for you:
221
+
To iterate through all results across all pages, you can use `autoPager`, which automatically handles fetching more pages for you:
If none of the above helpers meet your needs, you can also manually request pages one-by-one.
238
-
A page of results has a `data()` method to fetch the list of objects, as well as top-level
239
-
`response` and other methods to fetch top-level data about the page. It also has methods
240
-
`hasNextPage`, `getNextPage`, and `getNextPageParams` methods to help with pagination.
251
+
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.
@@ -258,22 +272,22 @@ This library throws exceptions in a single hierarchy for easy handling:
258
272
259
273
-**`OpenAIException`** - Base exception for all exceptions
260
274
261
-
-**`OpenAIServiceException`** - HTTP errors with a well-formed response body we were able to parse. The exception message and the `.debuggingRequestId()` will be set by the server.
275
+
-**`OpenAIServiceException`** - HTTP errors with a well-formed response body we were able to parse. The exception message and the `.debuggingRequestId()` will be set by the server.
262
276
263
-
| 400 | BadRequestException |
264
-
| ------ | ----------------------------- |
265
-
| 401 | AuthenticationException |
266
-
| 403 | PermissionDeniedException |
267
-
| 404 | NotFoundException |
268
-
| 422 | UnprocessableEntityException |
269
-
| 429 | RateLimitException |
270
-
| 5xx | InternalServerException |
271
-
| others | UnexpectedStatusCodeException |
277
+
| 400 | BadRequestException |
278
+
| ------ | ----------------------------- |
279
+
| 401 | AuthenticationException |
280
+
| 403 | PermissionDeniedException |
281
+
| 404 | NotFoundException |
282
+
| 422 | UnprocessableEntityException |
283
+
| 429 | RateLimitException |
284
+
| 5xx | InternalServerException |
285
+
| others | UnexpectedStatusCodeException |
272
286
273
-
-**`OpenAIIoException`** - I/O networking errors
274
-
-**`OpenAIInvalidDataException`** - any other exceptions on the client side, e.g.:
275
-
- We failed to serialize the request body
276
-
- We failed to parse the response body (has access to response code and body)
287
+
-**`OpenAIIoException`** - I/O networking errors
288
+
-**`OpenAIInvalidDataException`** - any other exceptions on the client side, e.g.:
289
+
- We failed to serialize the request body
290
+
- We failed to parse the response body (has access to response code and body)
277
291
278
292
## Microsoft Azure OpenAI
279
293
@@ -319,10 +333,12 @@ See the complete Azure OpenAI examples in the [Azure OpenAI example](https://git
319
333
320
334
### Retries
321
335
322
-
Requests that experience certain errors are automatically retried 2 times by default, with a short exponential backoff. Connection errors (for example, due to a network connectivity problem), 408 Request Timeout, 409 Conflict, 429 Rate Limit, and >=500 Internal errors will all be retried by default.
323
-
You can provide a `maxRetries` on the client builder to configure this:
336
+
Requests that experience certain errors are automatically retried 2 times by default, with a short exponential backoff. Connection errors (for example, due to a network connectivity problem), 408 Request Timeout, 409 Conflict, 429 Rate Limit, and >=500 Internal errors will all be retried by default. You can provide a `maxRetries` on the client builder to configure this:
This library is typed for convenient access to the documented API. If you need to access undocumented
360
-
params or response properties, the library can still be used.
381
+
This library is typed for convenient access to the documented API. If you need to access undocumented params or response properties, the library can still be used.
361
382
362
383
### Undocumented request params
363
384
364
-
To make requests using undocumented parameters, you can provide or override parameters on the params object
365
-
while building it.
385
+
To make requests using undocumented parameters, you can provide or override parameters on the params object while building it.
To access undocumented response properties, you can use `res._additionalProperties()` on a response object to
377
-
get a map of untyped fields of type `Map<String, JsonValue>`. You can then access fields like
378
-
`._additionalProperties().get("secret_prop").asString()` or use other helpers defined on the `JsonValue` class
379
-
to extract it to a desired type.
396
+
To access undocumented response properties, you can use `res._additionalProperties()` on a response object to get a map of untyped fields of type `Map<String, JsonValue>`. You can then access fields like `._additionalProperties().get("secret_prop").asString()` or use other helpers defined on the `JsonValue` class to extract it to a desired type.
0 commit comments