Skip to content
This repository was archived by the owner on Feb 12, 2025. It is now read-only.

Commit 518c322

Browse files
YaroslavVorobyovKSemenenko
authored andcommitted
Added FileInfo for work with file in OpenAI
But, the problem is that files are created only when purpose = "fine-tune". When downloading files, the message "To help mitigate abuse, downloading of fine-tune training files is disabled for free accounts"
1 parent e47a4a1 commit 518c322

File tree

6 files changed

+188
-0
lines changed

6 files changed

+188
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Newtonsoft.Json;
2+
3+
namespace ManagedCode.OpenAI.Client.Files;
4+
5+
public class FileDeleteResult
6+
{
7+
[JsonProperty("id")] public string Id;
8+
9+
[JsonProperty("object")] public string Object;
10+
11+
[JsonProperty("deleted")] public bool Deleted;
12+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using Newtonsoft.Json;
2+
3+
namespace ManagedCode.OpenAI.Client.Files;
4+
5+
public class FileInfo
6+
{
7+
[JsonProperty("id")]
8+
public string Id;
9+
10+
[JsonProperty("object")]
11+
public string Object;
12+
13+
[JsonProperty("bytes")]
14+
public int Bytes;
15+
16+
[JsonProperty("created_at")]
17+
public int CreatedAt;
18+
19+
[JsonProperty("filename")]
20+
public string Filename;
21+
22+
[JsonProperty("purpose")]
23+
public string Purpose;
24+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Newtonsoft.Json;
2+
3+
namespace ManagedCode.OpenAI.Client.Files;
4+
5+
public class FileListResult
6+
{
7+
[JsonProperty("data")]
8+
public List<FileInfo> Data;
9+
10+
[JsonProperty("object")]
11+
public string Object;
12+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,127 @@
1+
using System.Net.Http.Headers;
2+
using System.Text;
3+
using ManagedCode.OpenAI.Client.Exceptions;
4+
using Newtonsoft.Json;
5+
16
namespace ManagedCode.OpenAI.Client.Files;
27

38
public class FileManager
49
{
10+
public const string URL_FILES = "https://api.openai.com/v1/files";
11+
12+
public const string URL_FILE = URL_FILES + "/{0}";
13+
14+
public const string URL_FILE_CONTEXT = URL_FILE + "/content";
15+
16+
public const string FINE_TUNE = "fine-tune";
17+
18+
private readonly OpenAIClient _client;
19+
20+
internal FileManager(OpenAIClient client)
21+
{
22+
_client = client;
23+
}
24+
25+
public async Task<FileListResult> ListAsync()
26+
{
27+
var httpResponseMessage = await _client.GetAsync(URL_FILES);
28+
29+
OpenAIExceptions.ThrowsIfError(httpResponseMessage.StatusCode);
30+
31+
string responseBody = await httpResponseMessage.Content.ReadAsStringAsync();
32+
33+
var result = JsonConvert.DeserializeObject<FileListResult>(responseBody);
34+
35+
return result;
36+
}
37+
38+
// TODO: Upload file
39+
40+
public async Task<FileInfo> UploadAsync(string fileName, HttpContent content)
41+
{
42+
MultipartFormDataContent multipartFormDataContent = new();
43+
44+
multipartFormDataContent.Add(new StringContent(FINE_TUNE), "purpose");
45+
46+
multipartFormDataContent.Add(content, "file", fileName);
47+
48+
var httpResponseMessage = await _client.PostAsync(URL_FILES, multipartFormDataContent);
49+
50+
OpenAIExceptions.ThrowsIfError(httpResponseMessage.StatusCode);
51+
52+
string responseBody = await httpResponseMessage.Content.ReadAsStringAsync();
53+
54+
var result = JsonConvert.DeserializeObject<FileInfo>(responseBody);
55+
56+
57+
return result;
58+
}
59+
60+
61+
public async Task<FileInfo> UploadAsync(string fileName, string content)
62+
{
63+
return await UploadAsync(fileName, new StringContent(content, Encoding.UTF8, "multipart/form-data"));
64+
}
565

66+
public async Task<FileInfo> UploadAsync(string fileName, Stream content)
67+
{
68+
return await UploadAsync(fileName, new StreamContent(content));
69+
}
70+
71+
public async Task<FileDeleteResult> DeleteAsync(FileInfo file)
72+
{
73+
return await DeleteAsync(file.Id);
74+
}
75+
76+
public async Task<FileDeleteResult> DeleteAsync(string fileId)
77+
{
78+
string resultUrl = string.Format(URL_FILE, fileId);
79+
80+
var httpResponseMessage = await _client.DeleteAsync(resultUrl);
81+
82+
OpenAIExceptions.ThrowsIfError(httpResponseMessage.StatusCode);
83+
84+
string responseBody = await httpResponseMessage.Content.ReadAsStringAsync();
85+
86+
var result = JsonConvert.DeserializeObject<FileDeleteResult>(responseBody);
87+
88+
return result;
89+
}
90+
91+
public async Task<FileInfo> RetrieveAsync(FileInfo file)
92+
{
93+
return await RetrieveAsync(file.Id);
94+
}
95+
96+
public async Task<FileInfo> RetrieveAsync(string fileId)
97+
{
98+
string resultUrl = string.Format(URL_FILE, fileId);
99+
100+
var httpResponseMessage = await _client.GetAsync(resultUrl);
101+
102+
OpenAIExceptions.ThrowsIfError(httpResponseMessage.StatusCode);
103+
104+
string responseBody = await httpResponseMessage.Content.ReadAsStringAsync();
105+
106+
var result = JsonConvert.DeserializeObject<FileInfo>(responseBody);
107+
108+
return result;
109+
}
110+
111+
112+
public async Task<Stream> ContentAsync(FileInfo info)
113+
{
114+
return await ContentAsync(info.Id);
115+
}
116+
117+
public async Task<Stream> ContentAsync(string fileId)
118+
{
119+
string resultUrl = string.Format(URL_FILE_CONTEXT, fileId);
120+
121+
var httpResponseMessage = await _client.GetAsync(resultUrl);
122+
123+
OpenAIExceptions.ThrowsIfError(httpResponseMessage.StatusCode);
124+
125+
return await httpResponseMessage.Content.ReadAsStreamAsync();
126+
}
6127
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace ManagedCode.OpenAI.Client.Files;
2+
3+
public static class FileManagerMethods
4+
{
5+
6+
public static FileManager AsFileManager(this OpenAIClient client)
7+
{
8+
return new FileManager(client);
9+
}
10+
}

ManagedCode.OpenAI.Client.Files/ManagedCode.OpenAI.Client.Files.csproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,13 @@
66
<Nullable>enable</Nullable>
77
</PropertyGroup>
88

9+
<ItemGroup>
10+
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<ProjectReference Include="..\ManagedCode.OpenAI.Client.Exceptions\ManagedCode.OpenAI.Client.Exceptions.csproj" />
15+
<ProjectReference Include="..\ManagedCode.OpenAI.Client\ManagedCode.OpenAI.Client.csproj" />
16+
</ItemGroup>
17+
918
</Project>

0 commit comments

Comments
 (0)