Skip to content

Commit 82bb2c1

Browse files
feat(client): allow configuring env via system properties
1 parent f9cbca2 commit 82bb2c1

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

README.md

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ import com.openai.models.ChatModel;
7373
import com.openai.models.chat.completions.ChatCompletion;
7474
import com.openai.models.chat.completions.ChatCompletionCreateParams;
7575

76-
// Configures using the `OPENAI_API_KEY`, `OPENAI_ORG_ID`, `OPENAI_PROJECT_ID`, `OPENAI_WEBHOOK_SECRET` and `OPENAI_BASE_URL` environment variables
76+
// Configures using the `openai.apiKey`, `openai.orgId`, `openai.projectId`, `openai.webhookSecret` and `openai.baseUrl` system properties
77+
// Or configures using the `OPENAI_API_KEY`, `OPENAI_ORG_ID`, `OPENAI_PROJECT_ID`, `OPENAI_WEBHOOK_SECRET` and `OPENAI_BASE_URL` environment variables
7778
OpenAIClient client = OpenAIOkHttpClient.fromEnv();
7879

7980
ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
@@ -85,13 +86,14 @@ ChatCompletion chatCompletion = client.chat().completions().create(params);
8586

8687
## Client configuration
8788

88-
Configure the client using environment variables:
89+
Configure the client using system properties or environment variables:
8990

9091
```java
9192
import com.openai.client.OpenAIClient;
9293
import com.openai.client.okhttp.OpenAIOkHttpClient;
9394

94-
// Configures using the `OPENAI_API_KEY`, `OPENAI_ORG_ID`, `OPENAI_PROJECT_ID`, `OPENAI_WEBHOOK_SECRET` and `OPENAI_BASE_URL` environment variables
95+
// Configures using the `openai.apiKey`, `openai.orgId`, `openai.projectId`, `openai.webhookSecret` and `openai.baseUrl` system properties
96+
// Or configures using the `OPENAI_API_KEY`, `OPENAI_ORG_ID`, `OPENAI_PROJECT_ID`, `OPENAI_WEBHOOK_SECRET` and `OPENAI_BASE_URL` environment variables
9597
OpenAIClient client = OpenAIOkHttpClient.fromEnv();
9698
```
9799

@@ -113,21 +115,24 @@ import com.openai.client.OpenAIClient;
113115
import com.openai.client.okhttp.OpenAIOkHttpClient;
114116

115117
OpenAIClient client = OpenAIOkHttpClient.builder()
116-
// Configures using the `OPENAI_API_KEY`, `OPENAI_ORG_ID`, `OPENAI_PROJECT_ID`, `OPENAI_WEBHOOK_SECRET` and `OPENAI_BASE_URL` environment variables
118+
// Configures using the `openai.apiKey`, `openai.orgId`, `openai.projectId`, `openai.webhookSecret` and `openai.baseUrl` system properties
119+
Or configures using the `OPENAI_API_KEY`, `OPENAI_ORG_ID`, `OPENAI_PROJECT_ID`, `OPENAI_WEBHOOK_SECRET` and `OPENAI_BASE_URL` environment variables
117120
.fromEnv()
118121
.apiKey("My API Key")
119122
.build();
120123
```
121124

122125
See this table for the available options:
123126

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-
| `webhookSecret` | `OPENAI_WEBHOOK_SECRET` | false | - |
130-
| `baseUrl` | `OPENAI_BASE_URL` | true | `"https://api.openai.com/v1"` |
127+
| Setter | System property | Environment variable | Required | Default value |
128+
| --------------- | ---------------------- | ----------------------- | -------- | ----------------------------- |
129+
| `apiKey` | `openai.apiKey` | `OPENAI_API_KEY` | true | - |
130+
| `organization` | `openai.orgId` | `OPENAI_ORG_ID` | false | - |
131+
| `project` | `openai.projectId` | `OPENAI_PROJECT_ID` | false | - |
132+
| `webhookSecret` | `openai.webhookSecret` | `OPENAI_WEBHOOK_SECRET` | false | - |
133+
| `baseUrl` | `openai.baseUrl` | `OPENAI_BASE_URL` | true | `"https://api.openai.com/v1"` |
134+
135+
System properties take precedence over environment variables.
131136

132137
> [!TIP]
133138
> Don't create more than one client in the same application. Each client has a connection pool and
@@ -174,7 +179,8 @@ import com.openai.models.chat.completions.ChatCompletion;
174179
import com.openai.models.chat.completions.ChatCompletionCreateParams;
175180
import java.util.concurrent.CompletableFuture;
176181

177-
// Configures using the `OPENAI_API_KEY`, `OPENAI_ORG_ID`, `OPENAI_PROJECT_ID`, `OPENAI_WEBHOOK_SECRET` and `OPENAI_BASE_URL` environment variables
182+
// Configures using the `openai.apiKey`, `openai.orgId`, `openai.projectId`, `openai.webhookSecret` and `openai.baseUrl` system properties
183+
// Or configures using the `OPENAI_API_KEY`, `OPENAI_ORG_ID`, `OPENAI_PROJECT_ID`, `OPENAI_WEBHOOK_SECRET` and `OPENAI_BASE_URL` environment variables
178184
OpenAIClient client = OpenAIOkHttpClient.fromEnv();
179185

180186
ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
@@ -194,7 +200,8 @@ import com.openai.models.chat.completions.ChatCompletion;
194200
import com.openai.models.chat.completions.ChatCompletionCreateParams;
195201
import java.util.concurrent.CompletableFuture;
196202

197-
// Configures using the `OPENAI_API_KEY`, `OPENAI_ORG_ID`, `OPENAI_PROJECT_ID`, `OPENAI_WEBHOOK_SECRET` and `OPENAI_BASE_URL` environment variables
203+
// Configures using the `openai.apiKey`, `openai.orgId`, `openai.projectId`, `openai.webhookSecret` and `openai.baseUrl` system properties
204+
// Or configures using the `OPENAI_API_KEY`, `OPENAI_ORG_ID`, `OPENAI_PROJECT_ID`, `OPENAI_WEBHOOK_SECRET` and `OPENAI_BASE_URL` environment variables
198205
OpenAIClientAsync client = OpenAIOkHttpClientAsync.fromEnv();
199206

200207
ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()

0 commit comments

Comments
 (0)