From b6a0c411899ae7b58f349b75ea4b3360e14fe4d1 Mon Sep 17 00:00:00 2001 From: Yellow-- Date: Fri, 24 May 2024 20:27:50 +0800 Subject: [PATCH] Qianfan Models support http proxy setting(#951) (#1159) ## Issue https://github.com/langchain4j/langchain4j/issues/951 ## Change - ```QianfanChatModel``` support proxy now - ```QianfanEmbeddingModel``` support proxy now - ```QianfanLanguageModel``` support proxy now - ```QianfanStreamingChatModel``` support proxy now ## General checklist - [X] There are no breaking changes - [ ] I have added unit and integration tests for my change - [X] I have manually run all the unit and integration tests in the module I have added/changed, and they are all green - [ ] I have manually run all the unit and integration tests in the [core](https://github.com/langchain4j/langchain4j/tree/main/langchain4j-core) and [main](https://github.com/langchain4j/langchain4j/tree/main/langchain4j) modules, and they are all green - [ ] I have added/updated the [documentation](https://github.com/langchain4j/langchain4j/tree/main/docs/docs) - [ ] I have added an example in the [examples repo](https://github.com/langchain4j/langchain4j-examples) (only for "big" features) ## Checklist for adding new model integration - [ ] I have added my new module in the [BOM](https://github.com/langchain4j/langchain4j/blob/main/langchain4j-bom/pom.xml) ## Checklist for adding new embedding store integration - [ ] I have added a `{NameOfIntegration}EmbeddingStoreIT` that extends from either `EmbeddingStoreIT` or `EmbeddingStoreWithFilteringIT` - [ ] I have added my new module in the [BOM](https://github.com/langchain4j/langchain4j/blob/main/langchain4j-bom/pom.xml) ## Checklist for changing existing embedding store integration - [ ] I have manually verified that the `{NameOfIntegration}EmbeddingStore` works correctly with the data persisted using the latest released version of LangChain4j Co-authored-by: jianye.zheng --- .../langchain4j/model/qianfan/QianfanChatModel.java | 13 +++++++++---- .../model/qianfan/QianfanEmbeddingModel.java | 11 ++++++++--- .../model/qianfan/QianfanLanguageModel.java | 11 ++++++++--- .../model/qianfan/QianfanStreamingChatModel.java | 9 +++++++-- 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/langchain4j-qianfan/src/main/java/dev/langchain4j/model/qianfan/QianfanChatModel.java b/langchain4j-qianfan/src/main/java/dev/langchain4j/model/qianfan/QianfanChatModel.java index fe7f3f11b..03c2ca0c8 100644 --- a/langchain4j-qianfan/src/main/java/dev/langchain4j/model/qianfan/QianfanChatModel.java +++ b/langchain4j-qianfan/src/main/java/dev/langchain4j/model/qianfan/QianfanChatModel.java @@ -2,22 +2,25 @@ package dev.langchain4j.model.qianfan; import dev.langchain4j.agent.tool.ToolSpecification; -import dev.langchain4j.data.message.*; +import dev.langchain4j.data.message.AiMessage; +import dev.langchain4j.data.message.ChatMessage; import dev.langchain4j.internal.Utils; import dev.langchain4j.model.chat.ChatLanguageModel; import dev.langchain4j.model.output.Response; import dev.langchain4j.model.qianfan.client.QianfanClient; +import dev.langchain4j.model.qianfan.client.chat.ChatCompletionRequest; import dev.langchain4j.model.qianfan.client.chat.ChatCompletionResponse; import dev.langchain4j.model.qianfan.spi.QianfanChatModelBuilderFactory; import lombok.Builder; + +import java.net.Proxy; import java.util.List; + import static dev.langchain4j.internal.RetryUtils.withRetry; import static dev.langchain4j.internal.Utils.getOrDefault; import static dev.langchain4j.model.qianfan.InternalQianfanHelper.*; import static dev.langchain4j.spi.ServiceHelper.loadFactories; -import dev.langchain4j.model.qianfan.client.chat.ChatCompletionRequest; - /** * * see details here: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Nlks5zkzu @@ -54,7 +57,8 @@ public class QianfanChatModel implements ChatLanguageModel { String responseFormat, Double penaltyScore, Boolean logRequests, - Boolean logResponses + Boolean logResponses, + Proxy proxy ) { if (Utils.isNullOrBlank(apiKey)||Utils.isNullOrBlank(secretKey)) { throw new IllegalArgumentException(" api key and secret key must be defined. It can be generated here: https://console.bce.baidu.com/qianfan/ais/console/applicationConsole/application"); @@ -75,6 +79,7 @@ public class QianfanChatModel implements ChatLanguageModel { .secretKey(secretKey) .logRequests(logRequests) .logResponses(logResponses) + .proxy(proxy) .build(); this.temperature = getOrDefault(temperature, 0.7); this.maxRetries = getOrDefault(maxRetries, 3); diff --git a/langchain4j-qianfan/src/main/java/dev/langchain4j/model/qianfan/QianfanEmbeddingModel.java b/langchain4j-qianfan/src/main/java/dev/langchain4j/model/qianfan/QianfanEmbeddingModel.java index c022fbfbc..4ad33a262 100644 --- a/langchain4j-qianfan/src/main/java/dev/langchain4j/model/qianfan/QianfanEmbeddingModel.java +++ b/langchain4j-qianfan/src/main/java/dev/langchain4j/model/qianfan/QianfanEmbeddingModel.java @@ -6,14 +6,17 @@ import dev.langchain4j.internal.Utils; import dev.langchain4j.model.embedding.EmbeddingModel; import dev.langchain4j.model.output.Response; import dev.langchain4j.model.qianfan.client.QianfanClient; -import dev.langchain4j.model.qianfan.client.embedding.EmbeddingResponse; import dev.langchain4j.model.qianfan.client.embedding.EmbeddingRequest; +import dev.langchain4j.model.qianfan.client.embedding.EmbeddingResponse; import dev.langchain4j.model.qianfan.spi.QianfanEmbeddingModelBuilderFactory; import lombok.Builder; + +import java.net.Proxy; import java.util.List; + import static dev.langchain4j.internal.RetryUtils.withRetry; import static dev.langchain4j.internal.Utils.getOrDefault; -import static dev.langchain4j.model.qianfan.InternalQianfanHelper.*; +import static dev.langchain4j.model.qianfan.InternalQianfanHelper.tokenUsageFrom; import static dev.langchain4j.spi.ServiceHelper.loadFactories; import static java.util.stream.Collectors.toList; /** @@ -46,7 +49,8 @@ public class QianfanEmbeddingModel implements EmbeddingModel { String endpoint, String user, Boolean logRequests, - Boolean logResponses + Boolean logResponses, + Proxy proxy ) { if (Utils.isNullOrBlank(apiKey)||Utils.isNullOrBlank(secretKey)) { throw new IllegalArgumentException(" api key and secret key must be defined. It can be generated here: https://console.bce.baidu.com/qianfan/ais/console/applicationConsole/application"); @@ -67,6 +71,7 @@ public class QianfanEmbeddingModel implements EmbeddingModel { .secretKey(secretKey) .logRequests(logRequests) .logResponses(logResponses) + .proxy(proxy) .build(); this.maxRetries = getOrDefault(maxRetries, 3); this.user = user; diff --git a/langchain4j-qianfan/src/main/java/dev/langchain4j/model/qianfan/QianfanLanguageModel.java b/langchain4j-qianfan/src/main/java/dev/langchain4j/model/qianfan/QianfanLanguageModel.java index d3d4afc9b..0f5799d3c 100644 --- a/langchain4j-qianfan/src/main/java/dev/langchain4j/model/qianfan/QianfanLanguageModel.java +++ b/langchain4j-qianfan/src/main/java/dev/langchain4j/model/qianfan/QianfanLanguageModel.java @@ -1,7 +1,6 @@ package dev.langchain4j.model.qianfan; - import dev.langchain4j.internal.Utils; import dev.langchain4j.model.language.LanguageModel; import dev.langchain4j.model.output.Response; @@ -10,9 +9,13 @@ import dev.langchain4j.model.qianfan.client.completion.CompletionRequest; import dev.langchain4j.model.qianfan.client.completion.CompletionResponse; import dev.langchain4j.model.qianfan.spi.QianfanLanguageModelBuilderFactory; import lombok.Builder; + +import java.net.Proxy; + import static dev.langchain4j.internal.RetryUtils.withRetry; import static dev.langchain4j.internal.Utils.getOrDefault; -import static dev.langchain4j.model.qianfan.InternalQianfanHelper.*; +import static dev.langchain4j.model.qianfan.InternalQianfanHelper.finishReasonFrom; +import static dev.langchain4j.model.qianfan.InternalQianfanHelper.tokenUsageFrom; import static dev.langchain4j.spi.ServiceHelper.loadFactories; @@ -51,7 +54,8 @@ public class QianfanLanguageModel implements LanguageModel { String endpoint, Double penaltyScore, Boolean logRequests, - Boolean logResponses + Boolean logResponses, + Proxy proxy ) { if (Utils.isNullOrBlank(apiKey)||Utils.isNullOrBlank(secretKey)) { throw new IllegalArgumentException(" api key and secret key must be defined. It can be generated here: https://console.bce.baidu.com/qianfan/ais/console/applicationConsole/application"); @@ -72,6 +76,7 @@ public class QianfanLanguageModel implements LanguageModel { .secretKey(secretKey) .logRequests(logRequests) .logResponses(logResponses) + .proxy(proxy) .build(); this.temperature = getOrDefault(temperature, 0.7); this.maxRetries = getOrDefault(maxRetries, 3); diff --git a/langchain4j-qianfan/src/main/java/dev/langchain4j/model/qianfan/QianfanStreamingChatModel.java b/langchain4j-qianfan/src/main/java/dev/langchain4j/model/qianfan/QianfanStreamingChatModel.java index 5ff6baa42..dc2550f0b 100644 --- a/langchain4j-qianfan/src/main/java/dev/langchain4j/model/qianfan/QianfanStreamingChatModel.java +++ b/langchain4j-qianfan/src/main/java/dev/langchain4j/model/qianfan/QianfanStreamingChatModel.java @@ -15,9 +15,12 @@ import dev.langchain4j.model.qianfan.client.chat.ChatCompletionRequest; import dev.langchain4j.model.qianfan.client.chat.ChatCompletionResponse; import dev.langchain4j.model.qianfan.spi.QianfanStreamingChatModelBuilderFactory; import lombok.Builder; -import static dev.langchain4j.model.qianfan.InternalQianfanHelper.*; + +import java.net.Proxy; import java.util.List; + import static dev.langchain4j.internal.Utils.getOrDefault; +import static dev.langchain4j.model.qianfan.InternalQianfanHelper.getSystemMessage; import static dev.langchain4j.spi.ServiceHelper.loadFactories; /** @@ -52,7 +55,8 @@ public class QianfanStreamingChatModel implements StreamingChatLanguageModel { String responseFormat, Double penaltyScore, Boolean logRequests, - Boolean logResponses + Boolean logResponses, + Proxy proxy ) { if (Utils.isNullOrBlank(apiKey)||Utils.isNullOrBlank(secretKey)) { throw new IllegalArgumentException(" api key and secret key must be defined. It can be generated here: https://console.bce.baidu.com/qianfan/ais/console/applicationConsole/application"); @@ -71,6 +75,7 @@ public class QianfanStreamingChatModel implements StreamingChatLanguageModel { .secretKey(secretKey) .logRequests(logRequests) .logStreamingResponses(logResponses) + .proxy(proxy) .build(); this.temperature = getOrDefault(temperature, 0.7); this.topP = topP;