之前的文章中,我们的案例使用的是LangChian4j中提供的Low Level API,这种级别的API使用灵活,但是各种对象之间关联起来相对比较麻烦。
LangChain4j中还提供了High Level API,AI Services就属于High Level API。AI Services通过实现对自定义接口的代理对象,内部隐藏了诸多实现细节,可以使开发者更多的关注业务细节。AI Service提供了对格式化输入和输出转换的常规操作,而且还支持Chat Memory、工具调用、RAG等。
注意:AI Services不支持多模态模型
我们先看一个AIService使用的基本案例。
基本使用
自定义一个接口,提供一个聊天时需要调用的方法。该方法的参数,表示用户发送的问题,返回值表示ai返回的回答。
package com.renr.langchain4jnew.app2;
interface Assistant {
String chat(String userMessage);
}
通过AiServices.create()方法关联大模型对象,并返回Assistant的代理对象。
package com.renr.langchain4jnew.app2;
import com.renr.langchain4jnew.constant.CommonConstants;
import dev.langchain4j.community.model.zhipu.ZhipuAiChatModel;
import dev.langchain4j.service.AiServices;
import java.time.Duration;
/**
* @Title: App1
* @Author 老任与码
* @Date 2025-04-12 9:25
*/
public class App1 {
public static void main(String[] args) {
// 创建智谱的模型对象
ZhipuAiChatModel zhipuAiChatModel = ZhipuAiChatModel.builder()
// 模型key
.apiKey(CommonConstants.API_KEY)
// 精确度
.temperature(0.9)
.model("GLM-4-Flash")
.maxRetries(3)
.callTimeout(Duration.ofSeconds(60))
.connectTimeout(Duration.ofSeconds(60))
.writeTimeout(Duration.ofSeconds(60))
.readTimeout(Duration.ofSeconds(60))
.build();
// 创建代理对象
Assistant assistant = AiServices.create(Assistant.class, zhipuAiChatModel);
// 发送消息
String info = assistant.chat("你好");
System.out.println(info);
}
}
执行程序的输出如下:

通过打断点,我们可以看到assistant是代理对象,而且使用的是jdk代理

查看源码
那么LangChain4j内部是如何创建的代理对象呢?

AiServices类中的create方法先调用builder()方法

builder()方法中返回DefaultAiServices对象
然后DefaultAiServices对象调用chatLanguageModel方法,再调用build()方法

build()方法中通过Proxy.newProxyInstance()创建代理对象

@SystemMessage
大模型的API中一般提供有SystemMessage作为系统消息,系统消息一般用于给AI立人设。
关于大模型支持的消息类型,可以参考:LangChain4j(1)——基本使用-优快云博客
使用Low Level API时,我们是如何发送SystemMessage呢?

具体使用可以参考:LangChain4j(3)——调用DeepSeek_langchain4j支持deepseek吗-优快云博客
从上面的代码,可以看出,使用SystemMessage时,相对比较麻烦。如果使用AI Services,只需要一个@SystemMessage注解即可搞定。我们需要修改之前的Assitant接口:
package com.renr.langchain4jnew.app2;
import dev.langchain4j.service.SystemMessage;
interface Assistant {
String chat(String userMessage);
@SystemMessage("你是一个著名的诗人")
String chat2(String userMessage);
}
代码中调用chat2方法
package com.renr.langchain4jnew.app2;
import com.renr.langchain4jnew.constant.CommonConstants;
import dev.langchain4j.community.model.zhipu.ZhipuAiChatModel;
import dev.langchain4j.service.AiServices;
import java.time.Duration;
/**
* @Title: App1
* @Author 老任与码
* @Date 2025-04-12 9:25
*/
public class App2 {
public static void main(String[] args) {
// 创建智谱的模型对象
ZhipuAiChatModel zhipuAiChatModel = ZhipuAiChatModel.builder()
// 模型key
.apiKey(CommonConstants.API_KEY)
// 精确度
.temperature(0.9)
.model("GLM-4-Flash")
.maxRetries(3)
.callTimeout(Duration.ofSeconds(60))
.connectTimeout(Duration.ofSeconds(60))
.writeTimeout(Duration.ofSeconds(60))
.readTimeout(Duration.ofSeconds(60))
.logRequests(true)
.logResponses(true)
.build();
// 创建代理对象
Assistant assistant = AiServices.create(Assistant.class, zhipuAiChatModel);
// 发送消息
String info = assistant.chat2("请写一首和清明相关的五言绝句");
System.out.println(info);
}
}
查看日志的输出,可以看到发送了SystemMessage

怎么样?比之前的Low Level API简单吧!
@UserMessage
如果大模型不支持SystemMessage,而我们又想给大模型能体现出类似的功能,这需要怎么办呢?
我们可以使用@UserMessage修饰接口中方法,在该注解中,给大模型设置人设,发送用户消息时,除了人设的内容,还会绑定用户的提问信息。具体使用如下:
package com.renr.langchain4jnew.app2;
import dev.langchain4j.service.SystemMessage;
import dev.langchain4j.service.UserMessage;
interface Assistant {
String chat(String userMessage);
@SystemMessage("你是一个著名的诗人")
String chat2(String userMessage);
@UserMessage("你是一个著名的诗人,请回答如下问题:{{it}}")
String chat3(String userMessage);
}
{{it}}可以理解为占位符,用于替换我们提出的具体问题
package com.renr.langchain4jnew.app2;
import com.renr.langchain4jnew.constant.CommonConstants;
import dev.langchain4j.community.model.zhipu.ZhipuAiChatModel;
import dev.langchain4j.service.AiServices;
import java.time.Duration;
/**
* @Title: App1
* @Author 老任与码
* @Date 2025-04-12 9:25
*/
public class App2 {
public static void main(String[] args) {
// 创建智谱的模型对象
ZhipuAiChatModel zhipuAiChatModel = ZhipuAiChatModel.builder()
// 模型key
.apiKey(CommonConstants.API_KEY)
// 精确度
.temperature(0.9)
.model("GLM-4-Flash")
.maxRetries(3)
.callTimeout(Duration.ofSeconds(60))
.connectTimeout(Duration.ofSeconds(60))
.writeTimeout(Duration.ofSeconds(60))
.readTimeout(Duration.ofSeconds(60))
.logRequests(true)
.logResponses(true)
.build();
// 创建代理对象
Assistant assistant = AiServices.create(Assistant.class, zhipuAiChatModel);
// 发送消息
// String info = assistant.chat2("请写一首和清明相关的五言绝句");
String info = assistant.chat3("请写一首和清明相关的五言绝句");
System.out.println(info);
}
}
请求日志如下:

{{it}}是LangChain4j提供的语法,如果想使用自定义的名称参数,我们可以使用@V注解。Assistant中增加如下接口:
@UserMessage("你是一个著名的诗人,请回答如下问题:{{info}}")
String chat4(@V("info") String userMessage);
发送用户消息时代码如下:
String info = assistant.chat4("请写一首和清明相关的五言绝句");
LangChain4j之AI Services基本使用
2155

被折叠的 条评论
为什么被折叠?



