openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.1.0-beta.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/Custom/Files/OpenAIFileClient.cs

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