plugins;
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/src/main/java/com/gearwenxin/entity/chatmodel/ChatBaseRequest.java:
--------------------------------------------------------------------------------
1 | package com.gearwenxin.entity.chatmodel;
2 |
3 | import lombok.AllArgsConstructor;
4 | import lombok.Builder;
5 | import lombok.Data;
6 | import lombok.NoArgsConstructor;
7 |
8 | import java.io.Serializable;
9 |
10 | /**
11 | * @author Ge Mingjia
12 | * {@code @date} 2023/8/3
13 | */
14 | @Data
15 | @Builder
16 | @AllArgsConstructor
17 | @NoArgsConstructor
18 | public class ChatBaseRequest implements Serializable {
19 |
20 | /**
21 | * 表示最终用户的唯一标识符,可以监视和检测滥用行为,防止接口恶意调用
22 | */
23 | private String userId;
24 |
25 | /**
26 | * 聊天信息,不能为空
27 | */
28 | private String content;
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/java/com/gearwenxin/entity/chatmodel/ChatErnieRequest.java:
--------------------------------------------------------------------------------
1 | package com.gearwenxin.entity.chatmodel;
2 |
3 | import com.gearwenxin.entity.FunctionCall;
4 | import com.gearwenxin.entity.FunctionInfo;
5 | import lombok.*;
6 | import lombok.extern.slf4j.Slf4j;
7 |
8 | import java.util.List;
9 |
10 | /**
11 | * @author Ge Mingjia
12 | * {@code @date} 2023/7/20
13 | *
14 | * ContBot 模型
15 | */
16 | @Slf4j
17 | @Data
18 | @NoArgsConstructor
19 | @AllArgsConstructor
20 | @EqualsAndHashCode(callSuper = true)
21 | public class ChatErnieRequest extends ChatBaseRequest {
22 |
23 | /**
24 | * 输出更加随机,而较低的数值会使其更加集中和确定,默认0.95,范围 (0, 1.0]
25 | */
26 | private Float temperature;
27 |
28 | /**
29 | * (影响输出文本的多样性,越大生成文本的多样性越强
30 | */
31 | private Float topP;
32 |
33 | /**
34 | * 通过对已生成的token增加惩罚,减少重复生成的现象。
35 | */
36 | private Float penaltyScore;
37 |
38 | /**
39 | * 一个可触发函数的描述列表
40 | */
41 | private List functions;
42 |
43 | /**
44 | * 模型人设,主要用于人设设定,例如,你是xxx公司制作的AI助手,说明:
45 | * (1)长度限制1024个字符
46 | * (2)如果使用functions参数,不支持设定人设system
47 | */
48 | private String system;
49 |
50 | /**
51 | * message作者;当role=function时,必填,且是响应内容中function_call中的name
52 | */
53 | private String name;
54 |
55 | /**
56 | * 函数调用,function call场景下第一轮对话的返回,第二轮对话作为历史信息在message中传入
57 | */
58 | private FunctionCall functionCall;
59 |
60 | /**
61 | * 生成停止标识,当模型生成结果以stop中某个元素结尾时,停止文本生成。说明:
62 | * (1)每个元素长度不超过20字符
63 | * (2)最多4个元素
64 | */
65 | private List stop;
66 |
67 | /**
68 | * 是否强制关闭实时搜索功能,默认false,表示不关闭
69 | */
70 | private Boolean disableSearch;
71 |
72 | /**
73 | * 是否开启上角标返回,说明:
74 | * (1)开启后,有概率触发搜索溯源信息search_info,search_info内容见响应参数介绍
75 | * (2)默认false,不开启
76 | */
77 | private Boolean enableCitation;
78 |
79 | /**
80 | * 指定响应内容的格式,说明:
81 | * (1)可选值:
82 | * · json_object:以json格式返回,可能出现不满足效果情况
83 | * · text:以文本格式返回
84 | * (2)如果不填写参数response_format值,默认为text
85 | */
86 | private String responseFormat;
87 |
88 | }
89 |
--------------------------------------------------------------------------------
/src/main/java/com/gearwenxin/entity/chatmodel/ChatPromptRequest.java:
--------------------------------------------------------------------------------
1 | package com.gearwenxin.entity.chatmodel;
2 |
3 | import lombok.*;
4 |
5 | import java.util.Map;
6 |
7 | /**
8 | * @author Ge Mingjia
9 | * {@code @date} 2023/7/26
10 | */
11 | @Data
12 | @NoArgsConstructor
13 | @AllArgsConstructor
14 | public class ChatPromptRequest {
15 |
16 | /**
17 | * prompt工程里面对应的模板id
18 | */
19 | private String id;
20 |
21 | /**
22 | * 参数map
23 | */
24 | private Map paramMap;
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/com/gearwenxin/entity/enums/ModelType.java:
--------------------------------------------------------------------------------
1 | package com.gearwenxin.entity.enums;
2 |
3 | import lombok.Getter;
4 |
5 | /**
6 | * @author Ge Mingjia
7 | * {@code @date} 2023/10/15
8 | */
9 | @Getter
10 | public enum ModelType {
11 | chat("chat"),
12 | prompt("prompt"),
13 | image("image"),
14 | embedding("embedding"),
15 | addTask("addTask"),
16 | check("check"),;
17 |
18 | private final String value;
19 |
20 | ModelType(String value) {
21 | this.value = value;
22 | }
23 |
24 | }
25 |
26 |
--------------------------------------------------------------------------------
/src/main/java/com/gearwenxin/entity/enums/ResponseFormatType.java:
--------------------------------------------------------------------------------
1 | package com.gearwenxin.entity.enums;
2 |
3 | import lombok.Getter;
4 |
5 | import java.util.Optional;
6 |
7 | @Getter
8 | public enum ResponseFormatType {
9 |
10 | /**
11 | * 以json格式返回
12 | */
13 | json_object("json_object"),
14 |
15 | /**
16 | * 以文本格式返回
17 | */
18 | text("text");
19 |
20 | private final String value;
21 |
22 | ResponseFormatType(String value) {
23 | this.value = value;
24 | }
25 |
26 | public static ResponseFormatType TypeFromString(String text) {
27 | for (ResponseFormatType b : ResponseFormatType.values()) {
28 | if (b.value.equalsIgnoreCase(text)) {
29 | return b;
30 | }
31 | }
32 | return null;
33 | }
34 |
35 | public static Optional fromString(String text) {
36 | return Optional.ofNullable(TypeFromString(text));
37 | }
38 |
39 | public static void ifPresent(String text, Runnable runnable) {
40 | fromString(text).ifPresent(result -> runnable.run());
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/main/java/com/gearwenxin/entity/enums/Role.java:
--------------------------------------------------------------------------------
1 | package com.gearwenxin.entity.enums;
2 |
3 | /**
4 | * @author Ge Mingjia
5 | * {@code @date} 2023/7/20
6 | */
7 | public enum Role {
8 |
9 | /**
10 | * 用户
11 | */
12 | user,
13 |
14 | /**
15 | * AI回复
16 | */
17 | assistant,
18 |
19 | /**
20 | * 只存在于function call的examples中
21 | */
22 | function
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/com/gearwenxin/entity/enums/SamplerType.java:
--------------------------------------------------------------------------------
1 | package com.gearwenxin.entity.enums;
2 |
3 | import lombok.Getter;
4 |
5 | /**
6 | * @author Ge Mingjia
7 | * {@code @date} 2023/11/4
8 | */
9 | @Getter
10 | public enum SamplerType {
11 | Euler("Euler"),
12 | Euler_A("Euler a"),
13 | DPM_2M("DPM++ 2M"),
14 | DPM_2M_Karras("DPM++ 2M Karras"),
15 | LMS_Karras("LMS Karras"),
16 | DPM_SDE("DPM++ SDE"),
17 | DPM_SDE_Karras("DPM++ SDE Karras"),
18 | DPM2_a_Karras("DPM2 a Karras"),
19 | Heun("Heun"),
20 | DPM_2M_SDE("DPM++ 2M SDE"),
21 | DPM_2M_SDE_Karras("DPM++ 2M SDE Karras"),
22 | DPM2("DPM2"),
23 | DPM2_Karras("DPM2 Karras"),
24 | DPM2_a("DPM2 a"),
25 | LMS("LMS");
26 |
27 | private final String value;
28 |
29 | SamplerType(String value) {
30 | this.value = value;
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/main/java/com/gearwenxin/entity/request/EmbeddingV1Request.java:
--------------------------------------------------------------------------------
1 | package com.gearwenxin.entity.request;
2 |
3 | import lombok.AllArgsConstructor;
4 | import lombok.Data;
5 | import lombok.NoArgsConstructor;
6 |
7 | /**
8 | * @author Ge Mingjia
9 | * {@code @date} 2023/7/26
10 | */
11 | @Data
12 | @AllArgsConstructor
13 | @NoArgsConstructor
14 | public class EmbeddingV1Request {
15 |
16 | /**
17 | * 内容
18 | */
19 | private String content;
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/java/com/gearwenxin/entity/request/ErnieRequest.java:
--------------------------------------------------------------------------------
1 | package com.gearwenxin.entity.request;
2 |
3 | import com.fasterxml.jackson.annotation.JsonProperty;
4 | import com.gearwenxin.entity.BaseRequest;
5 |
6 | import com.gearwenxin.entity.FunctionInfo;
7 | import com.gearwenxin.entity.Message;
8 | import lombok.*;
9 |
10 | import java.util.Deque;
11 | import java.util.List;
12 |
13 | /**
14 | * @author Ge Mingjia
15 | * {@code @date} 2023/7/20
16 | *
17 | * ContBot 模型
18 | */
19 | @Data
20 | @NoArgsConstructor
21 | @AllArgsConstructor
22 | @ToString(callSuper = true)
23 | @EqualsAndHashCode(callSuper = true)
24 | public class ErnieRequest extends BaseRequest {
25 |
26 | /**
27 | * (1)较高的数值会使输出更加随机,而较低的数值会使其更加集中和确定
28 | * (2)默认0.95,范围 (0, 1.0],不能为0
29 | * (3)建议该参数和top_p只设置1个
30 | * (4)建议top_p和temperature不要同时更改
31 | */
32 | @JsonProperty("temperature")
33 | private Float temperature;
34 |
35 | /**
36 | * (1)影响输出文本的多样性,取值越大,生成文本的多样性越强
37 | * (2)默认0.8,取值范围 [0, 1.0]
38 | * (3)建议该参数和temperature只设置1个
39 | * (4)建议top_p和temperature不要同时更改
40 | */
41 | @JsonProperty("top_p")
42 | private Float topP;
43 |
44 | /**
45 | * 通过对已生成的token增加惩罚,减少重复生成的现象。说明:
46 | * (1)值越大表示惩罚越大
47 | * (2)默认1.0,取值范围:[1.0, 2.0]
48 | */
49 | @JsonProperty("penalty_score")
50 | private Float penaltyScore;
51 |
52 | /**
53 | * 一个可触发函数的描述列表
54 | */
55 | @JsonProperty("functions")
56 | private List functions;
57 |
58 | /**
59 | * 模型人设,主要用于人设设定,例如,你是xxx公司制作的AI助手,说明:
60 | * (1)长度限制1024个字符
61 | * (2)如果使用functions参数,不支持设定人设system
62 | */
63 | @JsonProperty("system")
64 | private String system;
65 |
66 | /**
67 | * 生成停止标识,当模型生成结果以stop中某个元素结尾时,停止文本生成。说明:
68 | * (1)每个元素长度不超过20字符
69 | * (2)最多4个元素
70 | */
71 | @JsonProperty("stop")
72 | private List stop;
73 |
74 | /**
75 | * 是否强制关闭实时搜索功能,默认false,表示不关闭
76 | */
77 | @JsonProperty("disable_search")
78 | private Boolean disableSearch;
79 |
80 | /**
81 | * 是否开启上角标返回,说明:
82 | * (1)开启后,有概率触发搜索溯源信息search_info,search_info内容见响应参数介绍
83 | * (2)默认false,不开启
84 | */
85 | @JsonProperty("enable_citation")
86 | private Boolean enableCitation;
87 |
88 | /**
89 | * 指定响应内容的格式,说明:
90 | * (1)可选值:
91 | * · json_object:以json格式返回,可能出现不满足效果情况
92 | * · text:以文本格式返回
93 | * (2)如果不填写参数response_format值,默认为text
94 | */
95 | @JsonProperty("response_format")
96 | private String responseFormat;
97 |
98 | public static ErnieRequestBuilder builder() {
99 | return new ErnieRequestBuilder();
100 | }
101 |
102 | public static class ErnieRequestBuilder extends BaseRequestBuilder {
103 | private Float temperature;
104 | private Float topP;
105 | private Float penaltyScore;
106 | private String userId;
107 | private Deque messages;
108 | private Boolean stream;
109 | private List functions;
110 | private String system;
111 | private List stop;
112 | private Boolean disableSearch;
113 | private Boolean enableCitation;
114 | private String responseFormat;
115 |
116 | public ErnieRequestBuilder temperature(Float temperature) {
117 | this.temperature = temperature;
118 | return this;
119 | }
120 |
121 | public ErnieRequestBuilder topP(Float topP) {
122 | this.topP = topP;
123 | return this;
124 | }
125 |
126 | public ErnieRequestBuilder penaltyScore(Float penaltyScore) {
127 | this.penaltyScore = penaltyScore;
128 | return this;
129 | }
130 |
131 | @Override
132 | public ErnieRequestBuilder userId(String userId) {
133 | this.userId = userId;
134 | return this;
135 | }
136 |
137 | @Override
138 | public ErnieRequestBuilder messages(Deque messages) {
139 | this.messages = messages;
140 | return this;
141 | }
142 |
143 | @Override
144 | public ErnieRequestBuilder stream(Boolean stream) {
145 | this.stream = stream;
146 | return this;
147 | }
148 |
149 | public ErnieRequestBuilder functions(List functions) {
150 | this.functions = functions;
151 | return this;
152 | }
153 |
154 | public ErnieRequestBuilder system(String system) {
155 | this.system = system;
156 | return this;
157 | }
158 |
159 | public ErnieRequestBuilder stop(List stop) {
160 | this.stop = stop;
161 | return this;
162 | }
163 |
164 | public ErnieRequestBuilder disableSearch(Boolean disableSearch) {
165 | this.disableSearch = disableSearch;
166 | return this;
167 | }
168 |
169 | public ErnieRequestBuilder enableCitation(Boolean enableCitation) {
170 | this.enableCitation = enableCitation;
171 | return this;
172 | }
173 |
174 | public ErnieRequestBuilder responseFormat(String responseFormat) {
175 | this.responseFormat = responseFormat;
176 | return this;
177 | }
178 |
179 | @Override
180 | public ErnieRequest build() {
181 | ErnieRequest ernieRequest = new ErnieRequest();
182 | ernieRequest.setTemperature(temperature);
183 | ernieRequest.setTopP(topP);
184 | ernieRequest.setPenaltyScore(penaltyScore);
185 | ernieRequest.setUserId(userId);
186 | ernieRequest.setMessages(messages);
187 | ernieRequest.setStream(stream);
188 | ernieRequest.setFunctions(functions);
189 | ernieRequest.setSystem(system);
190 | ernieRequest.setStop(stop);
191 | ernieRequest.setDisableSearch(disableSearch);
192 | ernieRequest.setEnableCitation(enableCitation);
193 | ernieRequest.setResponseFormat(responseFormat);
194 |
195 | return ernieRequest;
196 | }
197 | }
198 |
199 | }
200 |
--------------------------------------------------------------------------------
/src/main/java/com/gearwenxin/entity/request/ImageBaseRequest.java:
--------------------------------------------------------------------------------
1 | package com.gearwenxin.entity.request;
2 |
3 | import com.fasterxml.jackson.annotation.JsonProperty;
4 | import com.gearwenxin.common.ErrorCode;
5 | import com.gearwenxin.entity.enums.SamplerType;
6 | import com.gearwenxin.exception.WenXinException;
7 | import lombok.AllArgsConstructor;
8 | import lombok.Builder;
9 | import lombok.Data;
10 | import lombok.NoArgsConstructor;
11 | import org.apache.commons.lang3.StringUtils;
12 |
13 | /**
14 | * @author Ge Mingjia
15 | * {@code @date} 2023/8/3
16 | */
17 | @Data
18 | @Builder
19 | @AllArgsConstructor
20 | @NoArgsConstructor
21 | public class ImageBaseRequest {
22 |
23 | /**
24 | * 提示词,即用户希望图片包含的元素。长度限制为1024字符,建议中文或者英文单词总数量不超过150个
25 | */
26 | @JsonProperty("prompt")
27 | private String prompt;
28 |
29 | /**
30 | * 反向提示词,即用户希望图片不包含的元素。长度限制为1024字符,建议中文或者英文单词总数量不超过150个
31 | */
32 | @JsonProperty("negative_prompt")
33 | private String negativePrompt;
34 |
35 | /**
36 | * 生成图片长宽,默认值 1024x1024,取值范围如下:
37 | * ["512x512", "768x768", "768x1024", "1024x768", "576x1024", "1024x576", "1024x1024"]
38 | * 注意:建议选择较大尺寸,结合完善的prompt,以保障图片质量。
39 | */
40 | @JsonProperty("size")
41 | private String size = "1024x1024";
42 |
43 | /**
44 | * 生成图片数量,说明:
45 | * · 默认值为1
46 | * · 取值范围为1-4
47 | * · 单次生成的图片较多及请求较频繁可能导致请求超时
48 | */
49 | @JsonProperty("n")
50 | private Integer n = 1;
51 |
52 | /**
53 | * 迭代轮次,说明:
54 | * · 默认值为20
55 | * · 取值范围为10-50
56 | */
57 | @JsonProperty("steps")
58 | private Integer steps = 20;
59 |
60 | @JsonProperty("sampler_index")
61 | private String samplerIndex = SamplerType.DPM2_a.getValue();
62 |
63 | @JsonProperty("user_id")
64 | private String userId;
65 |
66 | public void validSelf() {
67 |
68 | // 检查content不为空
69 | if (StringUtils.isBlank(prompt)) {
70 | throw new WenXinException(ErrorCode.PARAMS_ERROR, "prompt cannot be empty");
71 | }
72 | // 检查单个content长度
73 | if (prompt.length() > 1024) {
74 | throw new WenXinException(ErrorCode.PARAMS_ERROR, "prompt's length cannot be more than 1024");
75 | }
76 | // 检查单个content长度
77 | if (negativePrompt.length() > 1024) {
78 | throw new WenXinException(ErrorCode.PARAMS_ERROR, "prompt's length cannot be more than 1024");
79 | }
80 |
81 | }
82 |
83 | }
84 |
--------------------------------------------------------------------------------
/src/main/java/com/gearwenxin/entity/request/PluginParams.java:
--------------------------------------------------------------------------------
1 | package com.gearwenxin.entity.request;
2 |
3 | import com.fasterxml.jackson.annotation.JsonProperty;
4 | import lombok.AllArgsConstructor;
5 | import lombok.Data;
6 | import lombok.NoArgsConstructor;
7 |
8 | import java.util.List;
9 |
10 | /**
11 | * 请求参数类
12 | */
13 | @Data
14 | @NoArgsConstructor
15 | @AllArgsConstructor
16 | public class PluginParams {
17 |
18 | /**
19 | * 查询信息
20 | * (1)成员不能为空
21 | * (2)长度不能超过1000个字符
22 | */
23 | @JsonProperty("query")
24 | private String query;
25 |
26 | /**
27 | * 需要调用的插件,参数为插件ID,插件ID可在插件列表-插件详情中获取。
28 | * (1)最多3个插件,最少1个插件。
29 | * (2)当多个插件时,插件触发由大模型意图判断控制。
30 | * (3)当只有1个插件时,强制指定使用该插件工具。
31 | */
32 | @JsonProperty("plugins")
33 | private List plugins;
34 |
35 | /**
36 | * 是否以流式接口的形式返回数据,默认false,可选值如下:
37 | * (1)true: 是,以流式接口的形式返回数据
38 | * (2)false:否,非流式接口形式返回数据
39 | */
40 | @JsonProperty("stream")
41 | private boolean stream;
42 |
43 | /**
44 | * llm相关参数,不指定参数时,使用调试过程中的默认值。
45 | */
46 | @JsonProperty("llm")
47 | private Object llm;
48 |
49 | /**
50 | * 如果prompt中使用了变量,推理时可以填写具体值;
51 | * 如果prompt中未使用变量,该字段不填。
52 | */
53 | @JsonProperty("input_variables")
54 | private Object inputVariables;
55 |
56 | /**
57 | * 聊天上下文信息。
58 | */
59 | @JsonProperty("history")
60 | private Object history;
61 |
62 | /**
63 | * 是否返回插件的原始请求信息,默认false,可选值如下:
64 | * true:是,返回插件的原始请求信息meta_info
65 | * false:否,不返回插件的原始请求信息meta_info
66 | */
67 | @JsonProperty("verbose")
68 | private boolean verbose;
69 |
70 | /**
71 | * 文件的http地址
72 | */
73 | @JsonProperty("fileurl")
74 | private String fileUrl;
75 |
76 | }
77 |
--------------------------------------------------------------------------------
/src/main/java/com/gearwenxin/entity/request/PromptRequest.java:
--------------------------------------------------------------------------------
1 | package com.gearwenxin.entity.request;
2 |
3 | import lombok.AllArgsConstructor;
4 | import lombok.Builder;
5 | import lombok.Data;
6 | import lombok.NoArgsConstructor;
7 |
8 | import java.util.Map;
9 |
10 | /**
11 | * @author Ge Mingjia
12 | * {@code @date} 2023/7/26
13 | */
14 | @Data
15 | @Builder
16 | @AllArgsConstructor
17 | @NoArgsConstructor
18 | public class PromptRequest {
19 |
20 | /**
21 | * prompt工程里面对应的模板id
22 | */
23 | private String id;
24 |
25 | /**
26 | * 参数map
27 | */
28 | private Map paramMap;
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/java/com/gearwenxin/entity/response/ChatResponse.java:
--------------------------------------------------------------------------------
1 | package com.gearwenxin.entity.response;
2 |
3 | import com.fasterxml.jackson.annotation.JsonProperty;
4 | import com.gearwenxin.entity.FunctionCall;
5 | import com.gearwenxin.entity.Usage;
6 | import lombok.AllArgsConstructor;
7 | import lombok.Data;
8 | import lombok.NoArgsConstructor;
9 |
10 | import java.io.Serializable;
11 |
12 | /**
13 | * @author Ge Mingjia
14 | * {@code @date} 2023/7/20
15 | *
16 | * ContBot 模型
17 | */
18 | @Data
19 | @NoArgsConstructor
20 | @AllArgsConstructor
21 | public class ChatResponse implements Serializable {
22 |
23 | /**
24 | * 本轮对话的id
25 | */
26 | @JsonProperty("id")
27 | private String id;
28 |
29 | /**
30 | * 用于定位的log_id
31 | */
32 | @JsonProperty("log_id")
33 | private String logId;
34 |
35 | /**
36 | * 回包类型
37 | * chat.completion:多轮对话返回
38 | */
39 | @JsonProperty("object")
40 | private String object;
41 |
42 | /**
43 | * 时间戳
44 | */
45 | @JsonProperty("created")
46 | private Integer created;
47 |
48 | /**
49 | * 表示当前子句的序号。只有在流式接口模式下会返回该字段
50 | */
51 | @JsonProperty("sentence_id")
52 | private Integer sentenceId;
53 |
54 | /**
55 | * 表示当前子句是否是最后一句。只有在流式接口模式下会返回该字段
56 | */
57 | @JsonProperty("is_end")
58 | private Boolean isEnd;
59 |
60 | /**
61 | * 当前生成的结果是否被截断
62 | */
63 | @JsonProperty("is_truncated")
64 | private Boolean isTruncated;
65 |
66 | /**
67 | * 输出内容标识,说明:
68 | * · normal:输出内容完全由大模型生成,未触发截断、替换
69 | * · stop:输出结果命中入参stop中指定的字段后被截断
70 | * · length:达到了最大的token数,根据EB返回结果is_truncated来截断
71 | * · content_filter:输出内容被截断、兜底、替换为**等
72 | * · function_call:调用了funtion call功能
73 | */
74 | @JsonProperty("finish_reason")
75 | private String finishReason;
76 |
77 | /**
78 | * 搜索数据,当请求参数enable_citation为true并且触发搜索时,会返回该字段
79 | */
80 | @JsonProperty("search_info")
81 | private SearchInfo searchInfo;
82 |
83 | /**
84 | * 对话返回结果
85 | */
86 | @JsonProperty("result")
87 | private String result;
88 |
89 | /**
90 | * 表示用户输入是否存在安全,是否关闭当前会话,清理历史回话信息
91 | * true:是,表示用户输入存在安全风险,建议关闭当前会话,清理历史会话信息
92 | * false:否,表示用户输入无安全风险
93 | */
94 | @JsonProperty("need_clear_history")
95 | private Boolean needClearHistory;
96 |
97 | /**
98 | * token统计信息,token数 = 汉字数+单词数*1.3 (仅为估算逻辑)
99 | */
100 | @JsonProperty("usage")
101 | private Usage usage;
102 |
103 | /**
104 | * 当need_clear_history为true时,此字段会告知第几轮对话有敏感信息,如果是当前问题,ban_round=-1
105 | */
106 | @JsonProperty("ban_round")
107 | private Integer banRound;
108 |
109 | /**
110 | * 说明:
111 | * · 0:正常返回
112 | * · 其他:非正常
113 | */
114 | @JsonProperty("flag")
115 | private Integer flag;
116 |
117 | /**
118 | * 错误代码,正常为 null
119 | */
120 | @JsonProperty("error_code")
121 | private Integer errorCode;
122 |
123 | /**
124 | * 错误代码,正常为 null
125 | */
126 | @JsonProperty("eb_code")
127 | private Integer ebCode;
128 |
129 | /**
130 | * 错误信息,正常为 null
131 | */
132 | @JsonProperty("error_msg")
133 | private String errorMsg;
134 |
135 | /**
136 | * 由模型生成的函数调用,包含函数名称,和调用参数
137 | */
138 | @JsonProperty("function_call")
139 | private FunctionCall functionCall;
140 |
141 | }
142 |
--------------------------------------------------------------------------------
/src/main/java/com/gearwenxin/entity/response/ErrorResponse.java:
--------------------------------------------------------------------------------
1 | package com.gearwenxin.entity.response;
2 |
3 | import com.fasterxml.jackson.annotation.JsonProperty;
4 | import lombok.AllArgsConstructor;
5 | import lombok.Builder;
6 | import lombok.Data;
7 | import lombok.NoArgsConstructor;
8 |
9 | /**
10 | * @author Ge Mingjia
11 | * {@code @date} 2023/10/26
12 | */
13 | @Data
14 | @Builder
15 | @NoArgsConstructor
16 | @AllArgsConstructor
17 | public class ErrorResponse {
18 |
19 | /**
20 | * 本轮对话的id
21 | */
22 | @JsonProperty("id")
23 | private String id;
24 |
25 | /**
26 | * 用于定位的log_id
27 | */
28 | @JsonProperty("log_id")
29 | private String logId;
30 |
31 | /**
32 | * 错误代码,正常为 null
33 | */
34 | @JsonProperty("error_code")
35 | private Integer errorCode;
36 |
37 | /**
38 | * 错误代码,正常为 null
39 | */
40 | @JsonProperty("eb_code")
41 | private Integer ebCode;
42 |
43 | /**
44 | * 错误信息,正常为 null
45 | */
46 | @JsonProperty("error_msg")
47 | private String errorMsg;
48 |
49 | @Override
50 | public String toString() {
51 | return "error_response { " +
52 | "id: '" + id + '\'' +
53 | ", logId: '" + logId + '\'' +
54 | ", errorCode: " + errorCode +
55 | ", ebCode: " + ebCode +
56 | ", errorMsg: '" + errorMsg + '\'' +
57 | '}' + " ";
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/src/main/java/com/gearwenxin/entity/response/ImageData.java:
--------------------------------------------------------------------------------
1 | package com.gearwenxin.entity.response;
2 |
3 | import com.fasterxml.jackson.annotation.JsonProperty;
4 | import lombok.Data;
5 |
6 | /**
7 | * 表示生成图片的详细信息。
8 | */
9 | @Data
10 | public class ImageData {
11 |
12 | /**
13 | * 固定值 "image",表示图像。
14 | */
15 | private String object;
16 |
17 | /**
18 | * 图片base64编码内容。
19 | */
20 | @JsonProperty("b64_image")
21 | private String b64Image;
22 |
23 | /**
24 | * 图片序号。
25 | */
26 | private int index;
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/src/main/java/com/gearwenxin/entity/response/ImageResponse.java:
--------------------------------------------------------------------------------
1 | package com.gearwenxin.entity.response;
2 |
3 | import com.fasterxml.jackson.annotation.JsonProperty;
4 | import com.gearwenxin.entity.Usage;
5 | import lombok.AllArgsConstructor;
6 | import lombok.Data;
7 | import lombok.NoArgsConstructor;
8 |
9 | import java.util.List;
10 |
11 | /**
12 | * @author Ge Mingjia
13 | * {@code @date} 2023/8/3
14 | */
15 | @Data
16 | @AllArgsConstructor
17 | @NoArgsConstructor
18 | public class ImageResponse {
19 |
20 | /**
21 | * 请求的ID。
22 | */
23 | private String id;
24 |
25 | /**
26 | * 回包类型。固定值为 "image",表示图像生成返回。
27 | */
28 | private String object;
29 |
30 | /**
31 | * 时间戳,表示生成响应的时间。
32 | */
33 | private int created;
34 |
35 | /**
36 | * 生成图片结果列表。
37 | */
38 | private List data;
39 |
40 | /**
41 | * token统计信息,token数 = 汉字数 + 单词数 * 1.3 (仅为估算逻辑)。
42 | */
43 | private Usage usage;
44 |
45 | /**
46 | * 错误代码,正常为 null
47 | */
48 | @JsonProperty("error_code")
49 | private Integer errorCode;
50 |
51 | /**
52 | * 错误信息,正常为 null
53 | */
54 | @JsonProperty("error_msg")
55 | private String errorMsg;
56 |
57 | }
58 |
--------------------------------------------------------------------------------
/src/main/java/com/gearwenxin/entity/response/PromptErrMessage.java:
--------------------------------------------------------------------------------
1 | package com.gearwenxin.entity.response;
2 |
3 | import lombok.AllArgsConstructor;
4 | import lombok.Data;
5 | import lombok.NoArgsConstructor;
6 |
7 | /**
8 | * @author Ge Mingjia
9 | * {@code @date} 2023/7/26
10 | */
11 | @Data
12 | @AllArgsConstructor
13 | @NoArgsConstructor
14 | public class PromptErrMessage {
15 |
16 | private String global;
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/src/main/java/com/gearwenxin/entity/response/PromptResponse.java:
--------------------------------------------------------------------------------
1 | package com.gearwenxin.entity.response;
2 |
3 | import com.fasterxml.jackson.annotation.JsonProperty;
4 | import lombok.AllArgsConstructor;
5 | import lombok.Data;
6 | import lombok.NoArgsConstructor;
7 |
8 | /**
9 | * @author Ge Mingjia
10 | * {@code @date} 2023/7/23
11 | */
12 | @Data
13 | @AllArgsConstructor
14 | @NoArgsConstructor
15 | public class PromptResponse {
16 |
17 | @JsonProperty("log_id")
18 | private String logId;
19 |
20 | private PromptResult result;
21 |
22 | private Integer status;
23 |
24 | private Boolean success;
25 |
26 | @JsonProperty("error_code")
27 | private Integer errorCode;
28 |
29 | @JsonProperty("error_msg")
30 | private String errorMsg;
31 |
32 | @JsonProperty("code")
33 | private String promptErrCode;
34 |
35 | @JsonProperty("message")
36 | private PromptErrMessage promptErrMessage;
37 | }
38 |
--------------------------------------------------------------------------------
/src/main/java/com/gearwenxin/entity/response/PromptResult.java:
--------------------------------------------------------------------------------
1 | package com.gearwenxin.entity.response;
2 |
3 | import lombok.AllArgsConstructor;
4 | import lombok.Data;
5 | import lombok.NoArgsConstructor;
6 |
7 | /**
8 | * @author Ge Mingjia
9 | * {@code @date} 2023/7/26
10 | */
11 |
12 | @Data
13 | @AllArgsConstructor
14 | @NoArgsConstructor
15 | public class PromptResult {
16 |
17 | /**
18 | * prompt工程里面对应的模板id
19 | */
20 | private String templateId;
21 |
22 | /**
23 | * 模板名称
24 | */
25 | private String templateName;
26 |
27 | /**
28 | * 模板原始内容
29 | */
30 | private String templateContent;
31 |
32 | /**
33 | * 模板变量插值
34 | */
35 | private String templateVariables;
36 |
37 | /**
38 | * 将变量插值填充到模板原始内容后得到的模板内容
39 | */
40 | private String content;
41 |
42 | }
43 |
--------------------------------------------------------------------------------
/src/main/java/com/gearwenxin/entity/response/SSEResponse.java:
--------------------------------------------------------------------------------
1 | package com.gearwenxin.entity.response;
2 |
3 | import lombok.Data;
4 |
5 | @Data
6 | public class SSEResponse {
7 |
8 | private String content;
9 |
10 | @Override
11 | public String toString() {
12 | return "data: " + content + "\n\n";
13 | }
14 |
15 | }
--------------------------------------------------------------------------------
/src/main/java/com/gearwenxin/entity/response/SearchInfo.java:
--------------------------------------------------------------------------------
1 | package com.gearwenxin.entity.response;
2 |
3 | import com.fasterxml.jackson.annotation.JsonProperty;
4 | import lombok.AllArgsConstructor;
5 | import lombok.Data;
6 | import lombok.NoArgsConstructor;
7 |
8 | import java.util.List;
9 |
10 | @Data
11 | @AllArgsConstructor
12 | @NoArgsConstructor
13 | class SearchInfo {
14 |
15 | /**
16 | * 搜索结果的列表
17 | */
18 | @JsonProperty("search_results")
19 | private List searchResults;
20 |
21 | }
--------------------------------------------------------------------------------
/src/main/java/com/gearwenxin/entity/response/SearchResult.java:
--------------------------------------------------------------------------------
1 | package com.gearwenxin.entity.response;
2 |
3 | import lombok.AllArgsConstructor;
4 | import lombok.Data;
5 | import lombok.NoArgsConstructor;
6 |
7 | @Data
8 | @AllArgsConstructor
9 | @NoArgsConstructor
10 | class SearchResult {
11 |
12 | /**
13 | * 搜索结果的序号
14 | */
15 | private int index;
16 |
17 | /**
18 | * 搜索结果的url
19 | */
20 | private String url;
21 |
22 | /**
23 | * 搜索结果的标题
24 | */
25 | private String title;
26 |
27 | }
--------------------------------------------------------------------------------
/src/main/java/com/gearwenxin/entity/response/TokenResponse.java:
--------------------------------------------------------------------------------
1 | package com.gearwenxin.entity.response;
2 |
3 | import com.fasterxml.jackson.annotation.JsonProperty;
4 | import lombok.AllArgsConstructor;
5 | import lombok.Data;
6 | import lombok.NoArgsConstructor;
7 |
8 | /**
9 | * @author Ge Mingjia
10 | */
11 | @Data
12 | @AllArgsConstructor
13 | @NoArgsConstructor
14 | public class TokenResponse {
15 |
16 | @JsonProperty("refresh_token")
17 | private String refreshToken;
18 |
19 | @JsonProperty("expires_in")
20 | private int expiresIn;
21 |
22 | @JsonProperty("session_key")
23 | private String sessionKey;
24 |
25 | @JsonProperty("access_token")
26 | private String accessToken;
27 |
28 | @JsonProperty("scope")
29 | private String scope;
30 |
31 | @JsonProperty("session_secret")
32 | private String sessionSecret;
33 |
34 | @JsonProperty("error_description")
35 | private String errorDescription;
36 |
37 | @JsonProperty("error")
38 | private String error;
39 |
40 | }
--------------------------------------------------------------------------------
/src/main/java/com/gearwenxin/entity/response/plugin/PluginResponse.java:
--------------------------------------------------------------------------------
1 | package com.gearwenxin.entity.response.plugin;
2 |
3 | import com.fasterxml.jackson.annotation.JsonProperty;
4 | import com.gearwenxin.entity.Usage;
5 | import lombok.AllArgsConstructor;
6 | import lombok.Data;
7 | import lombok.NoArgsConstructor;
8 |
9 | /**
10 | * 返回结果信息类
11 | */
12 | @Data
13 | @NoArgsConstructor
14 | @AllArgsConstructor
15 | public class PluginResponse {
16 |
17 | /**
18 | * 唯一的log id,用于问题定位
19 | */
20 | @JsonProperty("log_id")
21 | private String logId;
22 |
23 | /**
24 | * 本轮对话的id
25 | */
26 | @JsonProperty("id")
27 | private String id;
28 |
29 | /**
30 | * 回包类型。
31 | * chat.completion:多轮对话返回
32 | */
33 | @JsonProperty("object")
34 | private String object;
35 |
36 | /**
37 | * 时间戳
38 | */
39 | @JsonProperty("created")
40 | private int created;
41 |
42 | /**
43 | * 表示当前子句的序号,只有在流式接口模式下会返回该字段
44 | */
45 | @JsonProperty("sentence_id")
46 | private int sentenceId;
47 |
48 | /**
49 | * 表示当前子句是否是最后一句,只有在流式接口模式下会返回该字段
50 | */
51 | @JsonProperty("is_end")
52 | private boolean isEnd;
53 |
54 | /**
55 | * 插件返回结果
56 | */
57 | @JsonProperty("result")
58 | private String result;
59 |
60 | /**
61 | * 当前生成的结果是否被截断
62 | */
63 | @JsonProperty("is_truncated")
64 | private boolean isTruncated;
65 |
66 | /**
67 | * 表示用户输入是否存在安全,是否关闭当前会话,清理历史会话信息
68 | * true:是,表示用户输入存在安全风险,建议关闭当前会话,清理历史会话信息
69 | * false:否,表示用户输入无安全风险
70 | */
71 | @JsonProperty("need_clear_history")
72 | private boolean needClearHistory;
73 |
74 | /**
75 | * 当need_clear_history为true时,此字段会告知第几轮对话有敏感信息,如果是当前问题,ban_round = -1
76 | */
77 | @JsonProperty("ban_round")
78 | private int banRound;
79 |
80 | /**
81 | * token统计信息,token数 = 汉字数+单词数*1.3 (仅为估算逻辑)
82 | */
83 | @JsonProperty("usage")
84 | private Usage usage;
85 |
86 | /**
87 | * 插件的原始请求信息
88 | */
89 | @JsonProperty("meta_info")
90 | private T metaInfo;
91 |
92 | }
--------------------------------------------------------------------------------
/src/main/java/com/gearwenxin/entity/response/plugin/knowledge/KnowledgeBaseMI.java:
--------------------------------------------------------------------------------
1 | package com.gearwenxin.entity.response.plugin.knowledge;
2 |
3 | import com.fasterxml.jackson.annotation.JsonProperty;
4 | import lombok.AllArgsConstructor;
5 | import lombok.Data;
6 | import lombok.NoArgsConstructor;
7 |
8 | /**
9 | * @author Ge Mingjia
10 | * {@code @date} 2023/10/22
11 | */
12 | @Data
13 | @AllArgsConstructor
14 | @NoArgsConstructor
15 | public class KnowledgeBaseMI {
16 |
17 | /**
18 | * 插件 Id,为“uuid-zhishiku”
19 | */
20 | @JsonProperty("plugin_id")
21 | private String pluginId;
22 |
23 | /**
24 | * 知识库原始请求参数
25 | */
26 | @JsonProperty("request")
27 | private KnowledgeMIRequest request;
28 |
29 | /**
30 | * 知识库原始返回结果
31 | */
32 | @JsonProperty("response")
33 | private KnowledgeMIResponse response;
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/src/main/java/com/gearwenxin/entity/response/plugin/knowledge/KnowledgeMIRequest.java:
--------------------------------------------------------------------------------
1 | package com.gearwenxin.entity.response.plugin.knowledge;
2 |
3 | import com.fasterxml.jackson.annotation.JsonProperty;
4 | import lombok.AllArgsConstructor;
5 | import lombok.Data;
6 | import lombok.NoArgsConstructor;
7 |
8 | /**
9 | * @author Ge Mingjia
10 | * {@code @date} 2023/10/22
11 | */
12 | @Data
13 | @NoArgsConstructor
14 | @AllArgsConstructor
15 | public class KnowledgeMIRequest {
16 |
17 | /**
18 | * 用于查询知识库的用户请求
19 | */
20 | @JsonProperty("query")
21 | private String query;
22 |
23 | /**
24 | * 使用知识库的 Id 列表
25 | */
26 | @JsonProperty("kbIds")
27 | private String[] kbIds;
28 |
29 | /**
30 | * 分片和query的相似度分数的下限,低于该下限的文档分片不会被返回
31 | */
32 | @JsonProperty("score")
33 | private float score;
34 |
35 | /**
36 | * 返回的最相关的文档数
37 | */
38 | @JsonProperty("topN")
39 | private Integer topN;
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/src/main/java/com/gearwenxin/entity/response/plugin/knowledge/KnowledgeMIResponse.java:
--------------------------------------------------------------------------------
1 | package com.gearwenxin.entity.response.plugin.knowledge;
2 |
3 | import com.fasterxml.jackson.annotation.JsonProperty;
4 | import lombok.AllArgsConstructor;
5 | import lombok.Data;
6 | import lombok.NoArgsConstructor;
7 |
8 | /**
9 | * @author Ge Mingjia
10 | * {@code @date} 2023/10/22
11 | */
12 | @Data
13 | @NoArgsConstructor
14 | @AllArgsConstructor
15 | public class KnowledgeMIResponse {
16 |
17 | /**
18 | * 错误码
19 | */
20 | @JsonProperty("retCode")
21 | private Integer retCode;
22 |
23 | /**
24 | * 错误信息
25 | */
26 | @JsonProperty("message")
27 | private String message;
28 |
29 | /**
30 | * 返回结果
31 | */
32 | @JsonProperty("result")
33 | private KnowledgeMIResult result;
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/src/main/java/com/gearwenxin/entity/response/plugin/knowledge/KnowledgeMIResponses.java:
--------------------------------------------------------------------------------
1 | package com.gearwenxin.entity.response.plugin.knowledge;
2 |
3 | import com.fasterxml.jackson.annotation.JsonProperty;
4 | import lombok.AllArgsConstructor;
5 | import lombok.Data;
6 | import lombok.NoArgsConstructor;
7 |
8 | /**
9 | * @author Ge Mingjia
10 | * {@code @date} 2023/10/22
11 | */
12 | @Data
13 | @NoArgsConstructor
14 | @AllArgsConstructor
15 | public class KnowledgeMIResponses {
16 |
17 | /**
18 | * 文档分片的下载地址
19 | */
20 | @JsonProperty("contentUrl")
21 | private String contentUrl;
22 |
23 | /**
24 | * 文档 Id
25 | */
26 | @JsonProperty("docId")
27 | private String docId;
28 |
29 | /**
30 | * 文档的名称
31 | */
32 | @JsonProperty("docName")
33 | private String docName;
34 |
35 | /**
36 | * 文档上传的知识库 Id
37 | */
38 | @JsonProperty("kbId")
39 | private String kbId;
40 |
41 | /**
42 | * 当前分片和用户请求的相关度,取值范围(0-1)
43 | */
44 | @JsonProperty("score")
45 | private float score;
46 |
47 | /**
48 | * 分片 ID
49 | */
50 | @JsonProperty("shardId")
51 | private String shardId;
52 |
53 | /**
54 | * 分片序号
55 | */
56 | @JsonProperty("shardIndex")
57 | private Integer shardIndex;
58 |
59 | /**
60 | * 分片的实际内容
61 | */
62 | @JsonProperty("content")
63 | private String content;
64 |
65 | }
66 |
--------------------------------------------------------------------------------
/src/main/java/com/gearwenxin/entity/response/plugin/knowledge/KnowledgeMIResult.java:
--------------------------------------------------------------------------------
1 | package com.gearwenxin.entity.response.plugin.knowledge;
2 |
3 | import com.fasterxml.jackson.annotation.JsonProperty;
4 | import lombok.AllArgsConstructor;
5 | import lombok.Data;
6 | import lombok.NoArgsConstructor;
7 |
8 | /**
9 | * @author Ge Mingjia
10 | * {@code @date} 2023/10/22
11 | */
12 | @Data
13 | @NoArgsConstructor
14 | @AllArgsConstructor
15 | public class KnowledgeMIResult {
16 |
17 | /**
18 | * bes查询耗时
19 | */
20 | @JsonProperty("besQueryCostMilsec3")
21 | private Integer besQueryCostMilsec3;
22 |
23 | /**
24 | * db查询耗时
25 | */
26 | @JsonProperty("dbQueryCostMilsec1")
27 | private Integer dbQueryCostMilsec1;
28 |
29 | /**
30 | * embedding查询耗时
31 | */
32 | @JsonProperty("embeddedCostMilsec2")
33 | private Integer embeddedCostMilsec2;
34 |
35 | /**
36 | * 知识库返回的最相关文档信息
37 | */
38 | @JsonProperty("responses")
39 | private KnowledgeMIResponses responses;
40 |
41 | /**
42 | * bos url生成耗时
43 | */
44 | @JsonProperty("urlSignedCostMilsec4")
45 | private Integer urlSignedCostMilsec4;
46 |
47 | }
48 |
--------------------------------------------------------------------------------
/src/main/java/com/gearwenxin/exception/WenXinException.java:
--------------------------------------------------------------------------------
1 | package com.gearwenxin.exception;
2 |
3 | import com.gearwenxin.common.ErrorCode;
4 | import lombok.Getter;
5 |
6 | /**
7 | * @author Ge Mingjia
8 | * {@code @date} 2023/7/22
9 | */
10 | @Getter
11 | public class WenXinException extends RuntimeException {
12 |
13 | /**
14 | * 错误码
15 | */
16 | private final int code;
17 |
18 | public WenXinException(int code, String message) {
19 | super(message);
20 | this.code = code;
21 | }
22 |
23 | public WenXinException(ErrorCode errorCode) {
24 | super(errorCode.getMessage());
25 | this.code = errorCode.getCode();
26 | }
27 |
28 | public WenXinException(ErrorCode errorCode, String message) {
29 | super(message);
30 | this.code = errorCode.getCode();
31 | }
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/src/main/java/com/gearwenxin/model/BasicChatModel.java:
--------------------------------------------------------------------------------
1 | package com.gearwenxin.model;
2 |
3 | import com.gearwenxin.entity.chatmodel.ChatBaseRequest;
4 | import com.gearwenxin.entity.response.ChatResponse;
5 | import reactor.core.publisher.Flux;
6 | import reactor.core.publisher.Mono;
7 |
8 | public interface BasicChatModel {
9 |
10 | /** 单次对话 **/
11 | Mono chat(String content);
12 |
13 | Mono chat(String content, float weight);
14 |
15 | Mono chat(T chatRequest);
16 |
17 | Mono chat(T chatRequest, float weight);
18 |
19 | Flux chatStream(String content);
20 |
21 | Flux chatStream(String content, float weight);
22 |
23 | Flux chatStream(T chatRequest);
24 |
25 | Flux chatStream(T chatRequest, float weight);
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/src/main/java/com/gearwenxin/model/ChatModel.java:
--------------------------------------------------------------------------------
1 | package com.gearwenxin.model;
2 |
3 | import com.gearwenxin.entity.chatmodel.ChatBaseRequest;
4 | import com.gearwenxin.entity.response.ChatResponse;
5 | import reactor.core.publisher.Flux;
6 | import reactor.core.publisher.Mono;
7 |
8 | import java.util.Map;
9 |
10 | public interface ChatModel {
11 |
12 | /** 单次对话 **/
13 | Mono chat(String content);
14 |
15 | Mono chat(String content, float weight);
16 |
17 | Mono chat(T chatRequest);
18 |
19 | Mono chat(T chatRequest, float weight);
20 |
21 | Flux chatStream(String content);
22 |
23 | Flux chatStream(String content, float weight);
24 |
25 | Flux chatStream(T chatRequest);
26 |
27 | Flux chatStream(T chatRequest, float weight);
28 |
29 | Flux chatStream(Map