Skip to content

Commit 37ced20

Browse files
committed
add wenxinyiyan support
1 parent 54f5cc9 commit 37ced20

3 files changed

Lines changed: 13 additions & 20 deletions

File tree

chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/wenxin/client/WenxinAIClient.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,24 +58,21 @@ private static WenxinAIStreamClient singleton() {
5858
}
5959

6060
public static void refresh() {
61-
String apiHost = "";
61+
String apiHost = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions_pro";
6262
String accessToken = "";
63-
String model = "";
6463
ConfigService configService = ApplicationContextUtil.getBean(ConfigService.class);
6564
Config apiHostConfig = configService.find(WENXIN_HOST).getData();
6665
if (apiHostConfig != null && StringUtils.isNotBlank(apiHostConfig.getContent())) {
6766
apiHost = apiHostConfig.getContent();
67+
if (apiHost.endsWith("/")) {
68+
apiHost = apiHost.substring(0, apiHost.length() - 1);
69+
}
6870
}
6971
Config config = configService.find(WENXIN_ACCESS_TOKEN).getData();
7072
if (config != null && StringUtils.isNotBlank(config.getContent())) {
7173
accessToken = config.getContent();
7274
}
73-
Config deployConfig = configService.find(WENXIN_MODEL).getData();
74-
if (deployConfig != null && StringUtils.isNotBlank(deployConfig.getContent())) {
75-
model = deployConfig.getContent();
76-
}
77-
WENXIN_AI_CLIENT = WenxinAIStreamClient.builder().accessToken(accessToken).apiHost(apiHost).model(model)
78-
.build();
75+
WENXIN_AI_CLIENT = WenxinAIStreamClient.builder().accessToken(accessToken).apiHost(apiHost).build();
7976
}
8077

8178
}

chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/wenxin/client/WenxinAIStreamClient.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@
88
import com.fasterxml.jackson.databind.ObjectMapper;
99
import lombok.Getter;
1010
import lombok.extern.slf4j.Slf4j;
11-
import okhttp3.MediaType;
12-
import okhttp3.OkHttpClient;
13-
import okhttp3.Request;
14-
import okhttp3.RequestBody;
11+
import okhttp3.*;
1512
import okhttp3.sse.EventSource;
1613
import okhttp3.sse.EventSourceListener;
1714
import okhttp3.sse.EventSources;
@@ -179,7 +176,6 @@ public void streamCompletions(List<FastChatMessage> chatMessages, EventSourceLis
179176

180177
FastChatCompletionsOptions chatCompletionsOptions = new FastChatCompletionsOptions(chatMessages);
181178
chatCompletionsOptions.setStream(true);
182-
chatCompletionsOptions.setModel(this.model);
183179

184180
EventSource.Factory factory = EventSources.createFactory(this.okHttpClient);
185181
ObjectMapper mapper = new ObjectMapper();
@@ -192,7 +188,7 @@ public void streamCompletions(List<FastChatMessage> chatMessages, EventSourceLis
192188
EventSource eventSource = factory.newEventSource(request, eventSourceListener);
193189
log.info("finish invoking fast chat ai");
194190
} catch (Exception e) {
195-
log.error("fast chat ai error", e);
191+
log.error("wenxin chat ai error", e);
196192
eventSourceListener.onFailure(null, e, null);
197193
throw new ParamBusinessException();
198194
}

chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/wenxin/listener/WenxinAIEventSourceListener.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public WenxinAIEventSourceListener(SseEmitter sseEmitter) {
3838
*/
3939
@Override
4040
public void onOpen(EventSource eventSource, Response response) {
41-
log.info("Fast Chat Sse connecting...");
41+
log.info("Wenxin chat Sse connecting...");
4242
}
4343

4444
/**
@@ -81,7 +81,7 @@ public void onClosed(EventSource eventSource) {
8181
throw new RuntimeException(e);
8282
}
8383
sseEmitter.complete();
84-
log.info("FastChatAI close sse connection...");
84+
log.info("WenxinChatAI close sse connection...");
8585
}
8686

8787
@Override
@@ -107,13 +107,13 @@ public void onFailure(EventSource eventSource, Throwable t, Response response) {
107107
if (StringUtils.isBlank(bodyString) && Objects.nonNull(t)) {
108108
bodyString = t.getMessage();
109109
}
110-
log.error("Fast Chat AI sse response:{}", bodyString);
110+
log.error("Wenxin chat AI sse response:{}", bodyString);
111111
} else {
112-
log.error("Fast Chat AI sse response:{},error:{}", response, t);
112+
log.error("Wenxin chat AI sse response:{},error:{}", response, t);
113113
}
114114
eventSource.cancel();
115115
Message message = new Message();
116-
message.setContent("Fast Chat AI error:" + bodyString);
116+
message.setContent("Wenxin chat AI error:" + bodyString);
117117
sseEmitter.send(SseEmitter.event()
118118
.id("[ERROR]")
119119
.data(message));
@@ -122,7 +122,7 @@ public void onFailure(EventSource eventSource, Throwable t, Response response) {
122122
.data("[DONE]"));
123123
sseEmitter.complete();
124124
} catch (Exception exception) {
125-
log.error("Fast Chat AI send data error:", exception);
125+
log.error("Wenxin chat AI send data error:", exception);
126126
}
127127
}
128128
}

0 commit comments

Comments
 (0)