小程序开放平台

文档中心
智能体API
消费侧(小组件/小程序)API
sendMessage
getAgentInfo
getHistoryMessages
getConversations
queryTasks
uploadOpenAiFile(新增)

uploadOpenAiFile

应用开发
>
API和SDK
>
消费侧(小组件/小程序)API
>
uploadOpenAiFile(新增)
>
更新时间:2025-11-20 20:56:49

uploadOpenAiFile

使用示例

用于将文件上传至 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
});

入参说明

属性名
类型
说明
最低支持版本
filePathstring文件路径,通过
xhs.chooseSystemFile
获取
0.3.0
purposestring文件用途,默认为
'file-extract'
0.3.0
fileNamestring文件名(可选)0.3.0

返回值

属性名
类型
说明
最低支持版本
dataobject文件信息对象0.3.0
/** 文件信息对象 */
data: {
  id: string;              // 文件ID
  filename: string;        // 文件名
  purpose: string;         // 文件用途
  bytes: number;           // 文件大小(字节)
  created_at: number;      // 创建时间戳
  [key: string]: any;      // 其他扩展字段
};

注意事项

  1. 可先通过
    xhs.chooseSystemFile
    或其他系统 api 选择文件获取
    filePath
  2. 将purpose指定为file-extract,文件格式支持文本文件( TXT、DOCX、PDF、XLSX、EPUB、MOBI、MD、CSV、JSON),图片文件(BMP、PNG、JPG/JPEG、GIF和PDF扫描件),单个文件最大为 10 MB
  3. 非调试模式下会自动获取并设置
    accessToken
  4. 上传失败会抛出错误,建议使用
    try-catch
    处理