Introduction
This document mainly introduces developing AI agents based on
Function-based Cloud Hosting
. To learn more aboutFunction-based Cloud Hosting
, see the documentation: https://docs.cloudbase.net/cbrf/intro
CloudBase currently supports two types of Agent services:
CloudBase Agent
: An AI agent quickly built based on platform-preset agent services, requiring no code development. Configure persona, knowledge base, opening remarks, recommended questions, etc., via a visual interface. Basic agent deployment can be completed in 5 minutes, suitable for quick launch in general scenarios.Function-based Agent
: Developing AI agents based onFunction-based Cloud Hosting
. For this type of AI agent, developers can fully control the business logic to meet highly personalized requirements. In functions, they can call third-party models, design complex decision-making processes, and use open-source agent development frameworks to achieve capabilities such asPerception
,Planning
, andAction
for agents, thereby better fulfilling personalized requirements.
The technical architecture of both Agents is as shown in the figure:
For the frontend, the usage of both agents is consistent. Both can connect to the agent backend service through methods such as Agent UI components
, SDK
, or HTTP API
. Developers can choose the agent development approach that best fits their business scenarios based on requirements.
Developing an AI agent based on cloud functions is a process of backend adapting to the frontend. That is, by implementing the agreed-upon API interfaces with the frontend in cloud functions, seamless integration with the frontend can be achieved.
With the help of the AI agent development framework @cloudbase/aiagent-framework
provided by CloudBase, you can quickly implement a simple blank agent based on Function-based Cloud Hosting
with just a few lines of code. The example code is as follows:
const { IBot } = require("@cloudbase/aiagent-framework");
const { BotRunner } = require("@cloudbase/aiagent-framework");
const ANSWER = "Hello, I am an agent, but I can only say this one sentence.";
/**
* @typedef {import('@cloudbase/aiagent-framework').IAbstractBot} IAbstractBot
*
* @class
* @implements {IAbstractBot}
*/
class MyBot extends IBot {
async sendMessage() {
return new Promise((res) => {
// Create a character array
const charArr = ANSWER.split("");
const interval = setInterval(() => {
// Periodically take a character from the array
const char = charArr.shift();
if (typeof char === "string") {
// When there are characters, send an SSE message to the client
this.sseSender.send({ data: { content: char } });
} else {
// When characters run out, end the periodic loop
clearInterval(interval);
// End SSE
this.sseSender.end();
res();
}
}, 50);
});
}
}
/**
* For the complete type definition, please refer to: https://docs.cloudbase.net/cbrf/how-to-writing-functions-code#%E5%AE%8C%E6%95%B4%E7%A4%BA%E4%BE%8B
* "{demo: string}" is a sample type declaration for the event parameter. Please modify it according to your actual situation.
* Type hints will only take effect after installing dependencies with `pnpm install`.
*
* @type {import('@cloudbase/functions-typings').TcbEventFunction<unknown>}
*/
exports.main = function (event, context) {
return BotRunner.run(event, context, new MyBot(context));
};