zhipu updata api interface

This commit is contained in:
shenj
2024-08-14 00:20:44 +08:00
parent 64af6424a2
commit 115a6a6f1b
8 changed files with 45 additions and 58 deletions

View File

@ -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())) {

View File

@ -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

View File

@ -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

View File

@ -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<FastChatMessage> choices;
private List<FastChatChoice> choices;
@JsonProperty(value = "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;
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<FastChatChoice> 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<FastChatChoice> choices,
@JsonProperty(value = "usage") FastChatCompletionsUsage usage) {
this.id = id;
this.created = created;
this.choices = choices;
this.usage = usage;
}
}

View File

@ -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<FastChatMessage> prompt;