Initial commit
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
package com.iwe3.langchain4j;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class MemoryApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(MemoryApplication.class,args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.iwe3.langchain4j.config;
|
||||
|
||||
import dev.langchain4j.model.chat.ChatModel;
|
||||
import dev.langchain4j.model.openai.OpenAiChatModel;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class LLMConfig {
|
||||
|
||||
/**
|
||||
* @Description: 普通对话接口 ChatModel
|
||||
*/
|
||||
@Bean(name = "qwen")
|
||||
public ChatModel chatModelQwen()
|
||||
{
|
||||
return OpenAiChatModel.builder()
|
||||
.apiKey(System.getenv("DASH_SCOPE_API_KEY"))
|
||||
.modelName("qwen-plus")
|
||||
.baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.iwe3.langchain4j.config;
|
||||
|
||||
import dev.langchain4j.memory.ChatMemory;
|
||||
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class MemoryChatAssistantConfig {
|
||||
|
||||
|
||||
|
||||
//@Bean -> 向IOC容器中注入 chatMemory 的实例
|
||||
@Bean
|
||||
ChatMemory windowChatMemory() {
|
||||
// 设置聊天记忆记录的message数量
|
||||
return MessageWindowChatMemory.withMaxMessages(10);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.iwe3.langchain4j.config;
|
||||
|
||||
import dev.langchain4j.memory.chat.ChatMemoryProvider;
|
||||
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
|
||||
import dev.langchain4j.store.memory.chat.InMemoryChatMemoryStore;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class SeparateChatAssistantConfig {
|
||||
|
||||
/**
|
||||
* 聊天记忆提供器
|
||||
* 配置:采用memoryId来完成隔离
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
ChatMemoryProvider chatMemoryProvider() {
|
||||
return memoryId -> MessageWindowChatMemory.builder()
|
||||
.id(memoryId)
|
||||
.maxMessages(10).chatMemoryStore(new InMemoryChatMemoryStore())
|
||||
.build();
|
||||
//如果未来想自定义 -> 则自己写一个类实现 ChatMemoryStore
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.iwe3.langchain4j.service;
|
||||
|
||||
import dev.langchain4j.service.spring.AiService;
|
||||
import dev.langchain4j.service.spring.AiServiceWiringMode;
|
||||
|
||||
/**
|
||||
* 知识出处:
|
||||
* https://docs.langchain4j.dev/tutorials/spring-boot-integration/#spring-boot-starter-for-declarative-ai-services
|
||||
* chatMemory = "chatMemory" 则表示:使用ChatMemory来完成聊天记忆
|
||||
*/
|
||||
@AiService(wiringMode = AiServiceWiringMode.EXPLICIT
|
||||
,chatModel = "qwen",chatMemory = "windowChatMemory")
|
||||
public interface ChatAssistant {
|
||||
/**
|
||||
* 普通聊天
|
||||
* @param prompt
|
||||
* @return
|
||||
*/
|
||||
String chat(String prompt);
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.iwe3.langchain4j.service;
|
||||
|
||||
import dev.langchain4j.service.MemoryId;
|
||||
import dev.langchain4j.service.UserMessage;
|
||||
import dev.langchain4j.service.spring.AiService;
|
||||
import dev.langchain4j.service.spring.AiServiceWiringMode;
|
||||
|
||||
@AiService(
|
||||
wiringMode = AiServiceWiringMode.EXPLICIT,
|
||||
chatModel = "qwen",
|
||||
chatMemoryProvider = "chatMemoryProvider"
|
||||
)
|
||||
public interface SeparateChatAssistant {
|
||||
|
||||
/**
|
||||
* 分离聊天记录
|
||||
* @param memoryId 聊天id
|
||||
* @param userMessage 用户消息
|
||||
* @return
|
||||
*/
|
||||
String chat(@MemoryId int memoryId, @UserMessage String userMessage);
|
||||
}
|
||||
10
langchain4j-ai-memory/src/main/resources/application.yml
Normal file
10
langchain4j-ai-memory/src/main/resources/application.yml
Normal file
@@ -0,0 +1,10 @@
|
||||
server:
|
||||
port: 9007
|
||||
servlet:
|
||||
encoding:
|
||||
charset: utf-8
|
||||
enabled: true
|
||||
force: true # 设置响应的字符编码,避免流式返回输出乱码
|
||||
spring:
|
||||
application:
|
||||
name: langchain4j-ai-memory
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.iwe3.langchain4j;
|
||||
|
||||
import com.iwe3.langchain4j.service.ChatAssistant;
|
||||
import dev.langchain4j.data.message.UserMessage;
|
||||
import dev.langchain4j.model.chat.ChatModel;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class MemoryApplicationTest {
|
||||
|
||||
@Resource(name="qwen")
|
||||
private ChatModel chatModel;
|
||||
|
||||
@Test
|
||||
public void testLowerLevel(){
|
||||
|
||||
// 第一轮对话
|
||||
var um01 = UserMessage.userMessage("我是蒲哥");
|
||||
var res01 = chatModel.chat(um01);
|
||||
var am01 = res01.aiMessage();
|
||||
// 输出大语言模型的回复
|
||||
System.out.println(am01.text());
|
||||
|
||||
// 第二轮对话
|
||||
var um02 = UserMessage.userMessage("你知道我是谁吗");
|
||||
//将第一轮对话记录,也一起发给 LLM
|
||||
var res02 = chatModel.chat(um02);
|
||||
|
||||
var am02 = res02.aiMessage();
|
||||
// 输出大语言模型的回复
|
||||
System.out.println(am02.text());
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Autowired
|
||||
private ChatAssistant chatAssistant;
|
||||
@Test
|
||||
public void testHighLevel(){
|
||||
var res = chatAssistant.chat("我是蒲哥");
|
||||
System.out.println("res = " + res);
|
||||
res = chatAssistant.chat("我是谁?");
|
||||
System.out.println("res = " + res);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.iwe3.langchain4j;
|
||||
|
||||
import com.iwe3.langchain4j.service.SeparateChatAssistant;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
public class SeparateChatTest {
|
||||
|
||||
@Autowired
|
||||
private SeparateChatAssistant separateChatAssistant;
|
||||
|
||||
@Test
|
||||
public void testSeparateChatMemory() {
|
||||
// 用户1的对话
|
||||
var res = separateChatAssistant.chat(1, "我是蒲哥");
|
||||
System.out.println(res);
|
||||
res = separateChatAssistant.chat(1, "你知道我是谁吗");
|
||||
System.out.println(res);
|
||||
|
||||
// 用户2的对话
|
||||
var res02 = separateChatAssistant.chat(2, "我是小明");
|
||||
System.out.println(res02);
|
||||
res02 = separateChatAssistant.chat(2, "我叫什么名字");
|
||||
System.out.println(res02);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user