1
+ using System . Text ;
2
+ using ManagedCode . OpenAI . API ;
3
+ using ManagedCode . OpenAI . Files . Abstractions ;
4
+ using ManagedCode . OpenAI . Files . Extensions ;
5
+
6
+
7
+ namespace ManagedCode . OpenAI . Files . Builders ;
8
+
9
+ public class FileManager : IFileManager
10
+ {
11
+ private readonly IOpenAiWebClient _client ;
12
+
13
+ internal FileManager ( IOpenAiWebClient webClient )
14
+ {
15
+ _client = webClient ;
16
+ }
17
+
18
+ public async Task < IFileInfo [ ] > FileListAsync ( )
19
+ {
20
+ var response = await _client . FilesInfoAsync ( ) ;
21
+ return response . ToFileInfoArray ( ) ;
22
+ }
23
+
24
+ public async Task < IFileInfo > CreateFileAsync (
25
+ string fileName ,
26
+ HttpContent content ,
27
+ string purpose = "fine-tune" )
28
+ {
29
+ var response =
30
+ await _client . CreateFileAsync ( content , fileName , purpose ) ;
31
+ return response . ToFileInfo ( ) ;
32
+ }
33
+
34
+ public async Task < IFileInfo > CreateFileAsync ( string fileName , string content , string purpose = "fine-tune" )
35
+ {
36
+ var stringContent = new StringContent ( content , Encoding . UTF8 , "multipart/form-data" ) ;
37
+ return await CreateFileAsync ( fileName , stringContent , purpose ) ;
38
+ }
39
+
40
+ public async Task < IFileInfo > CreateFileAsync ( string fileName , Stream content , string purpose = "fine-tune" )
41
+ {
42
+ var streamContent = new StreamContent ( content ) ;
43
+ return await CreateFileAsync ( fileName , streamContent , purpose ) ;
44
+ }
45
+
46
+
47
+
48
+ public async Task < bool > DeleteFileAsync ( string fileId )
49
+ {
50
+ var response = await _client . DeleteFileAsync ( fileId ) ;
51
+ return response . GetDeleteResult ( ) ;
52
+ }
53
+
54
+ public async Task < bool > DeleteFileAsync ( IFileInfo file )
55
+ {
56
+ return await DeleteFileAsync ( file . Id ) ;
57
+ }
58
+
59
+
60
+ public async Task < IFileInfo > FileInfoAsync ( string fileId )
61
+ {
62
+ var response = await _client . FileInfoAsync ( fileId ) ;
63
+ return response . ToFileInfo ( ) ;
64
+ }
65
+
66
+
67
+
68
+ public async Task < string > FileContentAsync ( IFileInfo info )
69
+ {
70
+ return await FileContentAsync ( info . Id ) ;
71
+ }
72
+
73
+ public async Task < string > FileContentAsync ( string fileId )
74
+ {
75
+ return await _client . GetContentFromFileAsync ( fileId ) ;
76
+ }
77
+ }
0 commit comments