Skip to content

feat(client): support setting base URL via env var #427

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import com.openai.models.ChatModel;
import com.openai.models.chat.completions.ChatCompletion;
import com.openai.models.chat.completions.ChatCompletionCreateParams;

// Configures using the `OPENAI_API_KEY`, `OPENAI_ORG_ID` and `OPENAI_PROJECT_ID` environment variables
// Configures using the `OPENAI_API_KEY`, `OPENAI_ORG_ID`, `OPENAI_PROJECT_ID` and `OPENAI_BASE_URL` environment variables
OpenAIClient client = OpenAIOkHttpClient.fromEnv();

ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
Expand All @@ -68,7 +68,7 @@ Configure the client using environment variables:
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;

// Configures using the `OPENAI_API_KEY`, `OPENAI_ORG_ID` and `OPENAI_PROJECT_ID` environment variables
// Configures using the `OPENAI_API_KEY`, `OPENAI_ORG_ID`, `OPENAI_PROJECT_ID` and `OPENAI_BASE_URL` environment variables
OpenAIClient client = OpenAIOkHttpClient.fromEnv();
```

Expand All @@ -90,19 +90,20 @@ import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;

OpenAIClient client = OpenAIOkHttpClient.builder()
// Configures using the `OPENAI_API_KEY`, `OPENAI_ORG_ID` and `OPENAI_PROJECT_ID` environment variables
// Configures using the `OPENAI_API_KEY`, `OPENAI_ORG_ID`, `OPENAI_PROJECT_ID` and `OPENAI_BASE_URL` environment variables
.fromEnv()
.apiKey("My API Key")
.build();
```

See this table for the available options:

| Setter | Environment variable | Required | Default value |
| -------------- | -------------------- | -------- | ------------- |
| `apiKey` | `OPENAI_API_KEY` | true | - |
| `organization` | `OPENAI_ORG_ID` | false | - |
| `project` | `OPENAI_PROJECT_ID` | false | - |
| Setter | Environment variable | Required | Default value |
| -------------- | -------------------- | -------- | ----------------------------- |
| `apiKey` | `OPENAI_API_KEY` | true | - |
| `organization` | `OPENAI_ORG_ID` | false | - |
| `project` | `OPENAI_PROJECT_ID` | false | - |
| `baseUrl` | `OPENAI_BASE_URL` | true | `"https://api.openai.com/v1"` |

> [!TIP]
> Don't create more than one client in the same application. Each client has a connection pool and
Expand Down Expand Up @@ -134,7 +135,7 @@ import com.openai.models.chat.completions.ChatCompletion;
import com.openai.models.chat.completions.ChatCompletionCreateParams;
import java.util.concurrent.CompletableFuture;

// Configures using the `OPENAI_API_KEY`, `OPENAI_ORG_ID` and `OPENAI_PROJECT_ID` environment variables
// Configures using the `OPENAI_API_KEY`, `OPENAI_ORG_ID`, `OPENAI_PROJECT_ID` and `OPENAI_BASE_URL` environment variables
OpenAIClient client = OpenAIOkHttpClient.fromEnv();

ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
Expand All @@ -154,7 +155,7 @@ import com.openai.models.chat.completions.ChatCompletion;
import com.openai.models.chat.completions.ChatCompletionCreateParams;
import java.util.concurrent.CompletableFuture;

// Configures using the `OPENAI_API_KEY`, `OPENAI_ORG_ID` and `OPENAI_PROJECT_ID` environment variables
// Configures using the `OPENAI_API_KEY`, `OPENAI_ORG_ID`, `OPENAI_PROJECT_ID` and `OPENAI_BASE_URL` environment variables
OpenAIClientAsync client = OpenAIOkHttpClientAsync.fromEnv();

ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,10 @@ class OpenAIOkHttpClient private constructor() {
class Builder internal constructor() {

private var clientOptions: ClientOptions.Builder = ClientOptions.builder()
private var baseUrl: String = ClientOptions.PRODUCTION_URL
private var timeout: Timeout = Timeout.default()
private var proxy: Proxy? = null

fun baseUrl(baseUrl: String) = apply {
clientOptions.baseUrl(baseUrl)
this.baseUrl = baseUrl
}
fun baseUrl(baseUrl: String) = apply { clientOptions.baseUrl(baseUrl) }

/**
* Whether to throw an exception if any of the Jackson versions detected at runtime are
Expand Down Expand Up @@ -184,7 +180,7 @@ class OpenAIOkHttpClient private constructor() {
clientOptions
.httpClient(
OkHttpClient.builder()
.baseUrl(baseUrl)
.baseUrl(clientOptions.baseUrl())
.timeout(timeout)
.proxy(proxy)
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,10 @@ class OpenAIOkHttpClientAsync private constructor() {
class Builder internal constructor() {

private var clientOptions: ClientOptions.Builder = ClientOptions.builder()
private var baseUrl: String = ClientOptions.PRODUCTION_URL
private var timeout: Timeout = Timeout.default()
private var proxy: Proxy? = null

fun baseUrl(baseUrl: String) = apply {
clientOptions.baseUrl(baseUrl)
this.baseUrl = baseUrl
}
fun baseUrl(baseUrl: String) = apply { clientOptions.baseUrl(baseUrl) }

/**
* Whether to throw an exception if any of the Jackson versions detected at runtime are
Expand Down Expand Up @@ -184,7 +180,7 @@ class OpenAIOkHttpClientAsync private constructor() {
clientOptions
.httpClient(
OkHttpClient.builder()
.baseUrl(baseUrl)
.baseUrl(clientOptions.baseUrl())
.timeout(timeout)
.proxy(proxy)
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,10 @@ private constructor(

fun removeAllQueryParams(keys: Set<String>) = apply { queryParams.removeAll(keys) }

fun baseUrl(): String = baseUrl

fun fromEnv() = apply {
System.getenv("OPENAI_BASE_URL")?.let { baseUrl(it) }
System.getenv("OPENAI_API_KEY")?.let { apiKey(it) }
System.getenv("OPENAI_ORG_ID")?.let { organization(it) }
System.getenv("OPENAI_PROJECT_ID")?.let { project(it) }
Expand Down