对话记忆
This commit is contained in:
parent
ae1ff68a75
commit
931da14ff4
|
|
@ -3,13 +3,17 @@ package com.baoshi.conf;
|
|||
import com.alibaba.cloud.ai.autoconfigure.dashscope.DashScopeChatProperties;
|
||||
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;
|
||||
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatOptions;
|
||||
import com.baoshi.core.JDBCChatMemory;
|
||||
import org.springframework.ai.chat.client.ChatClient;
|
||||
import org.springframework.ai.chat.memory.ChatMemory;
|
||||
import org.springframework.ai.model.ollama.autoconfigure.OllamaChatProperties;
|
||||
import org.springframework.ai.ollama.OllamaChatModel;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
@Configuration
|
||||
public class AiConfig {
|
||||
|
||||
|
|
@ -27,4 +31,9 @@ public class AiConfig {
|
|||
return ChatClient.builder(ollamaChatModel).defaultOptions(properties.getOptions()).build();
|
||||
}
|
||||
|
||||
|
||||
@Bean
|
||||
public ChatMemory chatMemory() {
|
||||
return new JDBCChatMemory(new HashMap<>());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,42 @@
|
|||
package com.baoshi.core;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springframework.ai.chat.memory.ChatMemory;
|
||||
import org.springframework.ai.chat.messages.Message;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Wen
|
||||
* @date 2026/3/23 18:46
|
||||
*/
|
||||
public class JDBCChatMemory implements ChatMemory {
|
||||
private final Map<String, List<Message>> table;
|
||||
|
||||
public JDBCChatMemory(Map<String, List<Message>> table) {
|
||||
this.table = table;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(@NotNull String conversationId, @NotNull Message message) {
|
||||
table.computeIfAbsent(conversationId, t -> new ArrayList<>()).add(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(@NotNull String conversationId, @NotNull List<Message> messages) {
|
||||
table.computeIfAbsent(conversationId, t -> new ArrayList<>()).addAll(messages);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public List<Message> get(@NotNull String conversationId) {
|
||||
return table.getOrDefault(conversationId, new ArrayList<>());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear(@NotNull String conversationId) {
|
||||
table.remove(conversationId);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
package com.baoshi.core;
|
||||
|
||||
import org.springframework.ai.chat.client.ChatClient;
|
||||
import org.springframework.ai.chat.client.ChatClientRequest;
|
||||
import org.springframework.ai.chat.client.ChatClientResponse;
|
||||
import org.springframework.ai.chat.client.advisor.api.AdvisorChain;
|
||||
import org.springframework.ai.chat.client.advisor.api.BaseAdvisor;
|
||||
import org.springframework.ai.chat.memory.ChatMemory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author Wen
|
||||
* @date 2026/3/23 18:48
|
||||
*/
|
||||
@Component
|
||||
public class MemoryAdvisor implements BaseAdvisor {
|
||||
|
||||
@Autowired
|
||||
private ChatMemory chatMemory;
|
||||
|
||||
@Override
|
||||
public ChatClientRequest before(ChatClientRequest chatClientRequest, AdvisorChain advisorChain) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChatClientResponse after(ChatClientResponse chatClientResponse, AdvisorChain advisorChain) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,13 @@
|
|||
package com.baoshi;
|
||||
|
||||
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.ai.chat.client.ChatClient;
|
||||
import org.springframework.ai.chat.client.advisor.PromptChatMemoryAdvisor;
|
||||
import org.springframework.ai.chat.memory.ChatMemory;
|
||||
import org.springframework.ai.chat.model.ChatModel;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
/**
|
||||
|
|
@ -10,4 +18,34 @@ import org.springframework.boot.test.context.SpringBootTest;
|
|||
public class ChatModelTest {
|
||||
|
||||
|
||||
private ChatClient chatClient;
|
||||
|
||||
@BeforeEach
|
||||
public void init(@Autowired
|
||||
ChatModel ollamaChatModel,
|
||||
@Autowired ChatMemory chatMemory) {
|
||||
chatClient = ChatClient
|
||||
.builder(ollamaChatModel)
|
||||
.defaultAdvisors(
|
||||
PromptChatMemoryAdvisor.builder(chatMemory).build()
|
||||
)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String content = chatClient.prompt()
|
||||
.user("我叫邹志文?")
|
||||
.call()
|
||||
.content();
|
||||
System.out.println(content);
|
||||
System.out.println("--------------------------------------------------------------------------");
|
||||
|
||||
content = chatClient.prompt()
|
||||
.user("我叫什么 ?")
|
||||
.call()
|
||||
.content();
|
||||
System.out.println(content);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue