Merge pull request #106 from 1045078399/chat_context

feat:Chat context
This commit is contained in:
zongzibinbin 2023-07-16 01:47:26 +08:00 committed by GitHub
commit 80163700ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 266 additions and 67 deletions

View File

@ -115,6 +115,10 @@
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>

View File

@ -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使用时间
*/

View File

@ -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
mallchat.chatglm2.uid=10002
mallchat.chatglm2.context=3

View File

@ -29,6 +29,12 @@
<version>5.3.19</version>
<scope>test</scope>
</dependency>
<!-- token计算 -->
<dependency>
<groupId>com.knuddels</groupId>
<artifactId>jtokkit</artifactId>
<version>0.6.1</version>
</dependency>
</dependencies>
<build>

View File

@ -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<ChatGPTMsg> msg = new ArrayList<>();
public void addMsg(ChatGPTMsg msg) {
this.msg.add(msg);
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -18,7 +18,7 @@ public class ChatGLM2Properties {
/**
* 使用
*/
private boolean use;
private boolean use = false;
/**
* url

View File

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

View File

@ -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<String, String> 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<ChatGPTMsg> messages;
// private List<ChatGPTMsg> 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<ChatGPTMsg> 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<String, Object> 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<ChatGPTMsg> msg) {
return countTokens(JSONObject.toJSONString(msg));
}
}

View File

@ -47,6 +47,7 @@
<jsoup.version>1.15.3</jsoup.version>
<okhttp.version>4.8.1</okhttp.version>
<redisson-spring-boot-starter.version>3.17.1</redisson-spring-boot-starter.version>
<fastjosn.version>1.2.83</fastjosn.version>
</properties>
<dependencyManagement>
@ -131,6 +132,11 @@
<artifactId>redisson-spring-boot-starter</artifactId>
<version>${redisson-spring-boot-starter.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjosn.version}</version>
</dependency>
</dependencies>
</dependencyManagement>