mirror of
https://github.com/tencentmusic/supersonic.git
synced 2025-12-13 04:57:28 +00:00
(improvement)(Chat) Refactor agent examples execution code (#1258)
Co-authored-by: lxwcodemonkey
This commit is contained in:
@@ -1,49 +0,0 @@
|
|||||||
package com.tencent.supersonic.chat.server.memory;
|
|
||||||
|
|
||||||
import com.tencent.supersonic.auth.api.authentication.pojo.User;
|
|
||||||
import com.tencent.supersonic.chat.api.pojo.request.ChatMemoryFilter;
|
|
||||||
import com.tencent.supersonic.chat.api.pojo.request.ChatParseReq;
|
|
||||||
import com.tencent.supersonic.chat.server.agent.Agent;
|
|
||||||
import com.tencent.supersonic.chat.server.persistence.dataobject.ChatMemoryDO;
|
|
||||||
import com.tencent.supersonic.chat.server.service.ChatService;
|
|
||||||
import com.tencent.supersonic.chat.server.service.MemoryService;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.scheduling.annotation.Async;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
@Component
|
|
||||||
@Slf4j
|
|
||||||
public class AgentExample2MemoryTransformer {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private ChatService chatService;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private MemoryService memoryService;
|
|
||||||
|
|
||||||
@Async
|
|
||||||
public void transform(Agent agent) {
|
|
||||||
if (!agent.containsLLMParserTool() || agent.getLlmConfig() == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
List<String> examples = agent.getExamples();
|
|
||||||
ChatMemoryFilter chatMemoryFilter = ChatMemoryFilter.builder().questions(examples).build();
|
|
||||||
List<String> memoriesExisted = memoryService.getMemories(chatMemoryFilter)
|
|
||||||
.stream().map(ChatMemoryDO::getQuestion).collect(Collectors.toList());
|
|
||||||
for (String example : examples) {
|
|
||||||
if (memoriesExisted.contains(example)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
ChatParseReq chatParseReq = new ChatParseReq();
|
|
||||||
chatParseReq.setAgentId(agent.getId());
|
|
||||||
chatParseReq.setQueryText(example);
|
|
||||||
chatParseReq.setUser(User.getFakeUser());
|
|
||||||
chatParseReq.setChatId(-1);
|
|
||||||
chatService.parseAndExecute(chatParseReq);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -2,27 +2,42 @@ package com.tencent.supersonic.chat.server.service.impl;
|
|||||||
|
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import com.tencent.supersonic.auth.api.authentication.pojo.User;
|
import com.tencent.supersonic.auth.api.authentication.pojo.User;
|
||||||
|
import com.tencent.supersonic.chat.api.pojo.request.ChatMemoryFilter;
|
||||||
|
import com.tencent.supersonic.chat.api.pojo.request.ChatParseReq;
|
||||||
import com.tencent.supersonic.chat.server.agent.Agent;
|
import com.tencent.supersonic.chat.server.agent.Agent;
|
||||||
import com.tencent.supersonic.chat.server.agent.MultiTurnConfig;
|
import com.tencent.supersonic.chat.server.agent.MultiTurnConfig;
|
||||||
import com.tencent.supersonic.chat.server.memory.AgentExample2MemoryTransformer;
|
|
||||||
import com.tencent.supersonic.chat.server.persistence.dataobject.AgentDO;
|
import com.tencent.supersonic.chat.server.persistence.dataobject.AgentDO;
|
||||||
|
import com.tencent.supersonic.chat.server.persistence.dataobject.ChatMemoryDO;
|
||||||
import com.tencent.supersonic.chat.server.persistence.mapper.AgentDOMapper;
|
import com.tencent.supersonic.chat.server.persistence.mapper.AgentDOMapper;
|
||||||
import com.tencent.supersonic.chat.server.service.AgentService;
|
import com.tencent.supersonic.chat.server.service.AgentService;
|
||||||
|
import com.tencent.supersonic.chat.server.service.ChatService;
|
||||||
|
import com.tencent.supersonic.chat.server.service.MemoryService;
|
||||||
|
import com.tencent.supersonic.chat.server.util.LLMConnHelper;
|
||||||
import com.tencent.supersonic.common.config.LLMConfig;
|
import com.tencent.supersonic.common.config.LLMConfig;
|
||||||
import com.tencent.supersonic.common.config.VisualConfig;
|
import com.tencent.supersonic.common.config.VisualConfig;
|
||||||
import com.tencent.supersonic.common.util.JsonUtil;
|
import com.tencent.supersonic.common.util.JsonUtil;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
public class AgentServiceImpl extends ServiceImpl<AgentDOMapper, AgentDO>
|
public class AgentServiceImpl extends ServiceImpl<AgentDOMapper, AgentDO>
|
||||||
implements AgentService {
|
implements AgentService {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private AgentExample2MemoryTransformer agentExample2MemoryTransformer;
|
private MemoryService memoryService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ChatService chatService;
|
||||||
|
|
||||||
|
private ExecutorService executorService = Executors.newFixedThreadPool(1);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Agent> getAgents() {
|
public List<Agent> getAgents() {
|
||||||
@@ -35,7 +50,7 @@ public class AgentServiceImpl extends ServiceImpl<AgentDOMapper, AgentDO>
|
|||||||
agent.createdBy(user.getName());
|
agent.createdBy(user.getName());
|
||||||
AgentDO agentDO = convert(agent);
|
AgentDO agentDO = convert(agent);
|
||||||
save(agentDO);
|
save(agentDO);
|
||||||
agentExample2MemoryTransformer.transform(agent);
|
executeAgentExamplesAsync(agent);
|
||||||
return agentDO.getId();
|
return agentDO.getId();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,7 +58,7 @@ public class AgentServiceImpl extends ServiceImpl<AgentDOMapper, AgentDO>
|
|||||||
public void updateAgent(Agent agent, User user) {
|
public void updateAgent(Agent agent, User user) {
|
||||||
agent.updatedBy(user.getName());
|
agent.updatedBy(user.getName());
|
||||||
updateById(convert(agent));
|
updateById(convert(agent));
|
||||||
agentExample2MemoryTransformer.transform(agent);
|
executeAgentExamplesAsync(agent);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -59,6 +74,36 @@ public class AgentServiceImpl extends ServiceImpl<AgentDOMapper, AgentDO>
|
|||||||
removeById(id);
|
removeById(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* the example in the agent will be executed by default,
|
||||||
|
* if the result is correct, it will be put into memory as a reference for LLM
|
||||||
|
* @param agent
|
||||||
|
*/
|
||||||
|
private void executeAgentExamplesAsync(Agent agent) {
|
||||||
|
executorService.execute(() -> doExecuteAgentExamples(agent));
|
||||||
|
}
|
||||||
|
|
||||||
|
private synchronized void doExecuteAgentExamples(Agent agent) {
|
||||||
|
if (!agent.containsLLMParserTool() || !LLMConnHelper.testConnection(agent.getLlmConfig())) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
List<String> examples = agent.getExamples();
|
||||||
|
ChatMemoryFilter chatMemoryFilter = ChatMemoryFilter.builder().questions(examples).build();
|
||||||
|
List<String> memoriesExisted = memoryService.getMemories(chatMemoryFilter)
|
||||||
|
.stream().map(ChatMemoryDO::getQuestion).collect(Collectors.toList());
|
||||||
|
for (String example : examples) {
|
||||||
|
if (memoriesExisted.contains(example)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
ChatParseReq chatParseReq = new ChatParseReq();
|
||||||
|
chatParseReq.setAgentId(agent.getId());
|
||||||
|
chatParseReq.setQueryText(example);
|
||||||
|
chatParseReq.setUser(User.getFakeUser());
|
||||||
|
chatParseReq.setChatId(-1);
|
||||||
|
chatService.parseAndExecute(chatParseReq);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private List<AgentDO> getAgentDOList() {
|
private List<AgentDO> getAgentDOList() {
|
||||||
return list();
|
return list();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -138,16 +138,16 @@ CREATE TABLE `s2_chat_config` (
|
|||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS `s2_chat_memory` (
|
CREATE TABLE IF NOT EXISTS `s2_chat_memory` (
|
||||||
`id` INT NOT NULL AUTO_INCREMENT,
|
`id` INT NOT NULL AUTO_INCREMENT,
|
||||||
`question` varchar(655) ,
|
`question` varchar(655) COMMENT '用户问题' ,
|
||||||
`agent_id` INT ,
|
`agent_id` INT COMMENT '助理ID' ,
|
||||||
`db_schema` TEXT ,
|
`db_schema` TEXT COMMENT 'Schema映射' ,
|
||||||
`s2_sql` TEXT ,
|
`s2_sql` TEXT COMMENT '大模型解析SQL' ,
|
||||||
`status` char(10) ,
|
`status` char(10) COMMENT '状态' ,
|
||||||
`llm_review` char(10) ,
|
`llm_review` char(10) COMMENT '大模型评估结果' ,
|
||||||
`llm_comment` TEXT,
|
`llm_comment` TEXT COMMENT '大模型评估意见' ,
|
||||||
`human_review` char(10) ,
|
`human_review` char(10) COMMENT '管理员评估结果',
|
||||||
`human_comment` TEXT ,
|
`human_comment` TEXT COMMENT '管理员评估意见',
|
||||||
`created_at` datetime NOT NULL ,
|
`created_at` datetime NOT NULL ,
|
||||||
`updated_at` datetime NOT NULL ,
|
`updated_at` datetime NOT NULL ,
|
||||||
`created_by` varchar(100) NOT NULL ,
|
`created_by` varchar(100) NOT NULL ,
|
||||||
`updated_by` varchar(100) NOT NULL ,
|
`updated_by` varchar(100) NOT NULL ,
|
||||||
|
|||||||
Reference in New Issue
Block a user