[FEATURE REQ] Allow setting expires_after on file upload
Opened this issue · 3 comments
Describe the feature or improvement you are requesting
The OpenAI API supports adding an expires_after property to an upload file request. I can't see that option available in this SDK. It would be really useful to be able to set that so we're able to more easily manage how long data stays on OpenAI. Currently we're having to run a background job to delete after a certain number of days.
Additional context
https://platform.openai.com/docs/api-reference/files/create?lang=curl
Hi @mgkeen. Thanks for reaching out and for your request. The expires_after object appears to be recently added and does not yet exist in the REST API Spec from which the client is generated. Once an updated spec is available with that parameter, it will be included in the library. In the meantime, you can populate this by dropping down to the protocol level:
// You must set an explicit boundary marker, because it has to appear in the
// content-type, which does not happen by default.
var boundary = "JRTaGGD35vdOu1wjW6Rou71qWybpyp_qs3F4kWkJMWMJY_Cy3RQkJAplASJ9L_ElHI853W";
var contentType = $"multipart/form-data; boundary=\"{boundary}\"";
var multiPart = new MultipartFormDataContent(boundary);
// Set the file (in this case, a fake one from a hard-coded string)
var fileStream = BinaryData.FromString("""
{"name": "John Doe", "age": 30, "city": "New York"}
{"name": "Jane Smith", "age": 25, "city": "San Francisco"}
{"name": "Bob Johnson", "age": 35, "city": "Chicago"}
""")
.ToStream();
multiPart.Add(new StreamContent(fileStream), "file", "my-file.jsonl");
// Set the purpose
multiPart.Add(new StringContent("fine-tune", new MediaTypeHeaderValue("text/plain")), "purpose");
// Set the expiration (don't be mislead by the "object" designation in the platform docs, they're content parts)
multiPart.Add(new StringContent("created_at", new MediaTypeHeaderValue("text/plain")), "expires_after[anchor]");
multiPart.Add(new StringContent("3600", new MediaTypeHeaderValue("text/plain")), "expires_after[seconds]");
// Create the client and send the request.
var client = new OpenAIFileClient("<< YOUR API KEY >>");
var rawResponse = await client.UploadFileAsync(BinaryContent.Create(memoryStream), contentType);
var file = ModelReaderWriter.Read<OpenAIFile>(rawResponse.GetRawResponse().Content);
Console.WriteLine($"The file Id is: {file.Id}");//cc: @trrwilson