Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure the OpenAI user-agent for LangChain4J #733

Merged
merged 2 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public class InternalOpenAiHelper {
static final String OPENAI_DEMO_API_KEY = "demo";
static final String OPENAI_DEMO_URL = "http://langchain4j.dev/demo/openai/v1";


static final String DEFAULT_USER_AGENT = "langchain4j-openai";
langchain4j marked this conversation as resolved.
Show resolved Hide resolved

public static List<Message> toOpenAiMessages(List<ChatMessage> messages) {
return messages.stream()
.map(InternalOpenAiHelper::toOpenAiMessage)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public OpenAiChatModel(String baseUrl,
.proxy(proxy)
.logRequests(logRequests)
.logResponses(logResponses)
.userAgent(DEFAULT_USER_AGENT)
.build();
this.modelName = getOrDefault(modelName, GPT_3_5_TURBO);
this.temperature = getOrDefault(temperature, 0.7);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public OpenAiEmbeddingModel(String baseUrl,
.proxy(proxy)
.logRequests(logRequests)
.logResponses(logResponses)
.userAgent(DEFAULT_USER_AGENT)
.build();
this.modelName = getOrDefault(modelName, TEXT_EMBEDDING_ADA_002);
this.dimensions = dimensions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static dev.langchain4j.internal.RetryUtils.withRetry;
import static dev.langchain4j.internal.Utils.getOrDefault;
import static dev.langchain4j.model.openai.InternalOpenAiHelper.DEFAULT_USER_AGENT;
import static dev.langchain4j.model.openai.InternalOpenAiHelper.OPENAI_URL;
import static dev.langchain4j.model.openai.OpenAiModelName.DALL_E_2;
import static dev.langchain4j.spi.ServiceHelper.loadFactories;
Expand Down Expand Up @@ -85,6 +86,7 @@ public OpenAiImageModel(
.proxy(proxy)
.logRequests(getOrDefault(logRequests, false))
.logResponses(getOrDefault(logResponses, false))
.userAgent(DEFAULT_USER_AGENT)
.persistTo(persistTo);

if (withPersisting != null && withPersisting) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public OpenAiLanguageModel(String baseUrl,
.proxy(proxy)
.logRequests(logRequests)
.logResponses(logResponses)
.userAgent(DEFAULT_USER_AGENT)
.build();
this.modelName = getOrDefault(modelName, GPT_3_5_TURBO_INSTRUCT);
this.temperature = getOrDefault(temperature, 0.7);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public OpenAiModerationModel(String baseUrl,
.proxy(proxy)
.logRequests(logRequests)
.logResponses(logResponses)
.userAgent(DEFAULT_USER_AGENT)
.build();
this.modelName = getOrDefault(modelName, TEXT_MODERATION_LATEST);
this.maxRetries = getOrDefault(maxRetries, 3);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public OpenAiStreamingChatModel(String baseUrl,
.proxy(proxy)
.logRequests(logRequests)
.logStreamingResponses(logResponses)
.userAgent(DEFAULT_USER_AGENT)
.build();
this.modelName = getOrDefault(modelName, GPT_3_5_TURBO);
this.temperature = getOrDefault(temperature, 0.7);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.time.Duration;

import static dev.langchain4j.internal.Utils.getOrDefault;
import static dev.langchain4j.model.openai.InternalOpenAiHelper.DEFAULT_USER_AGENT;
import static dev.langchain4j.model.openai.InternalOpenAiHelper.OPENAI_URL;
import static dev.langchain4j.model.openai.OpenAiModelName.GPT_3_5_TURBO_INSTRUCT;
import static dev.langchain4j.spi.ServiceHelper.loadFactories;
Expand Down Expand Up @@ -58,6 +59,7 @@ public OpenAiStreamingLanguageModel(String baseUrl,
.proxy(proxy)
.logRequests(logRequests)
.logStreamingResponses(logResponses)
.userAgent(DEFAULT_USER_AGENT)
.build();
this.modelName = getOrDefault(modelName, GPT_3_5_TURBO_INSTRUCT);
this.temperature = getOrDefault(temperature, 0.7);
Expand Down