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

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


### 1、聊天模型（ChatModel）相关的接口




| 接口                  | 描述                                                         | 
| ------------- | ---------------------------------- |
| ChatModel         | 聊天模型通用客户端（通过 ChatConfig 构建）       |   
| ChatConfig        | 聊天模型配置                                                     |   
| | | 
| ChatDialect        | 聊天方言（用于适配不同的 llm 接口规范）             | 
| | | 
| ChatRequest       | 聊天请求                                                               |   
| ChatOptions        | 聊天请求选项（单次请求时的选项）                             |     
| ChatMessage      | 聊天消息（分为：用户、系统、工具、助理四种消息）     |    
| ChatResponse     | 聊天响应                                                                 |   
| | | 
| ChatSession           | 聊天会话（历史记忆与持久化管理）                            |    
| | | 
| Prompt              | 聊天提示语      | 
| | | 
| FunctionTool          | 函数工具                                                        |     
| ToolCall                | 工具调用（由 llm 发起）                                |      
| | | 
| Talent                    | 才能（由“指令” + “工具”组合而成）。（相对于 Skill）它表示智能体的内在能力，是某一方面能力的完整封装               |    



### 2、四种聊天消息（ChatMessage）



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




### 3、主要接口字典



* ChatModel


```java

import org.noear.solon.Utils;
import org.noear.solon.ai.AiModel;
import org.noear.solon.ai.chat.dialect.ChatDialect;
import org.noear.solon.ai.chat.dialect.ChatDialectManager;
import org.noear.solon.ai.chat.interceptor.ChatInterceptor;
import org.noear.solon.ai.chat.prompt.Prompt;
import org.noear.solon.ai.chat.talent.Talent;
import org.noear.solon.ai.chat.tool.*;
import org.noear.solon.ai.chat.message.ChatMessage;
import org.noear.solon.core.Props;
import org.noear.solon.core.util.Assert;

import java.lang.reflect.Type;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.time.Duration;
import java.util.*;
import java.util.function.Consumer;

/**
 * 聊天模型
 */
public class ChatModel implements AiModel {
    private final ChatConfig config;
    private final ChatConfigReadonly configReadonly;
    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.configReadonly = new ChatConfigReadonly(config);
        this.dialect = ChatDialectManager.select(config);

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

    }

    public ChatDialect getDialect() {
        return dialect;
    }

    public ChatConfigReadonly getConfig() {
        return configReadonly;
    }

    public String getNameOrModel(){
        return config.getNameOrModel();
    }

    public String getModel(){
        return config.getModel();
    }

    /**
     * 接口规范
     *
     * @since 4.0
     */
    public String getStandard(){
        return config.getStandard();
    }

    /**
     * 接口规范或提供者（临时过渡）
     *
     * @since 4.0
     */
    public String getStandardOrProvider(){
        return config.getStandardOrProvider();
    }

    public String getProvider(){
        return config.getProvider();
    }

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

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

    /**
     * 提示语
     */
    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 Builder of(ChatConfig config) {
        return new Builder(config);
    }

    /**
     * 开始构建
     */
    public static Builder of(String apiUrl) {
        return new Builder(apiUrl);
    }

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

    /**
     * 聊天模型构建器实现
     *
     * @author noear
     * @since 3.1
     */
    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;
        }

        /**
         * 接口规范
         *
         * @since 4.0
         */
        public Builder standard(String standard) {
            config.setStandard(standard);
            return this;
        }

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

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

        /**
         * 使用模型
         */
        public Builder contextLength(long contextLength) {
            config.setContextLength(contextLength);
            return this;
        }

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

        /**
         * 头信息添加
         */
        public Builder headerSet(Map<String, String> headers) {
            config.setHeaders(headers);
            return this;
        }

        /**
         * User-Agent
         */
        public Builder userAgent(String userAgent) {
            config.setUserAgent(userAgent);
            return this;
        }

        /**
         * 角色
         */
        public Builder role(String role) {
            config.getModelOptions().role(role);
            return this;
        }

        /**
         * 指令
         */
        public Builder instruction(String instruction) {
            config.getModelOptions().instruction(instruction);
            return this;
        }

        /**
         * 系统提示词
         */
        public Builder systemPrompt(String systemPrompt) {
            config.getModelOptions().systemPrompt(systemPrompt);
            return this;
        }

        /**
         * 输出架构
         */
        public Builder outputSchema(String outputSchema) {
            config.getModelOptions().outputSchema(outputSchema);
            return this;
        }

        /**
         * 输出架构
         */
        public Builder outputSchema(Type type) {
            config.getModelOptions().outputSchema(type);
            return this;
        }

        /**
         * 模型选项
         */
        public Builder modelOptions(Consumer<ModelOptionsAmend<?, ChatInterceptor>> consumer) {
            consumer.accept(config.getModelOptions());
            return this;
        }

        /**
         * 默认工具添加（即每次请求都会带上）
         *
         * @param tools 工具
         */
        public Builder defaultToolAdd(FunctionTool... tools) {
            config.addDefaultTool(tools);
            return this;
        }

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

            return this;
        }

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


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


        /**
         * 默认才能添加
         *
         * @since 3.8.4
         */
        public Builder defaultTalentAdd(Talent... talents) {
            for (Talent t : talents) {
                defaultTalentAdd(0, t);
            }
            return this;
        }

        /**
         * 默认才能添加
         *
         * @since 3.8.4
         */
        public Builder defaultTalentAdd(int index, Talent talent) {
            config.addDefaultTalent(index, talent);
            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 timeout(Duration timeout) {
            config.setTimeout(timeout);

            return this;
        }

        /**
         * 网络代理
         */
        public Builder proxy(Proxy proxy) {
            config.setProxyInstance(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（聊天请求声明）


```java
import reactor.core.publisher.Flux;

import java.io.IOException;
import java.util.function.Consumer;

/**
 * 聊天请求描述
 */
public interface ChatRequestDesc {
    /**
     * 会话设置
     *
     * @param session 会话
     * @since 3.8.4
     */
    ChatRequestDesc session(ChatSession session);

    /**
     * 选项设置
     *
     * @param options 选项
     */
    ChatRequestDesc options(ChatOptions options);

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

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

    /**
     * 流响应
     */
    Flux<ChatResponse> stream();
}
```


