概述
提供云开发 AI 接入能力,快速接入大模型和 Agent。
基础使用示例
- 初始化配置
- 基础文本生成
- 流式文本生成
- Agent 对话
Publishable Key 可前往 云开发平台/API Key 配置 中生成
类型声明
function ai(): AI;
返回值
返回新创建的 AI 实例。
import cloudbase from "@cloudbase/js-sdk";
// 初始化
const app = cloudbase.init({
env: "your-env-id", // 替换为您的环境ID
region: "ap-shanghai", // 地域,默认为上海
accessKey: "", // 填入生成的 Publishable Key
});
// 如果填入了 accessKey,则不需要此步骤
await app.auth.signInAnonymously();
const ai = app.ai();
// 基础文本生成示例
async function generateText() {
const model = ai.createModel("hunyuan-exp");
const result = await model.generateText({
model: "hunyuan-lite",
messages: [{ role: "user", content: "你好,请你介绍一下李白" }],
});
console.log("生成的文本:", result.text);
console.log("消耗的token:", result.usage);
}
// 流式文本生成示例
async function streamText() {
const model = ai.createModel("hunyuan-exp");
const result = await model.streamText({
model: "hunyuan-lite",
messages: [{ role: "user", content: "1+1等于多少" }],
});
for await (let chunk of result.textStream) {
console.log("收到文本块:", chunk);
}
}
// Agent 对话示例
async function chatWithAgent() {
const res = await ai.bot.sendMessage({
botId: "botId-xxx",
msg: "你好,请介绍一下你自己",
history: [],
});
for await (let text of res.textStream) {
console.log("Agent 回复:", text);
}
}
AI
用于创建 AI 模型的类。
createModel
function createModel(model: string): ChatModel;
创建指定的 AI 模型。
- 创建一个新的 AI 模型实例
- 返回一个实现了 ChatModel 抽象类的模型实例
- 该实例提供 AI 生成文本相关能力
参数
模型标识符,如 'hunyuan-exp'、'hunyuan-lite' 等
返回
实现了 ChatModel 抽象类的模型实例,提供 AI 生成文本相关能力
示例
- 基础使用
- 多模型管理
// 创建混元体验版模型
const model = ai.createModel("hunyuan-exp");
// 创建混元轻量版模型
const liteModel = ai.createModel("hunyuan-lite");
// 创建多个模型实例用于不同场景
const expModel = ai.createModel("hunyuan-exp"); // 体验版,功能完整
const liteModel = ai.createModel("hunyuan-lite"); // 轻量版,响应快速
const proModel = ai.createModel("hunyuan-pro"); // 专业版,性能更强
// 根据需求选择不同模型
async function generateWithModel(scenario: string, content: string) {
let selectedModel;
switch (scenario) {
case "creative":
selectedModel = expModel;
break;
case "quick":
selectedModel = liteModel;
break;
case "professional":
selectedModel = proModel;
break;
default:
selectedModel = liteModel;
}
const result = await selectedModel.generateText({
model: scenario,
messages: [{ role: "user", content: content }],
});
return result.text;
}
bot
挂载了 Bot 类的实例,上面集合了一系列与 Agent 交互的方法。具体可参考 Bot 类 的详细文档。
使用示例
const agentList = await ai.bot.list({ pageNumber: 1, pageSize: 10 });
registerFunctionTool
function registerFunctionTool(functionTool: FunctionTool): void;
注册函数工具。在进行大模型调用时,可以告知大模型可用的函数工具,当大模型的响应被解析为工具调用时,会自动调用对应的函数工具。
参数
要注册的函数工具定义
返回
无返回值
示例
- 基础工具注册
- 多工具集成系统
- 动态工具管理
// 定义获取天气的工具
const getWeatherTool = {
name: "get_weather",
description: "返回某个城市的天气信息。调用示例:get_weather({city: '北京'})",
fn: ({ city }) => `${city}的天气是:秋高气爽!!!`,
parameters: {
type: "object",
properties: {
city: {
type: "string",
description: "要查询的城市",
},
},
required: ["city"],
},
};
// 注册工具
ai.registerFunctionTool(getWeatherTool);
// 使用工具进行对话
const model = ai.createModel("hunyuan-exp");
const result = await model.generateText({
model: "hunyuan-turbo",
tools: [getWeatherTool],
messages: [
{
role: "user",
content: "请告诉我北京的天气状况",
},
],
});
console.log(result.text);
// 多工具集成系统
class ToolIntegrationSystem {
private registeredTools: Map<string, FunctionTool> = new Map();
// 注册多个工具
registerTools(tools: FunctionTool[]): void {
tools.forEach((tool) => {
ai.registerFunctionTool(tool);
this.registeredTools.set(tool.name, tool);
console.log(`✓ 工具注册成功: ${tool.name}`);
});
}
// 获取所有可用工具
getAvailableTools(): FunctionTool[] {
return Array.from(this.registeredTools.values());
}
// 智能对话助手
async smartAssistant(question: string): Promise<string> {
const model = ai.createModel("hunyuan-exp");
const result = await model.generateText({
model: "hunyuan-turbo",
tools: this.getAvailableTools(),
messages: [
{
role: "user",
content: question,
},
],
});
return result.text;
}
// 工具使用统计
getToolUsageStats(): Map<string, number> {
const stats = new Map<string, number>();
// 在实际应用中,这里可以记录工具调用次数
return stats;
}
}
// 定义多个工具
const tools = [
{
name: "get_weather",
description: "获取城市天气信息",
fn: ({ city }) => `${city}的天气:晴,25℃`,
parameters: {
type: "object",
properties: { city: { type: "string", description: "城市名称" } },
required: ["city"],
},
},
{
name: "calculate",
description: "执行数学计算",
fn: ({ expression }) => `计算结果:${eval(expression)}`,
parameters: {
type: "object",
properties: { expression: { type: "string", description: "数学表达式" } },
required: ["expression"],
},
},
{
name: "search_info",
description: "搜索信息",
fn: ({ keyword }) => `搜索结果:关于${keyword}的信息`,
parameters: {
type: "object",
properties: { keyword: { type: "string", description: "搜索关键词" } },
required: ["keyword"],
},
},
];
// 使用示例
const toolSystem = new ToolIntegrationSystem();
toolSystem.registerTools(tools);
// 智能对话
const answer = await toolSystem.smartAssistant(
"计算一下 15*8+20 的 结果,并告诉我北京的天气"
);
console.log("助手回答:", answer);
// 动态工具管理系统
class DynamicToolManager {
private toolRegistry: Map<string, FunctionTool> = new Map();
// 动态注册工具
registerTool(tool: FunctionTool): void {
ai.registerFunctionTool(tool);
this.toolRegistry.set(tool.name, tool);
console.log(`工具注册: ${tool.name}`);
}
// 动态卸载工具
unregisterTool(toolName: string): boolean {
if (this.toolRegistry.has(toolName)) {
this.toolRegistry.delete(toolName);
console.log(`工具卸载: ${toolName}`);
return true;
}
return false;
}
// 根据上下文动态选择工具
async contextualAssistant(
context: string,
question: string
): Promise<string> {
const relevantTools = this.selectRelevantTools(context, question);
const model = ai.createModel("hunyuan-exp");
const result = await model.generateText({
model: "hunyuan-turbo",
tools: relevantTools,
messages: [
{
role: "system",
content: `当前上下文:${context}`,
},
{
role: "user",
content: question,
},
],
});
return result.text;
}
// 根据上下文选择相关工具
private selectRelevantTools(
context: string,
question: string
): FunctionTool[] {
const tools = Array.from(this.toolRegistry.values());
// 简单的关键词匹配算法
const keywords = [...context.split(" "), ...question.split(" ")];
return tools.filter((tool) => {
return keywords.some((keyword) =>
tool.description.toLowerCase().includes(keyword.toLowerCase())
);
});
}
// 热更新工具
updateTool(toolName: string, updatedTool: FunctionTool): boolean {
if (this.toolRegistry.has(toolName)) {
this.unregisterTool(toolName);
this.registerTool(updatedTool);
console.log(`工具更新: ${toolName}`);
return true;
}
return false;
}
// 获取工具列表
listTools(): string[] {
return Array.from(this.toolRegistry.keys());
}
}
// 使用示例
const toolManager = new DynamicToolManager();
// 注册初始工具
toolManager.registerTool({
name: "weather",
description: "获取天气信息",
fn: ({ city }) => `${city}天气:晴`,
parameters: {
type: "object",
properties: { city: { type: "string" } },
required: ["city"],
},
});
// 动态添加新工具
toolManager.registerTool({
name: "translate",
description: "翻译文本",
fn: ({ text, to }) => `${text} 翻译为 ${to}:示例翻译结果`,
parameters: {
type: "object",
properties: {
text: { type: "string" },
to: { type: "string" },
},
required: ["text", "to"],
},
});
// 上下文感知对话
const answer = await toolManager.contextualAssistant(
"天气和翻译相关的问题",
"把'你好'翻译成英语,并告诉我上海的天气"
);
console.log("智能回答:", answer);
// 查看可用工具
console.log("可用工具:", toolManager.listTools());
ChatModel
这个抽象类描述了 AI 生文模型类提供的接口。
generateText
function generateText(data: BaseChatModelInput): Promise<{
rawResponses: Array<unknown>;
text: string;
messages: Array<ChatModelMessage>;
usage: Usage;
error?: unknown;
}>;
调用大模型生成文本。
- 向大模型发送消息并获取生成的文本响应
- 支持完整的对话上下文管理
- 返回详细的调用信息和 token 消耗统计
参数
大模型输入参数,包含模型配置和消息内容
返回
大模型生成文本的响应结果
示例
- 基础文本生成
- 多轮对话
- 带工具调用
const model = ai.createModel("hunyuan-exp");
const result = await model.generateText({
model: "hunyuan-lite",
messages: [{ role: "user", content: "你好,请你介绍一下李白" }],
});
console.log("生成的文本:", result.text);
console.log("消耗的token:", result.usage);
const model = ai.createModel("hunyuan-exp");
// 多轮对话示例
const messages = [
{ role: "user", content: "你好,我想了解中国古代文学" },
{
role: "assistant",
content:
"中国古代文学源远流长,从诗经楚辞到唐诗宋词,都有着丰 富的文化内涵。",
},
{ role: "user", content: "那你能介绍一下唐诗的特点吗?" },
];
const result = await model.generateText({
model: "hunyuan-lite",
messages: messages,
temperature: 0.7,
topP: 0.9,
});
console.log("唐诗介绍:", result.text);
// 注册天气查询工具
ai.registerFunctionTool({
name: "get_weather",
description: "查询城市天气信息",
fn: ({ city }) => `${city}的天气是:晴朗,25℃`,
parameters: {
type: "object",
properties: {
city: {
type: "string",
description: "要查询的城市名称",
},
},
required: ["city"],
},
});
const model = ai.createModel("hunyuan-exp");
const result = await model.generateText({
model: "hunyuan-lite",
messages: [{ role: "user", content: "请告诉我北京的天气状况" }],
tools: [getWeatherTool],
toolChoice: "auto",
});
console.log("大模型响应:", result.text);
streamText
function streamText(data: BaseChatModelInput): Promise<StreamTextResult>;
以流式调用大模型生成文本。
- 流式调用时,生成的文本及其他响应数据会通过 SSE 返回,该接口的返回值对 SSE 做了不同程度的封装,开发者能根据实际需求获取到文本流和完整数据流。
- 以流式方式调用大模型生成文本,支持实时获取增量内容
参数
大模型输入参数,包含模型配置和消息内容
返回
流式文本生成的结果,包含文本流和数据流
示例
- 基础流式调用
- 实时显示应用
- 进度监控
const model = ai.createModel("hunyuan-exp");
const result = await model.streamText({
model: "hunyuan-lite",
messages: [{ role: "user", content: "你好,请你介绍一下李白" }],
});
// 获取文本流
for await (let chunk of result.textStream) {
console.log("收到文本块:", chunk);
}
// 1
// 加
// 1
// 的结果
// 是
// 2
// 。
// 获取数据流
for await (let data of result.dataStream) {
console.log("收到数据块:", data);
}
// {created: 1723013866, id: "a95a54b5c5d2144eb700e60d0dfa5c98", model: "hunyuan-lite", version: "202404011000", choices: Array(1), …}
// {created: 1723013866, id: "a95a54b5c5d2144eb700e60d0dfa5c98", model: "hunyuan-lite", version: "202404011000", choices: Array(1), …}
// {created: 1723013866, id: "a95a54b5c5d2144eb700e60d0dfa5c98", model: "hunyuan-lite", version: "202404011000", choices: Array(1), …}
// {created: 1723013866, id: "a95a54b5c5d2144eb700e60d0dfa5c98", model: "hunyuan-lite", version: "202404011000", choices: Array(1), …}
// {created: 1723013866, id: "a95a54b5c5d2144eb700e60d0dfa5c98", model: "hunyuan-lite", version: "202404011000", choices: Array(1), …}
// {created: 1723013866, id: "a95a54b5c5d2144eb700e60d0dfa5c98", model: "hunyuan-lite", version: "202404011000", choices: Array(1), …}
// {created: 1723013866, id: "a95a54b5c5d2144eb700e60d0dfa5c98", model: "hunyuan-lite", version: "202404011000", choices: Array(1), …}
// {created: 1723013866, id: "a95a54b5c5d2144eb700e60d0dfa5c98", model: "hunyuan-lite", version: "202404011000", choices: Array(1), …}
// 在网页中实时显示流式内容
async function displayStreamingText(question: string) {
const model = ai.createModel("hunyuan-exp");
const result = await model.streamText({
model: "hunyuan-lite",
messages: [{ role: "user", content: question }],
});
const outputElement = document.getElementById("output");
outputElement.innerHTML = "";
for await (let chunk of result.textStream) {
outputElement.innerHTML += chunk;
// 滚动到最新内容
outputElement.scrollTop = outputElement.scrollHeight;
}
}
// 使用示例
displayStreamingText("请详细介绍一下唐诗的发展历程");
// 监控流式生成进度
async function monitorStreamProgress(question: string) {
const model = ai.createModel("hunyuan-exp");
const result = await model.streamText({
model: "hunyuan-lite",
messages: [{ role: "user", content: question }],
});
let totalChunks = 0;
let startTime = Date.now();
for await (let chunk of result.textStream) {
totalChunks++;
console.log(`第${totalChunks}个文本块: ${chunk}`);
}
const endTime = Date.now();
console.log(
`生成完成,共${totalChunks}个文本块,耗时${endTime - startTime}ms`
);
// 获取完整消息和token统计
const messages = await result.messages;
const usage = await result.usage;
console.log("完整对话:", messages);
console.log("Token消耗:", usage);
}
Bot
用于与 Agent 交互的类。
get
function get(props: { botId: string }): Promise<BotInfo>;
获取某个 Agent 的信息。
- 根据 Agent ID 获取详细的 Agent 信息
- 返回 Agent 的基本配置、欢迎语、头像等完整信息
参数
获取 Agent 信息的参数
返回
Agent 的详细信息
示例
- 基础使用
- 应用启动配置
- 多 Agent 管理
const res = await ai.bot.get({ botId: "botId-xxx" });
console.log("Agent 信息:", res);
// 应用启动时加载 Agent 配置
async function initializeAgent(botId: string) {
try {
const agentInfo = await ai.bot.get({ botId });
// 设置应用界面
document.title = `${agentInfo.name} - AI助手`;
// 显示欢迎信息
const welcomeElement = document.getElementById("welcome");
welcomeElement.innerHTML = `
<div class="agent-header">
<img src="${agentInfo.avatar}" alt="${agentInfo.name}" class="avatar" />
<h2>${agentInfo.name}</h2>
<p>${agentInfo.introduction}</p>
</div>
`;
// 设置聊天背景
if (agentInfo.background) {
document.body.style.backgroundImage = `url(${agentInfo.background})`;
}
return agentInfo;
} catch (error) {
console.error("加载 Agent 配置失败:", error);
throw error;
}
}
// 使用示例
initializeAgent("bot-27973647");
// 管理多个 Agent 的配置
class AgentManager {
private agents: Map<string, BotInfo> = new Map();
async loadAgent(botId: string) {
if (this.agents.has(botId)) {
return this.agents.get(botId);
}
const agentInfo = await ai.bot.get({ botId });
this.agents.set(botId, agentInfo);
return agentInfo;
}
getAgentList() {
return Array.from(this.agents.values());
}
async switchAgent(botId: string) {
const agentInfo = await this.loadAgent(botId);
// 更新界面显示
this.updateUI(agentInfo);
return agentInfo;
}
private updateUI(agentInfo: BotInfo) {
// 更新界面元素
console.log("切换到 Agent:", agentInfo.name);
}
}
// 使用示例
const agentManager = new AgentManager();
await agentManager.loadAgent("bot-27973647");
await agentManager.loadAgent("bot-27973648");
list
function list(props: {
name: string;
introduction: string;
information: string;
enable: boolean;
pageSize: number;
pageNumber: number;
}): Promise<AgentListResult>;
批量获取多个 Agent 的信息。
- 查询和筛选可用的 Agent 列表
- 支持分页查询和条件筛选
- 返回 Agent 的基本信息和配置详情
- 适用于构建 Agent 选择器、Agent 管理界面等应用
参数
查询 Agent 列表的参数
返回
Agent 列表查询结果
示例
- 基础查询
- 条件筛选
- Agent 选择器
const agentList = await ai.bot.list({
pageNumber: 1,
pageSize: 10,
name: "",
enable: true,
information: "",
introduction: "",
});
console.log("Agent 总数:", agentList.total);
console.log("Agent 列表:", agentList.botList);
// 根据条件筛选 Agent
async function searchAgents(keyword: string, page: number = 1) {
const result = await ai.bot.list({
pageNumber: page,
pageSize: 20,
name: keyword,
enable: true,
information: keyword,
introduction: keyword,
});
console.log(`找到 ${result.total} 个匹配的 Agent`);
result.botList.forEach((agent) => {
console.log(`- ${agent.name}: ${agent.introduction}`);
});
return result;
}
// 使用示例
await searchAgents("翻译"); // 搜索翻译相关的 Agent
await searchAgents("客服", 2); // 搜索客服相关的 Agent,第二页
// 构建 Agent 选择器界面
class AgentSelector {
private currentPage: number = 1;
private pageSize: number = 12;
async loadAgents(keyword: string = "") {
const result = await ai.bot.list({
pageNumber: this.currentPage,
pageSize: this.pageSize,
name: keyword,
enable: true,
information: keyword,
introduction: keyword,
});
this.displayAgents(result.botList);
this.updatePagination(result.total);
return result;
}
private displayAgents(agents: any[]) {
const container = document.getElementById("agent-list");
container.innerHTML = "";
agents.forEach((agent) => {
const agentCard = document.createElement("div");
agentCard.className = "agent-card";
agentCard.innerHTML = `
<img src="${agent.avatar}" alt="${agent.name}" class="agent-avatar" />
<div class="agent-info">
<h3>${agent.name}</h3>
<p>${agent.introduction}</p>
<button onclick="selectAgent('${agent.botId}')">选择</button>
</div>
`;
container.appendChild(agentCard);
});
}
private updatePagination(total: number) {
const pagination = document.getElementById("pagination");
const totalPages = Math.ceil(total / this.pageSize);
pagination.innerHTML = `
<button onclick="prevPage()" ${
this.currentPage <= 1 ? "disabled" : ""
}>上一页</button>
<span>第 ${this.currentPage} 页,共 ${totalPages} 页</span>
<button onclick="nextPage()" ${
this.currentPage >= totalPages ? "disabled" : ""
}>下一页</button>
`;
}
async nextPage() {
this.currentPage++;
await this.loadAgents();
}
async prevPage() {
if (this.currentPage > 1) {
this.currentPage--;
await this.loadAgents();
}
}
selectAgent(botId: string) {
console.log("选择的 Agent ID:", botId);
// 开始与选中的 Agent 对话
this.startChat(botId);
}
private startChat(botId: string) {
// 实现与 Agent 开始对话的逻辑
console.log("开始与 Agent 对话:", botId);
}
}
// 使用示例
const selector = new AgentSelector();
await selector.loadAgents(); // 加载第一页 Agent
// 搜索特定类型的 Agent
await selector.loadAgents("写作助手");
sendMessage
function sendMessage(props: {
botId: string;
msg: string;
history: Array<{
role: string;
content: string;
}>;
}): Promise<StreamResult>;
与 Agent 进行对话。
- 响应会通过 SSE 返回,该接口的返回值对 SSE 做了不同程度的封装,开发者能根据实际需求获取到文本流和完整数据流。
- 支持多轮对话上下文管理
- 返回流式响应,支持实时获取 Agent 回复
- 适用于构建聊天机器人、智能助手等应用
参数
发送消息的参数
返回
流式对话结果,包含文本流和数据流
示例
- 基础对话
- 多轮对话
- 实时聊天应用
const res = await ai.bot.sendMessage({
botId: "botId-xxx",
history: [{ content: "你是李白。", role: "user" }],
msg: "你好",
});
// 获取文本流
for await (let text of res.textStream) {
console.log("Agent 回复:", text);
}
// 获取数据流
for await (let data of res.dataStream) {
console.log("详细数据:", data);
}
// 多轮对话示例
class ChatSession {
private history: Array<{ role: string; content: string }> = [];
async sendMessage(botId: string, message: string) {
const res = await ai.bot.sendMessage({
botId: botId,
history: this.history,
msg: message,
});
let fullResponse = "";
// 实时显示回复
for await (let chunk of res.textStream) {
fullResponse += chunk;
this.updateDisplay(fullResponse);
}
// 记录对话历史
this.history.push({ role: "user", content: message });
this.history.push({ role: "assistant", content: fullResponse });
return fullResponse;
}
private updateDisplay(text: string) {
const chatArea = document.getElementById("chat-area");
chatArea.innerHTML = text;
chatArea.scrollTop = chatArea.scrollHeight;
}
clearHistory() {
this.history = [];
}
getHistory() {
return this.history;
}
}
// 使用示例
const chatSession = new ChatSession();
await chatSession.sendMessage("botId-xxx", "你好,请介绍一下你自己");
await chatSession.sendMessage("botId-xxx", "你能帮我写一首诗吗?");
// 构建实时聊天应用
class ChatApp {
private botId: string;
private conversationId: string;
constructor(botId: string) {
this.botId = botId;
this.setupEventListeners();
}
private setupEventListeners() {
const sendBtn = document.getElementById("send-btn");
const input = document.getElementById("message-input") as HTMLInputElement;
sendBtn.addEventListener("click", () => {
this.sendMessage(input.value);
input.value = "";
});
input.addEventListener("keypress", (e) => {
if (e.key === "Enter") {
this.sendMessage(input.value);
input.value = "";
}
});
}
async sendMessage(message: string) {
if (!message.trim()) return;
// 显示用户消息
this.displayMessage("user", message);
try {
const res = await ai.bot.sendMessage({
botId: this.botId,
history: this.getChatHistory(),
msg: message,
});
// 显示流式回复
const assistantDiv = this.displayMessage("assistant", "");
for await (let chunk of res.textStream) {
assistantDiv.innerHTML += chunk;
assistantDiv.scrollIntoView({ behavior: "smooth" });
}
} catch (error) {
this.displayMessage("error", "发送消息失败: " + error.message);
}
}
private displayMessage(role: string, content: string): HTMLDivElement {
const chatContainer = document.getElementById("chat-container");
const messageDiv = document.createElement("div");
messageDiv.className = `message ${role}`;
messageDiv.innerHTML = content;
chatContainer.appendChild(messageDiv);
chatContainer.scrollTop = chatContainer.scrollHeight;
return messageDiv;
}
private getChatHistory() {
const messages = document.querySelectorAll(".message");
const history = [];
for (let message of messages) {
const role = message.classList.contains("user") ? "user" : "assistant";
const content = message.innerHTML;
history.push({ role, content });
}
return history;
}
}
// 使用示例
const chatApp = new ChatApp("botId-xxx");
getChatRecords
function getChatRecords(props: {
botId: string;
sort: string;
pageSize: number;
pageNumber: number;
}): Promise<ChatRecordsResult>;
获取聊天记录。
- 获取指定 Agent 的历史聊天记录
- 支持分页查询和排序
- 返回完整的对话历史信息
- 适用于构建聊天历史查看功能
参数
获取聊天记录的参数
返回
聊天记录查询结果
示例
- 基础使用
- 分页查询
- 聊天历史界面
const records = await ai.bot.getChatRecords({
botId: "botId-xxx",
pageNumber: 1,
pageSize: 10,
sort: "asc",
});
console.log("总记录数:", records.total);
console.log("记录列表:", records.recordList);
// 分页查询聊天记录
class ChatHistoryManager {
private botId: string;
private currentPage: number = 1;
private pageSize: number = 20;
constructor(botId: string) {
this.botId = botId;
}
async loadPage(pageNumber: number) {
const records = await ai.bot.getChatRecords({
botId: this.botId,
pageNumber: pageNumber,
pageSize: this.pageSize,
sort: "desc", // 按时间倒序排列
});
this.currentPage = pageNumber;
return records;
}
async nextPage() {
return this.loadPage(this.currentPage + 1);
}
async previousPage() {
if (this.currentPage > 1) {
return this.loadPage(this.currentPage - 1);
}
return null;
}
getTotalPages(totalRecords: number) {
return Math.ceil(totalRecords / this.pageSize);
}
}
// 使用示例
const historyManager = new ChatHistoryManager("botId-xxx");
const firstPage = await historyManager.loadPage(1);
console.log("第1页记录:", firstPage.recordList);
const secondPage = await historyManager.nextPage();
console.log("第2页记录:", secondPage.recordList);
// 构建聊天历史查看界面
class ChatHistoryViewer {
private botId: string;
constructor(botId: string) {
this.botId = botId;
this.setupUI();
}
private setupUI() {
const historyContainer = document.getElementById("history-container");
const prevBtn = document.getElementById("prev-btn");
const nextBtn = document.getElementById("next-btn");
const pageInfo = document.getElementById("page-info");
prevBtn.addEventListener("click", () => this.previousPage());
nextBtn.addEventListener("click", () => this.nextPage());
}
async loadHistory(pageNumber: number = 1) {
const records = await ai.bot.getChatRecords({
botId: this.botId,
pageNumber: pageNumber,
pageSize: 10,
sort: "desc",
});
this.displayRecords(records.recordList);
this.updatePageInfo(pageNumber, records.total);
}
private displayRecords(records: any[]) {
const container = document.getElementById("records-container");
container.innerHTML = "";
records.forEach((record) => {
const recordDiv = document.createElement("div");
recordDiv.className = "chat-record";
recordDiv.innerHTML = `
<div class="record-header">
<span class="role">${record.role}</span>
<span class="time">${record.createTime}</span>
</div>
<div class="content">${record.content}</div>
`;
container.appendChild(recordDiv);
});
}
private updatePageInfo(currentPage: number, total: number) {
const pageInfo = document.getElementById("page-info");
const totalPages = Math.ceil(total / 10);
pageInfo.textContent = `第${currentPage}页,共${totalPages}页,总计${total}条记录`;
}
async nextPage() {
const currentPage = parseInt(
document.getElementById("page-info").textContent.match(/第(\d+)页/)[1]
);
await this.loadHistory(currentPage + 1);
}
async previousPage() {
const currentPage = parseInt(
document.getElementById("page-info").textContent.match(/第(\d+)页/)[1]
);
if (currentPage > 1) {
await this.loadHistory(currentPage - 1);
}
}
}
// 使用示例
const historyViewer = new ChatHistoryViewer("botId-xxx");
await historyViewer.loadHistory(1);
sendFeedback
function sendFeedback(props: {
userFeedback: IUserFeedback;
botId: string;
}): Promise<void>;
发送对某条聊天记录的反馈信息。
- 对指定的聊天记录进行评价和反馈
- 支持评分、评论、标签等多种反馈方式
- 帮助改进 Agent 的回答质量
- 适用于构建用户反馈系统
参数
发送反馈的参数
返回
无返回值
示例
- 基础反馈
- 用户反馈系统
- 批量反馈处理
const res = await ai.bot.sendFeedback({
userFeedback: {
botId: "botId-xxx",
recordId: "recordId-xxx",
comment: "非常棒",
rating: 5,
tags: ["优美"],
aiAnswer: "落英缤纷",
input: "来个成语",
type: "upvote",
},
botId: "botId-xxx",
});
console.log("反馈发送成功");
// 构建用户反馈系统
class FeedbackSystem {
private botId: string;
constructor(botId: string) {
this.botId = botId;
this.setupFeedbackUI();
}
private setupFeedbackUI() {
const feedbackButtons = document.querySelectorAll(".feedback-btn");
feedbackButtons.forEach((button) => {
button.addEventListener("click", (e) => {
const recordId = e.target.dataset.recordId;
const type = e.target.dataset.type; // upvote 或 downvote
this.showFeedbackDialog(recordId, type);
});
});
}
private showFeedbackDialog(recordId: string, type: string) {
const dialog = document.getElementById("feedback-dialog");
const submitBtn = document.getElementById("submit-feedback");
const ratingInput = document.getElementById("rating") as HTMLInputElement;
const commentInput = document.getElementById(
"comment"
) as HTMLTextAreaElement;
const tagsInput = document.getElementById("tags") as HTMLInputElement;
dialog.style.display = "block";
submitBtn.onclick = async () => {
await this.submitFeedback(recordId, type, {
rating: parseInt(ratingInput.value),
comment: commentInput.value,
tags: tagsInput.value.split(",").map((tag) => tag.trim()),
});
dialog.style.display = "none";
};
}
async submitFeedback(
recordId: string,
type: string,
feedback: {
rating: number;
comment: string;
tags: string[];
}
) {
try {
await ai.bot.sendFeedback({
userFeedback: {
botId: this.botId,
recordId: recordId,
comment: feedback.comment,
rating: feedback.rating,
tags: feedback.tags,
aiAnswer: this.getCurrentAnswer(),
input: this.getCurrentQuestion(),
type: type,
},
botId: this.botId,
});
this.showSuccessMessage("感谢您的反馈!");
} catch (error) {
this.showErrorMessage("反馈发送失败: " + error.message);
}
}
private getCurrentAnswer(): string {
// 获取当前对话的回答内容
const answerElement = document.querySelector(
".assistant-message:last-child"
);
return answerElement?.textContent || "";
}
private getCurrentQuestion(): string {
// 获取当前对话的问题内容
const questionElement = document.querySelector(".user-message:last-child");
return questionElement?.textContent || "";
}
private showSuccessMessage(message: string) {
const notification = document.createElement("div");
notification.className = "notification success";
notification.textContent = message;
document.body.appendChild(notification);
setTimeout(() => notification.remove(), 3000);
}
private showErrorMessage(message: string) {
const notification = document.createElement("div");
notification.className = "notification error";
notification.textContent = message;
document.body.appendChild(notification);
setTimeout(() => notification.remove(), 5000);
}
}
// 使用示例
const feedbackSystem = new FeedbackSystem("botId-xxx");
// 批量处理用户反馈
class BatchFeedbackProcessor {
private botId: string;
private feedbackQueue: Array<{
recordId: string;
type: string;
rating: number;
comment: string;
tags: string[];
}> = [];
constructor(botId: string) {
this.botId = botId;
this.startProcessing();
}
addFeedback(
recordId: string,
type: string,
rating: number,
comment: string,
tags: string[]
) {
this.feedbackQueue.push({ recordId, type, rating, comment, tags });
}
private async startProcessing() {
setInterval(async () => {
if (this.feedbackQueue.length > 0) {
await this.processBatch();
}
}, 5000); // 每5秒处理一次
}
private async processBatch() {
const batch = this.feedbackQueue.splice(0, 10); // 每次处理最多10个反馈
const promises = batch.map((feedback) => {
return ai.bot.sendFeedback({
userFeedback: {
botId: this.botId,
recordId: feedback.recordId,
comment: feedback.comment,
rating: feedback.rating,
tags: feedback.tags,
aiAnswer: "", // 需要根据实际情况获取
input: "", // 需要根据实际情况获取
type: feedback.type,
},
botId: this.botId,
});
});
try {
await Promise.all(promises);
console.log(`成功处理 ${batch.length} 个反馈`);
} catch (error) {
console.error("批量处理反馈失败:", error);
// 将失败的反馈重新加入队列
this.feedbackQueue.unshift(...batch);
}
}
getQueueSize() {
return this.feedbackQueue.length;
}
}
// 使用示例
const feedbackProcessor = new BatchFeedbackProcessor("botId-xxx");
// 添加多个反馈
feedbackProcessor.addFeedback("record-1", "upvote", 5, "回答很棒", [
"准确",
"详细",
]);
feedbackProcessor.addFeedback("record-2", "downvote", 2, "回答不够准确", [
"不准确",
]);
console.log("当前队列大小:", feedbackProcessor.getQueueSize());
getFeedback
function getFeedback(props: {
botId: string;
type: string;
sender: string;
senderFilter: string;
minRating: number;
maxRating: number;
from: number;
to: number;
pageSize: number;
pageNumber: number;
}): Promise<FeedbackResult>;
获取已存在的反馈信息。
- 查询指定 Agent 的用户反馈记录
- 支持多种过滤条件:时间范围、评分范围、用户过滤等
- 返回分页的反馈结果和统计信息
- 适用于构建反馈分析和管理系统
参数
查询反馈信息的参数
返回
反馈查询结果
示例
- 基础查询
- 反馈分析系统
- 反馈管理界面
const res = await ai.bot.getFeedback({
botId: "botId-xxx",
from: 0,
to: 0,
maxRating: 4,
minRating: 3,
pageNumber: 1,
pageSize: 10,
sender: "user-a",
senderFilter: "include",
type: "upvote",
});
console.log("反馈总数:", res.total);
console.log("反馈列表:", res.feedbackList);
// 构建反馈分析系统
class FeedbackAnalyzer {
private botId: string;
constructor(botId: string) {
this.botId = botId;
}
async analyzeFeedback(timeRange: { from: number; to: number }) {
const feedback = await ai.bot.getFeedback({
botId: this.botId,
from: timeRange.from,
to: timeRange.to,
minRating: 1,
maxRating: 5,
pageNumber: 1,
pageSize: 1000, // 获取足够多的数据进行分析
sender: "",
senderFilter: "include",
type: "",
});
return this.generateReport(feedback.feedbackList);
}
private generateReport(feedbackList: any[]) {
const report = {
total: feedbackList.length,
upvotes: feedbackList.filter((f) => f.type === "upvote").length,
downvotes: feedbackList.filter((f) => f.type === "downvote").length,
averageRating: this.calculateAverageRating(feedbackList),
commonTags: this.getCommonTags(feedbackList),
ratingDistribution: this.getRatingDistribution(feedbackList),
topComments: this.getTopComments(feedbackList),
};
return report;
}
private calculateAverageRating(feedbackList: any[]): number {
const ratings = feedbackList.map((f) => f.rating).filter((r) => r > 0);
return ratings.length > 0
? ratings.reduce((a, b) => a + b, 0) / ratings.length
: 0;
}
private getCommonTags(feedbackList: any[]): string[] {
const tagCounts = new Map<string, number>();
feedbackList.forEach((feedback) => {
feedback.tags?.forEach((tag) => {
tagCounts.set(tag, (tagCounts.get(tag) || 0) + 1);
});
});
return Array.from(tagCounts.entries())
.sort((a, b) => b[1] - a[1])
.slice(0, 10)
.map((entry) => entry[0]);
}
private getRatingDistribution(feedbackList: any[]): Record<number, number> {
const distribution = { 1: 0, 2: 0, 3: 0, 4: 0, 5: 0 };
feedbackList.forEach((feedback) => {
if (feedback.rating >= 1 && feedback.rating <= 5) {
distribution[feedback.rating]++;
}
});
return distribution;
}
private getTopComments(feedbackList: any[]): any[] {
return feedbackList
.filter((f) => f.comment && f.comment.length > 0)
.sort((a, b) => b.rating - a.rating)
.slice(0, 5);
}
}
// 使用示例
const analyzer = new FeedbackAnalyzer("botId-xxx");
const report = await analyzer.analyzeFeedback({
from: Date.now() - 7 * 24 * 60 * 60 * 1000, // 最近7天
to: Date.now(),
});
console.log("反馈分析报告:", report);
// 构建反馈管理界面
class FeedbackManager {
private botId: string;
constructor(botId: string) {
this.botId = botId;
this.setupUI();
}
private setupUI() {
const filterForm = document.getElementById("feedback-filter-form");
const resultsContainer = document.getElementById("feedback-results");
const exportBtn = document.getElementById("export-feedback");
filterForm.addEventListener("submit", (e) => {
e.preventDefault();
this.loadFeedback(this.getFilterValues());
});
exportBtn.addEventListener("click", () => {
this.exportFeedback();
});
}
private getFilterValues() {
const formData = new FormData(
document.getElementById("feedback-filter-form")
);
return {
type: formData.get("type") as string,
minRating: parseInt(formData.get("minRating") as string),
maxRating: parseInt(formData.get("maxRating") as string),
from: this.parseDate(formData.get("fromDate") as string),
to: this.parseDate(formData.get("toDate") as string),
pageNumber: 1,
pageSize: 50,
};
}
private parseDate(dateString: string): number {
return dateString ? new Date(dateString).getTime() : 0;
}
async loadFeedback(filters: any) {
try {
const res = await ai.bot.getFeedback({
botId: this.botId,
...filters,
sender: "",
senderFilter: "include",
});
this.displayResults(res.feedbackList, res.total);
} catch (error) {
this.showError("加载反馈失败: " + error.message);
}
}
private displayResults(feedbackList: any[], total: number) {
const container = document.getElementById("feedback-results");
container.innerHTML = "";
const summary = document.createElement("div");
summary.className = "feedback-summary";
summary.innerHTML = `共找到 ${total} 条反馈记录`;
container.appendChild(summary);
feedbackList.forEach((feedback) => {
const feedbackCard = this.createFeedbackCard(feedback);
container.appendChild(feedbackCard);
});
}
private createFeedbackCard(feedback: any): HTMLDivElement {
const card = document.createElement("div");
card.className = `feedback-card ${feedback.type}`;
card.innerHTML = `
<div class="feedback-header">
<span class="type ${feedback.type}">${
feedback.type === "upvote" ? "👍" : "👎"
}</span>
<span class="rating">评分: ${feedback.rating}/5</span>
<span class="time">${new Date(
feedback.createTime
).toLocaleString()}</span>
</div>
<div class="feedback-content">
<div class="question"><strong>问题:</strong> ${feedback.input}</div>
<div class="answer"><strong>回答:</strong> ${feedback.aiAnswer}</div>
<div class="comment"><strong>评论:</strong> ${feedback.comment}</div>
<div class="tags"><strong>标签:</strong> ${
feedback.tags?.join(", ") || "无"
}</div>
</div>
`;
return card;
}
private async exportFeedback() {
const res = await ai.bot.getFeedback({
botId: this.botId,
from: 0,
to: Date.now(),
minRating: 1,
maxRating: 5,
pageNumber: 1,
pageSize: 10000,
sender: "",
senderFilter: "include",
type: "",
});
const csv = this.convertToCSV(res.feedbackList);
this.downloadCSV(csv, `feedback_${this.botId}_${Date.now()}.csv`);
}
private convertToCSV(feedbackList: any[]): string {
const headers = ["类型", "评分", "问题", "回答", "评论", "标签", "时间"];
const rows = feedbackList.map((f) => [
f.type,
f.rating,
f.input,
f.aiAnswer,
f.comment,
f.tags?.join(";"),
new Date(f.createTime).toLocaleString(),
]);
return [headers, ...rows]
.map((row) => row.map((cell) => `"${cell}"`).join(","))
.join("\n");
}
private downloadCSV(csv: string, filename: string) {
const blob = new Blob([csv], { type: "text/csv" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = filename;
a.click();
URL.revokeObjectURL(url);
}
private showError(message: string) {
const errorDiv = document.createElement("div");
errorDiv.className = "error-message";
errorDiv.textContent = message;
document.getElementById("feedback-results").appendChild(errorDiv);
}
}
// 使用示例
const feedbackManager = new FeedbackManager("botId-xxx");
uploadFiles
function uploadFiles(props: {
botId: string;
fileList: Array<{
fileId: string;
fileName: string;
type: "file";
}>;
}): Promise<void>;
将云存储中的文件上传至 Agent,用于进行文档聊天。
- 支持将云存储中的文件上传到指定的 Agent
- 上传后的文件可用于文档聊天功能
- 支持批量上传多个文件
- 适用于构建文档问答、文件分析等应用场景
参数
文件上传参数
返回
无返回值
示例
- 基础上传
- 批量上传与文档聊天
- 文档问答系统
// 上传单个文件到指定 Agent
await ai.bot.uploadFiles({
botId: "botId-xxx",
fileList: [
{
fileId: "cloud://document.docx",
fileName: "document.docx",
type: "file",
},
],
});
console.log("文件上传成功");
// 批量上传多个文件
async function uploadAndChat(
botId: string,
files: Array<{ fileId: string; fileName: string }>
) {
// 上传文件
await ai.bot.uploadFiles({
botId: botId,
fileList: files.map((file) => ({
...file,
type: "file" as const,
})),
});
console.log("文件上传完成,开始文档聊天");
// 基于上传的文件进 行聊天
const res = await ai.bot.sendMessage({
botId: botId,
msg: "请分析这些文档的主要内容",
files: files.map((f) => f.fileId),
});
// 流式输出回答
for await (let text of res.textStream) {
console.log(text);
}
}
// 使用示例
await uploadAndChat("your-bot-id", [
{ fileId: "cloud://report.pdf", fileName: "季度报告.pdf" },
{ fileId: "cloud://data.xlsx", fileName: "数据表格.xlsx" },
]);
// 构建文档问答系统
class DocumentQASystem {
private botId: string;
private uploadedFiles: string[] = [];
constructor(botId: string) {
this.botId = botId;
}
async uploadDocuments(files: Array<{ fileId: string; fileName: string }>) {
await ai.bot.uploadFiles({
botId: this.botId,
fileList: files.map((file) => ({
...file,
type: "file" as const,
})),
});
this.uploadedFiles.push(...files.map((f) => f.fileId));
console.log(`已上传 ${files.length} 个文件`);
}
async askQuestion(question: string): Promise<string> {
const res = await ai.bot.sendMessage({
botId: this.botId,
msg: question,
files: this.uploadedFiles,
});
let fullAnswer = "";
for await (let text of res.textStream) {
fullAnswer += text;
}
return fullAnswer;
}
async interactiveChat() {
console.log("文档问答系统已启动,输入 'exit' 退出");
while (true) {
const question = await this.getUserInput("请输入问题: ");
if (question.toLowerCase() === "exit") {
console.log("再见!");
break;
}
console.log("思考中...");
const answer = await this.askQuestion(question);
console.log("回答:", answer);
console.log("---");
}
}
private getUserInput(prompt: string): Promise<string> {
return new Promise((resolve) => {
// 在实际应用中,这里可以是浏览器输入框或命令行输入
const input = prompt;
resolve(input);
});
}
}
// 使用示例
const qaSystem = new DocumentQASystem("doc-bot-id");
// 上传文档
await qaSystem.uploadDocuments([
{ fileId: "cloud://manual.pdf", fileName: "产品手册.pdf" },
{ fileId: "cloud://faq.docx", fileName: "常见问题.docx" },
]);
// 开始交互式问答
await qaSystem.interactiveChat();
getRecommendQuestions
function getRecommendQuestions(props: {
botId: string;
name: string;
introduction: string;
agentSetting: string;
msg: string;
history: Array<{
role: string;
content: string;
}>;
}): Promise<StreamResult>;
获取推荐的问题,基于对话上下文智能生成相关问题建议。
- 根据当前对话内容和历史记录智能推荐相关问题
- 支持流式返回,实时获取推荐问题
- 适用于构建智能对话助手、聊天机器人等应用
- 帮助用户发现更多相关话题,提升对话体验
参数
获取推荐问题的参数
返回
流式返回的推荐问题结果
示例
- 基础使用
- 智能对话助手
- 电商客服场景
// 获取推荐问题
const res = await ai.bot.getRecommendQuestions({
botId: "botId-xxx",
name: "智能助手",
introduction: "我是一个智能对话助手",
agentSetting: "专业、友好的AI助手",
msg: "你好,我想了解人工智能",
history: [
{ content: "你是谁啊", role: "user" },
{ content: "我是一个智能助手,可以帮助你解答各种问题", role: "assistant" },
],
});
// 流式获取推荐问题
for await (let question of res.textStream) {
console.log("推荐问题:", question);
}
// 构建智能对话助手,自动推荐相关问题
class SmartChatAssistant {
private botId: string;
private conversationHistory: Array<{ role: string; content: string }> = [];
constructor(botId: string) {
this.botId = botId;
}
async sendMessage(message: string): Promise<string> {
// 添加用户消息到历史记录
this.conversationHistory.push({ role: "user", content: message });
// 发送消息并获取回答
const response = await ai.bot.sendMessage({
botId: this.botId,
msg: message,
history: this.conversationHistory,
});
let assistantReply = "";
for await (let text of response.textStream) {
assistantReply += text;
}
// 添加助手回答到历史记录
this.conversationHistory.push({
role: "assistant",
content: assistantReply,
});
return assistantReply;
}
async getRecommendedQuestions(): Promise<string[]> {
if (this.conversationHistory.length === 0) {
return [];
}
const latestMessage =
this.conversationHistory[this.conversationHistory.length - 1].content;
const res = await ai.bot.getRecommendQuestions({
botId: this.botId,
name: "智能助手",
introduction: "智能对话助手",
agentSetting: "",
msg: latestMessage,
history: this.conversationHistory,
});
const questions: string[] = [];
for await (let question of res.textStream) {
questions.push(question);
}
return questions;
}
async interactiveChat() {
console.log(
"智能助手已启动,输入 'exit' 退出,输入 'suggest' 获取推荐问题"
);
while (true) {
const userInput = await this.getUserInput("请输入: ");
if (userInput.toLowerCase() === "exit") {
console.log("再见!");
break;
}
if (userInput.toLowerCase() === "suggest") {
console.log("正在为您推荐相关问题...");
const questions = await this.getRecommendedQuestions();
console.log("推荐问题:");
questions.forEach((q, i) => console.log(`${i + 1}. ${q}`));
continue;
}
console.log("思考中...");
const reply = await this.sendMessage(userInput);
console.log("助手回答:", reply);
// 自动推荐相关问题
console.log("\n您可能还想了解:");
const questions = await this.getRecommendedQuestions();
questions.slice(0, 3).forEach((q, i) => console.log(`${i + 1}. ${q}`));
console.log("---");
}
}
private getUserInput(prompt: string): Promise<string> {
return new Promise((resolve) => {
// 实际应用中这里可以是浏览器输入框
const input = prompt;
resolve(input);
});
}
}
// 使用示例
const assistant = new SmartChatAssistant("smart-bot-id");
await assistant.interactiveChat();
// 电商客服场景下的推荐问题应用
class EcommerceCustomerService {
private botId: string;
constructor(botId: string) {
this.botId = botId;
}
async handleCustomerQuery(
query: string
): Promise<{ answer: string; suggestions: string[] }> {
const response = await ai.bot.sendMessage({
botId: this.botId,
msg: query,
history: [],
});
let answer = "";
for await (let text of response.textStream) {
answer += text;
}
// 获取相关推荐问题
const recommendRes = await ai.bot.getRecommendQuestions({
botId: this.botId,
name: "电商客服",
introduction: "专业的电商客服助手",
agentSetting: "专注于解答电商相关问题",
msg: query,
history: [
{ role: "user", content: query },
{ role: "assistant", content: answer },
],
});
const suggestions: string[] = [];
for await (let suggestion of recommendRes.textStream) {
suggestions.push(suggestion);
}
return { answer, suggestions };
}
// 处理常见电商场景
async handleProductInquiry(productName: string) {
const query = `我想了解 ${productName} 这个产品`;
const result = await this.handleCustomerQuery(query);
console.log("客服回答:", result.answer);
console.log("\n您可能还想了解:");
result.suggestions.forEach((suggestion, index) => {
console.log(`${index + 1}. ${suggestion}`);
});
return result;
}
async handleOrderInquiry(orderId: string) {
const query = `查询订单 ${orderId} 的状态`;
const result = await this.handleCustomerQuery(query);
console.log("订单状态:", result.answer);
console.log("\n相关服务:");
result.suggestions.forEach((suggestion, index) => {
console.log(`${index + 1}. ${suggestion}`);
});
return result;
}
}
// 使用示例
const customerService = new EcommerceCustomerService("ecommerce-bot-id");
// 处理产品咨询
await customerService.handleProductInquiry("iPhone 15");
// 处理订单查询
await customerService.handleOrderInquiry("ORD20231226001");
createConversation
function createConversation(props: {
botId: string;
title?: string;
}): Promise<IConversation>;
创建与 Agent 的新对话,建立独立的对话会话。
- 为指定的 Agent 创建新的对话会话
- 支持自定义对话标题,便于管理和识别
- 每个对话会话独立存储对话历史
- 适用于多用户、多场景的对话管理需求
参数
创建对话的参数
返回
创建的对话信息
示例
- 基础创建
- 多用户对话管理
- 多主题对话管理
// 创建新的对话会话
const conversation = await ai.bot.createConversation({
botId: "botId-xxx",
title: "技术咨询对话",
});
console.log("对话创建成功:", conversation.conversationId);
console.log("对话标题:", conversation.title);
// 多用户对话管理系统
class ConversationManager {
private botId: string;
private userConversations: Map<string, string> = new Map(); // userId -> conversationId
constructor(botId: string) {
this.botId = botId;
}
async createUserConversation(
userId: string,
title?: string
): Promise<string> {
const conversation = await ai.bot.createConversation({
botId: this.botId,
title: title || `${userId}的对话`,
});
this.userConversations.set(userId, conversation.conversationId);
console.log(`为用户 ${userId} 创建对话: ${conversation.conversationId}`);
return conversation.conversationId;
}
async getUserConversation(userId: string): Promise<string | null> {
return this.userConversations.get(userId) || null;
}
async sendMessageToUser(userId: string, message: string): Promise<string> {
let conversationId = await this.getUserConversation(userId);
if (!conversationId) {
conversationId = await this.createUserConversation(userId);
}
const response = await ai.bot.sendMessage({
botId: this.botId,
msg: message,
conversationId: conversationId,
});
let reply = "";
for await (let text of response.textStream) {
reply += text;
}
return reply;
}
async getConversationHistory(userId: string) {
const conversationId = await this.getUserConversation(userId);
if (!conversationId) {
return [];
}
const conversation = await ai.bot.getConversation({
botId: this.botId,
conversationId: conversationId,
});
return conversation.history || [];
}
}
// 使用示例
const manager = new ConversationManager("support-bot-id");
// 为用户创建对话
await manager.createUserConversation("user123", "技术支持对话");
// 发送消息
const reply = await manager.sendMessageToUser("user123", "我的账户有问题");
console.log("助手回复:", reply);
// 获取对话历史
const history = await manager.getConversationHistory("user123");
console.log("对话历史:", history);
// 多主题对话管理系统
class MultiTopicConversationManager {
private botId: string;
private topicConversations: Map<string, string> = new Map(); // topic -> conversationId
constructor(botId: string) {
this.botId = botId;
}
async createTopicConversation(topic: string): Promise<string> {
const conversation = await ai.bot.createConversation({
botId: this.botId,
title: `关于${topic}的讨论`,
});
this.topicConversations.set(topic, conversation.conversationId);
console.log(`创建主题对话: ${topic} -> ${conversation.conversationId}`);
return conversation.conversationId;
}
async switchTopic(topic: string, message: string): Promise<string> {
let conversationId = this.topicConversations.get(topic);
if (!conversationId) {
conversationId = await this.createTopicConversation(topic);
}
const response = await ai.bot.sendMessage({
botId: this.botId,
msg: message,
conversationId: conversationId,
});
let reply = "";
for await (let text of response.textStream) {
reply += text;
}
return reply;
}
async getTopicSummary(topic: string): Promise<string> {
const conversationId = this.topicConversations.get(topic);
if (!conversationId) {
return "该主题暂无对话记录";
}
const conversation = await ai.bot.getConversation({
botId: this.botId,
conversationId: conversationId,
});
const history = conversation.history || [];
const messageCount = history.length;
const lastMessage =
history.length > 0 ? history[history.length - 1].content : "无";
return `主题: ${topic}\n对话数量: ${messageCount}\n最后消息: ${lastMessage}`;
}
listTopics(): string[] {
return Array.from(this.topicConversations.keys());
}
}
// 使用示例
const topicManager = new MultiTopicConversationManager("multi-topic-bot-id");
// 在不同主题间切换对话
const techReply = await topicManager.switchTopic(
"技术问题",
"如何解决这个bug?"
);
console.log("技术问题回复:", techReply);
const businessReply = await topicManager.switchTopic(
"商务合作",
"我们想谈合作"
);
console.log("商务合作回复:", businessReply);
// 回到技术问题继续对话
const techReply2 = await topicManager.switchTopic(
"技术问题",
"还有其他解决方案吗?"
);
console.log("