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

相关推荐
庞轩px6 小时前
第七篇:Spring扩展点——如何优雅地介入Bean的创建流程
java·后端·spring·bean·aware·扩展点
ltl6 小时前
Q/K/V 三件套:把 Bahdanau 抽象成一个公式
后端
tongluowan0077 小时前
一个请求在Spring MVC 中是怎么流转的
java·spring·mvc
千叶风行8 小时前
Text-to-SQL 技术设计与注意事项
前端·人工智能·后端
夜郎king8 小时前
Spring AI 对接大模型开发易错点总结与实战解决办法
java·人工智能·spring
oradh8 小时前
Oracle数据库中的Java概述
java·数据库·oracle·sql基础·oracle数据库java概述
组合缺一8 小时前
Java AI 框架三国杀:Solon AI vs Spring AI vs LangChain4j 深度对比
java·人工智能·spring·ai·langchain·llm·solon
阿kun要赚马内8 小时前
后端数据操作组合:Pydantic与ORM
后端·python·orm·sqlalchemy
c++之路8 小时前
适配器模式(Adapter Pattern)
java·算法·适配器模式
吴声子夜歌9 小时前
Java——接口的细节
java·开发语言·算法