Solon v3.8.1

chat - 聊天模型的接口及字典

</> markdown
2026年1月2日 下午9:10:08

solon-ai 旨在提供一套通用的访问接口(通过方言适配)。可兼容各种不同的大模型接口。

1、聊天模型(ChatModel)相关的接口

接口描述
ChatModel聊天模型通用客户端(通过 ChatConfig 构建)
ChatConfig聊天模型配置
ChatDialect聊天方言(用于适配不同的 llm 接口规范)
ChatRequest聊天请求
ChatOptions聊天请求选项(单次请求时的选项)
ChatMessage聊天消息(分为:用户、系统、工具、助理四种消息)
ChatResponse聊天响应
FunctionTool聊天本地函数工具
ToolCall聊天本地工具调用(由AI发起)
ChatSession聊天会话(方便聊天历史记录管理)

2、四种聊天消息(ChatMessage)

消息描述
AssistantMessage助理消息(由 llm 生成的消息)
SystemMessage系统消息(为 llm 定义角色的初始化消息)
ToolMessage工具消息(本地函数工具生成的消息)
UserMessage用户消息(用户输入的消息)

3、主要接口字典

  • ChatModel
public class ChatModel implements AiModel {
    private final ChatConfig config;
    private final ChatDialect dialect;

    public ChatModel(Properties properties) {
        //支持直接注入
        this(Props.from(properties).bindTo(new ChatConfig()));
    }

    public ChatModel(ChatConfig config) {
        Assert.notNull(config, "The config is required");
        Assert.notNull(config.getApiUrl(), "The config.apiUrl is required");
        Assert.notNull(config.getModel(), "The config.model is required");

        this.config = config;
        this.dialect = ChatDialectManager.select(config);

        Assert.notNull(dialect, "The dialect(provider) no matched, check config or dependencies");

    }

    /**
     * 提示语
     */
    public ChatRequestDesc prompt(ChatSession prompt) {
        return new ChatRequestDescDefault(config, dialect, prompt);
    }

    /**
     * 提示语
     */
    public ChatRequestDesc prompt(ChatPrompt prompt) {
        return prompt(prompt.getMessages());
    }

    /**
     * 提示语
     */
    public ChatRequestDesc prompt(List<ChatMessage> messages) {
        return prompt(InMemoryChatSession.builder().messages(messages).build());
    }

    /**
     * 提示语
     */
    public ChatRequestDesc prompt(ChatMessage... messages) {
        return prompt(Utils.asList(messages));
    }

    /**
     * 提示语
     */
    public ChatRequestDesc prompt(String content) {
        return prompt(ChatMessage.ofUser(content));
    }


    @Override
    public String toString() {
        return "ChatModel{" +
                "config=" + config +
                ", dialect=" + dialect.getClass().getName() +
                '}';
    }


    /// /////////////////////////////////

    /**
     * 构建
     */
    public static ChatModel.Builder of(ChatConfig config) {
        return new ChatModel.Builder(config);
    }

    /**
     * 开始构建
     */
    public static ChatModel.Builder of(String apiUrl) {
        return new ChatModel.Builder(apiUrl);
    }
}
  • ChatModel.Builder
public static class Builder {
    private final ChatConfig config;

    /**
     * @param apiUrl 接口地址
     */
    public Builder(String apiUrl) {
        this.config = new ChatConfig();
        this.config.setApiUrl(apiUrl);
    }

    /**
     * @param config 配置
     */
    public Builder(ChatConfig config) {
        this.config = config;
    }

    /**
     * 接口密钥
     */
    public Builder apiKey(String apiKey) {
        config.setApiKey(apiKey);
        return this;
    }

    /**
     * 服务提供者
     */
    public Builder provider(String provider) {
        config.setProvider(provider);
        return this;
    }

    /**
     * 使用模型
     */
    public Builder model(String model) {
        config.setModel(model);
        return this;
    }

    /**
     * 头信息添加
     */
    public Builder headerSet(String key, String value) {
        config.setHeader(key, value);
        return this;
    }

    /**
     * 默认工具添加(即每次请求都会带上)
     *
     * @param tool 工具对象
     */
    public Builder defaultToolsAdd(FunctionTool tool) {
        config.addDefaultTools(tool);
        return this;
    }

    /**
     * 默认工具添加(即每次请求都会带上)
     *
     * @param toolColl 工具集合
     */
    public Builder defaultToolsAdd(Iterable<FunctionTool> toolColl) {
        for (FunctionTool f : toolColl) {
            config.addDefaultTools(f);
        }

        return this;
    }

    /**
     * 默认工具添加(即每次请求都会带上)
     *
     * @param toolProvider 工具提供者
     */
    public Builder defaultToolsAdd(ToolProvider toolProvider) {
        return defaultToolsAdd(toolProvider.getTools());
    }

    /**
     * 默认工具添加(即每次请求都会带上)
     *
     * @param toolObj 工具对象
     */
    public Builder defaultToolsAdd(Object toolObj) {
        return defaultToolsAdd(new MethodToolProvider(toolObj));
    }

