openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.3.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/Custom/Files/OpenAIFileClient.cs

336lines · modecode

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