为 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 || "服务暂不可用",
});
});
主要的服务类,用于处理与硅流平台的通信。
constructor(config: { apiKey: string })
-
config.apiKey
: 硅流平台的 API 密钥
创建一个新的对话请求。
参数:
-
prop
: 请求参数对象-
model
: 模型名称 -
messages
: 对话消息数组 -
stream
: 流式输出(Box3 暂不支持,固定为 false)
-
返回值:
- 成功时返回响应结果对象
- 失败时返回 null
请求参数格式接口。
interface ISiliconflowFormat {
model: string;
messages: {
role: "system" | "user" | "assistant";
content: string;
}[];
stream?: false;
[key: string]: any;
}
响应结果格式接口。
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 限制