Skip to main content

Overview

Provide cloud development AI access capability for quick integration with large models and Agent.


Basic Usage Example

Publishable Key can be generated by going to Cloud Development Platform/API Key Configuration

Type Declaration

function ai(): AI;

Return value

Return the newly created AI instance.

import cloudbase from "@cloudbase/js-sdk";

// Initialize it.
const app = cloudbase.init({
env: "your-env-id", // Replace with your environment ID
region: "ap-shanghai", // Region, defaults to Shanghai
accessKey: "", // Fill in the generated Publishable Key
});

// If accessKey is filled in, this step is not required
await app.auth.signInAnonymously();

const ai = app.ai();

AI

Class for creating AI models.

createModel

function createModel(model: string): ChatModel;

Create the specified AI model.

-Create a new AI model instance -Return a model instance implemented the ChatModel abstract class -The instance provides AI text generation capabilities.

参数

model
string

Model identifier, for example 'hunyuan-exp', 'hunyuan-lite'

返回

ChatModel
ChatModel

A model instance implemented the ChatModel abstract class, providing AI text generation capabilities

示例

Create a Hunyuan trial version model
const model = ai.createModel("hunyuan-exp");

Create a Hunyuan lightweight model
const liteModel = ai.createModel("hunyuan-lite");

bot

An instance of the Bot class is mounted, with a collection of methods to interact with the Agent. For details, see the Bot class detailed documentation.

Usage Example

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

registerFunctionTool

function registerFunctionTool(functionTool: FunctionTool): void;

Register function tools. When performing large model invocation, you can inform the large model of available function tools. When the large model's response is resolved to a tool call, it will automatically call the corresponding function tool.

参数

functionTool
FunctionTool

Function tool definition to register

返回

void
undefined

no return value

示例

// Define a tool to get weather
const getWeatherTool = {
name: "get_weather",
description: "Return the weather information of a certain city. Sample call: get_weather({city: 'Beijing'})",
fn: ({ city }) => `${city} weather: clear and crisp autumn air.`
parameters: {
type: "object",
properties: {
city: {
type: "string",
description: "City to query"
},
},
required: ["city"],
},
};

// Registration tool
ai.registerFunctionTool(getWeatherTool);

Use tools to perform dialogue
const model = ai.createModel("hunyuan-exp");
const result = await model.generateText({
model: "hunyuan-turbo",
tools: [getWeatherTool],
messages: [
{
role: "user",
how can i assist you with the weather in Beijing?
},
],
});

console.log(result.text);

ChatModel

This abstract class describes the interface provided by the AI Model Class.

generateText

function generateText(data: BaseChatModelInput): Promise<{
rawResponses: Array<unknown>;
text: string;
messages: Array<ChatModelMessage>;
usage: Usage;
error?: unknown;
}>;

Call the large model to generate text.

-Send message to the large model and obtain the generated text response -Support fully managed dialogue context -Return detailed invocation information and token consumption statistics

参数

data
BaseChatModelInput

Input parameters for the large model, including model configuration and message content

返回

Promise
object

Response result of text generated by the large model

示例

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("token consumption:", result.usage);

streamText

function streamText(data: BaseChatModelInput): Promise<StreamTextResult>;

Call the large model to generate text with streaming.

-When calling with streaming, the generated text and other response data are returned via SSE. This API's returned value encapsulates SSE at different levels, allowing developers to obtain the text stream and complete data stream according to actual needs. -Call the large model in streaming method to generate text, supporting real-time access to incremental content.

参数

data
BaseChatModelInput

Input parameters for the large model, including model configuration and message content

返回

Promise
StreamTextResult

Streaming text generation result, including text stream and data stream

示例

const model = ai.createModel("hunyuan-exp");
const result = await model.streamText({
model: "hunyuan-lite",
messages: [{ role: "user", content: "hello, please introduce Li Bai" }],
});

// Obtain text streams
for await (let chunk of result.textStream) {
console.log("Received text chunk:", chunk);
}

// 1
// Add
// 1
// Result
// Yes
// 2
// .

// Obtain data streams
for await (let data of result.dataStream) {
console.log("Received data chunk:", data);
}

