liang-ai/src/main/java/com/ai/controller/LangChainController.java

63 lines
2.2 KiB
Java
Raw Normal View History

package com.ai.controller;
2025-03-04 12:09:16 +00:00
import com.ai.service.Assist;
import com.ai.service.LangChainService;
2025-03-29 14:18:18 +00:00
import dev.langchain4j.community.model.dashscope.QwenChatModel;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.service.AiServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/langchain")
public class LangChainController {
@Autowired
private LangChainService langChainService;
2025-03-04 12:09:16 +00:00
@Autowired
private Assist assist;
/**
* 处理用户输入并调用 LangChainService 获取响应
* @param input 用户输入的内容
* @return 大语言模型生成的响应
*/
@GetMapping("/chat")
public String chat(@RequestParam("input") String input) {
System.out.println("start chat...");
return langChainService.getResponse(input);
}
2025-03-04 12:09:16 +00:00
@GetMapping("/high/chat")
public String highChat(@RequestParam("input") String input) {
System.out.println("start highlevel chat...");
return assist.chat(input);
}
2025-03-29 14:18:18 +00:00
// ChatLanguageModel qwenModel = QwenChatModel.builder()
// .apiKey("sk-2f703a41fff0488e9b6888013d2ee58a")
// .modelName("deepseek-v3")
// .build();
//
// ChatMemory chatMemory = MessageWindowChatMemory.builder()
// .maxMessages(10)
// .build();
//
// Assist assist = AiServices.builder(Assist.class)
// .chatLanguageModel(qwenModel) // the model
// .chatMemoryProvider(memoryId -> MessageWindowChatMemory.withMaxMessages(10)) // memory
// .build();
@GetMapping("/high/memory-chat")
public String memoryChat(@RequestParam("memoryId") String memoryId, @RequestParam("input") String input) {
System.out.println("start highlevel memory chat...");
return assist.memoryChat(memoryId, input);
}
}