通过 SDK/API 调用
前往 云开发平台/环境配置/API Key 配置页面 即可新建 API Key。
获取到 API Key 后,即可进行 HTTP API 调用,只需要在 HTTP 请求中附带 Authorization: Bearer <您的 API Key> 请求头。
ibot 类型的 Agent
小程序调用
// 初始化
wx.cloud.init({
env: "<云开发环境ID>",
});
// 用户的输入,这里我们以某个报错信息为例
const userInput =
"我的小程序这个报错是什么意思:FunctionName parameter could not be found";
const res = await wx.cloud.extend.AI.bot.sendMessage({
data: {
botId: "<AgentID>", // 第2步中获取的Agent唯一标识
msg: userInput, // 用户的输入
history: [], // 历史对话的内容,这里我们是第一轮对话,所以可以不传入
},
});
for await (let x of res.textStream) {
console.log(x);
}
// 输出结果:
// "### 报错解释\n"
// "**错误信息:** `FunctionName \n"
// "parameter could not be found` \n
// "这个错误通常表示在调用某个函数时,\n"
// "指定的函数名参数没有找到。具体来说,\n"
// "可能是以下几种情况之一:\n"
// ……
我们也可以把对话内容记录下来,重复调用 Agent 的接口,从而实现多轮对话
const res = await wx.cloud.extend.AI.bot.sendMessage({
data: {
botId: "xxx-bot-id", // 第2步中获取的Agent唯一标识
msg: userInput, // 用户的输入
history: [
{ role: "user", message: "我这个报错是什么意思?……"},
{ role: "bot", message: "### 报错解释……" },
{ role: "user", message: "那我要如何操作来修复呢?" }
// ……
]
},
});
云开发在 SDK 中提供了一整套接入 Agent(智能体)的 API 接口,包括基础对话、对话历史保存、对话反馈收集、次轮问题推荐等。
小程序开发者可在云开发平台中创建 Agent,然后在小程序前端代码中直接调用 wx.cloud.extend.AI 下的各类接口直接与 Agent 进行交互,包含:
- 获取聊天记录
- 发送、获取用户反馈
- 获取推荐次轮问题
下面是一些代码示例:
获取聊天记录
await wx.cloud.extend.AI.bot.getChatRecords({
botId: "botId-xxx",
pageNumber: 1,
pageSize: 10,
sort: "asc",
});
传入 botId、分页信息和排序方式,获取指定 Agent 的聊天记录。
发送反馈与获取反馈
发送用户反馈:
const res = await wx.cloud.extend.AI.bot.sendFeedback({
userFeedback: {
botId: "botId-xxx",
recordId: "recordId-xxx",
comment: "非常棒",
rating: 5,
tags: ["优美"],
aiAnswer: "落英缤纷",
input: "来个成语",
type: "upvote",
},
});
获取次轮推荐问题
const res = await wx.cloud.extend.AI.bot.getRecommendQuestions({
data: {
botId: "xxx-bot-id",
msg: "介绍一下 Python 语言",
},
});
for await (let x of res.textStream) {
console.log(x);
}
在 data 参数中设置 botId 和用户消息 msg,通过遍历 textStream 获取推荐问题。
cURL 示例
以下是一个使用 cURL 调用 AI Agent HTTP API 的示例:
curl 'https://<your-envId>.api.tcloudbasegateway.com/v1/aibot/bots/<your-botId>/send-message' \
-H 'Authorization: Bearer <您的 API Key>' \
-H 'Accept: text/event-stream' \
-H 'Content-Type: application/json' \
--data-raw '{"msg":"hi"}'