// {created: 1723013866, id: "a95a54b5c5d2144eb700e60d0dfa5c98", model: "hunyuan-lite", version: "202404011000", choices: Array(1), …}
// {created: 1723013866, id: "a95a54b5c5d2144eb700e60d0dfa5c98", model: "hunyuan-lite", version: "202404011000", choices: Array(1), …}
// {created: 1723013866, id: "a95a54b5c5d2144eb700e60d0dfa5c98", model: "hunyuan-lite", version: "202404011000", choices: Array(1), …}
// {created: 1723013866, id: "a95a54b5c5d2144eb700e60d0dfa5c98", model: "hunyuan-lite", version: "202404011000", choices: Array(1), …}
// {created: 1723013866, id: "a95a54b5c5d2144eb700e60d0dfa5c98", model: "hunyuan-lite", version: "202404011000", choices: Array(1), …}
// {created: 1723013866, id: "a95a54b5c5d2144eb700e60d0dfa5c98", model: "hunyuan-lite", version: "202404011000", choices: Array(1), …}
// {created: 1723013866, id: "a95a54b5c5d2144eb700e60d0dfa5c98", model: "hunyuan-lite", version: "202404011000", choices: Array(1), …}
// {created: 1723013866, id: "a95a54b5c5d2144eb700e60d0dfa5c98", model: "hunyuan-lite", version: "202404011000", choices: Array(1), …}

Bot

Class for interacting with Agent.

get

function get(props: { botId: string }): Promise<BotInfo>;

Get info of a certain Agent.

-Get detailed Agent info based on Agent ID -Return the complete information of Agent, including basic configuration, welcome message and avatar.

参数

props
object

Parameters for retrieving Agent Information

返回

Promise
BotInfo

Detailed information of the Agent

示例

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

list

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

Batch get info of multiple Agents.

-Query and filter the available Agent list -Support paging query and conditional filtering -Return the basic info and configuration detail of Agent -Suitable for building applications like Agent selector and management interface.

参数

props
object

Parameters for querying the Agent list

返回

Promise
AgentListResult

Agent list query result

示例

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

console.log("Total Agents:", agentList.total);
console.log("Agent list:", agentList.botList);

sendMessage

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

Start dialogue with the Agent.

-The response will be returned via SSE. This API's returned value encapsulates SSE at different levels, allowing developers to obtain text streams and complete data streams based on actual needs. -Support multi-round dialogue context management

  • Return streaming response and support real-time access to Agent reply -Suitable for building chatbots, smart assistants and other applications.

参数

props
object

Parameters for sending messages

返回

Promise
StreamResult

Streaming dialogue result, including text stream and data stream

示例

const res = await ai.bot.sendMessage({
botId: "botId-xxx",
history: [{ content: "You are Li Bai.", role: "user" }],
msg: "Hello",
});

// Obtain text streams
for await (let text of res.textStream) {
console.log("Agent reply:", text);
}

// Retrieve data streams
for await (let data of res.dataStream) {
console.log("Details:", data);
}

getChatRecords

function getChatRecords(props: {
botId: string;
sort: string;
pageSize: number;
pageNumber: number;
}): Promise<ChatRecordsResult>;

Retrieve chat record.

  • Retrieve historical chat records of the specified Agent
  • Support paging query and sort
  • Return complete chat history
  • Suitable to build chat history view function

参数

props
object

Parameters for chat record retrieval

返回

Promise
ChatRecordsResult

Chat record query result

示例

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

console.log("Total Record Count:", records.total);
console.log("Record List:", records.recordList);

sendFeedback

function sendFeedback(props: {
userFeedback: IUserFeedback;
botId: string;
}): Promise<void>;

Send feedback information for a chat record.

-Evaluate and provide feedback for the specified chat record.

  • Support multiple feedback methods such as score, comment, and tag. -Help improve Agent response quality -Suitable for building user feedback systems

参数

props
object

Parameters for sending feedback

返回

Promise
void

no return value

示例

const res = await ai.bot.sendFeedback({
userFeedback: {
botId: "botId-xxx",
recordId: "recordId-xxx",
comment: "super",
rating: 5,
tags: ["elegant"],
aiAnswer: "falling petals",
input: "Give me an idiom",
type: "upvote",
},
botId: "botId-xxx",
});

console.log("Feedback sent successfully");

getFeedback

function getFeedback(props: {
botId: string;
type: string;
sender: string;
senderFilter: string;
minRating: number;
maxRating: number;
from: number;
to: number;
pageSize: number;
pageNumber: number;
}): Promise<FeedbackResult>;

Retrieve existing feedback information.

-Query specified Agent user feedback records -Support various filter conditions: time range, score range, user filtering, etc.

  • Return paginated feedback results and statistical information -Suitable for building feedback analytics and management systems

参数

props
object

Parameters for querying feedback information

返回

Promise
FeedbackResult

Feedback query result

示例

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

console.log("Total Feedback:", res.total);
console.log("Feedback List:", res.feedbackList);

uploadFiles

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

Upload files in cloud storage to Agent for document chat.

-Support uploading files in cloud storage to the specified Agent -Files after uploading can be used for the chat feature. -Support batch uploading multiple files -Suitable for building document QA, file analysis and other applications.

