跳到主要内容

bot

bot

挂载了 Bot 类的实例,上面集合了一系列与 Agent 交互的方法。具体可参考 Bot 类 的详细文档。

使用示例

const agentList = await ai.bot.list({ pageNumber: 1, pageSize: 10 });

Bot

用于与 Agent 交互的类。

get()

获取某个 Agent 的信息。

使用示例

const res = await ai.bot.get({ botId: "botId-xxx" });
console.log(res);

类型声明

function get(props: { botId: string });

参数

参数名必填类型说明
props.botIdstring要获取信息的 Agent 的 id

返回值

属性名类型示例说明
botIdstring"bot-27973647"Agent ID
namestring"信达雅翻译"Agent 名称
introductionstringAgent 简介
welcomeMessagestringAgent 欢迎语
avatarstringAgent 头像链接
backgroundstringAgent 聊天背景图链接
isNeedRecommendbooleanAgent 回答后是否推荐问题
typestringAgent 类型

list()

批量获取多个 Agent 的信息。

使用示例

await ai.bot.list({
pageNumber: 1,
pageSize: 10,
name: "",
enable: true,
information: "",
introduction: "",
});

类型声明

function list(props: {
name: string;
introduction: string;
information: string;
enable: boolean;
pageSize: number;
pageNumber: number;
});

参数

参数名必填类型说明
props.pageNumbernumber分页下标
props.pageSizenumber分页大小
props.enablebooleanAgent 是否启用
props.namestringAgent 名字,用于模糊查询
props.informationstringAgent 信息,用于模糊查询
props.introductionstringAgent 简介,用于模糊查询

返回值

属性名类型示例说明
totalnumber---Agent 总数
botListArray<object>Agent 列表
botList[n].botIdstring"bot-27973647"Agent ID
botList[n].namestring"信达雅翻译"Agent 名称
botList[n].introductionstringAgent 简介
botList[n].welcomeMessagestringAgent 欢迎语
botList[n].avatarstringAgent 头像链接
botList[n].backgroundstringAgent 聊天背景图链接
botList[n].isNeedRecommendbooleanAgent 回答后是否推荐问题
botList[n].typestringAgent 类型

sendMessage()

与 Agent 进行对话。响应会通过 SSE 返回,该接口的返回值对 SSE 做了不同程度的封装,开发者能根据实际需求获取到文本流和完整数据流。

使用示例

const res = await ai.bot.sendMessage({
botId: "botId-xxx",
history: [{ content: "你是李白。", role: "user" }],
msg: "你好",
});
for await (let str of res.textStream) {
console.log(str);
}
for await (let data of res.dataStream) {
console.log(data);
}

类型声明

function sendMessage(props: {
botId: string;
msg: string;
history: Array<{
role: string;
content: string;
}>;
}): Promise<StreamResult>;

参数

参数名必填类型说明
props.botIdstringAgent id
props.msgstring此次对话要发送的消息
props.history[]在此次对话前发生的聊天记录
props.history[n].rolestring本聊天信息的发送角色
props.history[n].contentstring本聊天信息的内容

返回值

Promise<StreamResult>

StreamResult 属性名类型说明
textStreamAsyncIterable<string>以流式返回的 Agent 生成文本,可参考使用示例获取到生成的增量文本。
dataStreamAsyncIterable<AgentStreamChunk>以流式返回的 Agent 生成文本,可参考使用示例获取到生成的增量文本。
AgentStreamChunk 属性名类型说明
creatednumber对话时间戳
record_idstring对话记录ID
modelstring大模型类型
versionstring大模型版本
typestring回复类型: text: 主要回答内容,thinking: 思考过程,search: 查询结果,knowledge: 知识库
rolestring对话角色,响应中固定为 assistant
contentstring对话内容
finish_reasionstring对话结束标志,continue 表示对话未结束,stop 表示对话结束
reasoning_contentstring深度思考内容(仅deepseek-r1是不为空字符串)
usageobjecttoken使用量
usage.prompt_tokensnumber表示prompt的tokens数,多次返回中保持不变
usage.completion_tokensnumber回答的token总数,在流式返回中,表示到目前为止所有completion的tokens总数,多次返回中持续累加
usage.total_tokensnumber表示prompt_tokens和completion_tokens之和
knowledge_basestring[]对话中使用到的知识库
search_infoobject搜索结果信息,需要开启联网查询
search_info.search_resultsobject[]搜索引文信息
search_info.search_results[n].indexstring搜索引文序号
search_info.search_results[n].titlestring搜索引文标题
search_info.search_results[n].urlstring搜索引文链接

getChatRecords()

获取聊天记录。

使用示例

await ai.bot.getChatRecords({
botId: "botId-xxx",
pageNumber: 1,
pageSize: 10,
sort: "asc",
});

类型声明

function getChatRecords(props: {
botId: string;
sort: string;
pageSize: number;
pageNumber: number;
});

参数

参数名必填类型说明
props.botIdstringAgent id
props.sortstring排序方式
props.pageSizenumber分页大小
props.pageNumbernumber分页下标

