|
|
@ -3,6 +3,7 @@ package com.wok.supportbot.app; |
|
|
import com.wok.supportbot.advisor.MyLoggerAdvisor; |
|
|
import com.wok.supportbot.advisor.MyLoggerAdvisor; |
|
|
import com.wok.supportbot.advisor.ReReadingAdvisor; |
|
|
import com.wok.supportbot.advisor.ReReadingAdvisor; |
|
|
import com.wok.supportbot.chatmemory.DatabaseChatMemory; |
|
|
import com.wok.supportbot.chatmemory.DatabaseChatMemory; |
|
|
|
|
|
import com.wok.supportbot.config.ChatModelFactory; |
|
|
import com.wok.supportbot.rag.preretrieval.CompressionQueryRewriter; |
|
|
import com.wok.supportbot.rag.preretrieval.CompressionQueryRewriter; |
|
|
import com.wok.supportbot.rag.preretrieval.MultiQueryExpanderRewriter; |
|
|
import com.wok.supportbot.rag.preretrieval.MultiQueryExpanderRewriter; |
|
|
import com.wok.supportbot.rag.preretrieval.RewriteQueryRewriter; |
|
|
import com.wok.supportbot.rag.preretrieval.RewriteQueryRewriter; |
|
|
@ -36,6 +37,7 @@ import java.util.Collections; |
|
|
import java.util.LinkedHashMap; |
|
|
import java.util.LinkedHashMap; |
|
|
import java.util.List; |
|
|
import java.util.List; |
|
|
import java.util.Map; |
|
|
import java.util.Map; |
|
|
|
|
|
import java.util.concurrent.ConcurrentHashMap; |
|
|
import java.util.stream.Collectors; |
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
|
import static org.springframework.ai.chat.memory.ChatMemory.CONVERSATION_ID; |
|
|
import static org.springframework.ai.chat.memory.ChatMemory.CONVERSATION_ID; |
|
|
@ -54,10 +56,12 @@ public class AssistantApp { |
|
|
@Resource |
|
|
@Resource |
|
|
private VectorStore pgVectorVectorStore; |
|
|
private VectorStore pgVectorVectorStore; |
|
|
|
|
|
|
|
|
private final ChatClient chatClient; |
|
|
|
|
|
|
|
|
private final ChatModelFactory chatModelFactory; |
|
|
|
|
|
|
|
|
private final DatabaseChatMemory chatMemory; |
|
|
private final DatabaseChatMemory chatMemory; |
|
|
|
|
|
|
|
|
|
|
|
private final ConcurrentHashMap<String, ChatClient> chatClientCache = new ConcurrentHashMap<>(); |
|
|
|
|
|
|
|
|
private static final String SYSTEM_PROMPT = "你是一名智能客服助手,负责解答用户问题。" + |
|
|
private static final String SYSTEM_PROMPT = "你是一名智能客服助手,负责解答用户问题。" + |
|
|
"请主动引导用户提供关键信息,并尽量在不转人工的情况下解决问题。保持专业、耐心、礼貌。"; |
|
|
"请主动引导用户提供关键信息,并尽量在不转人工的情况下解决问题。保持专业、耐心、礼貌。"; |
|
|
|
|
|
|
|
|
@ -66,24 +70,27 @@ public class AssistantApp { |
|
|
* |
|
|
* |
|
|
* @param dashscopeChatModel |
|
|
* @param dashscopeChatModel |
|
|
*/ |
|
|
*/ |
|
|
public AssistantApp(ChatModel dashscopeChatModel, DatabaseChatMemory chatMemory) { |
|
|
|
|
|
// 初始化基于文件的对话记忆 |
|
|
|
|
|
//String fileDir = System.getProperty("user.dir") + "/tmp/chat-memory"; |
|
|
|
|
|
//ChatMemory chatMemory = new FileBasedChatMemory(fileDir); |
|
|
|
|
|
// 初始化基于内存的对话记忆 |
|
|
|
|
|
// ChatMemory chatMemory = new InMemoryChatMemory(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public AssistantApp(ChatModelFactory chatModelFactory, DatabaseChatMemory chatMemory) { |
|
|
|
|
|
this.chatModelFactory = chatModelFactory; |
|
|
this.chatMemory = chatMemory; |
|
|
this.chatMemory = chatMemory; |
|
|
chatClient = ChatClient.builder(dashscopeChatModel) |
|
|
|
|
|
.defaultSystem(SYSTEM_PROMPT) |
|
|
|
|
|
.defaultAdvisors( |
|
|
|
|
|
MessageChatMemoryAdvisor.builder(chatMemory).build(), |
|
|
|
|
|
// 自定义日志 Advisor,可按需开启 |
|
|
|
|
|
new MyLoggerAdvisor() |
|
|
|
|
|
// 自定义推理增强 Advisor,可按需开启 |
|
|
|
|
|
//,new ReReadingAdvisor() |
|
|
|
|
|
) |
|
|
|
|
|
.build(); |
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private ChatClient getChatClient(String appType) { |
|
|
|
|
|
return chatClientCache.computeIfAbsent(appType, type -> { |
|
|
|
|
|
ChatModel chatModel = chatModelFactory.getChatModel(type); |
|
|
|
|
|
return ChatClient.builder(chatModel) |
|
|
|
|
|
.defaultSystem(SYSTEM_PROMPT) |
|
|
|
|
|
.defaultAdvisors( |
|
|
|
|
|
MessageChatMemoryAdvisor.builder(chatMemory).build(), |
|
|
|
|
|
new MyLoggerAdvisor() |
|
|
|
|
|
) |
|
|
|
|
|
.build(); |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void clearCache() { |
|
|
|
|
|
chatClientCache.clear(); |
|
|
|
|
|
log.info("AssistantApp ChatClient cache cleared"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
@ -106,7 +113,7 @@ public class AssistantApp { |
|
|
* @return AI 回答 |
|
|
* @return AI 回答 |
|
|
*/ |
|
|
*/ |
|
|
public String doChat(String message, String chatId, String systemPrompt) { |
|
|
public String doChat(String message, String chatId, String systemPrompt) { |
|
|
ChatClient.ChatClientRequestSpec spec = chatClient |
|
|
|
|
|
|
|
|
ChatClient.ChatClientRequestSpec spec = getChatClient("CHAT") |
|
|
.prompt() |
|
|
.prompt() |
|
|
.user(message) |
|
|
.user(message) |
|
|
.advisors(s -> s.param(CONVERSATION_ID, chatId)); |
|
|
.advisors(s -> s.param(CONVERSATION_ID, chatId)); |
|
|
@ -148,7 +155,7 @@ public class AssistantApp { |
|
|
* @return 流式回答 |
|
|
* @return 流式回答 |
|
|
*/ |
|
|
*/ |
|
|
public Flux<String> doChatByStream(String message, String chatId, String systemPrompt) { |
|
|
public Flux<String> doChatByStream(String message, String chatId, String systemPrompt) { |
|
|
ChatClient.ChatClientRequestSpec spec = chatClient |
|
|
|
|
|
|
|
|
ChatClient.ChatClientRequestSpec spec = getChatClient("CHAT") |
|
|
.prompt() |
|
|
.prompt() |
|
|
.user(message) |
|
|
.user(message) |
|
|
.advisors(s -> s.param(CONVERSATION_ID, chatId)); |
|
|
.advisors(s -> s.param(CONVERSATION_ID, chatId)); |
|
|
@ -181,7 +188,7 @@ public class AssistantApp { |
|
|
// String rewrittenMessage = translationQueryRewriter.doQueryRewrite(message); |
|
|
// String rewrittenMessage = translationQueryRewriter.doQueryRewrite(message); |
|
|
String rewrittenMessage = rewriteQueryRewriter.doQueryRewrite(message); |
|
|
String rewrittenMessage = rewriteQueryRewriter.doQueryRewrite(message); |
|
|
|
|
|
|
|
|
ChatResponse chatResponse = chatClient |
|
|
|
|
|
|
|
|
ChatResponse chatResponse = getChatClient("CHAT") |
|
|
.prompt() |
|
|
.prompt() |
|
|
.user(rewrittenMessage) |
|
|
.user(rewrittenMessage) |
|
|
.advisors(spec -> spec.param(CONVERSATION_ID, chatId)) |
|
|
.advisors(spec -> spec.param(CONVERSATION_ID, chatId)) |
|
|
@ -222,7 +229,7 @@ public class AssistantApp { |
|
|
// 其他策略:单查询处理 |
|
|
// 其他策略:单查询处理 |
|
|
String rewrittenMessage = rewriteQuery(message, chatId, strategy); |
|
|
String rewrittenMessage = rewriteQuery(message, chatId, strategy); |
|
|
|
|
|
|
|
|
ChatClient.ChatClientRequestSpec spec = chatClient |
|
|
|
|
|
|
|
|
ChatClient.ChatClientRequestSpec spec = getChatClient("CHAT") |
|
|
.prompt() |
|
|
.prompt() |
|
|
.user(rewrittenMessage) |
|
|
.user(rewrittenMessage) |
|
|
.advisors(s -> s.param(CONVERSATION_ID, chatId)) |
|
|
.advisors(s -> s.param(CONVERSATION_ID, chatId)) |
|
|
@ -277,7 +284,7 @@ public class AssistantApp { |
|
|
|
|
|
|
|
|
String rewrittenMessage = rewriteQuery(message, chatId, strategy); |
|
|
String rewrittenMessage = rewriteQuery(message, chatId, strategy); |
|
|
|
|
|
|
|
|
ChatClient.ChatClientRequestSpec spec = chatClient |
|
|
|
|
|
|
|
|
ChatClient.ChatClientRequestSpec spec = getChatClient("CHAT") |
|
|
.prompt() |
|
|
.prompt() |
|
|
.user(rewrittenMessage) |
|
|
.user(rewrittenMessage) |
|
|
.advisors(s -> s.param(CONVERSATION_ID, chatId)) |
|
|
.advisors(s -> s.param(CONVERSATION_ID, chatId)) |
|
|
@ -301,7 +308,7 @@ public class AssistantApp { |
|
|
*/ |
|
|
*/ |
|
|
private String doChatWithMultiQueryRag(String message, String chatId, List<Long> categoryIds, String systemPrompt) { |
|
|
private String doChatWithMultiQueryRag(String message, String chatId, List<Long> categoryIds, String systemPrompt) { |
|
|
String ragSystem = buildMultiQueryRagSystem(message, categoryIds, systemPrompt); |
|
|
String ragSystem = buildMultiQueryRagSystem(message, categoryIds, systemPrompt); |
|
|
ChatResponse chatResponse = chatClient |
|
|
|
|
|
|
|
|
ChatResponse chatResponse = getChatClient("CHAT") |
|
|
.prompt() |
|
|
.prompt() |
|
|
.system(ragSystem) |
|
|
.system(ragSystem) |
|
|
.user(message) |
|
|
.user(message) |
|
|
@ -313,7 +320,7 @@ public class AssistantApp { |
|
|
|
|
|
|
|
|
private Flux<String> doChatWithMultiQueryRagByStream(String message, String chatId, List<Long> categoryIds, String systemPrompt) { |
|
|
private Flux<String> doChatWithMultiQueryRagByStream(String message, String chatId, List<Long> categoryIds, String systemPrompt) { |
|
|
String ragSystem = buildMultiQueryRagSystem(message, categoryIds, systemPrompt); |
|
|
String ragSystem = buildMultiQueryRagSystem(message, categoryIds, systemPrompt); |
|
|
return chatClient |
|
|
|
|
|
|
|
|
return getChatClient("CHAT") |
|
|
.prompt() |
|
|
.prompt() |
|
|
.system(ragSystem) |
|
|
.system(ragSystem) |
|
|
.user(message) |
|
|
.user(message) |
|
|
@ -498,7 +505,7 @@ public class AssistantApp { |
|
|
.build()) |
|
|
.build()) |
|
|
.build(); |
|
|
.build(); |
|
|
|
|
|
|
|
|
ChatResponse chatResponse = chatClient |
|
|
|
|
|
|
|
|
ChatResponse chatResponse = getChatClient("CHAT") |
|
|
.prompt() |
|
|
.prompt() |
|
|
.user(message) |
|
|
.user(message) |
|
|
.advisors(spec -> spec.param(CONVERSATION_ID, chatId)) |
|
|
.advisors(spec -> spec.param(CONVERSATION_ID, chatId)) |
|
|
|