3
3
package com.openai.core
4
4
5
5
import 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
9
6
import com.openai.core.http.Headers
10
7
import com.openai.core.http.HttpClient
11
8
import com.openai.core.http.PhantomReachableClosingHttpClient
12
9
import com.openai.core.http.QueryParams
13
10
import com.openai.core.http.RetryingHttpClient
14
- import com.openai.credential.BearerTokenCredential
15
- import com.openai.credential.Credential
16
11
import java.time.Clock
17
12
18
13
class ClientOptions
@@ -26,7 +21,7 @@ private constructor(
26
21
@get:JvmName(" queryParams" ) val queryParams: QueryParams ,
27
22
@get:JvmName(" responseValidation" ) val responseValidation: Boolean ,
28
23
@get:JvmName(" maxRetries" ) val maxRetries: Int ,
29
- @get:JvmName(" credential " ) val credential : Credential ,
24
+ @get:JvmName(" apiKey " ) val apiKey : String ,
30
25
@get:JvmName(" organization" ) val organization: String? ,
31
26
@get:JvmName(" project" ) val project: String? ,
32
27
) {
@@ -52,8 +47,7 @@ private constructor(
52
47
private var queryParams: QueryParams .Builder = QueryParams .builder()
53
48
private var responseValidation: Boolean = false
54
49
private var maxRetries: Int = 2
55
- private var credential: Credential ? = null
56
- private var azureServiceVersion: AzureOpenAIServiceVersion ? = null
50
+ private var apiKey: String? = null
57
51
private var organization: String? = null
58
52
private var project: String? = null
59
53
@@ -67,7 +61,7 @@ private constructor(
67
61
queryParams = clientOptions.queryParams.toBuilder()
68
62
responseValidation = clientOptions.responseValidation
69
63
maxRetries = clientOptions.maxRetries
70
- credential = clientOptions.credential
64
+ apiKey = clientOptions.apiKey
71
65
organization = clientOptions.organization
72
66
project = clientOptions.project
73
67
}
@@ -166,56 +160,21 @@ private constructor(
166
160
167
161
fun maxRetries (maxRetries : Int ) = apply { this .maxRetries = maxRetries }
168
162
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 }
178
164
179
165
fun organization (organization : String? ) = apply { this .organization = organization }
180
166
181
167
fun project (project : String? ) = apply { this .project = project }
182
168
183
169
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) }
214
173
}
215
174
216
175
fun build (): ClientOptions {
217
176
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" }
219
178
220
179
val headers = Headers .builder()
221
180
val queryParams = QueryParams .builder()
@@ -228,26 +187,11 @@ private constructor(
228
187
headers.put(" X-Stainless-Runtime-Version" , getJavaVersion())
229
188
organization?.let { headers.put(" OpenAI-Organization" , it) }
230
189
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 " )
241
193
}
242
194
}
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
-
251
195
headers.replaceAll(this .headers.build())
252
196
queryParams.replaceAll(this .queryParams.build())
253
197
@@ -267,7 +211,7 @@ private constructor(
267
211
queryParams.build(),
268
212
responseValidation,
269
213
maxRetries,
270
- credential !! ,
214
+ apiKey !! ,
271
215
organization,
272
216
project,
273
217
)
0 commit comments