使用示例
用于将文件上传至 OpenAI 兼容的文件服务,配合支持该能力的模型(如 Qwen-Doc-Turbo)进行文档问答、数据提取或作为批量推理任务的输入文件。
📢 注意:请确保智能体依赖版本正确以支持 /upload-openai-file 接口
该接口自智能体函数服务框架 0.6.2 版本起支持,默认智能体已支持该接口。推荐依赖版本如下,智能体 upload-openai-file 服务 服务
"@vectorx/agent-runtime": "0.6.2", "@vectorx/functions-framework": "0.6.2"
const agent = this.getAgent();
if (!agent) return;
// 选择文件
xhs.chooseSystemFile({
type: 'all',
success: async (result) => {
const files = (result && result.tempFiles) || [];
const first = files[0] || {};
const filePath = first.path || first.tempFilePath;
if (!filePath) {
xhs.showToast({ title: '未获取到文件路径', icon: 'none' });
return;
}
// 上传文件
const resp = await agent.uploadOpenAiFile({
filePath: filePath,
purpose: 'file-extract'
});
console.log('上传成功:', resp);
// 保存文件信息
const data = (resp && resp.data) || resp || {};
const fileItem = {
id: data.id,
filename: data.filename,
purpose: data.purpose,
bytes: data.bytes,
fileSource: 'openai',
created_at: data.created_at
};
},
fail: (error) => {
console.error('选择文件失败:', error);
}
});
// 选择并上传文件
onUploadFile() {
const that = this;
xhs.chooseSystemFile({
type: 'all',
success: async (result) => {
try {
console.log('选择成功:', result);
const files = (result && result.tempFiles) || [];
const first = files[0] || {};
const filePath = first.path || first.tempFilePath;
if (!filePath) {
xhs.showToast({ title: '未获取到文件路径', icon: 'none' });
return;
}
const agent = that.getAgent();
const resp = await agent.uploadOpenAiFile({
filePath: filePath,
purpose: 'file-extract'
});
xhs.showToast({ title: '上传成功', icon: 'success' });
// 保存上传成功的文件信息
const data = (resp && resp.data) || resp || {};
const fileItem = {
id: data.id,
filename: data.filename,
purpose: data.purpose,
bytes: data.bytes,
fileSource: 'openai',
created_at: data.created_at
};
const uploadedFiles = [...that.data.uploadedFiles, fileItem];
that.setData({ uploadedFiles: uploadedFiles });
// 同步在结果面板展示上传结果
that.setData({
result: typeof resp === 'string' ? resp : JSON.stringify(resp, null, 2)
});
} catch (error) {
console.error('上传失败:', error);
const errMsg = (error && (error.errMsg || error.message)) || error;
that.setData({ result: '错误: ' + errMsg });
xhs.showToast({ title: errMsg || '上传失败', icon: 'none' });
}
},
fail: (error) => {
console.error('选择文件失败:', error);
const errMsg = (error && (error.errMsg || error.message)) || error;
that.setData({ result: '错误: ' + errMsg });
xhs.showToast({ title: errMsg || '选择文件失败', icon: 'none' });
}
});
}
const resp = await agent.uploadOpenAiFile({
filePath: string,
purpose?: string,
fileName?: string
});
属性名 | 类型 | 说明 | 最低支持版本 |
|---|---|---|---|
| filePath | string | 文件路径,通过 xhs.chooseSystemFile 获取 | 0.3.0 |
| purpose | string | 文件用途,默认为 'file-extract' | 0.3.0 |
| fileName | string | 文件名(可选) | 0.3.0 |
属性名 | 类型 | 说明 | 最低支持版本 |
|---|---|---|---|
| data | object | 文件信息对象 | 0.3.0 |
/** 文件信息对象 */
data: {
id: string; // 文件ID
filename: string; // 文件名
purpose: string; // 文件用途
bytes: number; // 文件大小(字节)
created_at: number; // 创建时间戳
[key: string]: any; // 其他扩展字段
};