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
<!-- Please double-check the following points and mark them like this:
[X] -->
- [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
<!-- Before adding documentation and example(s) (below), please wait
until the PR is reviewed and approved. -->
- [ ] 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
<!-- Please double-check the following points and mark them like this:
[X] -->
- [ ] 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
<!-- Please double-check the following points and mark them like this:
[X] -->
- [ ] 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
<!-- Please double-check the following points and mark them like this:
[X] -->
- [ ] 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 <jianye.zheng@xuncetech.com>
This commit is contained in:
Yellow-- 2024-05-24 20:27:50 +08:00 committed by GitHub
parent 4a325d75b6
commit b6a0c41189
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 32 additions and 12 deletions

View File

@ -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);

View File

@ -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;

View File

@ -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);

View File

@ -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;