类 BaseChatModel<T extends ChatConfig>
java.lang.Object
com.agentsflex.core.model.chat.BaseChatModel<T>
- 类型参数:
T- 具体的配置类型,必须是ChatConfig的子类
- 所有已实现的接口:
ChatModel
- 直接已知子类:
OpenAICompatibleChatModel
支持责任链、统一上下文和协议客户端的聊天模型基类。
该类为所有具体的 LLM 实现(如 OpenAI、Qwen、Ollama)提供统一入口,并集成:
- 责任链模式:通过
ChatInterceptor实现请求拦截、监控、日志等横切逻辑 - 线程上下文管理:通过
ChatContextHolder在整个调用链中传递上下文信息 - 协议执行抽象:通过
ChatClient解耦协议细节,支持 HTTP/gRPC/WebSocket 等 - 可观测性:自动集成 OpenTelemetry(通过
ChatObservabilityInterceptor)
架构流程
- 调用
chat(Prompt, ChatOptions)或chatStream(Prompt, StreamResponseListener, ChatOptions) - 构建请求上下文(URL/Headers/Body)并初始化
ChatContext - 构建责任链:可观测性拦截器 → 全局拦截器 → 用户拦截器
- 责任链执行:每个拦截器可修改
ChatContext,最后由ChatClient执行实际调用 - 结果返回给调用方
-
字段概要
字段修饰符和类型字段说明protected ChatClientprotected ChatRequestSpecBuilderprotected final T聊天模型配置,包含 API Key、Endpoint、Model 等信息private final List<ChatInterceptor>拦截器链,按执行顺序存储(可观测性 → 全局 → 用户) -
构造器概要
构造器构造器说明BaseChatModel(T config) 构造一个聊天模型实例,不使用实例级拦截器。BaseChatModel(T config, List<ChatInterceptor> userInterceptors) 构造一个聊天模型实例,并指定实例级拦截器。 -
方法概要
修饰符和类型方法说明voidaddInterceptor(int index, ChatInterceptor interceptor) voidaddInterceptor(ChatInterceptor interceptor) 动态添加拦截器。private List<ChatInterceptor>buildInterceptorChain(List<ChatInterceptor> userInterceptors) 构建完整的拦截器链。private StreamChainbuildStreamChain(int index) 构建流式责任链。private SyncChainbuildSyncChain(int index) 构建同步责任链。chat(Prompt prompt, ChatOptions options) 执行同步聊天请求。voidchatStream(Prompt prompt, StreamResponseListener listener, ChatOptions options) 执行流式聊天请求。voidsetChatClient(ChatClient chatClient) voidsetChatRequestSpecBuilder(ChatRequestSpecBuilder chatRequestSpecBuilder) 从类继承的方法 java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait从接口继承的方法 com.agentsflex.core.model.chat.ChatModel
chat, chat, chat, chatStream, chatStream, chatStream
-
字段详细资料
-
config
聊天模型配置,包含 API Key、Endpoint、Model 等信息 -
chatClient
-
chatRequestSpecBuilder
-
interceptors
拦截器链,按执行顺序存储(可观测性 → 全局 → 用户)
-
-
构造器详细资料
-
BaseChatModel
构造一个聊天模型实例,不使用实例级拦截器。- 参数:
config- 聊天模型配置
-
BaseChatModel
构造一个聊天模型实例,并指定实例级拦截器。实例级拦截器会与全局拦截器(通过
GlobalChatInterceptors注册)合并, 执行顺序为:可观测性拦截器 → 全局拦截器 → 实例拦截器。- 参数:
config- 聊天模型配置userInterceptors- 实例级拦截器列表
-
-
方法详细资料
-
buildInterceptorChain
构建完整的拦截器链。执行顺序: 1. 可观测性拦截器(最外层,最早执行) 2. 全局拦截器(通过 GlobalChatInterceptors 注册) 3. 用户拦截器(实例级)
- 参数:
userInterceptors- 用户提供的拦截器列表- 返回:
- 按执行顺序排列的拦截器链
-
chat
执行同步聊天请求。流程: 1. 构建请求上下文(URL/Headers/Body) 2. 初始化线程上下文
ChatContext3. 构建并执行责任链 4. 返回 LLM 响应 -
chatStream
执行流式聊天请求。流程与同步请求类似,但返回结果通过回调方式分片返回。
- 指定者:
chatStream在接口中ChatModel- 参数:
prompt- 用户输入的提示listener- 流式响应监听器options- 聊天选项
-
buildSyncChain
构建同步责任链。递归构建拦截器链,链尾节点负责创建并调用
ChatClient。- 参数:
index- 当前拦截器索引- 返回:
- 同步责任链
-
buildStreamChain
构建流式责任链。与同步链类似,但支持流式监听器。
- 参数:
index- 当前拦截器索引- 返回:
- 流式责任链
-
getConfig
-
getChatClient
-
setChatClient
-
getChatRequestSpecBuilder
-
setChatRequestSpecBuilder
-
getInterceptors
-
addInterceptor
动态添加拦截器。新拦截器会被添加到链的末尾(在用户拦截器区域)。
- 参数:
interceptor- 要添加的拦截器
-
addInterceptor
-