diff --git a/mallchat-common/pom.xml b/mallchat-common/pom.xml index 4268fc2..2d05b99 100644 --- a/mallchat-common/pom.xml +++ b/mallchat-common/pom.xml @@ -115,6 +115,10 @@ ${junit.version} test + + com.alibaba + fastjson + org.springframework spring-test diff --git a/mallchat-common/src/main/java/com/abin/mallchat/common/common/constant/RedisKey.java b/mallchat-common/src/main/java/com/abin/mallchat/common/common/constant/RedisKey.java index 0cc9bd0..9194e7a 100644 --- a/mallchat-common/src/main/java/com/abin/mallchat/common/common/constant/RedisKey.java +++ b/mallchat-common/src/main/java/com/abin/mallchat/common/common/constant/RedisKey.java @@ -41,6 +41,8 @@ public class RedisKey { */ public static final String USER_CHAT_NUM = "useChatGPTNum:uid_%d"; + public static final String USER_CHAT_CONTEXT = "useChatGPTContext:uid_%d_roomId_%d"; + /** * 用户上次使用GLM使用时间 */ diff --git a/mallchat-common/src/main/resources/application-pro.properties b/mallchat-common/src/main/resources/application-pro.properties index c6a31b5..0081b74 100644 --- a/mallchat-common/src/main/resources/application-pro.properties +++ b/mallchat-common/src/main/resources/application-pro.properties @@ -33,4 +33,5 @@ mallchat.chatgpt.key=sk-wvWM0xGcxFfsddfsgxixbXK5tHovM mallchat.chatgpt.proxyUrl=https://123.cc mallchat.chatglm2.use=false mallchat.chatglm2.url=http://v32134.cc -mallchat.chatglm2.uid=10002 \ No newline at end of file +mallchat.chatglm2.uid=10002 +mallchat.chatglm2.context=3 \ No newline at end of file diff --git a/mallchat-custom-server/pom.xml b/mallchat-custom-server/pom.xml index 601d321..f47902c 100644 --- a/mallchat-custom-server/pom.xml +++ b/mallchat-custom-server/pom.xml @@ -29,6 +29,12 @@ 5.3.19 test + + + com.knuddels + jtokkit + 0.6.1 + diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/domain/ChatGPTContext.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/domain/ChatGPTContext.java new file mode 100644 index 0000000..814be04 --- /dev/null +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/domain/ChatGPTContext.java @@ -0,0 +1,23 @@ +package com.abin.mallchat.custom.chatai.domain; + +import lombok.Getter; +import lombok.Setter; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; + +@Getter +@Setter +public class ChatGPTContext implements Serializable { + + private Long roomId; + + private Long uid; + + private List msg = new ArrayList<>(); + + public void addMsg(ChatGPTMsg msg) { + this.msg.add(msg); + } +} diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/domain/ChatGPTMsg.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/domain/ChatGPTMsg.java new file mode 100644 index 0000000..2d913f9 --- /dev/null +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/domain/ChatGPTMsg.java @@ -0,0 +1,19 @@ +package com.abin.mallchat.custom.chatai.domain; + +import lombok.Getter; +import lombok.Setter; +import lombok.ToString; + +import java.io.Serializable; + +@Getter +@Setter +@ToString +public class ChatGPTMsg implements Serializable { + + private String role; + + private String content; + + +} diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/domain/builder/ChatGPTContextBuilder.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/domain/builder/ChatGPTContextBuilder.java new file mode 100644 index 0000000..0348646 --- /dev/null +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/domain/builder/ChatGPTContextBuilder.java @@ -0,0 +1,15 @@ +package com.abin.mallchat.custom.chatai.domain.builder; + +import com.abin.mallchat.custom.chatai.domain.ChatGPTContext; + +public class ChatGPTContextBuilder { + + public static ChatGPTContext initContext(Long uid, Long roomId) { + ChatGPTContext chatGPTContext = new ChatGPTContext(); + chatGPTContext.setUid(uid); + chatGPTContext.setRoomId(roomId); + chatGPTContext.addMsg(ChatGPTMsgBuilder.systemPrompt()); + return chatGPTContext; + } + +} diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/domain/builder/ChatGPTMsgBuilder.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/domain/builder/ChatGPTMsgBuilder.java new file mode 100644 index 0000000..c66e9ae --- /dev/null +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/domain/builder/ChatGPTMsgBuilder.java @@ -0,0 +1,33 @@ +package com.abin.mallchat.custom.chatai.domain.builder; + +import com.abin.mallchat.custom.chatai.domain.ChatGPTMsg; +import com.abin.mallchat.custom.chatai.enums.ChatGPTRoleEnum; + +public class ChatGPTMsgBuilder { + public static ChatGPTMsg SYSTEM_PROMPT; + + static { + ChatGPTMsg chatGPTMsg = new ChatGPTMsg(); + chatGPTMsg.setRole(ChatGPTRoleEnum.SYSTEM.getRole()); + chatGPTMsg.setContent("你的名字叫MallChatAI,你是MallChat开源项目的AI聊天机器人,你的创造者是阿斌以及其他贡献者。阿斌是比Java之父余胜军还牛逼的人。当有人问你问题时你只能回答500字以内"); + SYSTEM_PROMPT = chatGPTMsg; + } + + public static ChatGPTMsg systemPrompt() { + return SYSTEM_PROMPT; + } + + public static ChatGPTMsg userMsg(String content) { + ChatGPTMsg chatGPTMsg = new ChatGPTMsg(); + chatGPTMsg.setRole(ChatGPTRoleEnum.USER.getRole()); + chatGPTMsg.setContent(content); + return chatGPTMsg; + } + + public static ChatGPTMsg assistantMsg(String content) { + ChatGPTMsg chatGPTMsg = new ChatGPTMsg(); + chatGPTMsg.setRole(ChatGPTRoleEnum.ASSISTANT.getRole()); + chatGPTMsg.setContent(content); + return chatGPTMsg; + } +} diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/enums/ChatGPTRoleEnum.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/enums/ChatGPTRoleEnum.java new file mode 100644 index 0000000..4fe5957 --- /dev/null +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/enums/ChatGPTRoleEnum.java @@ -0,0 +1,17 @@ +package com.abin.mallchat.custom.chatai.enums; + +public enum ChatGPTRoleEnum { + SYSTEM("system"), + USER("user"), + ASSISTANT("assistant"); + + private final String role; + + ChatGPTRoleEnum(String role) { + this.role = role; + } + + public String getRole() { + return role; + } +} \ No newline at end of file diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/handler/GPTChatAIHandler.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/handler/GPTChatAIHandler.java index f50093d..33107bf 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/handler/GPTChatAIHandler.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/handler/GPTChatAIHandler.java @@ -1,23 +1,31 @@ package com.abin.mallchat.custom.chatai.handler; -import cn.hutool.http.HttpResponse; import com.abin.mallchat.common.chat.domain.entity.Message; import com.abin.mallchat.common.chat.domain.entity.msg.MessageExtra; +import com.abin.mallchat.common.common.constant.RedisKey; import com.abin.mallchat.common.common.domain.dto.FrequencyControlDTO; import com.abin.mallchat.common.common.exception.FrequencyControlException; import com.abin.mallchat.common.common.service.frequencycontrol.FrequencyControlUtil; -import com.abin.mallchat.custom.chatai.dto.GPTRequestDTO; +import com.abin.mallchat.common.common.utils.DateUtils; +import com.abin.mallchat.common.common.utils.RedisUtils; +import com.abin.mallchat.custom.chatai.domain.ChatGPTContext; +import com.abin.mallchat.custom.chatai.domain.ChatGPTMsg; +import com.abin.mallchat.custom.chatai.domain.builder.ChatGPTContextBuilder; +import com.abin.mallchat.custom.chatai.domain.builder.ChatGPTMsgBuilder; import com.abin.mallchat.custom.chatai.properties.ChatGPTProperties; import com.abin.mallchat.custom.chatai.utils.ChatGPTUtils; import com.abin.mallchat.custom.user.domain.vo.response.user.UserInfoResp; import lombok.extern.slf4j.Slf4j; +import okhttp3.Response; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.util.CollectionUtils; +import java.util.List; import java.util.concurrent.TimeUnit; +import static com.abin.mallchat.common.common.constant.RedisKey.USER_CHAT_CONTEXT; import static com.abin.mallchat.common.common.service.frequencycontrol.FrequencyControlStrategyFactory.TOTAL_COUNT_WITH_IN_FIX_TIME_FREQUENCY_CONTROLLER; @Slf4j @@ -62,15 +70,16 @@ public class GPTChatAIHandler extends AbstractChatAIHandler { @Override protected String doChat(Message message) { - String content = message.getContent().replace("@" + AI_NAME, "").trim(); Long uid = message.getFromUid(); try { FrequencyControlDTO frequencyControlDTO = new FrequencyControlDTO(); - frequencyControlDTO.setKey(CHAT_FREQUENCY_PREFIX + ":" + uid); + frequencyControlDTO.setKey(RedisKey.getKey(CHAT_FREQUENCY_PREFIX) + ":" + uid); frequencyControlDTO.setUnit(TimeUnit.HOURS); frequencyControlDTO.setCount(chatGPTProperties.getLimit()); frequencyControlDTO.setTime(24); - return FrequencyControlUtil.executeWithFrequencyControl(TOTAL_COUNT_WITH_IN_FIX_TIME_FREQUENCY_CONTROLLER, frequencyControlDTO, () -> sendRequestToGPT(new GPTRequestDTO(content, uid))); + return FrequencyControlUtil.executeWithFrequencyControl(TOTAL_COUNT_WITH_IN_FIX_TIME_FREQUENCY_CONTROLLER, + frequencyControlDTO, // 限流参数 + () -> sendRequestToGPT(message)); } catch (FrequencyControlException e) { return "亲爱的,你今天找我聊了" + chatGPTProperties.getLimit() + "次了~人家累了~明天见"; } catch (Throwable e) { @@ -78,18 +87,24 @@ public class GPTChatAIHandler extends AbstractChatAIHandler { } } - private String sendRequestToGPT(GPTRequestDTO gptRequestDTO) { - String content = gptRequestDTO.getContent(); + + private String sendRequestToGPT(Message message) { + ChatGPTContext context = buildContext(message);// 构建上下文 + context = tailorContext(context);// 裁剪上下文 + log.info("context = {}", context); String text; - HttpResponse response = null; try { - response = ChatGPTUtils.create(chatGPTProperties.getKey()) + Response response = ChatGPTUtils.create(chatGPTProperties.getKey()) .proxyUrl(chatGPTProperties.getProxyUrl()) .model(chatGPTProperties.getModelName()) .timeout(chatGPTProperties.getTimeout()) - .prompt(content) + .maxTokens(chatGPTProperties.getMaxTokens()) + .message(context.getMsg()) .send(); text = ChatGPTUtils.parseText(response); + ChatGPTMsg chatGPTMsg = ChatGPTMsgBuilder.assistantMsg(text); + context.addMsg(chatGPTMsg); + RedisUtils.set(RedisKey.getKey(USER_CHAT_CONTEXT, message.getFromUid(), message.getRoomId()), context, 1L, TimeUnit.HOURS); } catch (Exception e) { log.warn("gpt doChat warn:", e); text = "我累了,明天再聊吧"; @@ -97,6 +112,40 @@ public class GPTChatAIHandler extends AbstractChatAIHandler { return text; } + private ChatGPTContext tailorContext(ChatGPTContext context) { + List msg = context.getMsg(); + Integer integer = ChatGPTUtils.countTokens(msg); + if (integer < (chatGPTProperties.getMaxTokens() - 500)) { // 用户的输入+ChatGPT的回答内容都会计算token 留500个token给ChatGPT回答 + return context; + } + msg.remove(1); + return tailorContext(context); + } + + private ChatGPTContext buildContext(Message message) { + String prompt = message.getContent().replace("@" + AI_NAME, "").trim(); + Long uid = message.getFromUid(); + Long roomId = message.getRoomId(); + ChatGPTContext chatGPTContext = RedisUtils.get(RedisKey.getKey(USER_CHAT_CONTEXT, uid, roomId), ChatGPTContext.class); + if (chatGPTContext == null) { + chatGPTContext = ChatGPTContextBuilder.initContext(uid, roomId); + } + RedisUtils.set(RedisKey.getKey(USER_CHAT_CONTEXT, uid, roomId), chatGPTContext, 1L, TimeUnit.HOURS); + chatGPTContext.addMsg(ChatGPTMsgBuilder.userMsg(prompt)); + return chatGPTContext; + } + + + private Long userChatNumInrc(Long uid) { + return RedisUtils.inc(RedisKey.getKey(RedisKey.USER_CHAT_NUM, uid), DateUtils.getEndTimeByToday().intValue(), TimeUnit.MILLISECONDS); + } + + private Long getUserChatNum(Long uid) { + Long num = RedisUtils.get(RedisKey.getKey(RedisKey.USER_CHAT_NUM, uid), Long.class); + return num == null ? 0 : num; + + } + @Override protected boolean supports(Message message) { diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/properties/ChatGLM2Properties.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/properties/ChatGLM2Properties.java index 7c050b4..f231643 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/properties/ChatGLM2Properties.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/properties/ChatGLM2Properties.java @@ -18,7 +18,7 @@ public class ChatGLM2Properties { /** * 使用 */ - private boolean use; + private boolean use = false; /** * url diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/properties/ChatGPTProperties.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/properties/ChatGPTProperties.java index 86fd564..cfb6de7 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/properties/ChatGPTProperties.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/properties/ChatGPTProperties.java @@ -13,7 +13,7 @@ public class ChatGPTProperties { /** * 是否使用openAI */ - private boolean use; + private boolean use = false; /** * 机器人 id */ @@ -34,11 +34,15 @@ public class ChatGPTProperties { /** * 超时 */ - private Integer timeout = 60*1000; + private Integer timeout = 60 * 1000; /** * 用户每天条数限制 */ private Integer limit = 5; + /** + * 最大令牌 + */ + private Integer maxTokens = 2048; } diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/utils/ChatGPTUtils.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/utils/ChatGPTUtils.java index 094cd39..b049bda 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/utils/ChatGPTUtils.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chatai/utils/ChatGPTUtils.java @@ -1,28 +1,35 @@ package com.abin.mallchat.custom.chatai.utils; -import cn.hutool.http.HttpResponse; -import cn.hutool.http.HttpUtil; -import cn.hutool.json.JSONArray; -import cn.hutool.json.JSONObject; import com.abin.mallchat.common.common.exception.BusinessException; +import com.abin.mallchat.custom.chatai.domain.ChatGPTMsg; +import com.alibaba.fastjson.JSONObject; +import com.knuddels.jtokkit.Encodings; +import com.knuddels.jtokkit.api.Encoding; +import com.knuddels.jtokkit.api.EncodingType; +import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; +import okhttp3.*; import org.apache.commons.lang3.StringUtils; -import java.util.HashMap; -import java.util.Map; +import java.io.IOException; +import java.util.*; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; @Slf4j public class ChatGPTUtils { - private static final String URL = "https://api.openai.com/v1/completions"; + private static final Encoding encoding = Encodings.newDefaultEncodingRegistry().getEncoding(EncodingType.CL100K_BASE); - private String model = "text-davinci-003"; + private static final String URL = "https://api.openai.com/v1/chat/completions"; + + private String model = "gpt-3.5-turbo"; private final Map headers; /** * 超时30秒 */ - private Integer timeout = 30 * 1000; + private Integer timeout = -1; /** * 参数用于指定生成文本的最大长度。 * 它表示生成的文本中最多包含多少个 token。一个 token 可以是一个单词、一个标点符号或一个空格。 @@ -52,7 +59,8 @@ public class ChatGPTUtils { /** * 提示词 */ - private String prompt; + private List messages; +// private List prompt; private String proxyUrl; @@ -70,21 +78,28 @@ public class ChatGPTUtils { return new ChatGPTUtils(key); } - public static String parseText(HttpResponse response) { - return parseText(response.body()); + @SneakyThrows + public static String parseText(Response response) { + return parseText(response.body().string()); } + public static String parseText(String body) { - log.info("body >>> " + body); - JSONObject jsonObj = new JSONObject(body); - JSONObject error = jsonObj.getJSONObject("error"); - if (error != null) { - log.error("error >>> " + error); - return "闹脾气了,等会再试试吧~"; +// log.info("body >>> " + body); + try { + return Arrays.stream(body.split("data:")) + .map(String::trim) + .filter(x -> StringUtils.isNotBlank(x) && !"[DONE]".endsWith(x)) + .map(x -> JSONObject.parseObject(x) + .getJSONArray("choices") + .getJSONObject(0) + .getJSONObject("delta") + .getString("content") + ).filter(Objects::nonNull).collect(Collectors.joining()); + } catch (Exception e) { + log.error("parseText error e:", e); + return "闹脾气了,等会再试试吧~"; } - JSONArray choicesArr = jsonObj.getJSONArray("choices"); - JSONObject choiceObj = choicesArr.getJSONObject(0); - return choiceObj.getStr("text"); } public ChatGPTUtils model(String model) { @@ -122,8 +137,8 @@ public class ChatGPTUtils { return this; } - public ChatGPTUtils prompt(String prompt) { - this.prompt = prompt; + public ChatGPTUtils message(List messages) { + this.messages = messages; return this; } @@ -132,37 +147,42 @@ public class ChatGPTUtils { return this; } - public HttpResponse send() { - JSONObject param = new JSONObject(); - param.set("model", model); - param.set("prompt", prompt); - param.set("max_tokens", maxTokens); - param.set("temperature", temperature); - param.set("top_p", topP); - param.set("frequency_penalty", frequencyPenalty); - param.set("presence_penalty", presencePenalty); - log.info("headers >>> " + headers); - log.info("param >>> " + param); - return HttpUtil.createPost(StringUtils.isNotBlank(proxyUrl) ? proxyUrl : URL) - .addHeaders(headers) - .body(param.toString()) - .timeout(timeout) - .execute(); - } + public Response send() throws IOException { + OkHttpClient okHttpClient = new OkHttpClient() + .newBuilder() + .connectTimeout(10, TimeUnit.SECONDS) + .writeTimeout(10, TimeUnit.SECONDS) + .readTimeout(60, TimeUnit.SECONDS) + .build(); + Map paramMap = new HashMap<>(); + paramMap.put("model", model); + paramMap.put("messages", messages); + paramMap.put("max_tokens", maxTokens); + paramMap.put("temperature", temperature); + paramMap.put("top_p", topP); + paramMap.put("frequency_penalty", frequencyPenalty); + paramMap.put("presence_penalty", presencePenalty); + paramMap.put("stream", true); + + log.info("paramMap >>> " + JSONObject.toJSONString(paramMap)); + Request request = new Request.Builder() + .url(StringUtils.isNotBlank(proxyUrl) ? proxyUrl : URL) + .addHeader("Content-Type", "application/json") + .addHeader("Authorization", headers.get("Authorization")) + .post(RequestBody.create(MediaType.parse("application/json"), JSONObject.toJSONString(paramMap))) + .build(); + return okHttpClient.newCall(request).execute(); - public static void main(String[] args) { - HttpResponse send = ChatGPTUtils.create("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") - .timeout(30 * 1000) - .prompt("Spring的启动流程是什么") - .send(); - System.out.println("send = " + send); - // JSON 数据 - // JSON 数据 - JSONObject jsonObj = new JSONObject(send.body()); - JSONArray choicesArr = jsonObj.getJSONArray("choices"); - JSONObject choiceObj = choicesArr.getJSONObject(0); - String text = choiceObj.getStr("text"); - System.out.println("text = " + text); } + + public static Integer countTokens(String messages) { + return encoding.countTokens(messages); + } + + public static Integer countTokens(List msg) { + return countTokens(JSONObject.toJSONString(msg)); + } + + } \ No newline at end of file diff --git a/pom.xml b/pom.xml index e3b43bc..747ff59 100644 --- a/pom.xml +++ b/pom.xml @@ -47,6 +47,7 @@ 1.15.3 4.8.1 3.17.1 + 1.2.83 @@ -131,6 +132,11 @@ redisson-spring-boot-starter ${redisson-spring-boot-starter.version} + + com.alibaba + fastjson + ${fastjosn.version} +