参数

props
object

File upload parameters

返回

Promise
void

no return value

示例

// Upload a single file to the specified Agent
await ai.bot.uploadFiles({
botId: "botId-xxx",
fileList: [
{
fileId: "cloud://document.docx",
fileName: "document.docx",
type: "file",
},
],
});

console.log("File uploaded successfully");

getRecommendQuestions

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

Get recommended issues, intelligently generate related issues based on dialogue context.

-Intelligently recommend related issues based on current conversation content and history -Support streaming return and real-time access to recommended issues -Suitable for building intelligent dialogue assistants, chatbots and other applications. -Help users discover more related topics and enhance dialogue experience

参数

props
object

Parameters for obtaining recommended questions

返回

Promise
StreamResult

Streaming results of recommended questions

示例

// Get recommended questions
const res = await ai.bot.getRecommendQuestions({
botId: "botId-xxx",
name: "smart assistant",
introduction: "I am an intelligent dialogue assistant",
agentSetting: "Professional, friendly AI assistant",
msg: "Hello, I want to learn about AI",
history: [
{ content: "Who are you", role: "user" },
{
content:
"I am a smart assistant that can help you answer various questions",
role: "assistant",
},
],
});

// Get recommended questions via streaming
for await (let question of res.textStream) {
console.log("Recommended questions:", question);
}

createConversation

function createConversation(props: {
botId: string;
title?: string;
}): Promise<IConversation>;

Create a new dialog with the Agent and establish an independent session.

-Create a new session for the specified Agent

  • Support customizable dialogue titles for easy management and identification
  • Each session independently stores dialogue history
  • Suitable for multi-user dialogue management requirements in various scenarios

参数

props
object

Parameters for creating a dialogue

返回

Promise
IConversation

Created dialogue info

示例

Create a new dialogue session
const conversation = await ai.bot.createConversation({
botId: "botId-xxx",
title: "Technical Consulting Chat"
});

console.log("Dialogue created successfully:", conversation.conversationId);
console.log("Dialogue title:", conversation.title);

getConversation

function getConversation(props: {
botId: string;
pageSize?: number;
pageNumber?: number;
isDefault?: boolean;
}): Promise<ConversationListResult>;

Retrieve the conversation list of the Agent, support paging query and conditional filtering.

  • Query specified Agent ALL dialogue records -Support paging query, easy to manage large number of dialogues
  • Filter default dialogue or ALL dialogues
  • Suitable for dialogue management, view history record and other scenarios

参数

props
object

Parameters for retrieving the conversation list

返回

Promise
ConversationListResult

Conversation list query result

示例

// Get the conversation list
const result = await ai.bot.getConversation({
botId: "botId-xxx",
pageSize: 10,
pageNumber: 1,
isDefault: false,
});

console.log(`Total ${result.total} dialogues`);
result.conversations.forEach((conversation, index) => {
console.log(
`${index + 1}. ${conversation.title} (${
conversation.messageCount
} messages)`
);
});

deleteConversation

function deleteConversation(props: {
botId: string;
conversationId: string;
}): Promise<void>;

Delete specified dialogue session and clear dialogue history.

-Delete specified dialogue session permanently -Clear all messages in the dialogue -Free up storage space, optimize system performance

  • Suitable for dialogue management, data clearing and other scenarios

参数

props
object

Parameters for deleting a conversation

返回

Promise
void

Delete operation complete, no return value

示例

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

console.log("Dialogue deleted successfully");

speechToText

function speechToText(props: {
botId: string;
engSerViceType: string;
voiceFormat: string;
url: string;
isPreview?: boolean;
}): Promise<SpeechToTextResult>;

Convert voice files into text, support various audio formats and language recognition.

  • Support various audio formats such as MP3, WAV, and AAC -Support multiple language recognition engines -Process local or remote audio files -Suitable for voice assistant, meeting minutes, voice note and other scenarios

参数

props
object

Parameters for speech to text

返回

Promise
SpeechToTextResult

ASR result

示例

Convert voice file to text
const result = await ai.bot.speechToText({
botId: "speech-bot-id",
engSerViceType: "16k_zh",
voiceFormat: "mp3",
url: "https://example.com/audio.mp3",
});

console.log("Recognition result:", result.text);
console.log("Confidence:", result.confidence);
console.log("audio duration:", result.duration, "s");
console.log("Recognition language:", result.language);

textToSpeech

function textToSpeech(props: {
botId: string;
voiceType: number;
text: string;
isPreview?: boolean;
}): Promise<TextToSpeechResult>;

Convert text to speech, support various speech types and voice types.

