Skip to content

Commit 5bab5cc

Browse files
stainless-botStainless Bot
authored andcommitted
feat(client): add User-Agent header
chore: unknown commit message
1 parent 1576020 commit 5bab5cc

File tree

3 files changed

+81
-20
lines changed

3 files changed

+81
-20
lines changed

openai-java-core/src/main/kotlin/com/openai/client/OpenAIClientAsyncImpl.kt

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package com.openai.client
44

55
import com.openai.core.ClientOptions
6+
import com.openai.core.getPackageVersion
67
import com.openai.models.*
78
import com.openai.services.async.*
89

@@ -11,35 +12,52 @@ constructor(
1112
private val clientOptions: ClientOptions,
1213
) : OpenAIClientAsync {
1314

15+
private val clientOptionsWithUserAgent =
16+
if (clientOptions.headers.containsKey("User-Agent")) clientOptions
17+
else
18+
clientOptions
19+
.toBuilder()
20+
.putHeader("User-Agent", "${javaClass.simpleName}/Java ${getPackageVersion()}")
21+
.build()
22+
23+
// Pass the original clientOptions so that this client sets its own User-Agent.
1424
private val sync: OpenAIClient by lazy { OpenAIClientImpl(clientOptions) }
1525

1626
private val completions: CompletionServiceAsync by lazy {
17-
CompletionServiceAsyncImpl(clientOptions)
27+
CompletionServiceAsyncImpl(clientOptionsWithUserAgent)
1828
}
1929

20-
private val chat: ChatServiceAsync by lazy { ChatServiceAsyncImpl(clientOptions) }
30+
private val chat: ChatServiceAsync by lazy { ChatServiceAsyncImpl(clientOptionsWithUserAgent) }
2131

2232
private val embeddings: EmbeddingServiceAsync by lazy {
23-
EmbeddingServiceAsyncImpl(clientOptions)
33+
EmbeddingServiceAsyncImpl(clientOptionsWithUserAgent)
2434
}
2535

26-
private val files: FileServiceAsync by lazy { FileServiceAsyncImpl(clientOptions) }
36+
private val files: FileServiceAsync by lazy { FileServiceAsyncImpl(clientOptionsWithUserAgent) }
2737

28-
private val images: ImageServiceAsync by lazy { ImageServiceAsyncImpl(clientOptions) }
38+
private val images: ImageServiceAsync by lazy {
39+
ImageServiceAsyncImpl(clientOptionsWithUserAgent)
40+
}
2941

3042
private val moderations: ModerationServiceAsync by lazy {
31-
ModerationServiceAsyncImpl(clientOptions)
43+
ModerationServiceAsyncImpl(clientOptionsWithUserAgent)
3244
}
3345

34-
private val models: ModelServiceAsync by lazy { ModelServiceAsyncImpl(clientOptions) }
46+
private val models: ModelServiceAsync by lazy {
47+
ModelServiceAsyncImpl(clientOptionsWithUserAgent)
48+
}
3549

3650
private val fineTuning: FineTuningServiceAsync by lazy {
37-
FineTuningServiceAsyncImpl(clientOptions)
51+
FineTuningServiceAsyncImpl(clientOptionsWithUserAgent)
3852
}
3953

40-
private val batches: BatchServiceAsync by lazy { BatchServiceAsyncImpl(clientOptions) }
54+
private val batches: BatchServiceAsync by lazy {
55+
BatchServiceAsyncImpl(clientOptionsWithUserAgent)
56+
}
4157

42-
private val uploads: UploadServiceAsync by lazy { UploadServiceAsyncImpl(clientOptions) }
58+
private val uploads: UploadServiceAsync by lazy {
59+
UploadServiceAsyncImpl(clientOptionsWithUserAgent)
60+
}
4361

4462
override fun sync(): OpenAIClient = sync
4563

openai-java-core/src/main/kotlin/com/openai/client/OpenAIClientImpl.kt

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package com.openai.client
44

55
import com.openai.core.ClientOptions
6+
import com.openai.core.getPackageVersion
67
import com.openai.models.*
78
import com.openai.services.blocking.*
89

@@ -11,27 +12,44 @@ constructor(
1112
private val clientOptions: ClientOptions,
1213
) : OpenAIClient {
1314

15+
private val clientOptionsWithUserAgent =
16+
if (clientOptions.headers.containsKey("User-Agent")) clientOptions
17+
else
18+
clientOptions
19+
.toBuilder()
20+
.putHeader("User-Agent", "${javaClass.simpleName}/Java ${getPackageVersion()}")
21+
.build()
22+
23+
// Pass the original clientOptions so that this client sets its own User-Agent.
1424
private val async: OpenAIClientAsync by lazy { OpenAIClientAsyncImpl(clientOptions) }
1525

16-
private val completions: CompletionService by lazy { CompletionServiceImpl(clientOptions) }
26+
private val completions: CompletionService by lazy {
27+
CompletionServiceImpl(clientOptionsWithUserAgent)
28+
}
1729

18-
private val chat: ChatService by lazy { ChatServiceImpl(clientOptions) }
30+
private val chat: ChatService by lazy { ChatServiceImpl(clientOptionsWithUserAgent) }
1931

20-
private val embeddings: EmbeddingService by lazy { EmbeddingServiceImpl(clientOptions) }
32+
private val embeddings: EmbeddingService by lazy {
33+
EmbeddingServiceImpl(clientOptionsWithUserAgent)
34+
}
2135

22-
private val files: FileService by lazy { FileServiceImpl(clientOptions) }
36+
private val files: FileService by lazy { FileServiceImpl(clientOptionsWithUserAgent) }
2337

24-
private val images: ImageService by lazy { ImageServiceImpl(clientOptions) }
38+
private val images: ImageService by lazy { ImageServiceImpl(clientOptionsWithUserAgent) }
2539

26-
private val moderations: ModerationService by lazy { ModerationServiceImpl(clientOptions) }
40+
private val moderations: ModerationService by lazy {
41+
ModerationServiceImpl(clientOptionsWithUserAgent)
42+
}
2743

28-
private val models: ModelService by lazy { ModelServiceImpl(clientOptions) }
44+
private val models: ModelService by lazy { ModelServiceImpl(clientOptionsWithUserAgent) }
2945

30-
private val fineTuning: FineTuningService by lazy { FineTuningServiceImpl(clientOptions) }
46+
private val fineTuning: FineTuningService by lazy {
47+
FineTuningServiceImpl(clientOptionsWithUserAgent)
48+
}
3149

32-
private val batches: BatchService by lazy { BatchServiceImpl(clientOptions) }
50+
private val batches: BatchService by lazy { BatchServiceImpl(clientOptionsWithUserAgent) }
3351

34-
private val uploads: UploadService by lazy { UploadServiceImpl(clientOptions) }
52+
private val uploads: UploadService by lazy { UploadServiceImpl(clientOptionsWithUserAgent) }
3553

3654
override fun async(): OpenAIClientAsync = async
3755

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ private constructor(
2121
@get:JvmName("headers") val headers: ListMultimap<String, String>,
2222
@get:JvmName("queryParams") val queryParams: ListMultimap<String, String>,
2323
@get:JvmName("responseValidation") val responseValidation: Boolean,
24+
@get:JvmName("maxRetries") val maxRetries: Int,
2425
) {
2526

27+
fun toBuilder() = Builder().from(this)
28+
2629
companion object {
2730

2831
const val PRODUCTION_URL = "https://api.openai.com/v1"
@@ -46,6 +49,27 @@ private constructor(
4649
private var organization: String? = null
4750
private var project: String? = null
4851

52+
@JvmSynthetic
53+
internal fun from(clientOptions: ClientOptions) = apply {
54+
httpClient = clientOptions.httpClient
55+
jsonMapper = clientOptions.jsonMapper
56+
clock = clientOptions.clock
57+
baseUrl = clientOptions.baseUrl
58+
headers =
59+
clientOptions.headers.asMap().mapValuesTo(mutableMapOf()) { (_, value) ->
60+
value.toMutableList()
61+
}
62+
queryParams =
63+
clientOptions.queryParams.asMap().mapValuesTo(mutableMapOf()) { (_, value) ->
64+
value.toMutableList()
65+
}
66+
responseValidation = clientOptions.responseValidation
67+
maxRetries = clientOptions.maxRetries
68+
apiKey = clientOptions.apiKey
69+
organization = clientOptions.organization
70+
project = clientOptions.project
71+
}
72+
4973
fun httpClient(httpClient: HttpClient) = apply { this.httpClient = httpClient }
5074

5175
fun jsonMapper(jsonMapper: JsonMapper) = apply { this.jsonMapper = jsonMapper }
@@ -150,6 +174,7 @@ private constructor(
150174
headers.toUnmodifiable(),
151175
queryParams.toUnmodifiable(),
152176
responseValidation,
177+
maxRetries,
153178
)
154179
}
155180
}

0 commit comments

Comments
 (0)