您的位置:首页 > 财经 > 金融 > 如何做游戏软件开发_网站建设价格槽闸阀_指数基金投资指南_百度招聘

如何做游戏软件开发_网站建设价格槽闸阀_指数基金投资指南_百度招聘

2024/12/23 16:54:04 来源:https://blog.csdn.net/suiusoar/article/details/142335509  浏览:    关键词:如何做游戏软件开发_网站建设价格槽闸阀_指数基金投资指南_百度招聘
如何做游戏软件开发_网站建设价格槽闸阀_指数基金投资指南_百度招聘

题意:你如何向 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 herelet key = "My key that I have replaced for obvious reasons";{// Create a reqwest clientlet client = reqwest::blocking::Client::new();// Make a POST request to the OpenAI APIlet 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 successfulif 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?;

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com