返回值

属性名类型说明
totalnumber对话总数
recordListArray<object>对话总数
recordList[n].botIdstringAgent ID
recordList[n].recordIdstring对话 ID,由系统生成
recordList[n].rolestring对话中的角色
recordList[n].contentstring对话内容
recordList[n].conversationstring用户标识
recordList[n].typestring对话数据类型
recordList[n].imagestring对话生成的图片链接
recordList[n].triggerSrcstring对话发起来源
recordList[n].replyTostring对话回复的记录 ID
recordList[n].createTimestring对话时间

sendFeedback()

发送对某条聊天记录的反馈信息。

使用示例

const res = await ai.bot.sendFeedback({
userFeedback: {
botId: "botId-xxx",
recordId: "recordId-xxx",
comment: "非常棒",
rating: 5,
tags: ["优美"],
aiAnswer: "落英缤纷",
input: "来个成语",
type: "upvote",
},
botId: "botId-xxx",
});

类型声明

function sendFeedback(props: { userFeedback: IUserFeedback; botId: string });

参数

参数名必填类型说明
props.userFeedbackIUserFeedback用户反馈,详见 IUserFeedback 类型定义
props.botIdstring将要反馈的 Agent id

getFeedback()

获取已存在的反馈信息。

使用示例

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",
});

类型声明

function sendFeedback(props: {
botId: string;
type: string;
sender: string;
senderFilter: string;
minRating: number;
maxRating: number;
from: number;
to: number;
pageSize: number;
pageNumber: number;
});

参数

参数名必填类型说明
props.botIdstringAgent id
props.typestring用户反馈类型,点赞 upvote 点踩 downvote
props.senderstring评论创建用户
props.senderFilterstring评论创建用户过滤关系 include:包含 exclude:不包含 equal:等于 unequal:不等于 prefix:前缀
props.minRatingnumber最低评分
props.maxRatingnumber最高评分
props.fromnumber开始时间戳
props.tonumber结束时间戳
props.pageSizenumber分页大小
props.pageNumbernumber分页下标

返回值

属性名类型说明
feedbackListobject[]反馈查询结果
feedbackList[n].recordIdstring对话记录 ID
feedbackList[n].typestring用户反馈类型,点赞 upvote 点踩 downvote
feedbackList[n].botIdstringAgent ID
feedbackList[n].commentstring用户评论
feedbackList[n].ratingnumber用户评分
feedbackList[n].tagsstring[]用户反馈的标签数组
feedbackList[n].inputstring用户输入的问题
feedbackList[n].aiAnswerstringAgent 的回答
totalnumber反馈总数

uploadFiles()

将云存储中的文件上传至 Agent,用于进行文档聊天。

使用示例

// 上传文件
await ai.bot.uploadFiles({
botId: "botId-xxx",
fileList: [
{
fileId: "cloud://xxx.docx",
fileName: "xxx.docx",
type: "file",
},
],
});

// 进行文档聊天
const res = await ai.bot.sendMessage({
botId: "your-bot-id",
msg: "这个文件的内容是什么",
files: ["xxx.docx"], // 文件 fileId 数组
});

for await (let text of res.textStream) {
console.let(text);
}

类型声明

function uploadFiles(props: {
botId: string;
fileList: Array<{
fileId: "string";
fileName: "string";
type: "file";
}>;
});

参数

参数名必填类型说明
props.botIdstringAgent id
props.fileListstring文件列表
props.fileList[n].fileIdstring云存储文件 id
props.fileList[n].fileNamestring文件名
props.fileList[n].typestring暂时只支持 "file"

getRecommendQuestions()

获取推荐的问题。

使用示例

const res = ai.bot.getRecommendQuestions({
botId: "botId-xxx",
history: [{ content: "你是谁啊", role: "user" }],
msg: "你好",
agentSetting: "",
introduction: "",
name: "",
});

for await (let str of res.textStream) {
console.log(str);
}

类型声明

function getRecommendQuestions(props: {
botId: string;
name: string;
introduction: string;
agentSetting: string;
msg: string;
history: Array<{
role: string;
content: string;
}>;
}): Promise<StreamResult>;

参数

参数名必填类型说明
props.botIdstringAgent id
props.namestringAgent 名称
props.introductionstringAgent 简介
props.agentSettingstringAgent 设定
props.msgstring用户发送信息
props.historyArray历史对话信息
props.history[n].rolestring历史信息角色
props.history[n].contentstring历史信息内容

返回值

Promise<StreamResult>

