当前位置: 首页 > article >正文

How do you send files to the OpenAI API?

题意:你如何向 OpenAI API 发送文件

问题背景:

For fun I wanted to try to make a tool to ask chatgpt to document rust files. I found an issue, in that the maximum message length the API allows seems to be 2048 characters.

为了好玩,我想尝试制作一个工具,让 ChatGPT 生成 Rust 文件的文档。我发现了一个问题,API 允许的最大消息长度似乎是 2048 个字符。

It seems that the OpenAI API allows you to send files, so I was hoping that by sending the files to the server the model would have the context it needs to generate the comments.

看起来 OpenAI API 允许发送文件,所以我希望通过将文件发送到服务器,模型可以获得生成注释所需的上下文

However, I don't seem to be able to do so, I tried this:

然而,我似乎无法做到这一点,我尝试了以下方法

use std::fmt::Write;
use std::fs;
use std::io;
use std::io::Read;

use chatgpt::prelude::*;
use clap::Parser;
use syn;
use syn::Item;
use reqwest;

#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args
{
    /// Path to the source code to document
    #[arg(short, long)]
    file_path: String,
}

fn main()
{
    let args = Args::parse();

    let mut file = std::fs::File::open(args.file_path).unwrap();
    let mut content = String::new();
    file.read_to_string(&mut content).unwrap();

    let ast = syn::parse_file(&content).unwrap();
    let mut input_buffer = String::new();

    // Getting the API key here
    let key = "My key that I have replaced for obvious reasons";

    {
        // Create a reqwest client
        let client = reqwest::blocking::Client::new();

        // Make a POST request to the OpenAI API
        let response = client
            .post("https://api.openai.com/v1/files")
            .header("Authorization", "Bearer My key that I have replaced for obvious reasons")
            .header("Content-Type", "application/json")
            .body(content.clone())
            .send().unwrap();

        // Check if the request was successful
        if response.status().is_success() {
            println!("File uploaded successfully!");
        } else {
            println!("Failed to upload file. Status code: {}", response.status());
        }

        std::mem::forget(client);
        std::mem::forget(response);
    }
}

The response I get is:        我得到的响应是:

Failed to upload file. Status code: 415 Unsupported Media Type

I am not sure what I am doing wrong. I have also tried changing the content type to text/plain, I get the same error.

我不确定我哪里做错了。我还尝试将内容类型更改为 text/plain,但仍然遇到相同的错误

问题解决:

The async-openai crate has Files, which allows you to upload files to OpenAI:

async-openai crate 提供了 Files,可以让你上传文件到 OpenAI

let client = Client::new();
let request = CreateFileRequest {
    input: "path/to/my/file.bin".into(),
    purpose: "test".into(),
};
let response = client.files().create (request).await?;


http://www.kler.cn/news/315766.html

相关文章:

  • 1.量化第一步,搭建属于自己的金融数据库!
  • 鸿蒙设置,修改APP图标和名称
  • Android Choreographer 监控应用 FPS
  • 如何在Chrome最新浏览器中调用ActiveX控件?
  • 什么时候用synchronized,什么时候用Reentrantlock
  • 高等数学——微分学
  • 5.《DevOps》系列K8S部署CICD流水线之K8S通过Yaml部署GitLab
  • C++从入门到起飞之——多态 全方位剖析!
  • 通信工程学习:什么是NFVI网络功能虚拟化基础设施层
  • Apache HttpComponents HttpClient
  • Blender软件三大渲染器Eevee、Cycles、Workbench对比解析
  • mysql学习教程,从入门到精通,SQL 删除数据(DELETE 语句)(18)
  • Tron/ETH/MATIC/TRX链上智能合约项目开发
  • 【系统架构设计师】软件架构的风格(经典习题)
  • SpringBoot启动横幅输出到控制台。
  • fiddler抓包07_抓IOS手机请求
  • 预付费计量系统实体模型
  • 在Docker中运行Tomcat:打造高效可移植的Java Web服务器
  • 01_RabbitMQ安装及工作模式
  • 阿里HPN-用于大型语言模型训练的数据中心网络
  • Kafka 下载安装及使用总结
  • JavaWeb初阶 day1
  • 从零开始学习Linux(14)---线程池
  • 『功能项目』QFrameWorkBug关联Slot(插槽)【67】
  • C++:使用tinyxml2获取节点下元素
  • android kotlin Extension扩展函数
  • HashMap源码
  • 【bug】通过lora方式微调sdxl inpainting踩坑
  • 用uniapp 及socket.io做一个简单聊天 升级 9
  • 【LeetCode】289.生命游戏