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

相关推荐
咸鱼2.06 小时前
【java入门到放弃】跨域
java·开发语言
indexsunny7 小时前
互联网大厂Java求职面试实战:微服务与Spring生态全攻略
java·数据库·spring boot·安全·微服务·面试·消息队列
沐苏瑶7 小时前
Java 搜索型数据结构全解:二叉搜索树、Map/Set 体系与哈希表
java·数据结构·算法
冬夜戏雪7 小时前
实习面经记录(十)
java·前端·javascript
skiy7 小时前
java与mysql连接 使用mysql-connector-java连接msql
java·开发语言·mysql
平生不喜凡桃李7 小时前
浅谈 Linux 中 namespace 相关系统调用
java·linux·服务器
zb200641207 小时前
CVE-2024-38819:Spring 框架路径遍历 PoC 漏洞复现
java·后端·spring
uzong8 小时前
AI Agent 是什么,如何理解它,未来挑战和思考
人工智能·后端·架构
2401_895521348 小时前
spring-ai 下载不了依赖spring-ai-openai-spring-boot-starter
java·人工智能·spring
追逐时光者8 小时前
DotNetGuide突破了10K + Star,一份全面且免费的C#/.NET/.NET Core学习、工作、面试指南知识库!
后端·.net