Skip to content

Commit 115a6a6

Browse files
committed
zhipu updata api interface
1 parent 64af642 commit 115a6a6

8 files changed

Lines changed: 45 additions & 58 deletions

File tree

chat2db-client/src/blocks/Setting/AiSetting/aiTypeConfig.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ const AIFormConfig: Record<AIType, IAiConfigBooleans> = {
2323
},
2424
[AIType.ZHIPUAI]: {
2525
apiKey: true,
26-
apiHost: 'https://open.bigmodel.cn/api/paas/v3/model-api/',
27-
model: 'chatglm_turbo',
26+
apiHost: 'https://open.bigmodel.cn/api/paas/v4/chat/completions',
27+
model: 'codegeex-4',
2828
},
2929
[AIType.BAICHUANAI]: {
3030
apiKey: true,

chat2db-client/src/typings/ai.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
export enum AIType {
2-
CHAT2DBAI = 'CHAT2DBAI',
2+
// CHAT2DBAI = 'CHAT2DBAI',
33
ZHIPUAI = 'ZHIPUAI',
44
BAICHUANAI='BAICHUANAI',
55
WENXINAI='WENXINAI',
6-
// TONGYIQIANWENAI='TONGYIQIANWENAI',
6+
TONGYIQIANWENAI='TONGYIQIANWENAI',
77
OPENAI = 'OPENAI',
88
AZUREAI = 'AZUREAI',
99
RESTAI = 'RESTAI',

chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/zhipu/client/ZhipuChatAIClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ private static ZhipuChatAIStreamClient singleton() {
5858

5959
public static void refresh() {
6060
String apiKey = "";
61-
String apiHost = "https://open.bigmodel.cn/api/paas/v3/model-api/";
62-
String model = "chatglm_turbo";
61+
String apiHost = "";
62+
String model = "";
6363
ConfigService configService = ApplicationContextUtil.getBean(ConfigService.class);
6464
Config apiHostConfig = configService.find(ZHIPU_HOST).getData();
6565
if (apiHostConfig != null && StringUtils.isNotBlank(apiHostConfig.getContent())) {

chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/zhipu/client/ZhipuChatAIStreamClient.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,10 @@ public void streamCompletions(List<FastChatMessage> chatMessages, EventSourceLis
204204
ObjectMapper mapper = new ObjectMapper();
205205
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
206206
String requestBody = mapper.writeValueAsString(completionsOptions);
207-
208-
String url = this.apiHost + "/" + this.model + "/" + "sse-invoke";
207+
log.info("使用的model:{}", this.model);
209208
EventSource.Factory factory = EventSources.createFactory(this.okHttpClient);
210209
Request request = new Request.Builder()
211-
.url(url)
210+
.url(apiHost)
212211
.post(RequestBody.create(MediaType.parse(ContentType.JSON.getValue()), requestBody))
213212
.build();
214213
//Create event

chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/zhipu/listener/ZhipuChatAIEventSourceListener.java

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package ai.chat2db.server.web.api.controller.ai.zhipu.listener;
22

3-
import ai.chat2db.server.web.api.controller.ai.fastchat.model.FastChatMessage;
43
import ai.chat2db.server.web.api.controller.ai.zhipu.model.ZhipuChatCompletions;
54
import com.fasterxml.jackson.databind.DeserializationFeature;
65
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -12,9 +11,9 @@
1211
import okhttp3.sse.EventSource;
1312
import okhttp3.sse.EventSourceListener;
1413
import org.apache.commons.lang3.StringUtils;
14+
import org.jetbrains.annotations.NotNull;
1515
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
1616

17-
import java.io.IOException;
1817
import java.util.Objects;
1918

2019
/**
@@ -47,7 +46,7 @@ public void onOpen(EventSource eventSource, Response response) {
4746
*/
4847
@SneakyThrows
4948
@Override
50-
public void onEvent(EventSource eventSource, String id, String type, String data) {
49+
public void onEvent(EventSource eventSource, String id, String type, @NotNull String data) {
5150
log.info("Zhipu Chat AI response data:{}", data);
5251
if (data.equals("[DONE]")) {
5352
log.info("Zhipu Chat AI closed");
@@ -60,14 +59,9 @@ public void onEvent(EventSource eventSource, String id, String type, String data
6059
}
6160

6261
ZhipuChatCompletions chatCompletions = mapper.readValue(data, ZhipuChatCompletions.class);
63-
String text = chatCompletions.getData();
64-
if (Objects.isNull(text)) {
65-
for (FastChatMessage message : chatCompletions.getBody().getChoices()) {
66-
if (message != null && message.getContent() != null) {
67-
text = message.getContent();
68-
}
69-
}
70-
}
62+
String text = chatCompletions.getChoices().get(0).getDelta()==null?
63+
chatCompletions.getChoices().get(0).getText()
64+
:chatCompletions.getChoices().get(0).getDelta().getContent();
7165

7266
Message message = new Message();
7367
message.setContent(text);
@@ -79,15 +73,7 @@ public void onEvent(EventSource eventSource, String id, String type, String data
7973

8074
@Override
8175
public void onClosed(EventSource eventSource) {
82-
try {
83-
sseEmitter.send(SseEmitter.event()
84-
.id("[DONE]")
85-
.data("[DONE]"));
86-
} catch (IOException e) {
87-
throw new RuntimeException(e);
88-
}
89-
sseEmitter.complete();
90-
log.info("ZhipuChatAI close sse connection...");
76+
log.info("Zhipu Chat AI closes sse connection closed");
9177
}
9278

9379
@Override

chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/zhipu/model/ZhipuChatBody.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package ai.chat2db.server.web.api.controller.ai.zhipu.model;
55

66
import ai.chat2db.server.web.api.controller.ai.baichuan.model.BaichuanChatMessage;
7+
import ai.chat2db.server.web.api.controller.ai.fastchat.model.FastChatChoice;
78
import ai.chat2db.server.web.api.controller.ai.fastchat.model.FastChatCompletionsUsage;
89
import ai.chat2db.server.web.api.controller.ai.fastchat.model.FastChatMessage;
910
import com.fasterxml.jackson.annotation.JsonCreator;
@@ -24,9 +25,16 @@ public final class ZhipuChatBody {
2425
* The log probabilities model for tokens associated with this completions choice.
2526
*/
2627
@JsonProperty(value = "choices")
27-
private List<FastChatMessage> choices;
28+
private List<FastChatChoice> choices;
2829

2930
@JsonProperty(value = "usage")
3031
private FastChatCompletionsUsage usage;
32+
@JsonCreator
33+
private ZhipuChatBody(
34+
@JsonProperty(value = "choices") List<FastChatChoice> choices,
35+
@JsonProperty(value = "usage") FastChatCompletionsUsage usage) {
36+
this.choices = choices;
37+
this.usage = usage;
38+
}
3139

3240
}
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,38 @@
11
package ai.chat2db.server.web.api.controller.ai.zhipu.model;
22

3+
import ai.chat2db.server.web.api.controller.ai.fastchat.model.FastChatChoice;
4+
import ai.chat2db.server.web.api.controller.ai.fastchat.model.FastChatCompletionsUsage;
35
import com.fasterxml.jackson.annotation.JsonCreator;
46
import com.fasterxml.jackson.annotation.JsonProperty;
57
import lombok.Data;
68

9+
import java.util.List;
10+
711
@Data
812
public class ZhipuChatCompletions {
913

1014
/*
1115
* A unique identifier associated with this chat completions response.
1216
*/
13-
private String msg;
14-
15-
private int statusCode;
17+
@JsonProperty(value = "id")
18+
private String id;
19+
@JsonProperty(value = "created")
20+
private Long created;
1621

17-
private String data;
22+
@JsonProperty(value = "choices")
23+
private List<FastChatChoice> choices;
1824

19-
/*
20-
* The collection of completions choices associated with this completions response.
21-
* Generally, `n` choices are generated per provided prompt with a default value of 1.
22-
* Token limits and other settings may limit the number of choices generated.
23-
*/
24-
@JsonProperty(value = "body")
25-
private ZhipuChatBody body;
26-
27-
/**
28-
* Creates an instance of ChatCompletions class.
29-
*
30-
* @param msg the id value to set.
31-
* @param code the created value to set.
32-
* @param body the body value to set.
33-
*/
25+
@JsonProperty(value = "usage")
26+
private FastChatCompletionsUsage usage;
3427
@JsonCreator
3528
private ZhipuChatCompletions(
36-
@JsonProperty(value = "msg") String msg,
37-
@JsonProperty(value = "code") int code,
38-
@JsonProperty(value = "body") ZhipuChatBody body) {
39-
this.msg = msg;
40-
this.statusCode = code;
41-
this.body = body;
29+
@JsonProperty(value = "id") String id,
30+
@JsonProperty(value = "created") Long created,
31+
@JsonProperty(value = "choices") List<FastChatChoice> choices,
32+
@JsonProperty(value = "usage") FastChatCompletionsUsage usage) {
33+
this.id = id;
34+
this.created = created;
35+
this.choices = choices;
36+
this.usage = usage;
4237
}
43-
4438
}

chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/zhipu/model/ZhipuChatCompletionsOptions.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public final class ZhipuChatCompletionsOptions {
2020
private String requestId;
2121

2222
// sse-params
23-
@JsonProperty(value = "incremental")
23+
@JsonProperty(value = "stream")
2424
private Boolean stream = true;
2525

2626
@JsonProperty(value = "sseFormat")
@@ -33,7 +33,7 @@ public final class ZhipuChatCompletionsOptions {
3333
* the behavior of the assistant, followed by alternating messages between the User and
3434
* Assistant roles.
3535
*/
36-
@JsonProperty(value = "prompt")
36+
@JsonProperty(value = "messages")
3737
private List<FastChatMessage> prompt;
3838

3939

0 commit comments

Comments
 (0)