文章目录
一. AI service介绍
AI Service提供了比chain更加简单、灵活的能力
AI Services provide a simpler and more flexible alternative to chains.
langchain4j在# Introduction 也介绍了:
Chains:为每个常见用例建立一个链,例如聊天机器人、RAG 等。链组合了多个低级组件并协调它们之间的交互。它们的主要问题是,如果您需要定制某些东西,它们就太僵化了。 LangChain4j 仅实现了两个 Chain(ConversationalChain 和 ConversationalRetrievalChain)。
推荐使用AiService,而且langchain4j也不会开发更多的链
Represents a chain step that takes an input and produces an output. Chains are not going to be developed further, it is recommended to use AiServices instead.
1. 如何工作的
AiServices提供了调用大模型的能力:将用户输入的信息转换为ChatMessage
[!NOTE]
用户定义接口,AiServices转换这个接口
You provide theClass
of your interface toAiServices
along with the low-level components, andAiServices
creates a proxy object implementing this interface. Currently, it uses reflection, but we are considering alternatives as well. This proxy object handles all the conversions for inputs and outputs. In this case, the input is a singleString
, but we are using aChatLanguageModel
which takesChatMessage
as input.将用户的消息转换为UserMessage并调用的ChatLanguageModel
So,AiService
will automatically convert it into aUserMessage
and invokeChatLanguageModel
. Since the output type of thechat
method is aString
, afterChatLanguageModel
returnsAiMessage
, it will be converted into aString
before being returned from thechat
method.
2. AiServices提供的能力
参考代码:dev.langchain4j.service.AiServices
目前AiServices提供了以下能力:
message相关
- message templates:Static system message templates, configured via @SystemMessage annotation on top of the method
- system message templates:Dynamic system message templates, configured via systemMessageProvider(Function)
- user message templates:Static user message templates, configured via @UserMessage annotation on top of the method
- Dynamic user message templates, configured via method parameter annotated with @UserMessage
ChatMemory相关
- Single (shared) ChatMemory, configured via chatMemory(ChatMemory)
- Separate (per-user) ChatMemory, configured via chatMemoryProvider(ChatMemoryProvider) and a method parameter annotated with @MemoryId
RAG的能力
- RAG, configured via contentRetriever(ContentRetriever) or retrievalAugmentor(RetrievalAugmentor)
相关工具
- Tools, configured via tools(List) or tools(Object…) and methods annotated with @Tool
输出结构
- Various method return types (output parsers), see more details below
返回流
- Streaming (use TokenStream as a return type)
StructuredPrompt:ing结构化提示作为方法参数?
- Structured prompts as method arguments (see @StructuredPrompt)
自动审核
- Auto-moderation, configured via @Moderate annotation
注意暂不支持多模态
AI services currently do not support multimodality, please use the low-level API for this.
3. 支持的返回形式
定义的接口返回形式是灵活的,可按需选择
- 直接回答:如果你想直接获取模型生成的文本,可以使用
String
或AiMessage
。这对于简单的对话或问题回答非常有用。 - 集合类型:如果你希望模型将回答作为一个有序集合返回,比如以列表或项目符号的形式列出多个项,可以使用
List<String>
或Set<String>
。 - 分类任务:如果你希望模型执行分类任务(比如判断文本的类别),你可以使用枚举类型(Enum)或布尔值(boolean)。这意味着模型会返回一个有限的类别集合或一个简单的真/假值。
- 数据提取:当你希望从模型的回答中提取特定数据时,可以使用基本类型(如
int
、Double
等)或者 Java 的日期、时间和数字类型(如Date
、LocalDateTime
、BigDecimal
等)。例如,你可以从模型的回答中提取一个特定的数字或者日期。 - 自定义对象<