openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.0.0-beta.12

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/Custom/Files/FileClient.cs

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