除了语音输入,您还可以通过发送文本指令的方式,模拟用户的文字输入,直接向智能体发起提问并立即获得语音回复。
应用场景
场景 | 描述 |
|---|
电商直播 | 将观众在弹幕或私信中提出的问题,实时发送给正在直播的智能体主播进行回答。 |
特殊内容输入 | 当问题包含生僻字、专业术语、复杂编码或同音字较多时,用户通过文本输入可以确保提问的准确性。 |
通过服务端实现
调用 UpdateVoiceChat 接口传入大模型上下文,可参考以下示例:
{
"AppId": "YOUR_AppId", // 与 StartVoiceChat 的一致
"RoomId": "YOUR_RoomId", // 与 StartVoiceChat 的一致
"TaskId": "YOUR_TaskId", // 与 StartVoiceChat 的一致
"Command": "ExternalTextToLLM", //必须为该值
"Message": "给一些出装建议。用户当前:金币落后,法师,脆皮", // 传入自定义文本
"InterruptMode": 2
}
通过客户端实现
使用 sendUserBinaryMessage 接口构造一个 Command 为 ExternalTextToLLM 的控制消息。该接口的 buffer 参数需要传入特定格式的内容,下图展示了 buffer 参数的格式:

参数名 | 类型 | 描述 |
|---|
magic_number | binary | 消息格式标识符,当前场景消息格式固定为 ctrl,用于标识该消息为控制消息。 |
length | binary | 传入大模型上下文消息长度,单位为字节,采用大端序(Big-endian)存储方式,用于说明 control_message 字段的字节长度。 |
control_message | binary | 传入大模型上下文配置信息,采用 JSON 格式,具体内容格式参见control_message。 |
control_message
参数名 | 类型 | 描述 |
|---|
Command | String | 控制命令,此处填入 ExternalTextToLLM。 |
Message | String | 传入大模型上下文内容,长度不超过 200 个字符。 |
InterruptMode | Int | 传入大模型上下文内容处理优先级。 1:高优先级。智能体会终止当前交互,将文本送入大模型进行处理并输出结果。2:中优先级。智能体会在当前交互结束后,将文本送入大模型进行处理并输出结果。3:低优先级。如果此时智能体正在交互,智能体会直接丢弃传入的文本内容。如果未在交互,智能体会将文本送入大模型进行处理并输出结果。
|
你可参看以下示例从客户端实现自定义传入大模型上下文。
// 传入大模型上下文信息
void sendLLMMessage(const std::string &uid, const std::string& content) {
nlohmann::json json_data;
json_data["Command"] = "ExternalTextToLLM";
json_data["Message"] = content;
json_data["InterruptMode"] = 1;
sendUserBinaryMessage(uid, json_data.dump());
}
void buildBinaryMessage(const std::string& magic_number, const std::string& message, size_t& binary_message_length, std::shared_ptr<uint8_t[]>& binary_message) {
auto magic_number_length = magic_number.size();
auto message_length = message.size();
binary_message_length = magic_number_length + 4 + message_length;
binary_message = std::shared_ptr<uint8_t[]>(new uint8_t[binary_message_length]);
std::memcpy(binary_message.get(), magic_number.data(), magic_number_length);
binary_message[magic_number_length] = static_cast<uint8_t>((message_length >> 24) & 0xFF);
binary_message[magic_number_length+1] = static_cast<uint8_t>((message_length >> 16) & 0xFF);
binary_message[magic_number_length+2] = static_cast<uint8_t>((message_length >> 8) & 0xFF);
binary_message[magic_number_length+3] = static_cast<uint8_t>(message_length & 0xFF);
std::memcpy(binary_message.get()+magic_number_length+4, message.data(), message_length);
}
// userId 为房间内智能体的 ID(对应 StartVoiceChat 中的 AgentConfig.UserId)
int sendUserBinaryMessage(const std::string &uid, const std::string& message) {
if (rtcRoom_ != nullptr)
{
size_t length = 0;
std::shared_ptr<uint8_t[]> binary_message = nullptr;
buildBinaryMessage("ctrl", message, length, binary_message);
return rtcRoom_->sendUserBinaryMessage(uid.c_str(), static_cast<int>(length), binary_message.get());
}
return -1;
}
// 传入大模型上下文信息
public void sendLLMMessage(String userId, String content) {
JSONObject json = new JSONObject();
try {
json.put("Command", "ExternalTextToLLM");
json.put("Message", content);
json.put("InterruptMode", 1); // InterruptMode 可选值1,2,3
} catch (JSONException e) {
throw new RuntimeException(e);
}
String jsonString = json.toString();
byte[] buildBinary = buildBinaryMessage("ctrl", jsonString);
sendUserBinaryMessage(userId, buildBinary);
}
private byte[] buildBinaryMessage(String magic_number, String content) {
byte[] prefixBytes = magic_number.getBytes(StandardCharsets.UTF_8);
byte[] contentBytes = content.getBytes(StandardCharsets.UTF_8);
int contentLength = contentBytes.length;
ByteBuffer buffer = ByteBuffer.allocate(prefixBytes.length + 4 + contentLength);
buffer.order(ByteOrder.BIG_ENDIAN);
buffer.put(prefixBytes);
buffer.putInt(contentLength);
buffer.put(contentBytes);
return buffer.array();
}
// userId 为房间内智能体的 ID(对应 StartVoiceChat 中的 AgentConfig.UserId)
public void sendUserBinaryMessage(String userId, byte[] buffer) {
if (rtcRoom_ != null) {
rtcRoom_.sendUserBinaryMessage(userId, buffer, MessageConfig.RELIABLE_ORDERED);
}
}
import VERTC from '@volcengine/rtc';
const BotName = 'RobotMan_'; // 房间内智能体的 ID(对应 StartVoiceChat 中的 AgentConfig.UserId)
const CommandKey = 'ctrl';
const engine = VERTC.createEngine('Your AppID');
/**
* @brief 指令类型
*/
enum COMMAND {
/**
* @brief 传入大模型上下文信息
*/
EXTERNAL_TEXT_TO_LLM = 'ExternalTextToLLM',
};
/**
* @brief 打断的类型
*/
enum INTERRUPT_PRIORITY {
/**
* @brief 占位
*/
NONE,
/**
* @brief 高优先级。智能体会终止当前交互,将文本送入大模型进行处理并输出结果。
*/
HIGH,
/**
* @brief 中优先级。智能体会在当前交互结束后,将文本送入大模型进行处理并输出结果。
*/
MEDIUM,
/**
* @brief 低优先级。如果此时智能体正在交互,智能体会直接丢弃传入的文本内容。如果未在交互,智能体会将文本送入大模型进行处理并输出结果。
*/
LOW,
};
/**
* @brief 将字符串包装成 TLV
*/
function stringToTLV(inputString: string, type = '') {
const typeBuffer = new Uint8Array(4);
for (let i = 0; i < type.length; i++) {
typeBuffer[i] = type.charCodeAt(i);
}
const lengthBuffer = new Uint32Array(1);
const valueBuffer = new TextEncoder().encode(inputString);
lengthBuffer[0] = valueBuffer.length;
const tlvBuffer = new Uint8Array(typeBuffer.length + 4 + valueBuffer.length);
tlvBuffer.set(typeBuffer, 0);
tlvBuffer[4] = (lengthBuffer[0] >> 24) & 0xff;
tlvBuffer[5] = (lengthBuffer[0] >> 16) & 0xff;
tlvBuffer[6] = (lengthBuffer[0] >> 8) & 0xff;
tlvBuffer[7] = lengthBuffer[0] & 0xff;
tlvBuffer.set(valueBuffer, 8);
return tlvBuffer.buffer;
};
/**
* @brief 传入大模型上下文信息
*/
engine.sendUserBinaryMessage(
BotName,
stringToTLV(
JSON.stringify({
Command: COMMAND.EXTERNAL_TEXT_TO_LLM,
Message: '给一些出装建议。用户当前的背景是:金币落后,法师,输出高,脆皮',
InterruptMode: INTERRUPT_PRIORITY.HIGH,
}),
CommandKey,
)
);