Skip to content

Commit a1977ca

Browse files
chore(docs): update readme (#58)
1 parent f6320fa commit a1977ca

File tree

1 file changed

+65
-48
lines changed

1 file changed

+65
-48
lines changed

README.md

Lines changed: 65 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ OpenAIClient client = OpenAIOkHttpClient.builder()
6161
Alternately, set the environment with `OPENAI_API_KEY`, `OPENAI_ORG_ID` or `OPENAI_PROJECT_ID`, and use `OpenAIOkHttpClient.fromEnv()` to read from the environment.
6262

6363
```java
64+
import com.openai.client.OpenAIClient;
65+
import com.openai.client.okhttp.OpenAIOkHttpClient;
66+
6467
OpenAIClient client = OpenAIOkHttpClient.fromEnv();
6568

6669
// 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.
8285

8386
### Example: creating a resource
8487

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.
8789

8890
```java
8991
import com.openai.models.ChatCompletion;
9092
import com.openai.models.ChatCompletionCreateParams;
93+
import com.openai.models.ChatCompletionMessageParam;
94+
import com.openai.models.ChatCompletionUserMessageParam;
95+
import com.openai.models.ChatModel;
9196
import java.util.List;
9297

9398
ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
@@ -102,12 +107,11 @@ ChatCompletion chatCompletion = client.chat().completions().create(params);
102107

103108
### Example: listing resources
104109

105-
The OpenAI API provides a `list` method to get a paginated list of jobs.
106-
You can retrieve the first page by:
110+
The OpenAI API provides a `list` method to get a paginated list of jobs. You can retrieve the first page by:
107111

108112
```java
109113
import com.openai.models.FineTuningJob;
110-
import com.openai.models.Page;
114+
import com.openai.models.FineTuningJobListPage;
111115

112116
FineTuningJobListPage page = client.fineTuning().jobs().list();
113117
for (FineTuningJob job : page.data()) {
@@ -118,6 +122,9 @@ for (FineTuningJob job : page.data()) {
118122
Use the `FineTuningJobListParams` builder to set parameters:
119123

120124
```java
125+
import com.openai.models.FineTuningJobListPage;
126+
import com.openai.models.FineTuningJobListParams;
127+
121128
FineTuningJobListParams params = FineTuningJobListParams.builder()
122129
.after("after")
123130
.limit(20L)
@@ -143,14 +150,14 @@ See [Pagination](#pagination) below for more information on transparently workin
143150

144151
To make a request to the OpenAI API, you generally build an instance of the appropriate `Params` class.
145152

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.
148154

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.
151156

152157
```java
153-
import com.openai.models.core.JsonValue;
158+
import com.openai.core.JsonValue;
159+
import com.openai.models.ChatCompletionCreateParams;
160+
154161
ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
155162
// ... normal properties
156163
.putAdditionalProperty("secret_param", JsonValue.from("4242"))
@@ -164,15 +171,19 @@ ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
164171
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.
165172

166173
```java
174+
import com.openai.models.ChatCompletion;
175+
167176
ChatCompletion chatCompletion = client.chat().completions().create().validate();
168177
```
169178

170179
### Response properties as JSON
171180

172-
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.
174182

175183
```java
184+
import com.openai.core.JsonField;
185+
import java.util.Optional;
186+
176187
JsonField field = responseObj._field();
177188

178189
if (field.isMissing()) {
@@ -194,24 +205,27 @@ if (field.isMissing()) {
194205
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:
195206

196207
```java
208+
import com.openai.core.JsonValue;
209+
197210
JsonValue secret = errorObject._additionalProperties().get("secret_field");
198211
```
199212

200213
---
201214

202215
## Pagination
203216

204-
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.
206218

207219
### Auto-pagination
208220

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:
211222

212223
### Synchronous
213224

214225
```java
226+
import com.openai.models.FineTuningJob;
227+
import com.openai.models.FineTuningJobListPage;
228+
215229
// As an Iterable:
216230
FineTuningJobListPage page = client.fineTuning().jobs().list(params);
217231
for (FineTuningJob job : page.autoPager()) {
@@ -234,12 +248,12 @@ asyncClient.fineTuning().jobs().list(params).autoPager()
234248

235249
### Manual pagination
236250

237-
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.
241252

242253
```java
254+
import com.openai.models.FineTuningJob;
255+
import com.openai.models.FineTuningJobListPage;
256+
243257
FineTuningJobListPage page = client.fineTuning().jobs().list(params);
244258
while (page != null) {
245259
for (FineTuningJob job : page.data()) {
@@ -258,22 +272,22 @@ This library throws exceptions in a single hierarchy for easy handling:
258272

259273
- **`OpenAIException`** - Base exception for all exceptions
260274

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.
262276

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 |
272286

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)
277291

278292
## Microsoft Azure OpenAI
279293

@@ -319,10 +333,12 @@ See the complete Azure OpenAI examples in the [Azure OpenAI example](https://git
319333

320334
### Retries
321335

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:
324337

325338
```java
339+
import com.openai.client.OpenAIClient;
340+
import com.openai.client.okhttp.OpenAIOkHttpClient;
341+
326342
OpenAIClient client = OpenAIOkHttpClient.builder()
327343
.fromEnv()
328344
.maxRetries(4)
@@ -334,6 +350,10 @@ OpenAIClient client = OpenAIOkHttpClient.builder()
334350
Requests time out after 10 minutes by default. You can configure this on the client builder:
335351

336352
```java
353+
import com.openai.client.OpenAIClient;
354+
import com.openai.client.okhttp.OpenAIOkHttpClient;
355+
import java.time.Duration;
356+
337357
OpenAIClient client = OpenAIOkHttpClient.builder()
338358
.fromEnv()
339359
.timeout(Duration.ofSeconds(30))
@@ -345,24 +365,24 @@ OpenAIClient client = OpenAIOkHttpClient.builder()
345365
Requests can be routed through a proxy. You can configure this on the client builder:
346366

347367
```java
368+
import com.openai.client.OpenAIClient;
369+
import com.openai.client.okhttp.OpenAIOkHttpClient;
370+
import java.net.InetSocketAddress;
371+
import java.net.Proxy;
372+
348373
OpenAIClient client = OpenAIOkHttpClient.builder()
349374
.fromEnv()
350-
.proxy(new Proxy(
351-
Type.HTTP,
352-
new InetSocketAddress("proxy.com", 8080)
353-
))
375+
.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("example.com", 8080)))
354376
.build();
355377
```
356378

357379
## Making custom/undocumented requests
358380

359-
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.
361382

362383
### Undocumented request params
363384

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.
366386

367387
```kotlin
368388
FooCreateParams address = FooCreateParams.builder()
@@ -373,10 +393,7 @@ FooCreateParams address = FooCreateParams.builder()
373393

374394
### Undocumented response properties
375395

376-
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.
380397

381398
## Logging
382399

0 commit comments

Comments
 (0)