@dao3fun/siliconflow-chat
v1.0.3
Published
在神岛中调用硅基流动平台对话相关AI
Downloads
14
Readme
神岛 AI 对话服务
为 Box3 游戏引擎打造的智能对话解决方案,基于硅流平台 AI 能力。
简介
这是一个专为神岛(Box3)创作者设计的 AI 对话服务封装包,让你能够轻松地在游戏中集成智能对话功能。通过简单的 API 调用,即可实现与 AI 助手的自然交互。
🔗 还没有硅流平台账号?点击这里快速注册
安装
npm install @dao3fun/siliconflow-chat快速开始
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
主要的服务类,用于处理与硅流平台的通信。
构造函数
constructor(config: { apiKey: string })config.apiKey: 硅流平台的 API 密钥
方法
create(prop: ISiliconflowFormat): Promise<ISiliconflowResult | null>
创建一个新的对话请求。
参数:
prop: 请求参数对象model: 模型名称messages: 对话消息数组stream: 流式输出(Box3 暂不支持,固定为 false)
返回值:
- 成功时返回响应结果对象
- 失败时返回 null
类型定义
ISiliconflowFormat
请求参数格式接口。
interface ISiliconflowFormat {
model: string;
messages: {
role: "system" | "user" | "assistant";
content: string;
}[];
stream?: false;
[key: string]: any;
}ISiliconflowResult
响应结果格式接口。
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";
}注意事项
- Box3 引擎目前不支持流式输出
- 请确保在使用前配置正确的 API 密钥
- 建议实现错误处理机制
- 注意控制对话频率,避免超出 API 限制
