Skip to content

Commit 06781fd

Browse files
feat: initial commit
1 parent f4fc7a5 commit 06781fd

File tree

13 files changed

+13
-294
lines changed

13 files changed

+13
-294
lines changed

openai-java-client-okhttp/src/main/kotlin/com/openai/client/okhttp/OpenAIOkHttpClient.kt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
package com.openai.client.okhttp
44

55
import com.fasterxml.jackson.databind.json.JsonMapper
6-
import com.openai.azure.AzureOpenAIServiceVersion
76
import com.openai.client.OpenAIClient
87
import com.openai.client.OpenAIClientImpl
98
import com.openai.core.ClientOptions
109
import com.openai.core.http.Headers
1110
import com.openai.core.http.QueryParams
12-
import com.openai.credential.Credential
1311
import java.net.Proxy
1412
import java.time.Clock
1513
import java.time.Duration
@@ -132,12 +130,6 @@ class OpenAIOkHttpClient private constructor() {
132130

133131
fun apiKey(apiKey: String) = apply { clientOptions.apiKey(apiKey) }
134132

135-
fun credential(credential: Credential) = apply { clientOptions.credential(credential) }
136-
137-
fun azureServiceVersion(azureServiceVersion: AzureOpenAIServiceVersion) = apply {
138-
clientOptions.azureServiceVersion(azureServiceVersion)
139-
}
140-
141133
fun organization(organization: String?) = apply { clientOptions.organization(organization) }
142134

143135
fun project(project: String?) = apply { clientOptions.project(project) }

openai-java-client-okhttp/src/main/kotlin/com/openai/client/okhttp/OpenAIOkHttpClientAsync.kt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
package com.openai.client.okhttp
44

55
import com.fasterxml.jackson.databind.json.JsonMapper
6-
import com.openai.azure.AzureOpenAIServiceVersion
76
import com.openai.client.OpenAIClientAsync
87
import com.openai.client.OpenAIClientAsyncImpl
98
import com.openai.core.ClientOptions
109
import com.openai.core.http.Headers
1110
import com.openai.core.http.QueryParams
12-
import com.openai.credential.Credential
1311
import java.net.Proxy
1412
import java.time.Clock
1513
import java.time.Duration
@@ -132,12 +130,6 @@ class OpenAIOkHttpClientAsync private constructor() {
132130

133131
fun apiKey(apiKey: String) = apply { clientOptions.apiKey(apiKey) }
134132

135-
fun credential(credential: Credential) = apply { clientOptions.credential(credential) }
136-
137-
fun azureServiceVersion(azureServiceVersion: AzureOpenAIServiceVersion) = apply {
138-
clientOptions.azureServiceVersion(azureServiceVersion)
139-
}
140-
141133
fun organization(organization: String?) = apply { clientOptions.organization(organization) }
142134

143135
fun project(project: String?) = apply { clientOptions.project(project) }

openai-java-core/src/main/kotlin/com/openai/azure/AzureOpenAIServiceVersion.kt

Lines changed: 0 additions & 36 deletions
This file was deleted.

openai-java-core/src/main/kotlin/com/openai/azure/credential/AzureApiKeyCredential.kt

Lines changed: 0 additions & 26 deletions
This file was deleted.

openai-java-core/src/main/kotlin/com/openai/core/ClientOptions.kt

Lines changed: 12 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,11 @@
33
package com.openai.core
44

55
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
96
import com.openai.core.http.Headers
107
import com.openai.core.http.HttpClient
118
import com.openai.core.http.PhantomReachableClosingHttpClient
129
import com.openai.core.http.QueryParams
1310
import com.openai.core.http.RetryingHttpClient
14-
import com.openai.credential.BearerTokenCredential
15-
import com.openai.credential.Credential
1611
import java.time.Clock
1712

1813
class 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
)

openai-java-core/src/main/kotlin/com/openai/core/Utils.kt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,4 @@ internal fun <K : Comparable<K>, V> SortedMap<K, V>.toImmutable(): SortedMap<K,
2323
if (isEmpty()) Collections.emptySortedMap()
2424
else Collections.unmodifiableSortedMap(toSortedMap(comparator()))
2525

26-
@JvmSynthetic
27-
internal fun isAzureEndpoint(baseUrl: String): Boolean {
28-
// Azure Endpoint should be in the format of `https://<region>.openai.azure.com`.
29-
// Or `https://<region>.azure-api.net` for Azure OpenAI Management URL.
30-
return baseUrl.endsWith(".openai.azure.com", true) || baseUrl.endsWith(".azure-api.net", true)
31-
}
32-
3326
internal interface Enum

openai-java-core/src/main/kotlin/com/openai/credential/BearerTokenCredential.kt

Lines changed: 0 additions & 36 deletions
This file was deleted.

openai-java-core/src/main/kotlin/com/openai/credential/Credential.kt

Lines changed: 0 additions & 4 deletions
This file was deleted.

openai-java-core/src/main/kotlin/com/openai/services/async/chat/CompletionServiceAsyncImpl.kt

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ import com.openai.core.handlers.withErrorHandler
1010
import com.openai.core.http.HttpMethod
1111
import com.openai.core.http.HttpRequest
1212
import com.openai.core.http.HttpResponse.Handler
13-
import com.openai.core.isAzureEndpoint
1413
import com.openai.core.json
15-
import com.openai.credential.BearerTokenCredential
1614
import com.openai.errors.OpenAIError
1715
import com.openai.models.ChatCompletion
1816
import com.openai.models.ChatCompletionCreateParams
@@ -41,23 +39,10 @@ constructor(
4139
val request =
4240
HttpRequest.builder()
4341
.method(HttpMethod.POST)
44-
.apply {
45-
if (isAzureEndpoint(clientOptions.baseUrl)) {
46-
addPathSegments("openai", "deployments", params.model().toString())
47-
}
48-
}
4942
.addPathSegments("chat", "completions")
5043
.putAllQueryParams(clientOptions.queryParams)
5144
.replaceAllQueryParams(params.getQueryParams())
5245
.putAllHeaders(clientOptions.headers)
53-
.apply {
54-
if (
55-
isAzureEndpoint(clientOptions.baseUrl) &&
56-
clientOptions.credential is BearerTokenCredential
57-
) {
58-
putHeader("Authorization", "Bearer ${clientOptions.credential.token()}")
59-
}
60-
}
6146
.replaceAllHeaders(params.getHeaders())
6247
.body(json(clientOptions.jsonMapper, params.getBody()))
6348
.build()

openai-java-core/src/main/kotlin/com/openai/services/blocking/chat/CompletionServiceImpl.kt

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ import com.openai.core.http.HttpMethod
1515
import com.openai.core.http.HttpRequest
1616
import com.openai.core.http.HttpResponse.Handler
1717
import com.openai.core.http.StreamResponse
18-
import com.openai.core.isAzureEndpoint
1918
import com.openai.core.json
20-
import com.openai.credential.BearerTokenCredential
2119
import com.openai.errors.OpenAIError
2220
import com.openai.models.ChatCompletion
2321
import com.openai.models.ChatCompletionChunk
@@ -46,23 +44,10 @@ constructor(
4644
val request =
4745
HttpRequest.builder()
4846
.method(HttpMethod.POST)
49-
.apply {
50-
if (isAzureEndpoint(clientOptions.baseUrl)) {
51-
addPathSegments("openai", "deployments", params.model().toString())
52-
}
53-
}
5447
.addPathSegments("chat", "completions")
5548
.putAllQueryParams(clientOptions.queryParams)
5649
.replaceAllQueryParams(params.getQueryParams())
5750
.putAllHeaders(clientOptions.headers)
58-
.apply {
59-
if (
60-
isAzureEndpoint(clientOptions.baseUrl) &&
61-
clientOptions.credential is BearerTokenCredential
62-
) {
63-
putHeader("Authorization", "Bearer ${clientOptions.credential.token()}")
64-
}
65-
}
6651
.replaceAllHeaders(params.getHeaders())
6752
.body(json(clientOptions.jsonMapper, params.getBody()))
6853
.build()

0 commit comments

Comments
 (0)