如何使用LangChain4j框架创建和使用多种AI服务。它通过定义接口和注解,将自然语言处理任务(如情感分析、数字提取、日期提取、POJO提取等)封装为服务,并通过LangChain4j的AiServices动态生成这些服务的实现。
本章主要讲述基于LangChain调用大模型如何进行结构化输出的真实示例,一一列举,本章主要介绍定义AI的角色和用户消息模版示例
整体代码结果说明
带系统消息和用户消息的AI服务(TextUtils)是LangChain4j框架中一个典型的AI服务实现,它展示了如何通过定义接口和注解,同时结合系统消息(@SystemMessage)和用户消息(@UserMessage),来封装自然语言处理任务。这种服务模式不仅能够指导AI的行为风格,还能灵活地处理用户输入并生成相应的输出。以下是带系统消息和用户消息的AI服务的技术实现总结与解析:
代码定义了多个静态内部类,每个类都展示了LangChain4j中不同类型的AI服务示例。这些服务通过接口和注解定义,并通过AiServices.create()方法动态生成实现。每个类都包含一个main方法,用于演示如何调用这些服务。
带系统消息和用户消息的AI服务(TextUtils)
1. 技术实现
1.1 接口定义
定义了一个接口TextUtils,用于封装文本处理功能。接口中包含两个方法:
- translate(String text, String language):将文本翻译为目标语言。
- summarize(String text, int n):将文本总结为n个要点。
interface TextUtils {
@SystemMessage("You are a professional translator into {{language}}")
@UserMessage("Translate the following text: {{text}}")
String translate(@V("text") String text, @V("language") String language);
@SystemMessage("Summarize every message from user in {{n}} bullet points. Provide only bullet points.")
List<String> summarize(@UserMessage String text, @V("n") int n);
}
解析:
方法定义:每个方法都通过@UserMessage注解定义了用户消息模板,{{text}}和{{language}}会被替换为方法参数。这使得AI能够理解用户的意图,并生成相应的响应。
系统消息:通过@SystemMessage注解定义了系统消息,用于设置AI的行为风格。例如,translate方法的系统消息指定了AI作为专业翻译的角色,而summarize方法的系统消息指定了AI总结文本的格式。
变量注解:使用**@V**注解定义了变量,用于在注解中传递参数。
1.2 动态生成服务实现
通过AiServices.create()方法,LangChain4j框架动态生成了TextUtils接口的实现。这意味着开发者不需要手动实现接口方法,而是由框架根据接口定义和注解自动生成实现逻辑。
TextUtils utils = AiServices.create(TextUtils.class, chatLanguageModel);
解析:
动态生成:LangChain4j框架根据接口定义和注解自动生成服务实现,减少了手动编码的工作量。
灵活性:通过注解定义用户消息模板和系统消息,可以灵活地调整AI的输入和输出格式,适应不同的业务需求。
1.3 调用服务
在main方法中,通过调用TextUtils的两个方法,展示了如何使用该服务:
调用translate(String text, String language)方法将文本翻译为目标语言。
调用summarize(String text, int n)方法将文本总结为n个要点。
String translation = utils.translate("Hello, how are you?", "italian");
System.out.println(translation); // 输出:Ciao, come stai?
String text = "AI, or artificial intelligence, is a branch of computer science that aims to create " +
"machines that mimic human intelligence. This can range from simple tasks such as recognizing " +
"patterns or speech to more complex tasks like making decisions or predictions.";
List<String> bulletPoints = utils.summarize(text, 3);
System.out.println(bulletPoints);
// 输出:
// [
// "- AI is a branch of computer science",
// "- It aims to create machines that mimic human intelligence",
// "- It can perform simple or complex tasks"
// ]
解析:
文本输入:输入的文本中包含要处理的内容,例如要翻译的文本或要总结的段落。
提取结果:AI能够根据系统消息和用户消息的定义,生成相应的翻译或总结内容。
2. 技术优势
封装性:通过接口和注解,将文本处理功能封装为一个服务,使得代码更加模块化,易于维护和扩展。
动态性:利用LangChain4j框架的动态生成能力,自动实现接口方法,减少了手动编码的工作量。
灵活性:通过注解定义用户消息模板和系统消息,可以灵活地调整AI的输入和输出格式,适应不同的业务需求。
可扩展性:可以轻松添加更多类型的文本处理功能或扩展到其他自然语言处理任务。
类型安全:支持多种返回类型,确保提取的信息可以无缝地转换为所需的类型,避免类型转换错误。
系统消息指导:通过@SystemMessage注解,可以明确指导AI的行为风格,提高生成内容的质量和一致性。
3. 应用场景
翻译服务:将文本从一种语言翻译为另一种语言,适用于多语言支持的应用。
内容总结:将长文本总结为要点,适用于新闻摘要、报告总结等场景。
自然语言理解:在聊天机器人或语音助手应用中,处理用户输入并生成相应的响应。
内容管理系统:从文章或报告中提取关键信息,用于内容管理和分析。
大家如果有安装飞书桌面版,可以结合本章想想,他是不是基于本章的技术实现的
完整代码
代码演示
public class OtherServiceExamples {
// 使用OpenAI的API密钥初始化ChatLanguageModel
static ChatLanguageModel chatLanguageModel = OpenAiChatModel
/**
* 带系统消息和用户消息的AI服务示例
*/
static class AI_Service_with_System_and_User_Messages_Example {
// 定义TextUtils接口
interface TextUtils {
// 使用@SystemMessage和@UserMessage注解定义系统消息和用户消息
@SystemMessage("You are a professional translator into {{language}}")
@UserMessage("Translate the following text: {{text}}")
String translate(@V("text") String text, @V("language") String language);
// 使用@SystemMessage注解定义系统消息
@SystemMessage("Summarize every message from user in {{n}} bullet points. Provide only bullet points.")
List<String> summarize(@UserMessage String text, @V("n") int n);
}
public static void main(String[] args) {
// 动态生成TextUtils服务的实现
TextUtils utils = AiServices.create(TextUtils.class, chatLanguageModel);
// 翻译文本
String translation = utils.translate("Hello, how are you?", "italian");
System.out.println(translation); // 输出:Ciao, come stai?
// 总结文本
String text = "AI, or artificial intelligence, is a branch of computer science that aims to create " +
"machines that mimic human intelligence. This can range from simple tasks such as recognizing " +
"patterns or speech to more complex tasks like making decisions or predictions.";
List<String> bulletPoints = utils.summarize(text, 3);
System.out.println(bulletPoints);
// 输出:
// [
// "- AI is a branch of computer science",
// "- It aims to create machines that mimic human intelligence",
// "- It can perform simple or complex tasks"
// ]
}
}
}
代码解读
功能:定义了一个AI服务,用于翻译文本和总结文本。
实现:
定义了一个TextUtils接口,包含两个方法:
- translate(String text, String language):翻译文本。
- summarize(String text, int n):总结文本为n个要点。
使用@SystemMessage和@UserMessage注解定义了系统消息和用户消息。
使用@V注解定义了变量。
通过AiServices.create()动态生成TextUtils的实现。
调用:
将文本“Hello, how are you?”翻译为意大利语。
将一段关于AI的文本总结为3个要点。
总结
带系统消息和用户消息的AI服务(TextUtils)通过定义接口、使用注解和动态生成服务实现,展示了LangChain4j框架的强大功能。这种实现方式不仅简化了开发流程,还提高了代码的可维护性和可扩展性。通过@UserMessage注解,AI能够理解用户意图并生成相应的响应,而@SystemMessage注解则提供了明确的指导,帮助AI更好地完成任务。这种模式可以广泛应用于其他自然语言处理任务,为开发者提供了一种高效、灵活的解决方案。
导读说明:
这是LangChain开发智能体的系列文档,欢迎连读
- 第1章:LangChain4j的聊天与语言模型
- 第2章:如何基于LangChain4j实现聊天记忆
- 第3章:在LangChain中如何设置模型参数
- 第4章:在LangChain中如何实现响应式流(Response Streaming)
- 第5章:在LangChain中如何使用AI Services
- 第6章:基于LangChain如何开发Agents,附带客户支持智能体示例
- 第7章:在LangChain中如何调用函数Tools (Function Calling)
- 第8章:LangChain检索增强生成RAG–1概述
- 第8章:LangChain检索增强生成RAG–2.1Easy RAG实现
- 第8章:LangChain检索增强生成RAG–2.2Core RAG APIs
- 第8章:LangChain检索增强生成RAG–2.3Naive RAG
- 第8章:LangChain检索增强生成RAG–2.4Advanced RAG【高级RAG】
- 第9章:LangChain让大模型结构化输出
- 第9章:LangChain结构化输出-示例1(情感分析AI服务)
- 第9章:LangChain结构化输出-示例2(数字提取服务)
- 第9章:LangChain结构化输出-示例3(日期和时间提取服务)
- 第9章:LangChain结构化输出-示例4(基于大模型从自然语言中提取POJO)
- 第9章:LangChain结构化输出-示例5(基于大模型如何精确匹配POJO的字段)
- 第9章:LangChain结构化输出-示例6(设置系统消息和用户消息模版)
- 第10章:基于LangChain的综合实战[开发企业助手,查知识、查订单、表单登记…]