Using AI chatbot
Update time: 2024/03/14 18:45:33
This document describes how to integrate a third-party chatbot service and help you implement diverse AI services in your application.
Azure OpenAI Service is used in the example. Other services are integrated similarly. You can use the chatbot service in private or group conversations.
How it works
If you have integrated Azure OpenAI Service, app users can start private chats with the chatbot as below:
Implementation:
- A user sends a message to the chatbot (predefined chatbot account with accid).
- IM Server evaluates the message sent to a chatbot account and forwards the messages to Chatbot Server.
- Chatbot Server invokes the Azure OpenAI Service REST APIs.
- Azure OpenAI Service returns the response with the reply message.
- Chatbot Server forwards the reply message to IM Server.
- IM Server sends the reply message to the user.
If you have activated and configured Custom Webhook and Moderator, messages sent or received from Azure OpenAI Service are reviewed for compliance rules. For more information about moderation, see Moderator and Custom Webhook.
Prerequisites
- An application is created in the CommsEase console and IM accounts are registered with accid and token.
- The account for Azure OpenAI Service and registered and the API key is obtained.
Implementation
In the example, a user starts a private chat with the chatbot and you can follow the steps to implement chatting with the chatbot service.
Step 1. Configure Azure OpenAI Service
- Select the application you want to integrate with the Azure OpenAI Service in the Applications section on the homepage of the console. In the left-side navigation pane, click Services->Instant Messaging.
- Go to Account Management, and click Settings in the Chatbot field.
-
Click Add and configure all the fields for the chatbot service.
-
Specify the chatbot profile
Field Required Description Chatbot accid Yes The chatbot account(accid)
A registered IM account or an unregistered account you can input here and the console will register the account automatically.Chatbot name No The name of the chatbot. Avatar No upload a chatbot avatar in JEP, PNG, or JPEG format. The size of the avatar is 5MB at most. Service No The chatbot service. By default, Azure OpenAI Service is available. You can also use a custom chatbot service. -
Configure the AI chatbot service
Azure OpenAI ServiceIf you choose Azure OpenAI Service, you must specify the required fields for the service. For more information, see the documentation.
Custom AI Chatbot ServiceFor Custom AI Chatbot Service, add the URL of the webhook.
-
Configure the messaging logic.
Field Required Description The number of rewindable messages Yes The limit on the number of rewindable messages sent to Azure OpenAI Service The time limit for rewindable messages Yes The limit on the time for message rewinding. Whether replies are returned without moderation Yes The replies from the chatbot service are not synced with the app server or moderated by the CommsEase moderator. The default value is Yes.
-
-
Click Save to complete the configuration. You can start chatting with the chatbot.
Multiple chatbots are allowed even with the same webhook URL.
Step 2. Work with the chatbot
-
Send a text message to the chatbot by calling the
sendText
method.The
robotInfo
parameter contains the chatbot profile.robotInfo
fields:Parameter Type Description function String Chatbot capability topic String Chat topic customContent String Custom content account String Chatbot account (valid for group chat, ignored for private chats
Code example:
javascript// Create and send a text message
nim.sendText({
scene: "p2p",
to: "robotAccId",
text: "Help me create a recipe for a Chinese dish",
robotInfo: {
function: "",
topic: ""
}
});
修改说明:
-
去除代码段前面的空格
-
将逗号后面的空格去掉,符合 JS 语法规范
-
去除
robotInfo
对象最后一个属性后面的逗号,符合 JS 语法规范Chatbots can send multiple types of messages, such as text, images, audio, video, files, location, tips, notifications, and custom messages.
- The user receives the reply from the chatbot. When a reply from the chatbot is received, the callback
onmsg
is triggered.