    /**
     * 默认工具添加(即每次请求都会带上)
     *
     * @param name        名字
     * @param toolBuilder 工具构建器
     */
    public Builder defaultToolsAdd(String name, Consumer<FunctionToolDesc> toolBuilder) {
        FunctionToolDesc decl = new FunctionToolDesc(name);
        toolBuilder.accept(decl);
        config.addDefaultTools(decl);
        return this;
    }

    /**
     * 默认工具上下文添加
     */
    public Builder defaultToolsContextAdd(String key, Object value) {
        config.addDefaultToolsContext(key, value);
        return this;
    }

    /**
     * 默认工具上下文添加
     */
    public Builder defaultToolsContextAdd(Map<String, Object> toolsContext) {
        config.addDefaultToolsContext(toolsContext);
        return this;
    }

    /**
     * 添加默认拦截器
     *
     * @param interceptor 拦截器
     */
    public Builder defaultInterceptorAdd(ChatInterceptor interceptor) {
        return defaultInterceptorAdd(0, interceptor);
    }

    /**
     * 添加默认拦截器
     *
     * @param index       顺序位
     * @param interceptor 拦截器
     */
    public Builder defaultInterceptorAdd(int index, ChatInterceptor interceptor) {
        config.addDefaultInterceptor(index, interceptor);
        return this;
    }

    /**
     * 添加默认选项
     */
    public Builder defaultOptionAdd(String key, Object val) {
        config.addDefaultOption(key, val);
        return this;
    }

    /**
     * 网络超时
     */
    public Builder timeout(Duration timeout) {
        config.setTimeout(timeout);

        return this;
    }

    /**
     * 网络代理
     */
    public Builder proxy(Proxy proxy) {
        config.setProxy(proxy);

        return this;
    }

    /**
     * 网络代理
     */
    public Builder proxy(String host, int port) {
        return proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host, port)));
    }

    /**
     * 构建
     */
    public ChatModel build() {
        return new ChatModel(config);
    }
}
  • ChatRequestDesc(聊天请求声明)
public interface ChatRequestDesc {
    /**
     * 选项设置
     *
     * @param options 选项
     */
    ChatRequestDesc options(ChatOptions options);

    /**
     * 选项配置
     *
     * @param optionsBuilder 选项构建器
     */
    ChatRequestDesc options(Consumer<ChatOptions> optionsBuilder);

    /**
     * 调用
     */
    ChatResponse call() throws IOException;

    /**
     * 流响应
     */
    Publisher<ChatResponse> stream();
}
  • ChatResponse(聊天响应)
public interface ChatResponse {
    /**
     * 获取响应数据
     */
    @Nullable
    String getResponseData();

    /**
     * 获取模型
     */
    String getModel();

    /**
     * 获取错误
     */
    @Nullable
    ChatException getError();

    /**
     * 是否有选择
     */
    boolean hasChoices();

    /**
     * 最后一个选择
     */
    @Nullable
    ChatChoice lastChoice();

    /**
     * 获取所有选择
     */
    @Nullable
    List<ChatChoice> getChoices();

    /**
     * 获取消息
     */
    @Nullable
    AssistantMessage getMessage();

    /**
     * 获取聚合消息(流响应完成时可用)
     */
    @Nullable
    AssistantMessage getAggregationMessage();

    /**
     * 是否有消息内容
     */
    boolean hasContent();

    /**
     * 获取消息原始内容
     */
    String getContent();

    /**
     * 获取消息结果内容(清理过思考)
     */
    String getResultContent();

    /**
     * 获取使用情况(完成时,才会有使用情况)
     */
    @Nullable
    AiUsage getUsage();

    /**
     * 是否完成
     */
    boolean isFinished();

    /**
     * 是否为流响应
     */
    boolean isStream();
}
  • ChatDialect(可以自己扩展适配,增加不同方言支持)
public interface ChatDialect extends AiModelDialect {
    /**
     * 是否为默认
     */
    default boolean isDefault() {
        return false;
    }

    /**
     * 匹配检测
     *
     * @param config 聊天配置
     */
    boolean matched(ChatConfig config);

    /**
     * 创建 http 工具
     *
     * @param config 聊天配置
     */
    HttpUtils createHttpUtils(ChatConfig config);

    /**
     * 构建请求数据
     *
     * @param config   聊天配置
     * @param options  聊天选项
     * @param messages 消息
     * @param isStream 是否流式获取
     */
    String buildRequestJson(ChatConfig config, ChatOptions options, List<ChatMessage> messages, boolean isStream);

    /**
     * 构建助理消息节点
     *
     * @param toolCallBuilders 工具调用构建器集合
     */
    ONode buildAssistantMessageNode(Map<String, ToolCallBuilder> toolCallBuilders);

    /**
     * 构建助理消息根据直接返回的工具消息
     *
     * @param toolMessages 直接返回的工具消息
     */
    AssistantMessage buildAssistantMessageByToolMessages(List<ToolMessage> toolMessages);

    /**
     * 分析响应数据
     *
     * @param config   聊天配置
     * @param resp     响应体
     * @param respJson 响应数据
     */
    boolean parseResponseJson(ChatConfig config, ChatResponseDefault resp, String respJson);

    /**
     * 分析工具调用
     *
     * @param resp     响应体
     * @param oMessage 消息节点
     */
    List<AssistantMessage> parseAssistantMessage(ChatResponseDefault resp, ONode oMessage);
}