Agent API
The current cloudbase SDK includes a built-in AI module that allows you to call Agents and implement conversations with Agents.
Before calling an Agent, you need to initialize the SDK. Please refer to "SDK Initialization" to proceed.
After completing SDK initialization, you can use the SDK to interact with Agents:
First, check the list of existing Agents:
const list = await ai.bot.list({ pageNumber: 1, pageSize: 10 });
console.log(list);
The returned Agent list will display the Agent's id, name, settings, and other information. In most Agent interfaces, you use the Agent id to specify which Agent to interact with.
Next, we'll chat with the selected Agent:
const res = await ai.bot.sendMessage({
botId: "botId-xxx", // Enter the Agent id
msg: "Hello",
});
for await (let str of res.textStream) {
console.log(str);
}
Through this concise streaming interface, we've completed a conversation with the Agent.
In addition to simple conversations, we provide other useful Agent interfaces. Complete usage examples are as follows:
Usage Examples
Chat with Agent
This interface is a streaming call.
const res = await ai.bot.sendMessage({
botId: "botId-xxx",
msg: "Give me an idiom.",
});
for await (let str of res.textStream) {
console.log(str);
}
// Of course
// you can
// !
// Here
// is an
// idiom
// :
//
//
// **
// Drawing
// a snake
// and
// adding
// feet
// **
//
//
// This idiom means
// adding feet to
// a snake while
// drawing it,
// later it's a metaphor
// for doing unnecessary
// things, not only
// useless, but also
// inappropriate,
// and it also means
// fabricating facts
// , inventing something
// from nothing
// .
Reference documentation: Bot.sendMessage().
View Agent
const res = await ai.bot.get({ botId: "botId-xxx" });
console.log(res);
// {
// "botId": "bot-809d4ad1",
// "name": "Agent",
// "agentSetting": "This is an Agent",
// "introduction": "Introduction",
// "welcomeMessage": "Welcome",
// "avatar": "http://xxx.avatar.image",
// "background": "http://xxx.avatar.image",
// "isNeedRecommend": false,
// "knowledgeBase": [
// ""
// ],
// "initQuestions": [
// "Is there anything I can help you with?"
// ],
// "enable": true,
// "type": "text",
// "tags": [
// ""
// ]
// }
Reference documentation: Bot.get().
View Chat History with Agent
const res = await ai.bot.getChatRecords({
botId: "botId-xxx",
pageNumber: 1,
pageSize: 10,
sort: "asc",
});
// {
// "recordList": [
// {
// "botId": "bot-809d4ad1",
// "recordId": "record-96617446",
// "role": "user",
// "content": "Hello",
// "conversation": "r3Hjz3qgms3UG7z9lwSYbA",
// "type": "text",
// "triggerSrc": "TCB"
// },
// {
// "botId": "bot-809d4ad1",
// "recordId": "record-632906dd",
// "role": "assistant",
// "content": "Hello! It looks like you might be facing some technical issues, or just want to say hi. Either way, I'm here to help. If you have any specific questions or need assistance, please feel free to tell me!",
// "conversation": "r3Hjz3qgms3UG7z9lwSYbA",
// "type": "text",
// "triggerSrc": "TCB"
// }
// ],
// "total": 2
// }
Reference documentation: Bot.getChatRecords().
Provide Feedback on a Chat Record
await ai.bot.sendFeedback({
userFeedback: {
botId: "botId-xxx",
recordId: "recordId-xxx",
comment: "I think your answer is great",
rating: 5,
tags: ["on-topic"],
aiAnswer: "Li Bai was a great poet of the Tang Dynasty.",
input: "Introduce Li Bai in one sentence.",
type: "upvote",
},
});
Reference documentation: Bot.sendFeedback().
View Feedback Records
await ai.bot.getFeedback({
botId: "botId-xxx",
});
// {
// "feedbackList": [
// {
// "comment": "dawd",
// "botId": "bot-809d4ad1",
// "aiAnswer": "ababa",
// "createTime": "2024-09-10T07:03:45.000Z",
// "input": "You're not good",
// "rating": 3,
// "sender": "r3Hjz3qgms3UG7z9lwSYbA",
// "tags": [],
// "type": "downvote"
// },
// {
// "comment": "dwajoidaw",
// "botId": "bot-809d4ad1",
// "aiAnswer": "ababa",
// "createTime": "2024-09-10T07:06:43.000Z",
// "input": "You're not good",
// "rating": 2,
// "sender": "r3Hjz3qgms3UG7z9lwSYbA",
// "tags": [],
// "type": "downvote"
// }
// ],
// "total": 2
// }
Reference documentation: Bot.getFeedBack().
Get Agent Recommended Questions
This interface is a streaming call.
const res = await ai.bot.getRecommendQuestions({
botId: "botId-xxx",
history: [{ content: "Who is Li Bai", role: "user" }],
msg: "Hello",
agentSetting: "",
introduction: "",
name: "",
});
for await (let str of res.textStream) {
console.log(str);
}
// What do
// you do
//
// What
// services
// do you
// provide
//
// How do
// I use
// your
// features
Reference documentation: Bot.getRecommendQuestions().
View Agent List
const list = await ai.bot.list({ pageNumber: 1, pageSize: 10 });
console.log(list);
// {
// "botList": [
// {
// "botId": "bot-c033e89e",
// "name": "Agent",
// "agentSetting": "This is an Agent",
// "introduction": "Introduction",
// "welcomeMessage": "Welcome",
// "avatar": "http://xxx.avatar.image",
// "background": "http://xxx.avatar.image",
// "isNeedRecommend": false,
// "knowledgeBase": [
// ""
// ],
// "initQuestions": [
// "Is there anything I can help you with?"
// ],
// "enable": true,
// "type": "text",
// "tags": [
// ""
// ]
// }
// ],
// "total": 1
// }
Reference documentation: Bot.list().
API Reference
For more details, see Reference Documentation | AI SDK.