即时通信 IM iOS(AI 聊天机器人)

该功能允许用户与 AI 机器人进行对话,实时接收机器人思考过程中的部分回复(流式输出)。2. 接收 AI 机器人流式消息的实时更新(内容追加)。

相关产品
即时通信 IM 产品页,含版本规格、价格与购买入口。

一、功能描述

核心功能包括:

1. 接收 AI 机器人的首次流式消息响应。

3. 用户中断正在生成中的流式消息。

4. 接收 AI 机器人的错误信息反馈。

所有相关消息均通过自定义消息(Custom Message) 类型传输,消息内容为特定结构的 JSON 字符串。

注意:说明:

二、1. 首次响应

用户发起问题后,AI 机器人开始生成回复。当生成出第一段有效内容时,会立即通过一条新的自定义消息发送给用户,触发 onRecvNewMessage。

三、2. 流式更新

AI 机器人持续生成内容。每当有新内容片段产生,它不会发送新消息,而是修改之前发送的那条自定义消息,会触发 onRecvMessageModified。接收方看到的是同一消息的内容在不断追加更新。

四、3. 完成标志

当 AI 机器人完成整个回复的生成,会在最后一次更新(或唯一一次消息,如果流很短)时,将消息中的 isFinished 标志置为 1。

五、4. 用户中断

用户在消息生成过程中可以主动中断。需发送一条特定格式的自定义消息到该 AI 机器人。

六、5. 错误反馈

如果 AI 机器人在处理请求或生成流式消息过程中发生错误,会通过 onRecvNewMessage ,发送给用户一条特定格式的错误消息 。

七、步骤1:创建机器人

集成 AI 聊天机器人组件之前,请先在控制台创建机器人。

八、步骤2:添加机器人好友

使用 RestAPI 添加上一步创建的机器人为好友。

注意:为避免你的 AI 资源被恶意消耗,现只支持使用 RestAPI 添加机器人好友,用户无法主动添加 AI 机器人为好友。

九、步骤3:向机器人发送文本消息

注意:说明:

注意:RBT_ID 为上一步添加的机器人好友的 ID;Text 为向机器人发送的文本内容。

NSString *msgID = [[V2TIMManager sharedInstance] sendC2CTextMessage:@"Text" to:@"RBT_ID" succ:^{ // success } fail:^(int code, NSString *msg) { // failed }];

十、监听回调

向 IMSDK 注册 onRecvNewMessage 和 onRecvMessageModified 回调监听器。

[[V2TIMManager sharedInstance] addAdvancedMsgListener:self];#pragma mark V2TIMAdvancedMsgListener- (void)onRecvNewMessage:(V2TIMMessage *)msg { NSLog(@"%@",msg);}- (void)onRecvMessageModified:(V2TIMMessage *)msg { NSLog(@"%@",msg);}

十一、处理消息

当你收到 onRecvNewMessage(首次)或 onRecvMessageModified(后续更新)回调,且消息来自 AI 机器人时,其自定义消息体结构(jsonString)如下:

{ "chatbotPlugin": 2, "src": 2, "chunks": [ "Hello", ], "isFinished": 0}

字段 类型 说明
chatbotPlugin Integer 固定值 2,标识聊天机器人消息。
src Integer 自定义消息类型 2:流式消息类型。
chunks Array 流式消息内容。
isFinished Integer 流式消息是否完结。 0:未发送完 1:已发送完。

十二、步骤5:发送给 AI 的中断消息

当用户想要中断一个正在生成中的 AI 流式回复时,用户客户端需要向该 AI 机器人主动发送一条自定义消息,自定义消息中携带想要中断的 AI 流式消息的 msgKey。

生成 msgKey

- (NSString *)generateMessageKey:(V2TIMMessage *)message { uint64_t msgSeq = message.seq; uint64_t random = message.random; NSTimeInterval timestamp = message.timestamp ? [message.timestamp timeIntervalSince1970] : 0; return [NSString stringWithFormat:@"%llu_%llu_%.0f", msgSeq, random, timestamp];}

- (V2TIMMessage *)buildChatbotInterruptMessage { // Build interrupt message content NSDictionary *interruptMessageContent = @{ @"chatbotPlugin": @(2), @"src": @(22), @"msgKey": [self generateMessageKey:messageData] }; // Convert to JSON NSError *error; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:interruptMessageContent options:0 error:&error]; if (error || !jsonData) { NSLog(@"Failed to create interrupt message JSON: %@", error.localizedDescription); return nil; } // Create custom message V2TIMMessage *message = [[V2TIMManager sharedInstance] createCustomMessage:jsonData]; message.isExcludedFromLastMessage = YES; message.isExcludedFromUnreadCount = YES; return message;}

自定义消息体(jsonString)结构如下:

{ "chatbotPlugin": 2, "src": 22, "msgKey": "xxx"}

字段 类型 说明
chatbotPlugin Integer 固定值 2,标识聊天机器人消息。
src Integer 自定义消息类型 22:中断流式消息。
msgKey String 要中断的 AI 消息的唯一标识 {seq}_{random}_{serverTime} ,生成方式参见 生成 MsgKey。

十三、步骤6:接收到 AI 报错消息

当 AI 机器人在处理请求或生成流式消息过程中遇到错误时,会发送一条错误消息,此时你会收到 onRecvNewMessage 回调。

[[V2TIMManager sharedInstance] addAdvancedMsgListener:self];- (void)onRecvNewMessage:(V2TIMMessage *)msg { NSDictionary *param = [NSJSONSerialization JSONObjectWithData:message.customElem.data options:NSJSONReadingAllowFragments error:nil]; if (param == nil) { return @""; } if ([param[@"src"] doubleValue] == 23) { NSString *errorInfo = param[@"errorInfo"]; NSLog(@"%@",errorInfo); }}

自定义消息体(jsonString)结构如下:

{ "chatbotPlugin": 2, "src": 23, "errorInfo": "xxxxxxxxxxx"}

字段 类型 说明
chatbotPlugin Integer 固定值 2,标识聊天机器人消息。
src Integer 自定义消息类型 23:发消息或者配置 LLM 报错。
errorInfo String 报错详细信息。
赞 (0)
上一篇 4天前
下一篇 4天前