33package com.openai.core
44
55import com.fasterxml.jackson.databind.json.JsonMapper
6- import com.openai.azure.AzureOpenAIServiceVersion
7- import com.openai.azure.AzureOpenAIServiceVersion.Companion.V2024_06_01
8- import com.openai.azure.credential.AzureApiKeyCredential
96import com.openai.core.http.Headers
107import com.openai.core.http.HttpClient
118import com.openai.core.http.PhantomReachableClosingHttpClient
129import com.openai.core.http.QueryParams
1310import com.openai.core.http.RetryingHttpClient
14- import com.openai.credential.BearerTokenCredential
15- import com.openai.credential.Credential
1611import java.time.Clock
1712
1813class ClientOptions
@@ -26,7 +21,7 @@ private constructor(
2621 @get:JvmName(" queryParams" ) val queryParams: QueryParams ,
2722 @get:JvmName(" responseValidation" ) val responseValidation: Boolean ,
2823 @get:JvmName(" maxRetries" ) val maxRetries: Int ,
29- @get:JvmName(" credential " ) val credential : Credential ,
24+ @get:JvmName(" apiKey " ) val apiKey : String ,
3025 @get:JvmName(" organization" ) val organization: String? ,
3126 @get:JvmName(" project" ) val project: String? ,
3227) {
@@ -52,8 +47,7 @@ private constructor(
5247 private var queryParams: QueryParams .Builder = QueryParams .builder()
5348 private var responseValidation: Boolean = false
5449 private var maxRetries: Int = 2
55- private var credential: Credential ? = null
56- private var azureServiceVersion: AzureOpenAIServiceVersion ? = null
50+ private var apiKey: String? = null
5751 private var organization: String? = null
5852 private var project: String? = null
5953
@@ -67,7 +61,7 @@ private constructor(
6761 queryParams = clientOptions.queryParams.toBuilder()
6862 responseValidation = clientOptions.responseValidation
6963 maxRetries = clientOptions.maxRetries
70- credential = clientOptions.credential
64+ apiKey = clientOptions.apiKey
7165 organization = clientOptions.organization
7266 project = clientOptions.project
7367 }
@@ -166,56 +160,21 @@ private constructor(
166160
167161 fun maxRetries (maxRetries : Int ) = apply { this .maxRetries = maxRetries }
168162
169- fun apiKey (apiKey : String ) = apply {
170- this .credential = BearerTokenCredential .create(apiKey)
171- }
172-
173- fun credential (credential : Credential ) = apply { this .credential = credential }
174-
175- fun azureServiceVersion (azureServiceVersion : AzureOpenAIServiceVersion ) = apply {
176- this .azureServiceVersion = azureServiceVersion
177- }
163+ fun apiKey (apiKey : String ) = apply { this .apiKey = apiKey }
178164
179165 fun organization (organization : String? ) = apply { this .organization = organization }
180166
181167 fun project (project : String? ) = apply { this .project = project }
182168
183169 fun fromEnv () = apply {
184- val openAIKey = System .getenv(" OPENAI_API_KEY" )
185- val openAIOrgId = System .getenv(" OPENAI_ORG_ID" )
186- val openAIProjectId = System .getenv(" OPENAI_PROJECT_ID" )
187- val azureOpenAIKey = System .getenv(" AZURE_OPENAI_KEY" )
188- val azureEndpoint = System .getenv(" AZURE_OPENAI_ENDPOINT" )
189-
190- when {
191- ! openAIKey.isNullOrEmpty() && ! azureOpenAIKey.isNullOrEmpty() -> {
192- throw IllegalArgumentException (
193- " Both OpenAI and Azure OpenAI API keys, `OPENAI_API_KEY` and `AZURE_OPENAI_KEY`, are set. Please specify only one"
194- )
195- }
196- ! openAIKey.isNullOrEmpty() -> {
197- credential(BearerTokenCredential .create(openAIKey))
198- organization(openAIOrgId)
199- project(openAIProjectId)
200- }
201- ! azureOpenAIKey.isNullOrEmpty() -> {
202- credential(AzureApiKeyCredential .create(azureOpenAIKey))
203- baseUrl(azureEndpoint)
204- }
205- ! azureEndpoint.isNullOrEmpty() -> {
206- // Both 'openAIKey' and 'azureOpenAIKey' are not set.
207- // Only 'azureEndpoint' is set here, and user still needs to call method
208- // '.credential(BearerTokenCredential(Supplier<String>))'
209- // to get the token through the supplier, which requires Azure Entra ID as a
210- // dependency.
211- baseUrl(azureEndpoint)
212- }
213- }
170+ System .getenv(" OPENAI_API_KEY" )?.let { apiKey(it) }
171+ System .getenv(" OPENAI_ORG_ID" )?.let { organization(it) }
172+ System .getenv(" OPENAI_PROJECT_ID" )?.let { project(it) }
214173 }
215174
216175 fun build (): ClientOptions {
217176 checkNotNull(httpClient) { " `httpClient` is required but was not set" }
218- checkNotNull(credential ) { " `credential ` is required but was not set" }
177+ checkNotNull(apiKey ) { " `apiKey ` is required but was not set" }
219178
220179 val headers = Headers .builder()
221180 val queryParams = QueryParams .builder()
@@ -228,26 +187,11 @@ private constructor(
228187 headers.put(" X-Stainless-Runtime-Version" , getJavaVersion())
229188 organization?.let { headers.put(" OpenAI-Organization" , it) }
230189 project?.let { headers.put(" OpenAI-Project" , it) }
231-
232- when (val currentCredential = credential) {
233- is AzureApiKeyCredential -> {
234- headers.put(" api-key" , currentCredential.apiKey())
235- }
236- is BearerTokenCredential -> {
237- headers.put(" Authorization" , " Bearer ${currentCredential.token()} " )
238- }
239- else -> {
240- throw IllegalArgumentException (" Invalid credential type" )
190+ apiKey?.let {
191+ if (! it.isEmpty()) {
192+ headers.put(" Authorization" , " Bearer $it " )
241193 }
242194 }
243-
244- if (isAzureEndpoint(baseUrl)) {
245- // Default Azure OpenAI version is used if Azure user doesn't
246- // specific a service API version in 'queryParams'.
247- // We can update the default value every major announcement if needed.
248- replaceQueryParams(" api-version" , (azureServiceVersion ? : V2024_06_01 ).value)
249- }
250-
251195 headers.replaceAll(this .headers.build())
252196 queryParams.replaceAll(this .queryParams.build())
253197
@@ -267,7 +211,7 @@ private constructor(
267211 queryParams.build(),
268212 responseValidation,
269213 maxRetries,
270- credential !! ,
214+ apiKey !! ,
271215 organization,
272216 project,
273217 )
0 commit comments