mirror of
https://github.com/CodePhiliaX/Chat2DB.git
synced 2025-07-31 11:42:41 +08:00
add wenxinyiyan support
This commit is contained in:
@ -58,24 +58,21 @@ public class WenxinAIClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void refresh() {
|
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 accessToken = "";
|
||||||
String model = "";
|
|
||||||
ConfigService configService = ApplicationContextUtil.getBean(ConfigService.class);
|
ConfigService configService = ApplicationContextUtil.getBean(ConfigService.class);
|
||||||
Config apiHostConfig = configService.find(WENXIN_HOST).getData();
|
Config apiHostConfig = configService.find(WENXIN_HOST).getData();
|
||||||
if (apiHostConfig != null && StringUtils.isNotBlank(apiHostConfig.getContent())) {
|
if (apiHostConfig != null && StringUtils.isNotBlank(apiHostConfig.getContent())) {
|
||||||
apiHost = apiHostConfig.getContent();
|
apiHost = apiHostConfig.getContent();
|
||||||
|
if (apiHost.endsWith("/")) {
|
||||||
|
apiHost = apiHost.substring(0, apiHost.length() - 1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Config config = configService.find(WENXIN_ACCESS_TOKEN).getData();
|
Config config = configService.find(WENXIN_ACCESS_TOKEN).getData();
|
||||||
if (config != null && StringUtils.isNotBlank(config.getContent())) {
|
if (config != null && StringUtils.isNotBlank(config.getContent())) {
|
||||||
accessToken = config.getContent();
|
accessToken = config.getContent();
|
||||||
}
|
}
|
||||||
Config deployConfig = configService.find(WENXIN_MODEL).getData();
|
WENXIN_AI_CLIENT = WenxinAIStreamClient.builder().accessToken(accessToken).apiHost(apiHost).build();
|
||||||
if (deployConfig != null && StringUtils.isNotBlank(deployConfig.getContent())) {
|
|
||||||
model = deployConfig.getContent();
|
|
||||||
}
|
|
||||||
WENXIN_AI_CLIENT = WenxinAIStreamClient.builder().accessToken(accessToken).apiHost(apiHost).model(model)
|
|
||||||
.build();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -8,10 +8,7 @@ import cn.hutool.http.ContentType;
|
|||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import okhttp3.MediaType;
|
import okhttp3.*;
|
||||||
import okhttp3.OkHttpClient;
|
|
||||||
import okhttp3.Request;
|
|
||||||
import okhttp3.RequestBody;
|
|
||||||
import okhttp3.sse.EventSource;
|
import okhttp3.sse.EventSource;
|
||||||
import okhttp3.sse.EventSourceListener;
|
import okhttp3.sse.EventSourceListener;
|
||||||
import okhttp3.sse.EventSources;
|
import okhttp3.sse.EventSources;
|
||||||
@ -179,7 +176,6 @@ public class WenxinAIStreamClient {
|
|||||||
|
|
||||||
FastChatCompletionsOptions chatCompletionsOptions = new FastChatCompletionsOptions(chatMessages);
|
FastChatCompletionsOptions chatCompletionsOptions = new FastChatCompletionsOptions(chatMessages);
|
||||||
chatCompletionsOptions.setStream(true);
|
chatCompletionsOptions.setStream(true);
|
||||||
chatCompletionsOptions.setModel(this.model);
|
|
||||||
|
|
||||||
EventSource.Factory factory = EventSources.createFactory(this.okHttpClient);
|
EventSource.Factory factory = EventSources.createFactory(this.okHttpClient);
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
@ -192,7 +188,7 @@ public class WenxinAIStreamClient {
|
|||||||
EventSource eventSource = factory.newEventSource(request, eventSourceListener);
|
EventSource eventSource = factory.newEventSource(request, eventSourceListener);
|
||||||
log.info("finish invoking fast chat ai");
|
log.info("finish invoking fast chat ai");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("fast chat ai error", e);
|
log.error("wenxin chat ai error", e);
|
||||||
eventSourceListener.onFailure(null, e, null);
|
eventSourceListener.onFailure(null, e, null);
|
||||||
throw new ParamBusinessException();
|
throw new ParamBusinessException();
|
||||||
}
|
}
|
||||||
|
@ -38,7 +38,7 @@ public class WenxinAIEventSourceListener extends EventSourceListener {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void onOpen(EventSource eventSource, Response response) {
|
public void onOpen(EventSource eventSource, Response response) {
|
||||||
log.info("Fast Chat Sse connecting...");
|
log.info("Wenxin chat Sse connecting...");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -81,7 +81,7 @@ public class WenxinAIEventSourceListener extends EventSourceListener {
|
|||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
sseEmitter.complete();
|
sseEmitter.complete();
|
||||||
log.info("FastChatAI close sse connection...");
|
log.info("WenxinChatAI close sse connection...");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -107,13 +107,13 @@ public class WenxinAIEventSourceListener extends EventSourceListener {
|
|||||||
if (StringUtils.isBlank(bodyString) && Objects.nonNull(t)) {
|
if (StringUtils.isBlank(bodyString) && Objects.nonNull(t)) {
|
||||||
bodyString = t.getMessage();
|
bodyString = t.getMessage();
|
||||||
}
|
}
|
||||||
log.error("Fast Chat AI sse response:{}", bodyString);
|
log.error("Wenxin chat AI sse response:{}", bodyString);
|
||||||
} else {
|
} else {
|
||||||
log.error("Fast Chat AI sse response:{},error:{}", response, t);
|
log.error("Wenxin chat AI sse response:{},error:{}", response, t);
|
||||||
}
|
}
|
||||||
eventSource.cancel();
|
eventSource.cancel();
|
||||||
Message message = new Message();
|
Message message = new Message();
|
||||||
message.setContent("Fast Chat AI error:" + bodyString);
|
message.setContent("Wenxin chat AI error:" + bodyString);
|
||||||
sseEmitter.send(SseEmitter.event()
|
sseEmitter.send(SseEmitter.event()
|
||||||
.id("[ERROR]")
|
.id("[ERROR]")
|
||||||
.data(message));
|
.data(message));
|
||||||
@ -122,7 +122,7 @@ public class WenxinAIEventSourceListener extends EventSourceListener {
|
|||||||
.data("[DONE]"));
|
.data("[DONE]"));
|
||||||
sseEmitter.complete();
|
sseEmitter.complete();
|
||||||
} catch (Exception exception) {
|
} catch (Exception exception) {
|
||||||
log.error("Fast Chat AI send data error:", exception);
|
log.error("Wenxin chat AI send data error:", exception);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user