Overview
Provides Cloud Development AI integration capabilities, enabling rapid access to large language models and AI agents.
Basic Usage Examples
- Initial Configuration
- Basic Text Generation
- Streaming Text Generation
- Agent Chat
Publishable Key can be generated in Cloud Development Platform/API Key Configuration
Type Declaration
function ai(): AI;
Return Value
Returns the newly created AI instance.
import cloudbase from "@cloudbase/js-sdk";
// Initialization
const app = cloudbase.init({
env: "your-env-id", // Replace with your environment ID
region: "ap-shanghai", // region, defaults to Shanghai
accessKey: "", // Enter the generated Publishable Key
});
// If the accessKey is filled in, this step is not required
await app.auth.signInAnonymously();
const ai = app.ai();
// Basic Text Generation Example
async function generateText() {
const model = ai.createModel("hunyuan-exp");
const result = await model.generateText({
model: "hunyuan-lite",
messages: [{ role: "user", content: "Hello, please introduce Li Bai." }],
});
console.log("Generated text:", result.text);
console.log("Consumed tokens:", result.usage);
}
// Streaming Text Generation Example
async function streamText() {
const model = ai.createModel("hunyuan-exp");
const result = await model.streamText({
model: "hunyuan-lite",
messages: [{ role: "user", content: "what is 1+1" }],
});
for await (let chunk of result.textStream) {
console.log("Received text chunk:", chunk);
}
}
// Agent Chat Example
async function chatWithAgent() {
const res = await ai.bot.sendMessage({
botId: "botId-xxx",
msg: "Hello, please introduce yourself.",
history: [],
});
for await (let text of res.textStream) {
console.log("Agent reply:", text);
}
}
AI
Class used to create AI models.
createModel
function createModel(model: string): ChatModel;
Create the specified AI model.
- Create a new AI model instance
- Return a model instance that implements the ChatModel abstract class
- This instance provides capabilities related to AI-generated text.
参数
Model identifier, such as 'hunyuan-exp', 'hunyuan-lite', etc.
返回
A model instance that implements the ChatModel abstract class, providing capabilities related to AI-generated text
示例
bot
An instance of the Bot class is mounted, which aggregates a series of methods for interacting with the Agent. For details, refer to the Bot class documentation.
Usage Examples
const agentList = await ai.bot.list({ pageNumber: 1, pageSize: 10 });
registerFunctionTool
function registerFunctionTool(functionTool: FunctionTool): void;
Register function tools. When making large model calls, you can inform the large model of the available function tools. If the large model's response is parsed as a tool call, the corresponding function tool will be automatically invoked.
参数
Definition of the function tool to be registered
返回
No return value
示例
ChatModel
This abstract class describes the interface provided by the AI text generation model class.
generateText
function generateText(data: BaseChatModelInput): Promise<{
rawResponses: Array<unknown>;
text: string;
messages: Array<ChatModelMessage>;
usage: Usage;
error?: unknown;
}>;
Invoking large models to generate text.
- Send a message to the large model and retrieve the generated text response
- Supports complete dialogue context management
- Return detailed invocation information and token consumption statistics
参数
Large model input parameters, including model configuration and message content
返回
Generated text response from the large model
示例
streamText
function streamText(data: BaseChatModelInput): Promise<StreamTextResult>;
Streaming invocation of large models to generate text.
- During streaming invocation, the generated text and other response data will be returned via SSE. The return value of this interface encapsulates SSE to varying degrees, allowing developers to retrieve both the text stream and the complete data stream according to their actual needs.
- Streaming invocation of large models to generate text, supporting real-time retrieval of incremental content
参数
Large model input parameters, including model configuration and message content
返回
Results of streaming text generation, including text streams and data streams
示例
Bot
A class for interacting with the Agent.
get
function get(props: { botId: string }): Promise<BotInfo>;
Retrieve information about an Agent.
- Retrieve detailed Agent information based on Agent ID.
- Return complete Agent information including basic configuration, welcome message, avatar, etc.
参数
Parameters for retrieving Agent information
返回
Detailed information of the Agent
示例
list
function list(props: {
name: string;
introduction: string;
information: string;
enable: boolean;
pageSize: number;
pageNumber: number;
}): Promise<AgentListResult>;
Batch retrieval of information for multiple Agents.
- Query and filter the available Agent list.
- Support paginated queries and conditional filtering
- Return Agent's basic information and configuration details
- Applicable for building applications such as Agent selectors and Agent management interfaces.
参数
Parameters for querying the Agent list
返回
Agent list query result
示例
sendMessage
function sendMessage(props: {
botId: string;
msg: string;
history: Array<{
role: string;
content: string;
}>;
}): Promise<StreamResult>;
Converse with the Agent.
- Response will be returned via SSE. The return value of this interface encapsulates SSE to varying degrees, allowing developers to retrieve both the text stream and the complete data stream according to their actual needs.
- Supports multi-turn conversation context management
- Returns streaming responses, supporting real-time retrieval of Agent replies
- Applicable for building applications such as chatbots and intelligent assistants.
参数
Parameters for sending messages
返回
Results of streaming conversation, including text streams and data streams
示例
getChatRecords
function getChatRecords(props: {
botId: string;
sort: string;
pageSize: number;
pageNumber: number;
}): Promise<ChatRecordsResult>;
Retrieve chat history.
- Retrieve the chat history of the specified Agent.
- Support paginated queries and sorting
- Return complete conversation history information
- Suitable for building chat history viewing features
参数
parameters for retrieving chat history
返回
Chat history query result
示例
sendFeedback
function sendFeedback(props: {
userFeedback: IUserFeedback;
botId: string;
}): Promise<void>;
Send feedback for a specific chat message.
- Evaluate and provide feedback for specified chat messages.
- Supports various feedback methods such as rating, commenting, and tagging.
- Help improve the quality of Agent responses
- Suitable for building user feedback systems
参数
Parameters for sending feedback
返回
No return value