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、主要接口字典
public interface ChatModel {
/**
* 提示语
*/
ChatRequest prompt(ChatSession prompt);
/**
* 提示语
*/
ChatRequest prompt(ChatPrompt prompt);
/**
* 提示语
*/
ChatRequest prompt(List<ChatMessage> messages);
/**
* 提示语
*/
default ChatRequest prompt(ChatMessage... messages) {
return prompt(new ArrayList<>(Arrays.asList(messages)));
}
/**
* 提示语
*/
default ChatRequest prompt(String content) {
return prompt(ChatMessage.ofUser(content));
}
/// /////////////////////////////////
/**
* 构建
*/
static ChatModel.Builder of(ChatConfig config) {
return new ChatModelBuilder(config);
}
/**
* 开始构建
*/
static ChatModel.Builder of(String apiUrl) {
return new ChatModelBuilder(apiUrl);
}
}
public static class Builder {
/**
* @param config 配置
*/
public Builder(ChatConfig config) ;
/**
* 接口密钥
*/
public Builder apiKey(String apiKey) ;
/**
* 服务提供者
*/
public Builder provider(String provider);
/**
* 使用模型
*/
public Builder model(String model) ;
/**
* 头信息添加
*/
public Builder headerSet(String key, String value) ;
/**
* 默认工具添加(即每次请求都会带上)
*
* @param tool 工具对象
*/
public Builder defaultToolsAdd(FunctionTool tool) ;
/**
* 默认工具添加(即每次请求都会带上)
*
* @param toolColl 工具集合
*/
public Builder defaultToolsAdd(Iterable<FunctionTool> toolColl) ;
/**
* 默认工具添加(即每次请求都会带上)
*
* @param toolProvider 工具提供者
*/
public Builder defaultToolsAdd(ToolProvider toolProvider) ;
/**
* 默认工具添加(即每次请求都会带上)
*
* @param toolObj 工具对象
*/
public Builder defaultToolsAdd(Object toolObj) ;
/**
* 默认工具添加(即每次请求都会带上)
*
* @param name 名字
* @param toolBuilder 工具构建器
*/
public Builder defaultToolsAdd(String name, Consumer<FunctionToolDesc> toolBuilder) ;
/**
* 默认工具上下文添加
*/
public Builder defaultToolsContextAdd(String key, Object value) ;
/**
* 默认工具上下文添加
*/
public Builder defaultToolsContextAdd(Map<String, Object> toolsContext) ;
/**
* 添加默认拦截器
*
* @param interceptor 拦截器
*/
public Builder defaultInterceptorAdd(ChatInterceptor interceptor) ;
/**
* 添加默认拦截器
*
* @param index 顺序位
* @param interceptor 拦截器
*/
public Builder defaultInterceptorAdd(int index, ChatInterceptor interceptor) ;
/**
* 添加默认选项
*/
public Builder defaultOptionAdd(String key, Object val);
/**
* 网络超时
*/
public Builder timeout(Duration timeout) ;
/**
* 网络代理
*/
public Builder proxy(Proxy proxy) ;
/**
* 网络代理
*/
public Builder proxy(String host, int port) ;
/**
* 构建
*/
public ChatModel build();
}
public interface ChatRequest {
/**
* 选项设置
*/
ChatRequest options(ChatOptions options);
/**
* 选项配置
*/
ChatRequest options(Consumer<ChatOptions> optionsBuilder);
/**
* 调用
*/
ChatResponse call() throws IOException;
/**
* 流响应
*/
Publisher<ChatResponse> stream();
}
public interface ChatResponse {
/**
* 获取响应数据
*/
@Nullable
String getResponseData();
/**
* 获取模型
*/
String getModel();
/**
* 获取错误
*/
@Nullable
ChatException getError();
/**
* 是否有选择
*/
boolean hasChoices();
/**
* 最后一个选择
*/
ChatChoice lastChoice();
/**
* 获取所有选择
*/
List<ChatChoice> getChoices();
/**
* 获取消息
*/
@Nullable
AssistantMessage getMessage();
/**
* 获取聚合消息(流响应完成时可用)
*/
@Nullable
AssistantMessage getAggregationMessage();
/**
* 获取使用情况(完成时,才会有使用情况)
*/
@Nullable
AiUsage getUsage();
/**
* 是否完成
*/
boolean isFinished();
/**
* 是否为流响应
*/
boolean isStream();
}
- ChatDialect(可以自己扩展适配,增加不同方言支持)
public interface ChatDialect extends AiModelDialect {
/**
* 是否为默认
*/
default boolean isDefault() {
return false;
}
/**
* 匹配检测
*
* @param config 聊天配置
*/
boolean matched(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<Integer, 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);
}