openai/openai-dotnet

Public

mirrored from https://github.com/openai/openai-dotnetAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.2.0-beta.2

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/Custom/Files/OpenAIFileClient.cs

316lines · modecode

1using System;
2using System.ClientModel;
3using System.ClientModel.Primitives;
4using System.IO;
5using System.Threading;
6using System.Threading.Tasks;
7
8namespace OpenAI.Files;
9
10// CUSTOM:
11// - Renamed.
12// - Suppressed constructor that takes endpoint parameter; endpoint is now a property in the options class.
13/// <summary> The service client for OpenAI file operations. </summary>
14[CodeGenClient("Files")]
15[CodeGenSuppress("OpenAIFileClient", typeof(ClientPipeline), typeof(ApiKeyCredential), typeof(Uri))]
16[CodeGenSuppress("ListFilesAsync", typeof(string), typeof(CancellationToken))]
17[CodeGenSuppress("ListFiles", typeof(string), typeof(CancellationToken))]
18[CodeGenSuppress("RetrieveFileAsync", typeof(string), typeof(CancellationToken))]
19[CodeGenSuppress("RetrieveFile", typeof(string), typeof(CancellationToken))]
20public partial class OpenAIFileClient
21{
22 private InternalUploadsClient _internalUploadsClient;
23
24 // CUSTOM: Added as a convenience.
25 /// <summary> Initializes a new instance of <see cref="OpenAIFileClient"/>. </summary>
26 /// <param name="apiKey"> The API key to authenticate with the service. </param>
27 /// <exception cref="ArgumentNullException"> <paramref name="apiKey"/> is null. </exception>
28 public OpenAIFileClient(string apiKey) : this(new ApiKeyCredential(apiKey), new OpenAIClientOptions())
29 {
30 }
31
32 // CUSTOM:
33 // - Used a custom pipeline.
34 // - Demoted the endpoint parameter to be a property in the options class.
35 /// <summary> Initializes a new instance of <see cref="OpenAIFileClient"/>. </summary>
36 /// <param name="credential"> The API key to authenticate with the service. </param>
37 /// <exception cref="ArgumentNullException"> <paramref name="credential"/> is null. </exception>
38 public OpenAIFileClient(ApiKeyCredential credential) : this(credential, new OpenAIClientOptions())
39 {
40 }
41
42 // CUSTOM:
43 // - Used a custom pipeline.
44 // - Demoted the endpoint parameter to be a property in the options class.
45 /// <summary> Initializes a new instance of <see cref="OpenAIFileClient"/>. </summary>
46 /// <param name="credential"> The API key to authenticate with the service. </param>
47 /// <param name="options"> The options to configure the client. </param>
48 /// <exception cref="ArgumentNullException"> <paramref name="credential"/> is null. </exception>
49 public OpenAIFileClient(ApiKeyCredential credential, OpenAIClientOptions options)
50 {
51 Argument.AssertNotNull(credential, nameof(credential));
52 options ??= new OpenAIClientOptions();
53
54 Pipeline = OpenAIClient.CreatePipeline(credential, options);
55 _endpoint = OpenAIClient.GetEndpoint(options);
56 _internalUploadsClient = new(Pipeline, options);
57 }
58
59 // CUSTOM:
60 // - Used a custom pipeline.
61 // - Demoted the endpoint parameter to be a property in the options class.
62 // - Made protected.
63 /// <summary> Initializes a new instance of <see cref="OpenAIFileClient"/>. </summary>
64 /// <param name="pipeline"> The HTTP pipeline to send and receive REST requests and responses. </param>
65 /// <param name="options"> The options to configure the client. </param>
66 /// <exception cref="ArgumentNullException"> <paramref name="pipeline"/> is null. </exception>
67 protected internal OpenAIFileClient(ClientPipeline pipeline, OpenAIClientOptions options)
68 {
69 Argument.AssertNotNull(pipeline, nameof(pipeline));
70 options ??= new OpenAIClientOptions();
71
72 Pipeline = pipeline;
73 _endpoint = OpenAIClient.GetEndpoint(options);
74 _internalUploadsClient = new(pipeline, options);
75 }
76
77 /// <summary> Uploads a file that can be used across various operations. </summary>
78 /// <remarks> Individual files can be up to 512 MB, and the size of all files uploaded by one organization can be up to 100 GB. </remarks>
79 /// <param name="file"> The file stream to upload. </param>
80 /// <param name="filename">
81 /// The filename associated with the file stream. The filename's extension (for example: .json) will be used to
82 /// validate the file format. The request may fail if the filename's extension and the actual file format do
83 /// not match.
84 /// </param>
85 /// <param name="purpose"> The intended purpose of the uploaded file. </param>
86 /// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
87 /// <exception cref="ArgumentNullException"> <paramref name="file"/> or <paramref name="filename"/> is null. </exception>
88 /// <exception cref="ArgumentException"> <paramref name="filename"/> is an empty string, and was expected to be non-empty. </exception>
89 public virtual async Task<ClientResult<OpenAIFile>> UploadFileAsync(Stream file, string filename, FileUploadPurpose purpose, CancellationToken cancellationToken = default)
90 {
91 Argument.AssertNotNull(file, nameof(file));
92 Argument.AssertNotNullOrEmpty(filename, nameof(filename));
93
94 InternalFileUploadOptions options = new()
95 {
96 Purpose = purpose
97 };
98
99 using MultipartFormDataBinaryContent content = options.ToMultipartContent(file, filename);
100 ClientResult result = await UploadFileAsync(content, content.ContentType, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
101 return ClientResult.FromValue((OpenAIFile)result, result.GetRawResponse());
102 }
103
104 /// <summary> Uploads a file that can be used across various operations. </summary>
105 /// <remarks> Individual files can be up to 512 MB, and the size of all files uploaded by one organization can be up to 100 GB. </remarks>
106 /// <param name="file"> The file stream to upload. </param>
107 /// <param name="filename">
108 /// The filename associated with the file stream. The filename's extension (for example: .json) will be used to
109 /// validate the file format. The request may fail if the filename's extension and the actual file format do
110 /// not match.
111 /// </param>
112 /// <param name="purpose"> The intended purpose of the uploaded file. </param>
113 /// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
114 /// <exception cref="ArgumentNullException"> <paramref name="file"/> or <paramref name="filename"/> is null. </exception>
115 /// <exception cref="ArgumentException"> <paramref name="filename"/> is an empty string, and was expected to be non-empty. </exception>
116 public virtual ClientResult<OpenAIFile> UploadFile(Stream file, string filename, FileUploadPurpose purpose, CancellationToken cancellationToken = default)
117 {
118 Argument.AssertNotNull(file, nameof(file));
119 Argument.AssertNotNullOrEmpty(filename, nameof(filename));
120
121 InternalFileUploadOptions options = new()
122 {
123 Purpose = purpose
124 };
125
126 using MultipartFormDataBinaryContent content = options.ToMultipartContent(file, filename);
127 ClientResult result = UploadFile(content, content.ContentType, cancellationToken.ToRequestOptions());
128 return ClientResult.FromValue((OpenAIFile)result, result.GetRawResponse());
129 }
130
131 /// <summary> Uploads a file that can be used across various operations. </summary>
132 /// <remarks> Individual files can be up to 512 MB, and the size of all files uploaded by one organization can be up to 100 GB. </remarks>
133 /// <param name="file"> The file bytes to upload. </param>
134 /// <param name="filename">
135 /// The filename associated with the file bytes. The filename's extension (for example: .json) will be used to
136 /// validate the file format. The request may fail if the filename's extension and the actual file format do
137 /// not match.
138 /// </param>
139 /// <param name="purpose"> The intended purpose of the uploaded file. </param>
140 /// <exception cref="ArgumentNullException"> <paramref name="file"/> or <paramref name="filename"/> is null. </exception>
141 /// <exception cref="ArgumentException"> <paramref name="filename"/> is an empty string, and was expected to be non-empty. </exception>
142 public virtual Task<ClientResult<OpenAIFile>> UploadFileAsync(BinaryData file, string filename, FileUploadPurpose purpose)
143 {
144 Argument.AssertNotNull(file, nameof(file));
145 Argument.AssertNotNullOrEmpty(filename, nameof(filename));
146
147 return UploadFileAsync(file?.ToStream(), filename, purpose);
148 }
149
150 /// <summary> Uploads a file that can be used across various operations. </summary>
151 /// <remarks> Individual files can be up to 512 MB, and the size of all files uploaded by one organization can be up to 100 GB. </remarks>
152 /// <param name="file"> The file bytes to upload. </param>
153 /// <param name="filename">
154 /// The filename associated with the file bytes. The filename's extension (for example: .json) will be used to
155 /// validate the file format. The request may fail if the filename's extension and the actual file format do
156 /// not match.
157 /// </param>
158 /// <param name="purpose"> The intended purpose of the uploaded file. </param>
159 /// <exception cref="ArgumentNullException"> <paramref name="file"/> or <paramref name="filename"/> is null. </exception>
160 /// <exception cref="ArgumentException"> <paramref name="filename"/> is an empty string, and was expected to be non-empty. </exception>
161 public virtual ClientResult<OpenAIFile> UploadFile(BinaryData file, string filename, FileUploadPurpose purpose)
162 {
163 Argument.AssertNotNull(file, nameof(file));
164 Argument.AssertNotNullOrEmpty(filename, nameof(filename));
165
166 return UploadFile(file?.ToStream(), filename, purpose);
167 }
168
169 /// <summary> Uploads a file that can be used across various operations. </summary>
170 /// <remarks> Individual files can be up to 512 MB, and the size of all files uploaded by one organization can be up to 100 GB. </remarks>
171 /// <param name="filePath">
172 /// The path of the file to upload. The provided file path's extension (for example: .json) will be used to
173 /// validate the file format. The request may fail if the file path's extension and the actual file format do
174 /// not match.
175 /// </param>
176 /// <param name="purpose"> The intended purpose of the uploaded file. </param>
177 /// <exception cref="ArgumentNullException"> <paramref name="filePath"/> was null. </exception>
178 /// <exception cref="ArgumentException"> <paramref name="filePath"/> is an empty string, and was expected to be non-empty. </exception>
179 public virtual async Task<ClientResult<OpenAIFile>> UploadFileAsync(string filePath, FileUploadPurpose purpose)
180 {
181 Argument.AssertNotNullOrEmpty(filePath, nameof(filePath));
182
183 using FileStream stream = File.OpenRead(filePath);
184 return await UploadFileAsync(stream, filePath, purpose).ConfigureAwait(false);
185 }
186
187 /// <summary> Uploads a file that can be used across various operations. </summary>
188 /// <remarks> Individual files can be up to 512 MB, and the size of all files uploaded by one organization can be up to 100 GB. </remarks>
189 /// <param name="filePath">
190 /// The path of the file to upload. The provided file path's extension (for example: .json) will be used to
191 /// validate the file format. The request may fail if the file path's extension and the actual file format do
192 /// not match.
193 /// </param>
194 /// <param name="purpose"> The intended purpose of the uploaded file. </param>
195 /// <exception cref="ArgumentNullException"> <paramref name="filePath"/> was null. </exception>
196 /// <exception cref="ArgumentException"> <paramref name="filePath"/> is an empty string, and was expected to be non-empty. </exception>
197 public virtual ClientResult<OpenAIFile> UploadFile(string filePath, FileUploadPurpose purpose)
198 {
199 Argument.AssertNotNullOrEmpty(filePath, nameof(filePath));
200
201 using FileStream stream = File.OpenRead(filePath);
202 return UploadFile(stream, filePath, purpose);
203 }
204
205 /// <summary> Gets basic information about each of the files belonging to the user's organization. </summary>
206 /// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
207 public virtual async Task<ClientResult<OpenAIFileCollection>> GetFilesAsync(CancellationToken cancellationToken = default)
208 {
209 ClientResult result = await GetFilesAsync(null, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
210 return ClientResult.FromValue((OpenAIFileCollection)result, result.GetRawResponse());
211 }
212
213 /// <summary> Gets basic information about each of the files belonging to the user's organization. </summary>
214 /// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
215 public virtual ClientResult<OpenAIFileCollection> GetFiles(CancellationToken cancellationToken = default)
216 {
217 ClientResult result = GetFiles(null, cancellationToken.ToRequestOptions());
218 return ClientResult.FromValue((OpenAIFileCollection)result, result.GetRawResponse());
219 }
220
221 /// <summary> Gets basic information about each of the files belonging to the user's organization. </summary>
222 /// <param name="purpose"> Only return files with the given purpose. </param>
223 /// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
224 public virtual async Task<ClientResult<OpenAIFileCollection>> GetFilesAsync(FilePurpose purpose, CancellationToken cancellationToken = default)
225 {
226 ClientResult result = await GetFilesAsync(purpose.ToSerialString(), cancellationToken.ToRequestOptions()).ConfigureAwait(false);
227 return ClientResult.FromValue((OpenAIFileCollection)result, result.GetRawResponse());
228 }
229
230 /// <summary> Gets basic information about each of the files belonging to the user's organization. </summary>
231 /// <param name="purpose"> Only return files with the given purpose. </param>
232 /// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
233 public virtual ClientResult<OpenAIFileCollection> GetFiles(FilePurpose purpose, CancellationToken cancellationToken = default)
234 {
235 ClientResult result = GetFiles(purpose.ToSerialString(), cancellationToken.ToRequestOptions());
236 return ClientResult.FromValue((OpenAIFileCollection)result, result.GetRawResponse());
237 }
238
239 /// <summary> Gets basic information about the specified file. </summary>
240 /// <param name="fileId"> The ID of the desired file. </param>
241 /// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
242 /// <exception cref="ArgumentNullException"> <paramref name="fileId"/> is null. </exception>
243 /// <exception cref="ArgumentException"> <paramref name="fileId"/> is an empty string, and was expected to be non-empty. </exception>
244 public virtual async Task<ClientResult<OpenAIFile>> GetFileAsync(string fileId, CancellationToken cancellationToken = default)
245 {
246 Argument.AssertNotNullOrEmpty(fileId, nameof(fileId));
247
248 ClientResult result = await GetFileAsync(fileId, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
249 return ClientResult.FromValue((OpenAIFile)result, result.GetRawResponse());
250 }
251
252 /// <summary> Gets basic information about the specified file. </summary>
253 /// <param name="fileId"> The ID of the desired file. </param>
254 /// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
255 /// <exception cref="ArgumentNullException"> <paramref name="fileId"/> is null. </exception>
256 /// <exception cref="ArgumentException"> <paramref name="fileId"/> is an empty string, and was expected to be non-empty. </exception>
257 public virtual ClientResult<OpenAIFile> GetFile(string fileId, CancellationToken cancellationToken = default)
258 {
259 Argument.AssertNotNullOrEmpty(fileId, nameof(fileId));
260
261 ClientResult result = GetFile(fileId, cancellationToken.ToRequestOptions());
262 return ClientResult.FromValue((OpenAIFile)result, result.GetRawResponse());
263 }
264
265 /// <summary> Deletes the specified file. </summary>
266 /// <param name="fileId"> The ID of the file to delete. </param>
267 /// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
268 /// <exception cref="ArgumentNullException"> <paramref name="fileId"/> is null. </exception>
269 /// <exception cref="ArgumentException"> <paramref name="fileId"/> is an empty string, and was expected to be non-empty. </exception>
270 public virtual async Task<ClientResult<FileDeletionResult>> DeleteFileAsync(string fileId, CancellationToken cancellationToken = default)
271 {
272 Argument.AssertNotNullOrEmpty(fileId, nameof(fileId));
273
274 ClientResult result = await DeleteFileAsync(fileId, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
275 return ClientResult.FromValue((FileDeletionResult)result, result.GetRawResponse());
276 }
277
278 /// <summary> Deletes the specified file. </summary>
279 /// <param name="fileId"> The ID of the file to delete. </param>
280 /// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
281 /// <exception cref="ArgumentNullException"> <paramref name="fileId"/> is null. </exception>
282 /// <exception cref="ArgumentException"> <paramref name="fileId"/> is an empty string, and was expected to be non-empty. </exception>
283 public virtual ClientResult<FileDeletionResult> DeleteFile(string fileId, CancellationToken cancellationToken = default)
284 {
285 Argument.AssertNotNullOrEmpty(fileId, nameof(fileId));
286
287 ClientResult result = DeleteFile(fileId, cancellationToken.ToRequestOptions());
288 return ClientResult.FromValue((FileDeletionResult)result, result.GetRawResponse());
289 }
290
291 /// <summary> Downloads the content of the specified file. </summary>
292 /// <param name="fileId"> The ID of the file to download. </param>
293 /// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
294 /// <exception cref="ArgumentNullException"> <paramref name="fileId"/> is null. </exception>
295 /// <exception cref="ArgumentException"> <paramref name="fileId"/> is an empty string, and was expected to be non-empty. </exception>
296 public virtual async Task<ClientResult<BinaryData>> DownloadFileAsync(string fileId, CancellationToken cancellationToken = default)
297 {
298 Argument.AssertNotNullOrEmpty(fileId, nameof(fileId));
299
300 ClientResult result = await DownloadFileAsync(fileId, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
301 return ClientResult.FromValue(result.GetRawResponse().Content, result.GetRawResponse());
302 }
303
304 /// <summary> Downloads the content of the specified file. </summary>
305 /// <param name="fileId"> The ID of the file to download. </param>
306 /// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
307 /// <exception cref="ArgumentNullException"> <paramref name="fileId"/> is null. </exception>
308 /// <exception cref="ArgumentException"> <paramref name="fileId"/> is an empty string, and was expected to be non-empty. </exception>
309 public virtual ClientResult<BinaryData> DownloadFile(string fileId, CancellationToken cancellationToken = default)
310 {
311 Argument.AssertNotNullOrEmpty(fileId, nameof(fileId));
312
313 ClientResult result = DownloadFile(fileId, cancellationToken.ToRequestOptions());
314 return ClientResult.FromValue(result.GetRawResponse().Content, result.GetRawResponse());
315 }
316}
317