LangChain4j 与 SpringBoot 整合

https://docs.langchain4j.dev/tutorials/spring-boot-integration

无论是导入 low 还是 high 级别的依赖,可公用 yaml:

yaml 复制代码
langchain4j:
  open-ai:
    chat-model:
      api-key: ${ALI_QWEN_API_KEY} # 将会自动从本地环境变量获取
      model-name: qwen-plus
      log-requests: true
      log-responses: true
      base-url: https://dashscope.aliyuncs.com/compatible-mode/v1

低阶(Low level)集成

Spring Boot starter依赖项的命名约定是:langchain4j-{integration-name}-spring-boot-starter

例如,对于 OpenAI(langchain4j-open-ai),依赖项名称将是langchain4j-open-ai-spring-boot-starter

xml 复制代码
<dependency>
    <groupId>dev.langchain4j</groupId>
    <artifactId>langchain4j-open-ai-spring-boot-starter</artifactId>
    <version>1.11.0-beta19</version>
</dependency>

controller

java 复制代码
@RestController
@RequestMapping("/low")
public class LowController {
    @Resource
    private ChatModel chatModelQwen;

    @GetMapping("/langchain4j/qwen")
    public String helloQwen(@RequestParam(value = "question", defaultValue = "你是谁?") String question) {
        String result = chatModelQwen.chat(question);
        return result;
    }
}

高阶(High level)集成

xml 复制代码
<dependency>
    <groupId>dev.langchain4j</groupId>
    <artifactId>langchain4j-spring-boot-starter</artifactId>
    <version>1.11.0-beta19</version>
</dependency>

定义 AI 服务接口,使用 @AiService 声明:

java 复制代码
@AiService
public interface Assistant {
    String chat(String question);
}

注意,如果同时存在多个 ChatModelBean,请手动指定 chatModel

否则,报错:

java 复制代码
dev.langchain4j.service.IllegalConfigurationException: Conflict: multiple beans of type dev.langchain4j.model.chat.ChatModel are found: [chatModelQwen, openAiChatModel]. Please specify which one you wish to wire in the @AiService annotation like this: @AiService(wiringMode = EXPLICIT, chatModel = "<beanName>").
java 复制代码
@AiService(
        wiringMode = AiServiceWiringMode.EXPLICIT,
        chatModel = "chatModelQwen"
)
public interface Assistant {
    String chat(String question);
}

将 AI 服务接口作为 Service 使用:

java 复制代码
@RestController
@RequestMapping("/high")
public class HighController {
    @Resource
    private Assistant assistant;

    @GetMapping("/langchain4j/qwen")
    public String helloQwen(@RequestParam(value = "question", defaultValue = "你是谁?") String question) {
        return assistant.chat(question);
    }
}

http://localhost:9001/high/langchain4j/qwen?question=xxx

优先发在:https://juejin.cn/post/7606173847994122294

相关推荐
014-code20 小时前
日志规范:怎么写才不算写废话
java·开发语言·设计模式·日志
3037820 小时前
消息推送削峰落地方案
后端
爱敲代码的小黄21 小时前
我重新梳理了一遍 RAG,终于明白它不只是接个向量库
后端·面试·agent
CQU_JIAKE21 小时前
4.17[Q]
java·linux·服务器
亦暖筑序21 小时前
Spring AI Alibaba 报错合集:我踩过的那些坑
java·后端
一只游鱼21 小时前
langchain4j+mysql+历史记录
mysql·langchain4j
石榴树下的七彩鱼21 小时前
OCR 识别不准确怎么办?模糊 / 倾斜 / 反光图片优化实战(附完整解决方案 + 代码示例)
图像处理·人工智能·后端·ocr·api·文字识别·图片识别
indexsunny1 天前
互联网大厂Java面试实战:核心技术与微服务架构在电商场景中的应用
java·spring boot·redis·kafka·maven·spring security·microservices
摇滚侠1 天前
Java 多线程基础 Java Multithreading Basics
java
今天你TLE了吗1 天前
LLM到Agent&RAG——AI概念概述 第一章:大模型
java·人工智能·语言模型·大模型