diff --git a/chat2db-client/src/blocks/Setting/AiSetting/aiTypeConfig.ts b/chat2db-client/src/blocks/Setting/AiSetting/aiTypeConfig.ts index e2fbef8b..8867614b 100644 --- a/chat2db-client/src/blocks/Setting/AiSetting/aiTypeConfig.ts +++ b/chat2db-client/src/blocks/Setting/AiSetting/aiTypeConfig.ts @@ -23,8 +23,8 @@ const AIFormConfig: Record = { }, [AIType.ZHIPUAI]: { apiKey: true, - apiHost: 'https://open.bigmodel.cn/api/paas/v3/model-api/', - model: 'chatglm_turbo', + apiHost: 'https://open.bigmodel.cn/api/paas/v4/chat/completions', + model: 'codegeex-4', }, [AIType.BAICHUANAI]: { apiKey: true, diff --git a/chat2db-client/src/typings/ai.ts b/chat2db-client/src/typings/ai.ts index e7baf564..0ed1d7b2 100644 --- a/chat2db-client/src/typings/ai.ts +++ b/chat2db-client/src/typings/ai.ts @@ -1,9 +1,9 @@ export enum AIType { - CHAT2DBAI = 'CHAT2DBAI', + // CHAT2DBAI = 'CHAT2DBAI', ZHIPUAI = 'ZHIPUAI', BAICHUANAI='BAICHUANAI', WENXINAI='WENXINAI', - // TONGYIQIANWENAI='TONGYIQIANWENAI', + TONGYIQIANWENAI='TONGYIQIANWENAI', OPENAI = 'OPENAI', AZUREAI = 'AZUREAI', RESTAI = 'RESTAI', diff --git a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/zhipu/client/ZhipuChatAIClient.java b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/zhipu/client/ZhipuChatAIClient.java index f205f17f..77e2aa0a 100644 --- a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/zhipu/client/ZhipuChatAIClient.java +++ b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/zhipu/client/ZhipuChatAIClient.java @@ -58,8 +58,8 @@ public class ZhipuChatAIClient { public static void refresh() { String apiKey = ""; - String apiHost = "https://open.bigmodel.cn/api/paas/v3/model-api/"; - String model = "chatglm_turbo"; + String apiHost = ""; + String model = ""; ConfigService configService = ApplicationContextUtil.getBean(ConfigService.class); Config apiHostConfig = configService.find(ZHIPU_HOST).getData(); if (apiHostConfig != null && StringUtils.isNotBlank(apiHostConfig.getContent())) { diff --git a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/zhipu/client/ZhipuChatAIStreamClient.java b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/zhipu/client/ZhipuChatAIStreamClient.java index 04bdb4b9..6ae3f1ee 100644 --- a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/zhipu/client/ZhipuChatAIStreamClient.java +++ b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/zhipu/client/ZhipuChatAIStreamClient.java @@ -204,11 +204,10 @@ public class ZhipuChatAIStreamClient { ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); String requestBody = mapper.writeValueAsString(completionsOptions); - - String url = this.apiHost + "/" + this.model + "/" + "sse-invoke"; + log.info("使用的model:{}", this.model); EventSource.Factory factory = EventSources.createFactory(this.okHttpClient); Request request = new Request.Builder() - .url(url) + .url(apiHost) .post(RequestBody.create(MediaType.parse(ContentType.JSON.getValue()), requestBody)) .build(); //Create event diff --git a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/zhipu/listener/ZhipuChatAIEventSourceListener.java b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/zhipu/listener/ZhipuChatAIEventSourceListener.java index 91ce3c78..efbc9f82 100644 --- a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/zhipu/listener/ZhipuChatAIEventSourceListener.java +++ b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/zhipu/listener/ZhipuChatAIEventSourceListener.java @@ -1,6 +1,5 @@ package ai.chat2db.server.web.api.controller.ai.zhipu.listener; -import ai.chat2db.server.web.api.controller.ai.fastchat.model.FastChatMessage; import ai.chat2db.server.web.api.controller.ai.zhipu.model.ZhipuChatCompletions; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; @@ -12,9 +11,9 @@ import okhttp3.ResponseBody; import okhttp3.sse.EventSource; import okhttp3.sse.EventSourceListener; import org.apache.commons.lang3.StringUtils; +import org.jetbrains.annotations.NotNull; import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; -import java.io.IOException; import java.util.Objects; /** @@ -47,7 +46,7 @@ public class ZhipuChatAIEventSourceListener extends EventSourceListener { */ @SneakyThrows @Override - public void onEvent(EventSource eventSource, String id, String type, String data) { + public void onEvent(EventSource eventSource, String id, String type, @NotNull String data) { log.info("Zhipu Chat AI response data:{}", data); if (data.equals("[DONE]")) { log.info("Zhipu Chat AI closed"); @@ -60,14 +59,9 @@ public class ZhipuChatAIEventSourceListener extends EventSourceListener { } ZhipuChatCompletions chatCompletions = mapper.readValue(data, ZhipuChatCompletions.class); - String text = chatCompletions.getData(); - if (Objects.isNull(text)) { - for (FastChatMessage message : chatCompletions.getBody().getChoices()) { - if (message != null && message.getContent() != null) { - text = message.getContent(); - } - } - } + String text = chatCompletions.getChoices().get(0).getDelta()==null? + chatCompletions.getChoices().get(0).getText() + :chatCompletions.getChoices().get(0).getDelta().getContent(); Message message = new Message(); message.setContent(text); @@ -79,15 +73,7 @@ public class ZhipuChatAIEventSourceListener extends EventSourceListener { @Override public void onClosed(EventSource eventSource) { - try { - sseEmitter.send(SseEmitter.event() - .id("[DONE]") - .data("[DONE]")); - } catch (IOException e) { - throw new RuntimeException(e); - } - sseEmitter.complete(); - log.info("ZhipuChatAI close sse connection..."); + log.info("Zhipu Chat AI closes sse connection closed"); } @Override diff --git a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/zhipu/model/ZhipuChatBody.java b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/zhipu/model/ZhipuChatBody.java index e84c81ed..d9bac352 100644 --- a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/zhipu/model/ZhipuChatBody.java +++ b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/zhipu/model/ZhipuChatBody.java @@ -4,6 +4,7 @@ package ai.chat2db.server.web.api.controller.ai.zhipu.model; import ai.chat2db.server.web.api.controller.ai.baichuan.model.BaichuanChatMessage; +import ai.chat2db.server.web.api.controller.ai.fastchat.model.FastChatChoice; import ai.chat2db.server.web.api.controller.ai.fastchat.model.FastChatCompletionsUsage; import ai.chat2db.server.web.api.controller.ai.fastchat.model.FastChatMessage; import com.fasterxml.jackson.annotation.JsonCreator; @@ -24,9 +25,16 @@ public final class ZhipuChatBody { * The log probabilities model for tokens associated with this completions choice. */ @JsonProperty(value = "choices") - private List choices; + private List choices; @JsonProperty(value = "usage") private FastChatCompletionsUsage usage; + @JsonCreator + private ZhipuChatBody( + @JsonProperty(value = "choices") List choices, + @JsonProperty(value = "usage") FastChatCompletionsUsage usage) { + this.choices = choices; + this.usage = usage; + } } diff --git a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/zhipu/model/ZhipuChatCompletions.java b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/zhipu/model/ZhipuChatCompletions.java index 323b63fb..00f78133 100644 --- a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/zhipu/model/ZhipuChatCompletions.java +++ b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/zhipu/model/ZhipuChatCompletions.java @@ -1,44 +1,38 @@ package ai.chat2db.server.web.api.controller.ai.zhipu.model; +import ai.chat2db.server.web.api.controller.ai.fastchat.model.FastChatChoice; +import ai.chat2db.server.web.api.controller.ai.fastchat.model.FastChatCompletionsUsage; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import lombok.Data; +import java.util.List; + @Data public class ZhipuChatCompletions { /* * A unique identifier associated with this chat completions response. */ - private String msg; + @JsonProperty(value = "id") + private String id; + @JsonProperty(value = "created") + private Long created; - private int statusCode; + @JsonProperty(value = "choices") + private List choices; - private String data; - - /* - * The collection of completions choices associated with this completions response. - * Generally, `n` choices are generated per provided prompt with a default value of 1. - * Token limits and other settings may limit the number of choices generated. - */ - @JsonProperty(value = "body") - private ZhipuChatBody body; - - /** - * Creates an instance of ChatCompletions class. - * - * @param msg the id value to set. - * @param code the created value to set. - * @param body the body value to set. - */ + @JsonProperty(value = "usage") + private FastChatCompletionsUsage usage; @JsonCreator private ZhipuChatCompletions( - @JsonProperty(value = "msg") String msg, - @JsonProperty(value = "code") int code, - @JsonProperty(value = "body") ZhipuChatBody body) { - this.msg = msg; - this.statusCode = code; - this.body = body; + @JsonProperty(value = "id") String id, + @JsonProperty(value = "created") Long created, + @JsonProperty(value = "choices") List choices, + @JsonProperty(value = "usage") FastChatCompletionsUsage usage) { + this.id = id; + this.created = created; + this.choices = choices; + this.usage = usage; } - } diff --git a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/zhipu/model/ZhipuChatCompletionsOptions.java b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/zhipu/model/ZhipuChatCompletionsOptions.java index 4b6359cc..931e4828 100644 --- a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/zhipu/model/ZhipuChatCompletionsOptions.java +++ b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/zhipu/model/ZhipuChatCompletionsOptions.java @@ -20,7 +20,7 @@ public final class ZhipuChatCompletionsOptions { private String requestId; // sse-params - @JsonProperty(value = "incremental") + @JsonProperty(value = "stream") private Boolean stream = true; @JsonProperty(value = "sseFormat") @@ -33,7 +33,7 @@ public final class ZhipuChatCompletionsOptions { * the behavior of the assistant, followed by alternating messages between the User and * Assistant roles. */ - @JsonProperty(value = "prompt") + @JsonProperty(value = "messages") private List prompt;