普通模型接口调试用于测试基础的文本对话功能,适用于
在终端输入以下命令启动智能体,并自动在浏览器中打开调试器:
rcb agent dev --open true
得到回复:
在智能体代码中实现
const { AgentRuntime, AgentDriver } = require("@vectorx/agent-runtime");
class MyAgent extends AgentRuntime {
/**
* 普通模型接口调试(基础文本对话)
* @param {Object} input - 输入参数
* @param {string} input.msg - 用户消息文本
* @param {Array} input.history - 历史消息(可选,多轮对话时使用)
*/
async sendMessage(input) {
console.log('=== 普通模型接口调试 ===');
console.log('输入参数:', JSON.stringify(input, null, 2));
try {
// 1. 创建模型实例
const model = this.createModel('qwen-turbo');
const messageText = input.msg || '';
// 2. 构建消息数组
const messages = [
{
role: 'system',
content: '你是一个AI助手。' // 可以修改系统提示词来优化AI回复
},
{
role: 'user',
content: messageText
}
];
console.log('发送给模型的消息:', JSON.stringify(messages, null, 2));
// 3. 调用模型流式接口
// 注意:以下参数会根据模型类型生效,详见《模型出入参标准》文档
const modelResponse = await model.streamText({
messages,
max_tokens: 1000, // 最大生成token数,控制回答长度
temperature: 0.7, // 温度参数,控制随机性(0-2)
top_p: 0.9, // 核采样参数,控制多样性(0-1)
frequency_penalty: 0, // 频率惩罚,减少重复(-2到2)
presence_penalty: 0 // 存在惩罚,鼓励新话题(-2到2)
});
// 4. 处理流式响应
let chunkCount = 0;
for await (const chunk of modelResponse) {
chunkCount++;
// 4.1 检查错误响应
if (chunk.code || chunk.message || chunk?.choices?.[0]?.finish_reason === 'error') {
const errorMsg = chunk.message || '未知错误';
const errorCode = chunk.code || 'UNKNOWN_ERROR';
console.error(`模型返回错误 (code: ${errorCode}):`, errorMsg);
// 发送错误信息给客户端
this.sseSender.send({
data: {
choices: [{
message: {
content: `错误: ${errorMsg} (错误代码: ${errorCode})`,
type: 'error'
},
finish_reason: 'error'
}],
error: {
message: errorMsg,
code: errorCode
}
}
});
this.sseSender.end();
return;
}
// 4.2 处理正常响应
// 流式内容在 delta.content 中(qwen-turbo 等标准模型)
const content = chunk?.choices?.[0]?.delta?.content ||
chunk?.choices?.[0]?.message?.content || '';
if (content) {
console.log(`Chunk ${chunkCount} 内容预览: ${content.substring(0, 50)}...`);
}
// 直接发送原始 chunk,SSE sender 会自动处理 JSON 序列化
this.sseSender.send({ data: chunk });
}
console.log(`✅ 普通模型调试完成,共处理 ${chunkCount} 个 chunk`);
this.sseSender.end();
} catch (error) {
// 5. 错误处理
console.error('❌ 普通模型调试错误:', error);
// 发送错误信息给客户端
this.sseSender.send({
data: {
choices: [{
message: {
content: `错误: ${error.message || '处理基础请求时出现错误'}`,
type: 'error'
}
}]
}
});
// 确保结束 SSE 连接
this.sseSender.end();
}
}
}
exports.main = function (event, context) {
return AgentDriver.run(event, context, new MyAgent(context));
};
参数 | 说明 | 默认值 | 推荐范围 |
|---|---|---|---|
max_tokens | 最大生成token数 | 1000 | 500-2000 |
temperature | 控制随机性 | 0.7 | 0.0-2.0 |
top_p | 核采样参数 | 0.9 | 0.5-1.0 |
frequency_penalty | 频率惩罚 | 0 | -2.0-2.0 |
presence_penalty | 存在惩罚 | 0 | -2.0-2.0 |