Chat 对话模型
This content is not available in your language yet.
Feat AI 的 Chat 对话模型提供了一种简单而强大的方式来与各种 AI 模型进行交互。它支持多种模型供应商、流式和非流式响应、函数调用等高级功能。
ChatModel 类
Section titled “ChatModel 类”ChatModel 是核心的聊天模型类,用于与 AI 模型进行交互。它提供了多种方法来发送聊天请求并处理响应:
- chat:发送非流式聊天请求
- chatStream:发送流式聊天请求
ChatOptions 类
Section titled “ChatOptions 类”ChatOptions 用于配置聊天模型的各种参数,包括模型供应商、API 密钥、系统提示等。
ChatModelVendor 类
Section titled “ChatModelVendor 类”ChatModelVendor 定义了不同的模型供应商及其能力,如 GiteeAI 和 Ollama。
初始化 ChatModel
Section titled “初始化 ChatModel”要开始使用 Chat 对话模型,首先需要创建一个 ChatModel 实例:
import tech.smartboot.feat.ai.FeatAI;import tech.smartboot.feat.ai.chat.ChatModel;import tech.smartboot.feat.ai.chat.ChatModelVendor;
// 创建 ChatModel 实例ChatModel chatModel = FeatAI.chatModel(opts -> { opts.model(ChatModelVendor.GiteeAI.Qwen2_5_72B_Instruct) .system("你是一个乐于助人的助手。") .debug(true);});发送非流式请求
Section titled “发送非流式请求”使用 chat 方法发送非流式请求:
chatModel.chat("你好,请自我介绍一下。", responseMessage -> { System.out.println("rsp: " + responseMessage.getContent()); System.out.println("usage: " + responseMessage.getUsage());});发送流式请求
Section titled “发送流式请求”使用 chatStream 方法发送流式请求:
chatModel.chatStream("你好,请自我介绍一下。", new StreamResponseCallback() { @Override public void onStreamResponse(String content) { System.out.print(content); }
@Override public void onCompletion(ResponseMessage responseMessage) { System.out.println("\nResponse completed."); }
@Override public void onFailure(Throwable throwable) { System.err.println("Error: " + throwable.getMessage()); }});有关完整示例,请参见 ChatDemo.java
ChatOptions 提供了多种配置选项:
ChatModel chatModel = FeatAI.chatModel(opts -> { opts.model(ChatModelVendor.GiteeAI.Qwen2_5_72B_Instruct) // 选择模型 .baseUrl("https://ai.gitee.com/v1/") // 设置基础URL .apiKey("your-api-key") // 设置API密钥 .system("你是一个乐于助人的助手。") // 设置系统提示 .debug(true); // 启用调试模式});Feat AI 支持多种模型供应商,包括 GiteeAI 和 Ollama:
// GiteeAI 模型opts.model(ChatModelVendor.GiteeAI.Qwen2_5_72B_Instruct)
// Ollama 模型opts.model(ChatModelVendor.Ollama.Qwen3_06B)有关完整示例,请参见 OllamaDemo.java
Chat 模型支持函数调用功能,允许模型调用预定义的函数:
ChatModel chatModel = FeatAI.chatModel(opts -> { opts.model(ChatModelVendor.GiteeAI.DeepSeek_R1) .addFunction(Function.of("get_current_weather")) // 添加函数 .system("你是一个天气助手,可以查询天气信息。");});
// 发送带有函数调用的请求chatModel.chat("今天北京的天气怎么样?", Arrays.asList("get_current_weather"), responseMessage -> { System.out.println("函数调用结果: " + responseMessage.getToolCalls());});响应格式控制
Section titled “响应格式控制”可以控制模型的响应格式:
chatModel.getOptions().responseFormat(ResponseFormat.JSON);不进行思考模式
Section titled “不进行思考模式”某些模型支持直接输出结果而不展示思考过程:
chatModel.getOptions().noThink(true);
graph TD
A[创建 ChatModel] --> B[配置 ChatOptions]
B --> C[选择模型供应商]
C --> D[设置参数]
D --> E[发送请求]
E --> F{流式请求?}
F -->|是| G[chatStream]
F -->|否| H[chat]
G --> I[逐步接收响应]
H --> J[一次性接收响应]
I --> K[处理完成]
J --> K
1. 错误处理
Section titled “1. 错误处理”始终处理可能的错误情况:
chatModel.chat("你好", responseMessage -> { if (responseMessage.isSuccess()) { System.out.println("响应: " + responseMessage.getContent()); } else { System.err.println("错误: " + responseMessage.getError()); }});2. 资源管理
Section titled “2. 资源管理”确保正确管理资源,特别是在使用流式响应时。
3. 调试模式
Section titled “3. 调试模式”在开发阶段启用调试模式以获取详细信息:
opts.debug(true);Feat AI 支持多种模型供应商和模型:
GiteeAI 模型
Section titled “GiteeAI 模型”- DeepSeek-R1
- Qwen2.5-72B-Instruct
- Qwen3-235B-A22B-Instruct-2507
- Kimi-K2-Instruct
Ollama 模型
Section titled “Ollama 模型”- Qwen3_06B
- Qwen2.5_3B
- Deepseek-r1_7B
Feat AI 的 Chat 对话模型提供了一个强大而灵活的接口来与各种 AI 模型进行交互。通过简单的 API 和丰富的配置选项,开发者可以轻松构建智能对话应用。其支持多种模型供应商、流式和非流式响应、函数调用等高级功能,使其成为构建 AI 应用的理想选择。