Skip to main content

Via SDK/API call

Go to the TCB/Environment Configuration/API Key Configuration page to create a new API Key.

After obtaining the API Key, you can perform HTTP API calls by simply including the Authorization: Bearer <your API Key> header in the HTTP request.

ibot-type Agent

Mini Program Call

// Initialize it.
wx.cloud.init({
env: "<TCB Environment ID>",
});

// User input, here we take a specific error message as an example.
const userInput =
"What does this error message in my mini-program mean: 'FunctionName parameter could not be found'";

const res = await wx.cloud.extend.AI.bot.sendMessage({
data: {
botId: "<AgentID>", // Agent unique identifier obtained in Step 2
msg: userInput, // User input
history: [], // Historical conversation content. Since this is the first round of conversation, it can be omitted.
},
});
for await (let x of res.textStream) {
console.log(x);
}
// Output result:
// "### Error Explanation\n"
// "**Error Message:** `FunctionName \n"
// "parameter could not be found` \n
// "This error usually indicates that when calling a certain function,\n"
// "The specified function name parameter was not found. Specifically,\n"
// "It may be one of the following situations:\n"
// ...

We can also record the conversation content and repeatedly call the Agent's interface, thereby enabling multi-turn conversations.

const res = await wx.cloud.extend.AI.bot.sendMessage({
data: {
botId: "xxx-bot-id", // Agent unique identifier obtained in Step 2
msg: userInput, // User input
history: [
{ role: "user", message: "What does this error mean?..."},
{ role: "bot", message: "### Error Explanation..." },
{ role: "user", message: "How do I proceed to fix this?" }
// ...
]
},
});

TCB provides a comprehensive set of API interfaces for Agent (intelligent agent) integration in the SDK, including basic conversation, conversation history storage, conversation feedback collection, and follow-up question recommendations.

Mini Program developers can create Agents on the TCB platform, then directly invoke various interfaces under wx.cloud.extend.AI in the frontend code to interact with Agents, including:

  • Obtain chat history
  • Send and obtain user feedback
  • Obtain recommended follow-up questions

Below are some code examples:

Obtain Chat History

await wx.cloud.extend.AI.bot.getChatRecords({
botId: "botId-xxx",
pageNumber: 1,
pageSize: 10,
sort: "asc",
});

Pass in the botId, pagination information, and sorting method to obtain the chat history for the specified Agent.

Send Feedback and Obtain Feedback

Send user feedback:

const res = await wx.cloud.extend.AI.bot.sendFeedback({
userFeedback: {
botId: "botId-xxx",
recordId: "recordId-xxx",
comment: "Excellent!",
rating: 5,
tags: ["elegant"],
aiAnswer: "Falling petals are scattered everywhere",
input: "Give me an idiom",
type: "upvote",
},
});
const res = await wx.cloud.extend.AI.bot.getRecommendQuestions({
data: {
botId: "xxx-bot-id",
msg: "Introduce the Python language",
},
});
for await (let x of res.textStream) {
console.log(x);
}

Set botId and the user message msg in the data parameter, and obtain recommended questions by traversing textStream.

cURL Example

Here is an example of invoking the AI Agent HTTP API using cURL:

curl 'https://<your-envId>.api.tcloudbasegateway.com/v1/aibot/bots/<your-botId>/send-message' \
-H 'Authorization: Bearer <your API Key>' \
-H 'Accept: text/event-stream' \
-H 'Content-Type: application/json' \
--data-raw '{"msg":"hi"}'