Merge pull request #1501 from shenbuxing/updata-api

zhipu updata api interface
This commit is contained in:
Chat2DB
2024-10-28 11:47:56 +08:00
committed by GitHub
8 changed files with 45 additions and 58 deletions

View File

@ -23,8 +23,8 @@ const AIFormConfig: Record<AIType, IAiConfigBooleans> = {
}, },
[AIType.ZHIPUAI]: { [AIType.ZHIPUAI]: {
apiKey: true, apiKey: true,
apiHost: 'https://open.bigmodel.cn/api/paas/v3/model-api/', apiHost: 'https://open.bigmodel.cn/api/paas/v4/chat/completions',
model: 'chatglm_turbo', model: 'codegeex-4',
}, },
[AIType.BAICHUANAI]: { [AIType.BAICHUANAI]: {
apiKey: true, apiKey: true,

View File

@ -1,9 +1,9 @@
export enum AIType { export enum AIType {
CHAT2DBAI = 'CHAT2DBAI', // CHAT2DBAI = 'CHAT2DBAI',
ZHIPUAI = 'ZHIPUAI', ZHIPUAI = 'ZHIPUAI',
BAICHUANAI='BAICHUANAI', BAICHUANAI='BAICHUANAI',
WENXINAI='WENXINAI', WENXINAI='WENXINAI',
// TONGYIQIANWENAI='TONGYIQIANWENAI', TONGYIQIANWENAI='TONGYIQIANWENAI',
OPENAI = 'OPENAI', OPENAI = 'OPENAI',
AZUREAI = 'AZUREAI', AZUREAI = 'AZUREAI',
RESTAI = 'RESTAI', RESTAI = 'RESTAI',

View File

@ -58,8 +58,8 @@ public class ZhipuChatAIClient {
public static void refresh() { public static void refresh() {
String apiKey = ""; String apiKey = "";
String apiHost = "https://open.bigmodel.cn/api/paas/v3/model-api/"; String apiHost = "";
String model = "chatglm_turbo"; String model = "";
ConfigService configService = ApplicationContextUtil.getBean(ConfigService.class); ConfigService configService = ApplicationContextUtil.getBean(ConfigService.class);
Config apiHostConfig = configService.find(ZHIPU_HOST).getData(); Config apiHostConfig = configService.find(ZHIPU_HOST).getData();
if (apiHostConfig != null && StringUtils.isNotBlank(apiHostConfig.getContent())) { if (apiHostConfig != null && StringUtils.isNotBlank(apiHostConfig.getContent())) {

View File

@ -204,11 +204,10 @@ public class ZhipuChatAIStreamClient {
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
String requestBody = mapper.writeValueAsString(completionsOptions); String requestBody = mapper.writeValueAsString(completionsOptions);
log.info("使用的model:{}", this.model);
String url = this.apiHost + "/" + this.model + "/" + "sse-invoke";
EventSource.Factory factory = EventSources.createFactory(this.okHttpClient); EventSource.Factory factory = EventSources.createFactory(this.okHttpClient);
Request request = new Request.Builder() Request request = new Request.Builder()
.url(url) .url(apiHost)
.post(RequestBody.create(MediaType.parse(ContentType.JSON.getValue()), requestBody)) .post(RequestBody.create(MediaType.parse(ContentType.JSON.getValue()), requestBody))
.build(); .build();
//Create event //Create event

View File

@ -1,6 +1,5 @@
package ai.chat2db.server.web.api.controller.ai.zhipu.listener; 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 ai.chat2db.server.web.api.controller.ai.zhipu.model.ZhipuChatCompletions;
import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
@ -12,9 +11,9 @@ import okhttp3.ResponseBody;
import okhttp3.sse.EventSource; import okhttp3.sse.EventSource;
import okhttp3.sse.EventSourceListener; import okhttp3.sse.EventSourceListener;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import java.io.IOException;
import java.util.Objects; import java.util.Objects;
/** /**
@ -47,7 +46,7 @@ public class ZhipuChatAIEventSourceListener extends EventSourceListener {
*/ */
@SneakyThrows @SneakyThrows
@Override @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); log.info("Zhipu Chat AI response data{}", data);
if (data.equals("[DONE]")) { if (data.equals("[DONE]")) {
log.info("Zhipu Chat AI closed"); log.info("Zhipu Chat AI closed");
@ -60,14 +59,9 @@ public class ZhipuChatAIEventSourceListener extends EventSourceListener {
} }
ZhipuChatCompletions chatCompletions = mapper.readValue(data, ZhipuChatCompletions.class); ZhipuChatCompletions chatCompletions = mapper.readValue(data, ZhipuChatCompletions.class);
String text = chatCompletions.getData(); String text = chatCompletions.getChoices().get(0).getDelta()==null?
if (Objects.isNull(text)) { chatCompletions.getChoices().get(0).getText()
for (FastChatMessage message : chatCompletions.getBody().getChoices()) { :chatCompletions.getChoices().get(0).getDelta().getContent();
if (message != null && message.getContent() != null) {
text = message.getContent();
}
}
}
Message message = new Message(); Message message = new Message();
message.setContent(text); message.setContent(text);
@ -79,15 +73,7 @@ public class ZhipuChatAIEventSourceListener extends EventSourceListener {
@Override @Override
public void onClosed(EventSource eventSource) { public void onClosed(EventSource eventSource) {
try { log.info("Zhipu Chat AI closes sse connection closed");
sseEmitter.send(SseEmitter.event()
.id("[DONE]")
.data("[DONE]"));
} catch (IOException e) {
throw new RuntimeException(e);
}
sseEmitter.complete();
log.info("ZhipuChatAI close sse connection...");
} }
@Override @Override

View File

@ -4,6 +4,7 @@
package ai.chat2db.server.web.api.controller.ai.zhipu.model; 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.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.FastChatCompletionsUsage;
import ai.chat2db.server.web.api.controller.ai.fastchat.model.FastChatMessage; import ai.chat2db.server.web.api.controller.ai.fastchat.model.FastChatMessage;
import com.fasterxml.jackson.annotation.JsonCreator; 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. * The log probabilities model for tokens associated with this completions choice.
*/ */
@JsonProperty(value = "choices") @JsonProperty(value = "choices")
private List<FastChatMessage> choices; private List<FastChatChoice> choices;
@JsonProperty(value = "usage") @JsonProperty(value = "usage")
private FastChatCompletionsUsage usage; private FastChatCompletionsUsage usage;
@JsonCreator
private ZhipuChatBody(
@JsonProperty(value = "choices") List<FastChatChoice> choices,
@JsonProperty(value = "usage") FastChatCompletionsUsage usage) {
this.choices = choices;
this.usage = usage;
}
} }

View File

@ -1,44 +1,38 @@
package ai.chat2db.server.web.api.controller.ai.zhipu.model; 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.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data; import lombok.Data;
import java.util.List;
@Data @Data
public class ZhipuChatCompletions { public class ZhipuChatCompletions {
/* /*
* A unique identifier associated with this chat completions response. * 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<FastChatChoice> choices;
private String data; @JsonProperty(value = "usage")
private FastChatCompletionsUsage usage;
/*
* 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.
*/
@JsonCreator @JsonCreator
private ZhipuChatCompletions( private ZhipuChatCompletions(
@JsonProperty(value = "msg") String msg, @JsonProperty(value = "id") String id,
@JsonProperty(value = "code") int code, @JsonProperty(value = "created") Long created,
@JsonProperty(value = "body") ZhipuChatBody body) { @JsonProperty(value = "choices") List<FastChatChoice> choices,
this.msg = msg; @JsonProperty(value = "usage") FastChatCompletionsUsage usage) {
this.statusCode = code; this.id = id;
this.body = body; this.created = created;
this.choices = choices;
this.usage = usage;
} }
} }

View File

@ -20,7 +20,7 @@ public final class ZhipuChatCompletionsOptions {
private String requestId; private String requestId;
// sse-params // sse-params
@JsonProperty(value = "incremental") @JsonProperty(value = "stream")
private Boolean stream = true; private Boolean stream = true;
@JsonProperty(value = "sseFormat") @JsonProperty(value = "sseFormat")
@ -33,7 +33,7 @@ public final class ZhipuChatCompletionsOptions {
* the behavior of the assistant, followed by alternating messages between the User and * the behavior of the assistant, followed by alternating messages between the User and
* Assistant roles. * Assistant roles.
*/ */
@JsonProperty(value = "prompt") @JsonProperty(value = "messages")
private List<FastChatMessage> prompt; private List<FastChatMessage> prompt;