LangChain4j框架入门项目学习
1. 初始化项目
创建项目 使用Java21 JDK21(要使用LangChain4j JDK版本不能小于17)
使用local配置文件来防止推送到云端时信息泄露
创建application.yml和application-local.yml
将application-local.yml加到.gitignore中
并指定actice为local
application.yml:
spring: application: name: ai-code-helper profiles: active: local引入Spring Boot LangChain4j的依赖(Qwen)
这里使用和教程版本一样的1.1.0-beta7版本
<dependency> <groupId>dev.langchain4j</groupId> <artifactId>langchain4j-community-dashscope-spring-boot-starter</artifactId> <version>${latest version here}</version></dependency>这个是langchain4j + 阿里模型 + Spring boot的依赖包
配置大模型
两种方式
- 在yml文件中配置:(配置后springboot会自动创建自动注入)
langchain4j: community: dashscope: chat-model: api-key: {这里写api key} model-name: qwen-plus- 通过构造器链式调手动用来创建
ChatModel qwenModel = QwenChatModel.builder() .apiKey("You API key here") .modelName("qwen-plus") .enableSearch(true) .temperature(0.7) .maxTokens(4096) .stops(List.of("Hello")) .build();基本使用
简单对话
- 注入Ai服务(chatModel)
- 构建UserMessage 然后传给ChatModel
@Service@Slf4jpublic class AiCodeHelper {
// 注入ai模型的bean类 @Resource private ChatModel qwenChatModel;
// 基本的对话功能 public String chat(String message){ UserMessage userMessage = UserMessage.from(message); ChatResponse chatResponse = qwenChatModel.chat(userMessage); AiMessage aiMessage = chatResponse.aiMessage(); log.info("AI回复:"+aiMessage.toString()); return aiMessage.text(); }
}下面是测试类:
@SpringBootTestclass AiCodeHelperApplicationTests {
@Resource private AiCodeHelper aiCodeHelper;
@Test void chat() { aiCodeHelper.chat("你好 你是谁?"); }}在使用测试用例的时候出现了一个bug,单独运行一个测试用例会报错说没有发现单元测试,但是运行整个测试类时可以成功,发现使用了Spring Boot4以上的版本就会出现这个问题,为了兼容性先换成了4以下版本
多模态
UserMessage不止能传入文本信息
// 自定义构建userMessagepublic String chat(UserMessage userMessage){ ChatResponse chatResponse = qwenChatModel.chat(userMessage); AiMessage aiMessage = chatResponse.aiMessage(); log.info("AI回复:"+aiMessage.toString()); return aiMessage.text();}测试方法:
@Testvoid testChatUserMessage() { UserMessage userMessage = UserMessage.from( TextContent.from("请描述图片内容"), ImageContent.from("https://tuchuang-1353351309.cos.ap-guangzhou.myqcloud.com/picture/20260424210247435.png") ); aiCodeHelper.chat(userMessage);}测试的是下面这张图片:

但是由于这个框架对多模态兼容性并不是特别好,目前有很多问题 所以这里只是演示下代码,具体运行的话,由于qwen-plus不支持多模态 只会回复说目前读不了图片。
系统提示词
系统提示词是设置 AI 模型行为规则和角色定位的隐藏指令,用户通常不能直接看到。系统 Prompt 相当于给 AI 设定人格和能力边界,也就是告诉 AI “你是谁?你能做什么?”。
这里根据需求写一段系统提示词:
你是编程领域的小助手,帮助用户解答编程学习和求职面试相关的问题,并给出建议。重点关注 4 个方向:1. 规划清晰的编程学习路线2. 提供项目学习建议3. 给出程序员求职全流程指南(比如简历优化、投递技巧)4. 分享高频面试题和面试技巧请用简洁易懂的语言回答,助力用户高效学习与求职。接下来新建SystemMessage对象,然后传这个对象给chat方法即可。
AI 服务 - AI Service
在学习更多特性前,需要了解 LangChain4j 最重要的开发模式 —— AI Service,它提供了很多高层抽象的、用起来更方便的 API,可以把 AI 应用当做服务来开发。
使用 AI Service
首先引入 langchain4j 依赖:
<dependency> <groupId>dev.langchain4j</groupId> <artifactId>langchain4j</artifactId> <version>1.1.0</version></dependency>然后创建一个编程助手 AI Service 服务,采用声明式开发方法,编写一个对话方法,然后可以直接通过 @SystemMessage 注解定义系统提示词。
public interface AiCodeHelperService { @SystemMessage("你是一位编程小助手") String chat(String userMessage);}不过由于我们提示词较长,写到注解里很不优雅,所以单独在 resources 目录下新建文件 system-prompt.txt 来存储系统提示词。
@SystemMessage 注解支持从文件中读取系统提示词:
public interface AiCodeHelperService { @SystemMessage(fromResource = "system-prompt.txt") String chat(String userMessage);}然后我们需要编写工厂类,用于创建 AI Service:
@Configurationpublic class AiCodeHelperServiceFactory { @Resource private ChatModel qwenChatModel;
@Bean public AiCodeHelperService aiCodeHelperService() { return AiServices.create(AiCodeHelperService.class, qwenChatModel); }}调用 AiServices.create 方法就可以创建出 AI Service 的实现类了,背后的原理是利用 Java 反射机制创建了一个实现接口的代理对象,代理对象负责输入和输出的转换,比如把 String 类型的用户消息参数转为 UserMessage 类型并调用 ChatModel,再将 AI 返回的 AiMessage 类型转换为 String 类型作为返回值。
但我们不用关心这么多,直接写接口和注解来开发就好。
Ai服务测试
