如何向OpenAI API发送文件?

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

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

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

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);    }}

我得到的响应是:

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

我不确定我做错了什么。我还尝试将内容类型更改为text/plain,但得到了相同的错误。


回答:

async-openai 包中有 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?;

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注