LangChain4j 是一款专为 Java 开发者设计的框架,旨在简化与 LLM 的集成过程,使开发者能够更加专注于业务逻辑而非底层的技术细节。LangChain4j 是 LangChain 的一个 Java 封装器,它将 LangChain 应用于 Java 环境中,利用 Java 强大的功能和丰富的库来处理数据,并提供了与多种流行的大语言模型无缝对接的能力。通过 LangChain4j,Java 开发者可以轻松地将最先进的 AI 技术融入到他们的应用程序中,无论是构建聊天机器人、还是智能客服系统,都能得心应手。
关于如何集成 AI 大模型到现有的 Java 应用程序中,LangChain4j 提供了两种主要的方法:一种是直接调用每种大模型提供的 API 接口,这种方式需要开发者熟悉不同模型的具体接口规范,并根据需求编写相应的代码来实现交互;另一种则是通过 Spring Boot 的自动装配机制,利用 LangChain4j 提供的 starter 依赖,自动化配置所需的组件和服务,极大地减少了手动设置的工作量 。在这篇文章中,我们将重点介绍 LangChain4j对接AI大模型的两种方式。
一、前提条件
- JDK为17以上版本,本人使用的jdk21版本;
- SpringBoot版本为3.x以上,本项目使用的是SpringBoot 3.4.0版本;
- 本文采用了阿里巴巴的Qwen大模型进行实验与验证,但您同样可以选择使用DeepSeek大模型作为替代方案。若选用阿里巴巴的AI服务,则需首先在阿里云平台上开通相应的大型模型服务,并获取所需的API密钥,以便在后续代码中调用。具体的开通与配置步骤,请参阅阿里云大模型服务平台“百炼”的相关文档和指南如何获取API Key_大模型服务平台百炼(Model Studio)-阿里云帮助中心。这样可以确保您能够顺利地集成和使用这些先进的AI资源。
二、方式一:手动编码方式对接大模型
1、添加maven依赖
本示例调用阿里巴巴大模型服务,所以引入langchain4j-community-dashscope-spring-boot-starter
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.4.0</version>
</parent>
<groupId>com.yuncheng</groupId>
<artifactId>langchain4j-rag-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<langchain4j.version>1.0.0-beta1</langchain4j.version>
</properties>
<dependencies>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-community-dashscope-spring-boot-starter</artifactId>
<version>${langchain4j.version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
2、手动编码调用大模型服务
手动编码创建ChatLanguageModel对象,实现跟大模型对话。由于本示例使用的是阿里云Qwen大模型,所以使用QwenChatModel类构建,如果你使用的是其他厂商的大模型,那么需要调用对应的SDK类进行创建。比如:百度千帆大模型使用QianfanChatModel、本地部署的大模型使用OllamaChatModel、OpenAI标准大模型使用OpenAiChatModel等。
import dev.langchain4j.community.model.dashscope.QwenChatModel;
import dev.langchain4j.model.chat.ChatLanguageModel;
public class HelloWorld {
public static void main(String[] args) {
ChatLanguageModel model = QwenChatModel.builder()
.apiKey("sk-4d6e6db446014411ad92c0e8fa143964")
.modelName("qwen-plus")
//以下的参数,根据具体业务需求设置
//.temperature(0.5F)
//.topK(2)
//.topP(0.7)
.build();
String answer = model.chat("请给我讲一个笑话");
System.out.println(answer);
}
}
三、方式二:spring自动装配对接大模型
1、添加maven依赖
本示例调用阿里巴巴大模型服务,所以引入langchain4j-community-dashscope-spring-boot-starter,本示例是一个springboot工程,还需要引入spring-boot-starter-webflux。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.4.0</version>
</parent>
<groupId>com.yuncheng</groupId>
<artifactId>langchain4j-rag-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<langchain4j.version>1.0.0-beta1</langchain4j.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-community-dashscope-spring-boot-starter</artifactId>
<version>${langchain4j.version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
2、调用大模型服务
2.1、配置yml文件
server:
port: 8099
langchain4j:
community:
dashscope:
chat-model:
model-name: qwen-plus
api-key: sk-4d6e6db446014411ad92c0e8fa143964
logging:
level:
org.springframework.boot.autoconfigure: DEBUG
注意:如果你不知道yml文件里的装配参数怎么写,可以把springboot自动装配参数日志打开,查看具体有哪些参数可配置使用。在yml文件配置org.springframework.boot.autoconfigure: DEBUG即可。
2.1、springboot调用大模型服务
由于springboot自动装配过程中,已经生成了ChatLanguageModel对象,所以无需手动构建了,在需要跟大模型对话的地方直接使用ChatLanguageModel即可。
import dev.langchain4j.model.chat.ChatLanguageModel;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ChatController {
ChatLanguageModel chatLanguageModel;
ChatController(ChatLanguageModel chatLanguageModel) {
this.chatLanguageModel = chatLanguageModel;
}
@GetMapping("/hello")
public String hello(@RequestParam(value = "message", defaultValue = "请给我讲一个笑话") String message) {
return chatLanguageModel.chat(message);
}
}
再创建一个springboot启动类即可:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
@SpringBootApplication
public class Langchain4jApplication {
private static final Logger log = LoggerFactory.getLogger(Langchain4jApplication.class);
public static void main(String[] args) {
ConfigurableApplicationContext application = SpringApplication.run(Langchain4jApplication.class, args);
Environment env = application.getEnvironment();
String port = env.getProperty("server.port");
log.info("Application启动成功,服务端口为:" + port);
}
}
启动Langchain4jApplication后,浏览器里直接输入http://localhost:8099/hello,测试调用大模型效果。
四、关于ChatLanguageModel和StreamingChatLanguageModel区别
我们在验证langchain4j框架的过程中,发现有两类对话模型的接口,分别是ChatLanguageModel和StreamingChatLanguageModel,这跟Spring AI框架的设计思想是有区别的,在SpringAI中,是通过一个统一的对象ChatClient
进行参数化构建的。那么,langchain4j中为什么会区分ChatLanguageModel和StreamingChatLanguageModel呢?
1、ChatLanguageModel 和 StreamingChatLanguageModel 都是 LangChain4j 库中用于与大语言模型(LLM)交互的接口,但它们在实现和使用场景上有显著的区别。
2、ChatLanguageModel 是一个同步接口,它接受一个或多个 ChatMessage 对象作为输入,并返回一个包含 AI 响应的 AiMessage 对象。这意味着整个响应是在一次调用中生成并返回的。这种方法适用于那些不需要实时反馈的应用场景,或者当您希望一次性接收完整响应时非常有用。
3、StreamingChatLanguageModel 提供了流式处理的能力。它允许逐个 token 地接收来自 LLM 的响应,这使得它可以像打字机一样逐步展示文本给用户,从而提供更好的用户体验。这对于聊天应用或其他需要即时反馈的场景尤为重要。
4、设计原则:保持接口职责单一是一个良好的设计实践。每个接口专注于一种特定的行为模式,有助于代码的可维护性和扩展性。例如,如果您需要修改流式处理逻辑,只需关注 StreamingChatLanguageModel 相关的部分,而不影响同步处理逻辑。