diff --git a/README.md b/README.md index c5d4a86a..3c4baed1 100644 --- a/README.md +++ b/README.md @@ -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() @@ -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(); ``` @@ -90,7 +90,7 @@ 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(); @@ -98,11 +98,12 @@ OpenAIClient client = OpenAIOkHttpClient.builder() 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 @@ -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() @@ -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() diff --git a/openai-java-client-okhttp/src/main/kotlin/com/openai/client/okhttp/OpenAIOkHttpClient.kt b/openai-java-client-okhttp/src/main/kotlin/com/openai/client/okhttp/OpenAIOkHttpClient.kt index ccf5dcd7..797a50c3 100644 --- a/openai-java-client-okhttp/src/main/kotlin/com/openai/client/okhttp/OpenAIOkHttpClient.kt +++ b/openai-java-client-okhttp/src/main/kotlin/com/openai/client/okhttp/OpenAIOkHttpClient.kt @@ -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 @@ -184,7 +180,7 @@ class OpenAIOkHttpClient private constructor() { clientOptions .httpClient( OkHttpClient.builder() - .baseUrl(baseUrl) + .baseUrl(clientOptions.baseUrl()) .timeout(timeout) .proxy(proxy) .build() diff --git a/openai-java-client-okhttp/src/main/kotlin/com/openai/client/okhttp/OpenAIOkHttpClientAsync.kt b/openai-java-client-okhttp/src/main/kotlin/com/openai/client/okhttp/OpenAIOkHttpClientAsync.kt index 1c183738..b4569aa0 100644 --- a/openai-java-client-okhttp/src/main/kotlin/com/openai/client/okhttp/OpenAIOkHttpClientAsync.kt +++ b/openai-java-client-okhttp/src/main/kotlin/com/openai/client/okhttp/OpenAIOkHttpClientAsync.kt @@ -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 @@ -184,7 +180,7 @@ class OpenAIOkHttpClientAsync private constructor() { clientOptions .httpClient( OkHttpClient.builder() - .baseUrl(baseUrl) + .baseUrl(clientOptions.baseUrl()) .timeout(timeout) .proxy(proxy) .build() diff --git a/openai-java-core/src/main/kotlin/com/openai/core/ClientOptions.kt b/openai-java-core/src/main/kotlin/com/openai/core/ClientOptions.kt index d2b4aeaa..a4501bee 100644 --- a/openai-java-core/src/main/kotlin/com/openai/core/ClientOptions.kt +++ b/openai-java-core/src/main/kotlin/com/openai/core/ClientOptions.kt @@ -217,7 +217,10 @@ private constructor( fun removeAllQueryParams(keys: Set) = 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) }