Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
shanhexi committed Nov 6, 2023
2 parents 54bfdcf + f461d9d commit 5883b49
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 22 deletions.
2 changes: 1 addition & 1 deletion chat2db-client/src/typings/ai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export enum AIType {
CHAT2DBAI = 'CHAT2DBAI',
ZHIPUAI = 'ZHIPUAI',
BAICHUANAI='BAICHUANAI',
// WENXINAI='WENXINAI',
WENXINAI='WENXINAI',
// TONGYIQIANWENAI='TONGYIQIANWENAI',
OPENAI = 'OPENAI',
AZUREAI = 'AZUREAI',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ chatgpt:
# 可以选择GPT3或GPT35
version: GPT35
context:
length: 4
length: 1

# Print the HTTP log
logbook:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,9 @@ private List<FastChatMessage> getFastChatMessage(String uid, String prompt) {
private SseEmitter chatWithWenxinAi(ChatQueryRequest queryRequest, SseEmitter sseEmitter, String uid) throws IOException {
String prompt = buildPrompt(queryRequest);
List<FastChatMessage> messages = getFastChatMessage(uid, prompt);
if (messages.size() >= 2 && messages.size() % 2 == 0) {
messages.remove(messages.size() - 1);
}

buildSseEmitter(sseEmitter, uid);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,21 @@ private static WenxinAIStreamClient singleton() {
}

public static void refresh() {
String apiHost = "";
String apiHost = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions_pro";
String accessToken = "";
String model = "";
ConfigService configService = ApplicationContextUtil.getBean(ConfigService.class);
Config apiHostConfig = configService.find(WENXIN_HOST).getData();
if (apiHostConfig != null && StringUtils.isNotBlank(apiHostConfig.getContent())) {
apiHost = apiHostConfig.getContent();
if (apiHost.endsWith("/")) {
apiHost = apiHost.substring(0, apiHost.length() - 1);
}
}
Config config = configService.find(WENXIN_ACCESS_TOKEN).getData();
if (config != null && StringUtils.isNotBlank(config.getContent())) {
accessToken = config.getContent();
}
Config deployConfig = configService.find(WENXIN_MODEL).getData();
if (deployConfig != null && StringUtils.isNotBlank(deployConfig.getContent())) {
model = deployConfig.getContent();
}
WENXIN_AI_CLIENT = WenxinAIStreamClient.builder().accessToken(accessToken).apiHost(apiHost).model(model)
.build();
WENXIN_AI_CLIENT = WenxinAIStreamClient.builder().accessToken(accessToken).apiHost(apiHost).build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.*;
import okhttp3.sse.EventSource;
import okhttp3.sse.EventSourceListener;
import okhttp3.sse.EventSources;
Expand Down Expand Up @@ -179,7 +176,6 @@ public void streamCompletions(List<FastChatMessage> chatMessages, EventSourceLis

FastChatCompletionsOptions chatCompletionsOptions = new FastChatCompletionsOptions(chatMessages);
chatCompletionsOptions.setStream(true);
chatCompletionsOptions.setModel(this.model);

EventSource.Factory factory = EventSources.createFactory(this.okHttpClient);
ObjectMapper mapper = new ObjectMapper();
Expand All @@ -192,7 +188,7 @@ public void streamCompletions(List<FastChatMessage> chatMessages, EventSourceLis
EventSource eventSource = factory.newEventSource(request, eventSourceListener);
log.info("finish invoking fast chat ai");
} catch (Exception e) {
log.error("fast chat ai error", e);
log.error("wenxin chat ai error", e);
eventSourceListener.onFailure(null, e, null);
throw new ParamBusinessException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public WenxinAIEventSourceListener(SseEmitter sseEmitter) {
*/
@Override
public void onOpen(EventSource eventSource, Response response) {
log.info("Fast Chat Sse connecting...");
log.info("Wenxin chat Sse connecting...");
}

/**
Expand Down Expand Up @@ -81,7 +81,7 @@ public void onClosed(EventSource eventSource) {
throw new RuntimeException(e);
}
sseEmitter.complete();
log.info("FastChatAI close sse connection...");
log.info("WenxinChatAI close sse connection...");
}

@Override
Expand All @@ -107,13 +107,13 @@ public void onFailure(EventSource eventSource, Throwable t, Response response) {
if (StringUtils.isBlank(bodyString) && Objects.nonNull(t)) {
bodyString = t.getMessage();
}
log.error("Fast Chat AI sse response:{}", bodyString);
log.error("Wenxin chat AI sse response:{}", bodyString);
} else {
log.error("Fast Chat AI sse response:{},error:{}", response, t);
log.error("Wenxin chat AI sse response:{},error:{}", response, t);
}
eventSource.cancel();
Message message = new Message();
message.setContent("Fast Chat AI error:" + bodyString);
message.setContent("Wenxin chat AI error:" + bodyString);
sseEmitter.send(SseEmitter.event()
.id("[ERROR]")
.data(message));
Expand All @@ -122,7 +122,7 @@ public void onFailure(EventSource eventSource, Throwable t, Response response) {
.data("[DONE]"));
sseEmitter.complete();
} catch (Exception exception) {
log.error("Fast Chat AI send data error:", exception);
log.error("Wenxin chat AI send data error:", exception);
}
}
}

0 comments on commit 5883b49

Please # to comment.