UNPKG

@dao3fun/siliconflow-chat

Version:

在神岛中调用硅基流动平台对话相关AI

131 lines (96 loc) 2.92 kB
# 神岛 AI 对话服务 为 Box3 游戏引擎打造的智能对话解决方案,基于硅流平台 AI 能力。 ## 简介 这是一个专为神岛(Box3)创作者设计的 AI 对话服务封装包,让你能够轻松地在游戏中集成智能对话功能。通过简单的 API 调用,即可实现与 AI 助手的自然交互。 🔗 还没有硅流平台账号?[点击这里快速注册](https://cloud.siliconflow.cn/i/uNnBUxXj) ## 安装 ```bash npm install @dao3fun/siliconflow-chat ``` ## 快速开始 ```typescript import { SiliconflowChat } from "@dao3fun/siliconflow-chat"; // 初始化聊天服务 const chatService = new SiliconflowChat({ apiKey: "your-api-key-here", }); // 在 Box3 中使用 world.onPlayerJoin(async ({ entity }) => { const response = await chatService.create({ model: "deepseek-ai/DeepSeek-V3", messages: [ { role: "user", content: `你好,我是${entity.player.name},很高兴见到你!`, }, ], }); entity.player.dialog({ type: GameDialogType.TEXT, title: "AI 助手", content: response?.choices?.[0]?.message?.content || "服务暂不可用", }); }); ``` ## API 文档 ### SiliconflowChat 主要的服务类,用于处理与硅流平台的通信。 #### 构造函数 ```typescript constructor(config: { apiKey: string }) ``` - `config.apiKey`: 硅流平台的 API 密钥 #### 方法 ##### create(prop: ISiliconflowFormat): Promise<ISiliconflowResult | null> 创建一个新的对话请求。 参数: - `prop`: 请求参数对象 - `model`: 模型名称 - `messages`: 对话消息数组 - `stream`: 流式输出(Box3 暂不支持,固定为 false) 返回值: - 成功时返回响应结果对象 - 失败时返回 null ### 类型定义 #### ISiliconflowFormat 请求参数格式接口。 🔗 [硅流平台 API 参数一览](https://docs.siliconflow.cn/cn/api-reference/chat-completions/chat-completions) ```typescript interface ISiliconflowFormat { model: string; messages: { role: "system" | "user" | "assistant"; content: string; }[]; stream?: false; [key: string]: any; } ``` #### ISiliconflowResult 响应结果格式接口。 ```typescript interface ISiliconflowResult { id: string; choices: Array<{ message: { role: "assistant"; content: string; reasoning_content: string; }; finish_reason: "stop"; }>; usage: { prompt_tokens: number; completion_tokens: number; total_tokens: number; }; created: number; model: string; object: "chat.completion"; } ``` ## 注意事项 1. Box3 引擎目前不支持流式输出 2. 请确保在使用前配置正确的 API 密钥 3. 建议实现错误处理机制 4. 注意控制对话频率,避免超出 API 限制