diff --git a/src/main/java/com/huangge1199/ai/config/LangChainConfig.java b/src/main/java/com/huangge1199/ai/config/LangChainConfig.java new file mode 100644 index 0000000..68f1141 --- /dev/null +++ b/src/main/java/com/huangge1199/ai/config/LangChainConfig.java @@ -0,0 +1,26 @@ +package com.huangge1199.ai.config; + +import com.huangge1199.ai.service.LangChainService; +import dev.langchain4j.model.chat.ChatModel; +import dev.langchain4j.service.AiServices; +import jakarta.annotation.Resource; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * LangChainConfig + * + * @author huangge1199 + * @since 2025/7/12 10:32:01 + */ +@Configuration +public class LangChainConfig { + + @Resource + private ChatModel qwenChatModel; + + @Bean + public LangChainService langChainService() { + return AiServices.create(LangChainService.class, qwenChatModel); + } +} diff --git a/src/main/java/com/huangge1199/ai/controller/LangChainController.java b/src/main/java/com/huangge1199/ai/controller/LangChainController.java new file mode 100644 index 0000000..1e2bbd2 --- /dev/null +++ b/src/main/java/com/huangge1199/ai/controller/LangChainController.java @@ -0,0 +1,35 @@ +package com.huangge1199.ai.controller; + +import com.huangge1199.ai.common.R; +import com.huangge1199.ai.service.LangChainService; +import jakarta.annotation.Resource; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * langChain的AI服务 + * + * @author huangge1199 + * @since 2025/7/12 10:36:40 + */ +@RestController +@RequestMapping("/langChain") +public class LangChainController { + + @Resource + private LangChainService langChainService; + + /** + * AI对话 + * + * @param message 输入信息 + * @return ai返回结果 + */ + @PostMapping("/chat") + public R chat(@RequestBody String message) { + String result = langChainService.chat(message); + return R.ok(result); + } +} diff --git a/src/main/java/com/huangge1199/ai/service/LangChainService.java b/src/main/java/com/huangge1199/ai/service/LangChainService.java new file mode 100644 index 0000000..236e423 --- /dev/null +++ b/src/main/java/com/huangge1199/ai/service/LangChainService.java @@ -0,0 +1,15 @@ +package com.huangge1199.ai.service; + +import dev.langchain4j.service.SystemMessage; + +/** + * LangChainService + * + * @author huangge1199 + * @since 2025/7/12 10:25:27 + */ +public interface LangChainService { + + @SystemMessage(fromResource = "system-prompt.txt") + String chat(String message); +}