-Support various speech types and voice types -Adjustable parameters such as speaking rate and pitch -Support long text segment switch -Suitable for voice broadcast, audiobook, voice assistant and other scenarios

参数

props
object

Parameters for text to speech

返回

Promise
TextToSpeechResult

Result of text to speech

示例

Convert text to speech
const result = await ai.bot.textToSpeech({
botId: "tts-bot-id",
voiceType: 1, // Standard female voice
Hello, I am an AI voice assistant. Glad to serve you.
});

console.log("TTS completed:");
console.log("audio address:", result.audioUrl);
console.log("audio duration:", result.duration, "s");
console.log("file format:", result.format);
console.log("file size:", result.size, "byte");

getTextToSpeechResult

function getTextToSpeechResult(props: {
botId: string;
taskId: string;
isPreview?: boolean;
}): Promise<TextToSpeechResult>;

Retrieve the result of the text to speech task, query the completion status of the TTS task and the generated audio file.

-Querying the status and result of the async TTS task -Retrieve the generated audio file info -Support preview mode and official mode -Suitable for batch TTS, long text processing and other async scenarios

参数

props
object

Parameters for obtaining TTS results

返回

Promise
TextToSpeechResult

TTS task result

示例

// Querying TTS task results
const result = await ai.bot.getTextToSpeechResult({
botId: "tts-bot-id",
taskId: "task-123456",
});

console.log("Task status:", result.status);
console.log("Task progress:", result.progress, "%");

if (result.status === "completed") {
console.log("audio address:", result.audioUrl);
console.log("audio duration:", result.duration, "s");
console.log("file format:", result.format);
console.log("file size:", result.size, "byte");
} else if (result.status === "processing") {
console.log("Task is still in processing, please retry later");
} else if (result.status === "failed") {
console.log("task processing failed");
}

Complete type definition

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;
}

BaseChatModelInput

interface BaseChatModelInput {
model: string;
messages: Array<ChatModelMessage>;
temperature?: number;
topP?: number;
tools?: Array<FunctionTool>;
toolChoice?: "none" | "auto" | "custom";
maxSteps?: number;
onStepFinish?: (prop: IOnStepFinish) => unknown;
}
BaseChatModelInput attribute namedata typedescription
modelstringmodel name
messagesArray<ChatModelMessage>message list
temperaturenumberSampling temperature, controls output randomness.
topPnumberTemperature sampling, consider results with probability mass top_p.
toolsArray<FunctionTool>Available tool list for large model
toolChoicestringSpecify the way for the large model to select tools.
maxStepsnumberMaximum number of times to request the large model.
onStepFinish(prop: IOnStepFinish) => unknownThe callback function that departs upon completion of a single request to the large model.

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;
}

ChatModelMessage

type ChatModelMessage =
| UserMessage
| SystemMessage
| AssistantMessage
| ToolMessage;

UserMessage

type UserMessage = {
role: "user";
content: string;
};

SystemMessage

type SystemMessage = {
role: "system";
content: string;
};

AssistantMessage

type AssistantMessage = {
role: "assistant";
content?: string;
tool_calls?: Array<ToolCall>;
};

ToolMessage

type ToolMessage = {
role: "tool";
tool_call_id: string;
content: string;
};

ToolCall

export type ToolCall = {
id: string;
type: string;
function: { name: string; arguments: string };
};

FunctionTool

Tool definition type.

type FunctionTool = {
name: string;
description: string;
fn: CallableFunction;
parameters: object;
};
FunctionTool attribute namedata typedescription
namestringtool name
descriptionstringtool description. A clear tool description helps the large model meet the tool purpose.
fnCallableFunctionThe execution function of the tool. When the AI SDK parses out the large model response requires this tool to invoke, it will invoke this function and return the result to the large model.
parametersobjectThe input parameters of the tool execution function. Require the use of JSON Schema format definition.

IOnStepFinish

Input parameter type of the callback function triggered after the large model responds.

interface IOnStepFinish {
messages: Array<ChatModelMessage>;
text?: string;
toolCall?: ToolCall;
toolResult?: unknown;
finishReason?: string;
stepUsage?: Usage;
totalUsage?: Usage;
}
IOnStepFinish attribute namedata typedescription
messagesArray<ChatModelMessage>List of all messages up to the current step.
textstringCurrent response text.
toolCallToolCallTool called by the current response.
toolResultunknownTool call result.
finishReasonstringThe causes for large model inference ended.
stepUsageUsageTokens spent in the current step.
totalUsageUsageTotal tokens spent up to the current step.

Usage

type Usage = {
completion_tokens: number;
prompt_tokens: number;
total_tokens: number;
};

IConversation

Agent session.

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