Skip to content
This repository was archived by the owner on Feb 11, 2025. It is now read-only.

Commit 98cb30c

Browse files
authored
Add a method to customize the User Agent (#17)
Allow the customization of the "User-Agent" sent as a header by okhttp3. My goal is to use it in LangChain4J OpenAI at https://github.com/langchain4j/langchain4j/tree/main/langchain4j-open-ai to have something similar to what we have in LangChain4J Azure OpenAI (see https://github.com/langchain4j/langchain4j/blob/200522f558509a67e940ae4c82284b85caaebef8/langchain4j-azure-open-ai/src/main/java/dev/langchain4j/model/azure/InternalAzureOpenAiHelper.java#L77 ). This is important as this allows Azure OpenAI (and probably also OpenAI) to see which tool or library people are using, which is important to get support from them.
1 parent f191eda commit 98cb30c

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/main/java/dev/ai4j/openai4j/DefaultOpenAiClient.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class DefaultOpenAiClient extends OpenAiClient {
3333
private final OkHttpClient okHttpClient;
3434
private final OpenAiApi openAiApi;
3535
private final boolean logStreamingResponses;
36+
private final String userAgent;
3637

3738
public DefaultOpenAiClient(String apiKey) {
3839
this(new Builder().openAiApiKey(apiKey));
@@ -41,6 +42,7 @@ public DefaultOpenAiClient(String apiKey) {
4142
private DefaultOpenAiClient(Builder serviceBuilder) {
4243
this.baseUrl = serviceBuilder.baseUrl;
4344
this.apiVersion = serviceBuilder.apiVersion;
45+
this.userAgent = serviceBuilder.userAgent;
4446

4547
OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder()
4648
.callTimeout(serviceBuilder.callTimeout)
@@ -68,6 +70,10 @@ private DefaultOpenAiClient(Builder serviceBuilder) {
6870
okHttpClientBuilder.proxy(serviceBuilder.proxy);
6971
}
7072

73+
if (serviceBuilder.userAgent != null) {
74+
okHttpClientBuilder.addInterceptor(new GenericHeaderInjector(Collections.singletonMap("User-Agent", serviceBuilder.userAgent)));
75+
}
76+
7177
if (serviceBuilder.logRequests) {
7278
okHttpClientBuilder.addInterceptor(new RequestLoggingInterceptor(serviceBuilder.logLevel));
7379
}

src/main/java/dev/ai4j/openai4j/OpenAiClient.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public abstract static class Builder<T extends OpenAiClient, B extends Builder<T
6767
public Duration readTimeout = Duration.ofSeconds(60);
6868
public Duration writeTimeout = Duration.ofSeconds(60);
6969
public Proxy proxy;
70+
public String userAgent;
7071
public boolean logRequests;
7172
public boolean logResponses;
7273
public LogLevel logLevel = DEBUG;
@@ -178,6 +179,11 @@ public B proxy(Proxy proxy) {
178179
return (B) this;
179180
}
180181

182+
public B userAgent(String userAgent) {
183+
this.userAgent = userAgent;
184+
return (B) this;
185+
}
186+
181187
public B logRequests() {
182188
return logRequests(true);
183189
}

0 commit comments

Comments
 (0)