async-openai
async-openai copied to clipboard
Unable to use FILES api with Azure config
I am unable to use file api with this piece of code
pub async fn upload_separate_batch_files(
app_data: web::Data<AppState>,
prompts: Vec<(String, String)>,
) -> Result<Vec<String>, String> {
let mut file_ids = Vec::new();
let folder_path = "batch_request_folder";
// Ensure the directory exists
if let Err(e) = std::fs::create_dir_all(folder_path) {
println!("Failed to create directory {}: {}", folder_path, e);
return Err(format!("Failed to create directory: {}", e));
}
for (id, prompt) in prompts.iter() {
let jsonl_content = serde_json::json!({
"custom_id": id,
"method":"POST",
"body": {
"model": "gpt-4o-batch",
"messages": [{
"role": "user",
"content": ""
}]
}
});
let temp_file_path = format!("{}/batch_request_{}.jsonl", folder_path, id);
let mut file = File::create(&temp_file_path).map_err(|e| {
println!("Failed to create file {}: {}", temp_file_path, e);
format!("Failed to create file: {}", e)
})?;
file.write_all(jsonl_content.to_string().as_bytes())
.map_err(|e| {
println!("Failed to write JSONL to file {}: {}", temp_file_path, e);
format!("Failed to write JSONL: {}", e)
})?;
let file_request = CreateFileRequestArgs::default()
.file(Path::new(&temp_file_path))
.purpose(FilePurpose::FineTune)
.build()
.map_err(|e| {
println!("Failed to build file request: {}", e);
format!("Failed to build file request: {}", e)
})?;
// app_data.openai.files()
let uploaded_file = app_data
.openai
.files()
.create(file_request)
.await
.map_err(|e| {
println!("File upload failed for {}: {}", temp_file_path, e);
format!("File upload failed: {}", e)
})?;
file_ids.push(uploaded_file.id);
}
Ok(file_ids)
}
but with this code it works fine
use reqwest::{Client, Error};
use tokio::fs::File;
use tokio::io::AsyncReadExt;
#[tokio::main]
async fn main() -> Result<(), Error> {
let api_url = "https://xyz.com/openai/deployments/gpt-4o-batch/files";
let api_key =
"-----";
let file_path = "/Users/shivral.somani/Documents/Repos/godel-dojo/batch_request_folder/a.jsonl";
let client = Client::new();
let mut file = File::open(file_path).await.unwrap();
let mut file_bytes = Vec::new();
file.read_to_end(&mut file_bytes).await.unwrap();
let file_part = reqwest::multipart::Part::bytes(file_bytes)
.file_name("a.jsonl")
.mime_str("application/json")?; // Explicitly set content type
let form = reqwest::multipart::Form::new()
.text("purpose", "batch")
.part("file", file_part);
let response = client
.post(api_url)
.header("api-key", api_key)
.multipart(form)
.send()
.await?;
let response_text = response.text().await?;
println!("Response: {}", response_text);
Ok(())
}
I know the issue It is with .url that we call for azure config is incorrect for /files while accessing through azure config edit - batches api is also broken when you do list the response type is different need to change to option one field for azure config to work fine