StreamResult 属性名类型说明
textStreamAsyncIterable<string>以流式返回的 Agent 生成文本,可参考使用示例获取到生成的增量文本。
dataStreamAsyncIterable<AgentStreamChunk>以流式返回的 Agent 生成文本,可参考使用示例获取到生成的增量文本。
AgentStreamChunk 属性名类型说明
creatednumber对话时间戳
record_idstring对话记录ID
modelstring大模型类型
versionstring大模型版本
typestring回复类型: text: 主要回答内容,thinking: 思考过程,search: 查询结果,knowledge: 知识库
rolestring对话角色,响应中固定为 assistant
contentstring对话内容
finish_reasionstring对话结束标志,continue 表示对话未结束,stop 表示对话结束
reasoning_contentstring深度思考内容(仅deepseek-r1是不为空字符串)
usageobjecttoken使用量
usage.prompt_tokensnumber表示prompt的tokens数,多次返回中保持不变
usage.completion_tokensnumber回答的token总数,在流式返回中,表示到目前为止所有completion的tokens总数,多次返回中持续累加
usage.total_tokensnumber表示prompt_tokens和completion_tokens之和
knowledge_basestring[]对话中使用到的知识库
search_infoobject搜索结果信息,需要开启联网查询
search_info.search_resultsobject[]搜索引文信息
search_info.search_results[n].indexstring搜索引文序号
search_info.search_results[n].titlestring搜索引文标题
search_info.search_results[n].urlstring搜索引文链接

createConversation()

创建与 Agent 的新对话。

使用示例

const res = await ai.bot.createConversation({
botId: "botId-xxx",
title: "我的对话",
}): Promise<IConversation>;

类型声明

function createConversation(props: IBotCreateConversation);

参数

参数名必填类型说明
props.botIdstringAgent ID
props.titlestring对话标题

返回值

Promise<IConversation>

相关类型:IConversation

getConversation()

获取对话列表。

使用示例

const res = await ai.bot.getConversation({
botId: "botId-xxx",
pageSize: 10,
pageNumber: 1,
isDefault: false,
});

类型声明

function getConversation(props: IBotGetConversation);

参数

参数名必填类型说明
props.botIdstringAgent ID
props.pageSizenumber分页大小,默认 10
props.pageNumbernumber分页下标,默认 1
props.isDefaultboolean是否只获取默认对话

deleteConversation()

删除指定对话。

使用示例

await ai.bot.deleteConversation({
botId: "botId-xxx",
conversationId: "conv-123",
});

类型声明

function deleteConversation(props: IBotDeleteConversation);

参数

参数名必填类型说明
props.botIdstringAgent ID
props.conversationIdstring要删除的对话 ID

speechToText()

语音转文字。

使用示例

const res = await ai.bot.speechToText({
botId: "botId-xxx",
engSerViceType: "16k_zh",
voiceFormat: "mp3",
url: "https://example.com/audio.mp3",
});

类型声明

function speechToText(props: IBotSpeechToText);

参数

参数名必填类型说明
props.botIdstringAgent ID
props.engSerViceTypestring引擎类型,如"16k_zh"
props.voiceFormatstring音频格式,如"mp3"
props.urlstring音频文件 URL
props.isPreviewboolean是否为预览模式

textToSpeech()

文字转语音。

使用示例

const res = await ai.bot.textToSpeech({
botId: "botId-xxx",
voiceType: 1,
text: "你好,我是AI助手",
});

类型声明

function textToSpeech(props: IBotTextToSpeech);

参数

参数名必填类型说明
props.botIdstringAgent ID
props.voiceTypenumber语音类型
props.textstring要转换的文本
props.isPreviewboolean是否为预览模式

getTextToSpeechResult()

获取文字转语音的结果。

使用示例

const res = await ai.bot.getTextToSpeechResult({
botId: "botId-xxx",
taskId: "task-123",
});

类型声明

function getTextToSpeechResult(props: IBotGetTextToSpeechResult);

参数

参数名必填类型说明
props.botIdstringAgent ID
props.taskIdstring任务 ID
props.isPreviewboolean是否为预览模式

IBotCreateConversation

interface IBotCreateConversation {
botId: string;
title?: string;
}

IBotGetConversation

interface IBotGetConversation {
botId: string;
pageSize?: number;
pageNumber?: number;
isDefault?: boolean;
}

IBotDeleteConversation

interface IBotDeleteConversation {
botId: string;
conversationId: string;
}

IBotSpeechToText

interface IBotSpeechToText {
botId: string;
engSerViceType: string;
voiceFormat: string;
url: string;
isPreview?: boolean;
}

IBotTextToSpeech

interface IBotTextToSpeech {
botId: string;
voiceType: number;
text: string;
isPreview?: boolean;
}

IBotGetTextToSpeechResult

interface IBotGetTextToSpeechResult {
botId: string;
taskId: string;
isPreview?: boolean;
}

BotInfo

interface BotInfo {
botId: string;
name: string;
introduction: string;
agentSetting: string;
welcomeMessage: string;
avatar: string;
background: string;
tags: Array<string>;
isNeedRecommend: boolean;
knowledgeBase: Array<string>;
type: string;
initQuestions: Array<string>;
enable: true;
}

IUserFeedback

interface IUserFeedback {
recordId: string;
type: string;
botId: string;
comment: string;
rating: number;
tags: Array<string>;
input: string;
aiAnswer: string;
}

IConversation

Agent 会话。

interface IConversation {
id: string;
envId: string;
ownerUin: string;
userId: string;
conversationId: string;
title: string;
startTime: string; // date-time format
createTime: string;
updateTime: string;
}