openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.1.0-beta.2

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/Custom/Images/ImageClient.cs

841lines · modeblame

9f9f2936Jose Arriaga Maldonado2 years ago1using System;
2using System.ClientModel;
3using System.ClientModel.Primitives;
4using System.IO;
5using System.Linq;
19a65a0aKrzysztof Cwalina2 years ago6using System.Threading;
9f9f2936Jose Arriaga Maldonado2 years ago7using System.Threading.Tasks;
8
9namespace OpenAI.Images;
10
13a9c686Jose Arriaga Maldonado1 years ago11// CUSTOM:
12// - Renamed.
13// - Suppressed constructor that takes endpoint parameter; endpoint is now a property in the options class.
14// - Suppressed methods that only take the options parameter.
9f9f2936Jose Arriaga Maldonado2 years ago15/// <summary> The service client for OpenAI image operations. </summary>
16[CodeGenClient("Images")]
17[CodeGenSuppress("ImageClient", typeof(ClientPipeline), typeof(ApiKeyCredential), typeof(Uri))]
18[CodeGenSuppress("CreateImageAsync", typeof(ImageGenerationOptions))]
19[CodeGenSuppress("CreateImage", typeof(ImageGenerationOptions))]
20[CodeGenSuppress("CreateImageEditAsync", typeof(ImageEditOptions))]
21[CodeGenSuppress("CreateImageEdit", typeof(ImageEditOptions))]
22[CodeGenSuppress("CreateImageVariationAsync", typeof(ImageVariationOptions))]
23[CodeGenSuppress("CreateImageVariation", typeof(ImageVariationOptions))]
24public partial class ImageClient
25{
26private readonly string _model;
27
75eded51ShivangiReja1 years ago28// CUSTOM: Remove virtual keyword.
29/// <summary>
30/// The HTTP pipeline for sending and receiving REST requests and responses.
31/// </summary>
32public ClientPipeline Pipeline => _pipeline;
33
2ab1a942Jose Arriaga Maldonado1 years ago34// CUSTOM: Added as a convenience.
35/// <summary> Initializes a new instance of <see cref="ImageClient">. </summary>
36/// <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>
37/// <param name="apiKey"> The API key to authenticate with the service. </param>
38/// <exception cref="ArgumentNullException"> <paramref name="model"/> or <paramref name="apiKey"/> is null. </exception>
39/// <exception cref="ArgumentException"> <paramref name="model"/> is an empty string, and was expected to be non-empty. </exception>
40public ImageClient(string model, string apiKey) : this(model, new ApiKeyCredential(apiKey), new OpenAIClientOptions())
41{
42}
43
9f9f2936Jose Arriaga Maldonado2 years ago44// CUSTOM:
45// - Added `model` parameter.
13a9c686Jose Arriaga Maldonado1 years ago46// - Used a custom pipeline.
47// - Demoted the endpoint parameter to be a property in the options class.
48/// <summary> Initializes a new instance of <see cref="ImageClient">. </summary>
49/// <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>
50/// <param name="credential"> The API key to authenticate with the service. </param>
51/// <exception cref="ArgumentNullException"> <paramref name="model"/> or <paramref name="credential"/> is null. </exception>
52/// <exception cref="ArgumentException"> <paramref name="model"/> is an empty string, and was expected to be non-empty. </exception>
53public ImageClient(string model, ApiKeyCredential credential) : this(model, credential, new OpenAIClientOptions())
54{
55}
9f9f2936Jose Arriaga Maldonado2 years ago56
57// CUSTOM:
58// - Added `model` parameter.
13a9c686Jose Arriaga Maldonado1 years ago59// - Used a custom pipeline.
60// - Demoted the endpoint parameter to be a property in the options class.
61/// <summary> Initializes a new instance of <see cref="ImageClient">. </summary>
62/// <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>
63/// <param name="credential"> The API key to authenticate with the service. </param>
64/// <param name="options"> The options to configure the client. </param>
65/// <exception cref="ArgumentNullException"> <paramref name="model"/> or <paramref name="credential"/> is null. </exception>
66/// <exception cref="ArgumentException"> <paramref name="model"/> is an empty string, and was expected to be non-empty. </exception>
67public ImageClient(string model, ApiKeyCredential credential, OpenAIClientOptions options)
68{
69Argument.AssertNotNullOrEmpty(model, nameof(model));
70Argument.AssertNotNull(credential, nameof(credential));
71options ??= new OpenAIClientOptions();
9f9f2936Jose Arriaga Maldonado2 years ago72
13a9c686Jose Arriaga Maldonado1 years ago73_model = model;
74_pipeline = OpenAIClient.CreatePipeline(credential, options);
75_endpoint = OpenAIClient.GetEndpoint(options);
76}
77
78// CUSTOM:
79// - Added `model` parameter.
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="ImageClient">. </summary>
84/// <param name="pipeline"> The HTTP pipeline to send and receive REST requests and responses. </param>
85/// <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>
86/// <param name="options"> The options to configure the client. </param>
87/// <exception cref="ArgumentNullException"> <paramref name="pipeline"/> or <paramref name="model"/> is null. </exception>
88/// <exception cref="ArgumentException"> <paramref name="model"/> is an empty string, and was expected to be non-empty. </exception>
89protected internal ImageClient(ClientPipeline pipeline, string model, OpenAIClientOptions options)
9f9f2936Jose Arriaga Maldonado2 years ago90{
13a9c686Jose Arriaga Maldonado1 years ago91Argument.AssertNotNull(pipeline, nameof(pipeline));
9f9f2936Jose Arriaga Maldonado2 years ago92Argument.AssertNotNullOrEmpty(model, nameof(model));
13a9c686Jose Arriaga Maldonado1 years ago93options ??= new OpenAIClientOptions();
9f9f2936Jose Arriaga Maldonado2 years ago94
95_model = model;
13a9c686Jose Arriaga Maldonado1 years ago96_pipeline = pipeline;
97_endpoint = OpenAIClient.GetEndpoint(options);
9f9f2936Jose Arriaga Maldonado2 years ago98}
99
100#region GenerateImages
101
13a9c686Jose Arriaga Maldonado1 years ago102/// <summary> Generates an image based on a prompt. </summary>
9f9f2936Jose Arriaga Maldonado2 years ago103/// <param name="prompt"> A text description of the desired image. </param>
13a9c686Jose Arriaga Maldonado1 years ago104/// <param name="options"> The options to configure the image generation. </param>
105/// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
9f9f2936Jose Arriaga Maldonado2 years ago106/// <exception cref="ArgumentNullException"> <paramref name="prompt"/> is null. </exception>
107/// <exception cref="ArgumentException"> <paramref name="prompt"/> is an empty string, and was expected to be non-empty. </exception>
19a65a0aKrzysztof Cwalina2 years ago108public virtual async Task<ClientResult<GeneratedImage>> GenerateImageAsync(string prompt, ImageGenerationOptions options = null, CancellationToken cancellationToken = default)
9f9f2936Jose Arriaga Maldonado2 years ago109{
110Argument.AssertNotNullOrEmpty(prompt, nameof(prompt));
111
112options ??= new();
113CreateImageGenerationOptions(prompt, null, ref options);
114
115using BinaryContent content = options.ToBinaryContent();
19a65a0aKrzysztof Cwalina2 years ago116ClientResult result = await GenerateImagesAsync(content, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
9f9f2936Jose Arriaga Maldonado2 years ago117return ClientResult.FromValue(GeneratedImageCollection.FromResponse(result.GetRawResponse()).FirstOrDefault(), result.GetRawResponse());
118}
119
13a9c686Jose Arriaga Maldonado1 years ago120/// <summary> Generates an image based on a prompt. </summary>
9f9f2936Jose Arriaga Maldonado2 years ago121/// <param name="prompt"> A text description of the desired image. </param>
13a9c686Jose Arriaga Maldonado1 years ago122/// <param name="options"> The options to configure the image generation. </param>
123/// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
9f9f2936Jose Arriaga Maldonado2 years ago124/// <exception cref="ArgumentNullException"> <paramref name="prompt"/> is null. </exception>
125/// <exception cref="ArgumentException"> <paramref name="prompt"/> is an empty string, and was expected to be non-empty. </exception>
19a65a0aKrzysztof Cwalina2 years ago126public virtual ClientResult<GeneratedImage> GenerateImage(string prompt, ImageGenerationOptions options = null, CancellationToken cancellationToken = default)
9f9f2936Jose Arriaga Maldonado2 years ago127{
128Argument.AssertNotNullOrEmpty(prompt, nameof(prompt));
129
130options ??= new();
131CreateImageGenerationOptions(prompt, null, ref options);
132
133using BinaryContent content = options.ToBinaryContent();
19a65a0aKrzysztof Cwalina2 years ago134ClientResult result = GenerateImages(content, cancellationToken.ToRequestOptions());
9f9f2936Jose Arriaga Maldonado2 years ago135return ClientResult.FromValue(GeneratedImageCollection.FromResponse(result.GetRawResponse()).FirstOrDefault(), result.GetRawResponse());
136}
137
13a9c686Jose Arriaga Maldonado1 years ago138/// <summary> Generates images based on a prompt. </summary>
9f9f2936Jose Arriaga Maldonado2 years ago139/// <param name="prompt"> A text description of the desired images. </param>
140/// <param name="imageCount"> The number of images to generate. </param>
13a9c686Jose Arriaga Maldonado1 years ago141/// <param name="options"> The options to configure the image generation. </param>
142/// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
9f9f2936Jose Arriaga Maldonado2 years ago143/// <exception cref="ArgumentNullException"> <paramref name="prompt"/> is null. </exception>
144/// <exception cref="ArgumentException"> <paramref name="prompt"/> is an empty string, and was expected to be non-empty. </exception>
19a65a0aKrzysztof Cwalina2 years ago145public virtual async Task<ClientResult<GeneratedImageCollection>> GenerateImagesAsync(string prompt, int imageCount, ImageGenerationOptions options = null, CancellationToken cancellationToken = default)
9f9f2936Jose Arriaga Maldonado2 years ago146{
147Argument.AssertNotNullOrEmpty(prompt, nameof(prompt));
148
149options ??= new();
150CreateImageGenerationOptions(prompt, imageCount, ref options);
151
152using BinaryContent content = options.ToBinaryContent();
19a65a0aKrzysztof Cwalina2 years ago153ClientResult result = await GenerateImagesAsync(content, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
9f9f2936Jose Arriaga Maldonado2 years ago154return ClientResult.FromValue(GeneratedImageCollection.FromResponse(result.GetRawResponse()), result.GetRawResponse());
155}
156
13a9c686Jose Arriaga Maldonado1 years ago157/// <summary> Generates images based on a prompt. </summary>
9f9f2936Jose Arriaga Maldonado2 years ago158/// <param name="prompt"> A text description of the desired images. </param>
159/// <param name="imageCount"> The number of images to generate. </param>
13a9c686Jose Arriaga Maldonado1 years ago160/// <param name="options"> The options to configure the image generation. </param>
161/// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
9f9f2936Jose Arriaga Maldonado2 years ago162/// <exception cref="ArgumentNullException"> <paramref name="prompt"/> is null. </exception>
163/// <exception cref="ArgumentException"> <paramref name="prompt"/> is an empty string, and was expected to be non-empty. </exception>
19a65a0aKrzysztof Cwalina2 years ago164public virtual ClientResult<GeneratedImageCollection> GenerateImages(string prompt, int imageCount, ImageGenerationOptions options = null, CancellationToken cancellationToken = default)
9f9f2936Jose Arriaga Maldonado2 years ago165{
166Argument.AssertNotNullOrEmpty(prompt, nameof(prompt));
167
168options ??= new();
169CreateImageGenerationOptions(prompt, imageCount, ref options);
170
171using BinaryContent content = options.ToBinaryContent();
19a65a0aKrzysztof Cwalina2 years ago172ClientResult result = GenerateImages(content, cancellationToken.ToRequestOptions());
9f9f2936Jose Arriaga Maldonado2 years ago173return ClientResult.FromValue(GeneratedImageCollection.FromResponse(result.GetRawResponse()), result.GetRawResponse());
174}
175
176#endregion
177
178#region GenerateImageEdits
179
13a9c686Jose Arriaga Maldonado1 years ago180/// <summary> Generates an edited or extended image based on an original image and a prompt. </summary>
9f9f2936Jose Arriaga Maldonado2 years ago181/// <param name="image">
13a9c686Jose Arriaga Maldonado1 years ago182/// The image stream to edit. Must be a valid PNG file, less than 4MB, and square. The image must have transparency, which
183/// will be used as the mask.
9f9f2936Jose Arriaga Maldonado2 years ago184/// </param>
185/// <param name="imageFilename">
13a9c686Jose Arriaga Maldonado1 years ago186/// The filename associated with the image stream. The filename's extension (for example: .png) will be used to
187/// validate the format of the input image. The request may fail if the filename's extension and the actual
188/// format of the input image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago189/// </param>
190/// <param name="prompt"> A text description of the desired image. </param>
13a9c686Jose Arriaga Maldonado1 years ago191/// <param name="options"> The options to configure the image edit. </param>
192/// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
9f9f2936Jose Arriaga Maldonado2 years ago193/// <exception cref="ArgumentNullException"> <paramref name="image"/>, <paramref name="imageFilename"/>, or <paramref name="prompt"/> is null. </exception>
194/// <exception cref="ArgumentException"> <paramref name="imageFilename"/> or <paramref name="prompt"/> is an empty string, and was expected to be non-empty. </exception>
19a65a0aKrzysztof Cwalina2 years ago195public virtual async Task<ClientResult<GeneratedImage>> GenerateImageEditAsync(Stream image, string imageFilename, string prompt, ImageEditOptions options = null, CancellationToken cancellationToken = default)
9f9f2936Jose Arriaga Maldonado2 years ago196{
197Argument.AssertNotNull(image, nameof(image));
198Argument.AssertNotNullOrEmpty(imageFilename, nameof(imageFilename));
199Argument.AssertNotNullOrEmpty(prompt, nameof(prompt));
200
201options ??= new();
202CreateImageEditOptions(image, imageFilename, prompt, null, null, null, ref options);
203
204using MultipartFormDataBinaryContent content = options.ToMultipartContent(image, imageFilename, null, null);
19a65a0aKrzysztof Cwalina2 years ago205ClientResult result = await GenerateImageEditsAsync(content, content.ContentType, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
9f9f2936Jose Arriaga Maldonado2 years ago206return ClientResult.FromValue(GeneratedImageCollection.FromResponse(result.GetRawResponse()).FirstOrDefault(), result.GetRawResponse());
207}
208
13a9c686Jose Arriaga Maldonado1 years ago209/// <summary> Generates an edited or extended image based on an original image and a prompt. </summary>
9f9f2936Jose Arriaga Maldonado2 years ago210/// <param name="image">
13a9c686Jose Arriaga Maldonado1 years ago211/// The image stream to edit. Must be a valid PNG file, less than 4MB, and square. The image must have transparency, which
212/// will be used as the mask.
9f9f2936Jose Arriaga Maldonado2 years ago213/// </param>
214/// <param name="imageFilename">
13a9c686Jose Arriaga Maldonado1 years ago215/// The filename associated with the image stream. The filename's extension (for example: .png) will be used to
216/// validate the format of the input image. The request may fail if the filename's extension and the actual
217/// format of the input image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago218/// </param>
219/// <param name="prompt"> A text description of the desired image. </param>
13a9c686Jose Arriaga Maldonado1 years ago220/// <param name="options"> The options to configure the image edit. </param>
221/// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
9f9f2936Jose Arriaga Maldonado2 years ago222/// <exception cref="ArgumentNullException"> <paramref name="image"/>, <paramref name="imageFilename"/>, or <paramref name="prompt"/> is null. </exception>
223/// <exception cref="ArgumentException"> <paramref name="imageFilename"/> or <paramref name="prompt"/> is an empty string, and was expected to be non-empty. </exception>
19a65a0aKrzysztof Cwalina2 years ago224public virtual ClientResult<GeneratedImage> GenerateImageEdit(Stream image, string imageFilename, string prompt, ImageEditOptions options = null, CancellationToken cancellationToken = default)
9f9f2936Jose Arriaga Maldonado2 years ago225{
226Argument.AssertNotNull(image, nameof(image));
227Argument.AssertNotNullOrEmpty(imageFilename, nameof(imageFilename));
228Argument.AssertNotNullOrEmpty(prompt, nameof(prompt));
229
230options ??= new();
231CreateImageEditOptions(image, imageFilename, prompt, null, null, null, ref options);
232
233using MultipartFormDataBinaryContent content = options.ToMultipartContent(image, imageFilename, null, null);
19a65a0aKrzysztof Cwalina2 years ago234ClientResult result = GenerateImageEdits(content, content.ContentType, cancellationToken.ToRequestOptions());
9f9f2936Jose Arriaga Maldonado2 years ago235return ClientResult.FromValue(GeneratedImageCollection.FromResponse(result.GetRawResponse()).FirstOrDefault(), result.GetRawResponse());
236}
237
13a9c686Jose Arriaga Maldonado1 years ago238/// <summary> Generates an edited or extended image based on an original image and a prompt. </summary>
9f9f2936Jose Arriaga Maldonado2 years ago239/// <param name="imageFilePath">
13a9c686Jose Arriaga Maldonado1 years ago240/// The path of the image file to edit. Must be a valid PNG file, less than 4MB, and square. The image must
241/// have transparency, which will be used as the mask. The provided file path's extension (for example: .png)
242/// will be used to validate the format of the input image. The request may fail if the file path's extension
243/// and the actual format of the input image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago244/// </param>
245/// <param name="prompt"> A text description of the desired image. </param>
13a9c686Jose Arriaga Maldonado1 years ago246/// <param name="options"> The options to configure the image edit. </param>
9f9f2936Jose Arriaga Maldonado2 years ago247/// <exception cref="ArgumentNullException"> <paramref name="imageFilePath"/> or <paramref name="prompt"/> is null. </exception>
248/// <exception cref="ArgumentException"> <paramref name="imageFilePath"/> or <paramref name="prompt"/> is an empty string, and was expected to be non-empty. </exception>
249public virtual async Task<ClientResult<GeneratedImage>> GenerateImageEditAsync(string imageFilePath, string prompt, ImageEditOptions options = null)
250{
251Argument.AssertNotNullOrEmpty(imageFilePath, nameof(imageFilePath));
252Argument.AssertNotNullOrEmpty(prompt, nameof(prompt));
253
254using FileStream imageStream = File.OpenRead(imageFilePath);
255return await GenerateImageEditAsync(imageStream, imageFilePath, prompt, options).ConfigureAwait(false);
256}
257
13a9c686Jose Arriaga Maldonado1 years ago258/// <summary> Generates an edited or extended image based on an original image and a prompt. </summary>
9f9f2936Jose Arriaga Maldonado2 years ago259/// <param name="imageFilePath">
13a9c686Jose Arriaga Maldonado1 years ago260/// The path of the image file to edit. Must be a valid PNG file, less than 4MB, and square. The image must
261/// have transparency, which will be used as the mask. The provided file path's extension (for example: .png)
262/// will be used to validate the format of the input image. The request may fail if the file path's extension
263/// and the actual format of the input image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago264/// </param>
265/// <param name="prompt"> A text description of the desired image. </param>
13a9c686Jose Arriaga Maldonado1 years ago266/// <param name="options"> The options to configure the image edit. </param>
9f9f2936Jose Arriaga Maldonado2 years ago267/// <exception cref="ArgumentNullException"> <paramref name="imageFilePath"/> or <paramref name="prompt"/> is null. </exception>
268/// <exception cref="ArgumentException"> <paramref name="imageFilePath"/> or <paramref name="prompt"/> is an empty string, and was expected to be non-empty. </exception>
269public virtual ClientResult<GeneratedImage> GenerateImageEdit(string imageFilePath, string prompt, ImageEditOptions options = null)
270{
271Argument.AssertNotNullOrEmpty(imageFilePath, nameof(imageFilePath));
272Argument.AssertNotNullOrEmpty(prompt, nameof(prompt));
273
274using FileStream imageStream = File.OpenRead(imageFilePath);
79014abcShivangiReja1 years ago275return GenerateImageEdit(imageStream, imageFilePath, prompt, options);
9f9f2936Jose Arriaga Maldonado2 years ago276}
277
13a9c686Jose Arriaga Maldonado1 years ago278/// <summary> Generates an edited or extended image based on an original image, a prompt, and a mask. </summary>
279/// <param name="image"> The image stream to edit. Must be a valid PNG file, less than 4MB, and square. </param>
9f9f2936Jose Arriaga Maldonado2 years ago280/// <param name="imageFilename">
13a9c686Jose Arriaga Maldonado1 years ago281/// The filename associated with the image stream. The filename's extension (for example: .png) will be used to
282/// validate the format of the input image. The request may fail if the filename's extension and the actual
283/// format of the input image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago284/// </param>
285/// <param name="prompt"> A text description of the desired image. </param>
286/// <param name="mask">
13a9c686Jose Arriaga Maldonado1 years ago287/// An additional image whose fully transparent areas (i.e., where alpha is zero) indicate where the original image
288/// should be edited. Must be a valid PNG file, less than 4MB, and have the same dimensions as image.
9f9f2936Jose Arriaga Maldonado2 years ago289/// </param>
290/// <param name="maskFilename">
13a9c686Jose Arriaga Maldonado1 years ago291/// The filename associated with the mask image stream. The filename's extension (for example: .png) will be
292/// used to validate the format of the mask image. The request may fail if the filename's extension and the
293/// actual format of the mask image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago294/// </param>
13a9c686Jose Arriaga Maldonado1 years ago295/// <param name="options"> The options to configure the image edit. </param>
296/// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
9f9f2936Jose Arriaga Maldonado2 years ago297/// <exception cref="ArgumentNullException"> <paramref name="image"/>, <paramref name="imageFilename"/>, <paramref name="prompt"/>, <paramref name="mask"/>, or <paramref name="maskFilename"/> is null. </exception>
298/// <exception cref="ArgumentException"> <paramref name="imageFilename"/>, <paramref name="prompt"/>, or <paramref name="maskFilename"/> is an empty string, and was expected to be non-empty. </exception>
19a65a0aKrzysztof Cwalina2 years ago299public virtual async Task<ClientResult<GeneratedImage>> GenerateImageEditAsync(Stream image, string imageFilename, string prompt, Stream mask, string maskFilename, ImageEditOptions options = null, CancellationToken cancellationToken = default)
9f9f2936Jose Arriaga Maldonado2 years ago300{
301Argument.AssertNotNull(image, nameof(image));
302Argument.AssertNotNullOrEmpty(imageFilename, nameof(imageFilename));
303Argument.AssertNotNullOrEmpty(prompt, nameof(prompt));
304Argument.AssertNotNull(mask, nameof(mask));
305Argument.AssertNotNullOrEmpty(maskFilename, nameof(maskFilename));
306
307options ??= new();
308CreateImageEditOptions(image, imageFilename, prompt, mask, maskFilename, null, ref options);
309
310using MultipartFormDataBinaryContent content = options.ToMultipartContent(image, imageFilename, mask, maskFilename);
19a65a0aKrzysztof Cwalina2 years ago311ClientResult result = await GenerateImageEditsAsync(content, content.ContentType, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
9f9f2936Jose Arriaga Maldonado2 years ago312return ClientResult.FromValue(GeneratedImageCollection.FromResponse(result.GetRawResponse()).FirstOrDefault(), result.GetRawResponse());
313}
314
13a9c686Jose Arriaga Maldonado1 years ago315/// <summary> Generates an edited or extended image based on an original image, a prompt, and a mask. </summary>
316/// <param name="image"> The image stream to edit. Must be a valid PNG file, less than 4MB, and square. </param>
9f9f2936Jose Arriaga Maldonado2 years ago317/// <param name="imageFilename">
13a9c686Jose Arriaga Maldonado1 years ago318/// The filename associated with the image stream. The filename's extension (for example: .png) will be used to
319/// validate the format of the input image. The request may fail if the filename's extension and the actual
320/// format of the input image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago321/// </param>
322/// <param name="prompt"> A text description of the desired image. </param>
323/// <param name="mask">
13a9c686Jose Arriaga Maldonado1 years ago324/// An additional image whose fully transparent areas (i.e., where alpha is zero) indicate where the original image
325/// should be edited. Must be a valid PNG file, less than 4MB, and have the same dimensions as image.
9f9f2936Jose Arriaga Maldonado2 years ago326/// </param>
327/// <param name="maskFilename">
13a9c686Jose Arriaga Maldonado1 years ago328/// The filename associated with the mask image stream. The filename's extension (for example: .png) will be
329/// used to validate the format of the mask image. The request may fail if the filename's extension and the
330/// actual format of the mask image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago331/// </param>
13a9c686Jose Arriaga Maldonado1 years ago332/// <param name="options"> The options to configure the image edit. </param>
333/// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
9f9f2936Jose Arriaga Maldonado2 years ago334/// <exception cref="ArgumentNullException"> <paramref name="image"/>, <paramref name="imageFilename"/>, <paramref name="prompt"/>, <paramref name="mask"/>, or <paramref name="maskFilename"/> is null. </exception>
335/// <exception cref="ArgumentException"> <paramref name="imageFilename"/>, <paramref name="prompt"/>, or <paramref name="maskFilename"/> is an empty string, and was expected to be non-empty. </exception>
19a65a0aKrzysztof Cwalina2 years ago336public virtual ClientResult<GeneratedImage> GenerateImageEdit(Stream image, string imageFilename, string prompt, Stream mask, string maskFilename, ImageEditOptions options = null, CancellationToken cancellationToken = default)
9f9f2936Jose Arriaga Maldonado2 years ago337{
338Argument.AssertNotNull(image, nameof(image));
339Argument.AssertNotNullOrEmpty(imageFilename, nameof(imageFilename));
340Argument.AssertNotNullOrEmpty(prompt, nameof(prompt));
341Argument.AssertNotNull(mask, nameof(mask));
342Argument.AssertNotNullOrEmpty(maskFilename, nameof(maskFilename));
343
344options ??= new();
345CreateImageEditOptions(image, imageFilename, prompt, mask, maskFilename, null, ref options);
346
347using MultipartFormDataBinaryContent content = options.ToMultipartContent(image, imageFilename, mask, maskFilename);
19a65a0aKrzysztof Cwalina2 years ago348ClientResult result = GenerateImageEdits(content, content.ContentType, cancellationToken.ToRequestOptions());
9f9f2936Jose Arriaga Maldonado2 years ago349return ClientResult.FromValue(GeneratedImageCollection.FromResponse(result.GetRawResponse()).FirstOrDefault(), result.GetRawResponse());
350}
351
13a9c686Jose Arriaga Maldonado1 years ago352/// <summary> Generates an edited or extended image based on an original image, a prompt, and a mask. </summary>
9f9f2936Jose Arriaga Maldonado2 years ago353/// <param name="imageFilePath">
13a9c686Jose Arriaga Maldonado1 years ago354/// The path of the image file to edit. Must be a valid PNG file, less than 4MB, and square. The provided file
355/// path's extension (for example: .png) will be used to validate the format of the input image. The request
356/// may fail if the file path's extension and the actual format of the input image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago357/// </param>
358/// <param name="prompt"> A text description of the desired image. </param>
359/// <param name="maskFilePath">
13a9c686Jose Arriaga Maldonado1 years ago360/// The path of the mask image file whose fully transparent areas (i.e., where alpha is zero) indicate where
361/// the original image should be edited. Must be a valid PNG file, less than 4MB, and have the same dimensions
362/// as the original image. The provided file path's extension (for example: .png) will be used to validate the
363/// format of the mask image. The request may fail if the file path's extension and the actual format of the
364/// mask image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago365/// </param>
13a9c686Jose Arriaga Maldonado1 years ago366/// <param name="options"> The options to configure the image edit. </param>
9f9f2936Jose Arriaga Maldonado2 years ago367/// <exception cref="ArgumentNullException"> <paramref name="imageFilePath"/>, <paramref name="prompt"/> or <paramref name="maskFilePath"/> is null. </exception>
368/// <exception cref="ArgumentException"> <paramref name="imageFilePath"/>, <paramref name="prompt"/>, or <paramref name="maskFilePath"/> is an empty string, and was expected to be non-empty. </exception>
369public virtual async Task<ClientResult<GeneratedImage>> GenerateImageEditAsync(string imageFilePath, string prompt, string maskFilePath, ImageEditOptions options = null)
370{
371Argument.AssertNotNullOrEmpty(imageFilePath, nameof(imageFilePath));
372Argument.AssertNotNullOrEmpty(prompt, nameof(prompt));
373Argument.AssertNotNullOrEmpty(maskFilePath, nameof(maskFilePath));
374
375using FileStream imageStream = File.OpenRead(imageFilePath);
376using FileStream maskStream = File.OpenRead(maskFilePath);
377return await GenerateImageEditAsync(imageStream, imageFilePath, prompt, maskStream, maskFilePath, options).ConfigureAwait(false);
378}
379
13a9c686Jose Arriaga Maldonado1 years ago380/// <summary> Generates an edited or extended image based on an original image, a prompt, and a mask. </summary>
9f9f2936Jose Arriaga Maldonado2 years ago381/// <param name="imageFilePath">
13a9c686Jose Arriaga Maldonado1 years ago382/// The path of the image file to edit. Must be a valid PNG file, less than 4MB, and square. The provided file
383/// path's extension (for example: .png) will be used to validate the format of the input image. The request
384/// may fail if the file path's extension and the actual format of the input image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago385/// </param>
386/// <param name="prompt"> A text description of the desired image. </param>
387/// <param name="maskFilePath">
13a9c686Jose Arriaga Maldonado1 years ago388/// The path of the mask image file whose fully transparent areas (i.e., where alpha is zero) indicate where
389/// the original image should be edited. Must be a valid PNG file, less than 4MB, and have the same dimensions
390/// as the original image. The provided file path's extension (for example: .png) will be used to validate the
391/// format of the mask image. The request may fail if the file path's extension and the actual format of the
392/// mask image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago393/// </param>
13a9c686Jose Arriaga Maldonado1 years ago394/// <param name="options"> The options to configure the image edit. </param>
9f9f2936Jose Arriaga Maldonado2 years ago395/// <exception cref="ArgumentNullException"> <paramref name="imageFilePath"/>, <paramref name="prompt"/> or <paramref name="maskFilePath"/> is null. </exception>
396/// <exception cref="ArgumentException"> <paramref name="imageFilePath"/>, <paramref name="prompt"/>, or <paramref name="maskFilePath"/> is an empty string, and was expected to be non-empty. </exception>
397public virtual ClientResult<GeneratedImage> GenerateImageEdit(string imageFilePath, string prompt, string maskFilePath, ImageEditOptions options = null)
398{
399Argument.AssertNotNullOrEmpty(imageFilePath, nameof(imageFilePath));
400Argument.AssertNotNullOrEmpty(prompt, nameof(prompt));
401Argument.AssertNotNullOrEmpty(maskFilePath, nameof(maskFilePath));
402
403using FileStream imageStream = File.OpenRead(imageFilePath);
404using FileStream maskStream = File.OpenRead(maskFilePath);
405return GenerateImageEdit(imageStream, imageFilePath, prompt, maskStream, maskFilePath, options);
406}
407
13a9c686Jose Arriaga Maldonado1 years ago408/// <summary> Generates edited or extended images based on an original image and a prompt. </summary>
9f9f2936Jose Arriaga Maldonado2 years ago409/// <param name="image">
13a9c686Jose Arriaga Maldonado1 years ago410/// The image stream to edit. Must be a valid PNG file, less than 4MB, and square. The image must have transparency, which
411/// will be used as the mask.
9f9f2936Jose Arriaga Maldonado2 years ago412/// </param>
413/// <param name="imageFilename">
13a9c686Jose Arriaga Maldonado1 years ago414/// The filename associated with the image stream. The filename's extension (for example: .png) will be used to
415/// validate the format of the input image. The request may fail if the filename's extension and the actual
416/// format of the input image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago417/// </param>
418/// <param name="prompt"> A text description of the desired image. </param>
13a9c686Jose Arriaga Maldonado1 years ago419/// <param name="imageCount"> The number of edited or extended images to generate. </param>
420/// <param name="options"> The options to configure the image edit. </param>
421/// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
9f9f2936Jose Arriaga Maldonado2 years ago422/// <exception cref="ArgumentNullException"> <paramref name="image"/>, <paramref name="imageFilename"/>, or <paramref name="prompt"/> is null. </exception>
423/// <exception cref="ArgumentException"> <paramref name="imageFilename"/> or <paramref name="prompt"/> is an empty string, and was expected to be non-empty. </exception>
19a65a0aKrzysztof Cwalina2 years ago424public virtual async Task<ClientResult<GeneratedImageCollection>> GenerateImageEditsAsync(Stream image, string imageFilename, string prompt, int imageCount, ImageEditOptions options = null, CancellationToken cancellationToken = default)
9f9f2936Jose Arriaga Maldonado2 years ago425{
426Argument.AssertNotNull(image, nameof(image));
427Argument.AssertNotNullOrEmpty(imageFilename, nameof(imageFilename));
428Argument.AssertNotNullOrEmpty(prompt, nameof(prompt));
429
430options ??= new();
431CreateImageEditOptions(image, imageFilename, prompt, null, null, imageCount, ref options);
432
433using MultipartFormDataBinaryContent content = options.ToMultipartContent(image, imageFilename, null, null);
19a65a0aKrzysztof Cwalina2 years ago434ClientResult result = await GenerateImageEditsAsync(content, content.ContentType, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
9f9f2936Jose Arriaga Maldonado2 years ago435return ClientResult.FromValue(GeneratedImageCollection.FromResponse(result.GetRawResponse()), result.GetRawResponse());
436}
437
13a9c686Jose Arriaga Maldonado1 years ago438/// <summary> Generates edited or extended images based on an original image and a prompt. </summary>
9f9f2936Jose Arriaga Maldonado2 years ago439/// <param name="image">
13a9c686Jose Arriaga Maldonado1 years ago440/// The image stream to edit. Must be a valid PNG file, less than 4MB, and square. The image must have transparency, which
441/// will be used as the mask.
9f9f2936Jose Arriaga Maldonado2 years ago442/// </param>
443/// <param name="imageFilename">
13a9c686Jose Arriaga Maldonado1 years ago444/// The filename associated with the image stream. The filename's extension (for example: .png) will be used to
445/// validate the format of the input image. The request may fail if the filename's extension and the actual
446/// format of the input image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago447/// </param>
448/// <param name="prompt"> A text description of the desired image. </param>
13a9c686Jose Arriaga Maldonado1 years ago449/// <param name="imageCount"> The number of edited or extended images to generate. </param>
450/// <param name="options"> The options to configure the image edit. </param>
451/// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
9f9f2936Jose Arriaga Maldonado2 years ago452/// <exception cref="ArgumentNullException"> <paramref name="image"/>, <paramref name="imageFilename"/>, or <paramref name="prompt"/> is null. </exception>
453/// <exception cref="ArgumentException"> <paramref name="imageFilename"/> or <paramref name="prompt"/> is an empty string, and was expected to be non-empty. </exception>
19a65a0aKrzysztof Cwalina2 years ago454public virtual ClientResult<GeneratedImageCollection> GenerateImageEdits(Stream image, string imageFilename, string prompt, int imageCount, ImageEditOptions options = null, CancellationToken cancellationToken = default)
9f9f2936Jose Arriaga Maldonado2 years ago455{
456Argument.AssertNotNull(image, nameof(image));
457Argument.AssertNotNullOrEmpty(imageFilename, nameof(imageFilename));
458Argument.AssertNotNullOrEmpty(prompt, nameof(prompt));
459
460options ??= new();
461CreateImageEditOptions(image, imageFilename, prompt, null, null, imageCount, ref options);
462
463using MultipartFormDataBinaryContent content = options.ToMultipartContent(image, imageFilename, null, null);
19a65a0aKrzysztof Cwalina2 years ago464ClientResult result = GenerateImageEdits(content, content.ContentType, cancellationToken.ToRequestOptions());
9f9f2936Jose Arriaga Maldonado2 years ago465return ClientResult.FromValue(GeneratedImageCollection.FromResponse(result.GetRawResponse()), result.GetRawResponse());
466}
467
13a9c686Jose Arriaga Maldonado1 years ago468/// <summary> Generates edited or extended images based on an original image and a prompt. </summary>
9f9f2936Jose Arriaga Maldonado2 years ago469/// <param name="imageFilePath">
13a9c686Jose Arriaga Maldonado1 years ago470/// The path of the image file to edit. Must be a valid PNG file, less than 4MB, and square. The image must
471/// have transparency, which will be used as the mask. The provided file path's extension (for example: .png)
472/// will be used to validate the format of the input image. The request may fail if the file path's extension
473/// and the actual format of the input image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago474/// </param>
475/// <param name="prompt"> A text description of the desired image. </param>
13a9c686Jose Arriaga Maldonado1 years ago476/// <param name="imageCount"> The number of edited or extended images to generate. </param>
477/// <param name="options"> The options to configure the image edit. </param>
9f9f2936Jose Arriaga Maldonado2 years ago478/// <exception cref="ArgumentNullException"> <paramref name="imageFilePath"/> or <paramref name="prompt"/> is null. </exception>
479/// <exception cref="ArgumentException"> <paramref name="imageFilePath"/> or <paramref name="prompt"/> is an empty string, and was expected to be non-empty. </exception>
480public virtual async Task<ClientResult<GeneratedImageCollection>> GenerateImageEditsAsync(string imageFilePath, string prompt, int imageCount, ImageEditOptions options = null)
481{
482Argument.AssertNotNullOrEmpty(imageFilePath, nameof(imageFilePath));
483Argument.AssertNotNullOrEmpty(prompt, nameof(prompt));
484
485using FileStream imageStream = File.OpenRead(imageFilePath);
486return await GenerateImageEditsAsync(imageStream, imageFilePath, prompt, imageCount, options).ConfigureAwait(false);
487}
488
13a9c686Jose Arriaga Maldonado1 years ago489/// <summary> Generates edited or extended images based on an original image and a prompt. </summary>
9f9f2936Jose Arriaga Maldonado2 years ago490/// <param name="imageFilePath">
13a9c686Jose Arriaga Maldonado1 years ago491/// The path of the image file to edit. Must be a valid PNG file, less than 4MB, and square. The image must
492/// have transparency, which will be used as the mask. The provided file path's extension (for example: .png)
493/// will be used to validate the format of the input image. The request may fail if the file path's extension
494/// and the actual format of the input image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago495/// </param>
496/// <param name="prompt"> A text description of the desired image. </param>
13a9c686Jose Arriaga Maldonado1 years ago497/// <param name="imageCount"> The number of edited or extended images to generate. </param>
498/// <param name="options"> The options to configure the image edit. </param>
9f9f2936Jose Arriaga Maldonado2 years ago499/// <exception cref="ArgumentNullException"> <paramref name="imageFilePath"/> or <paramref name="prompt"/> is null. </exception>
500/// <exception cref="ArgumentException"> <paramref name="imageFilePath"/> or <paramref name="prompt"/> is an empty string, and was expected to be non-empty. </exception>
501public virtual ClientResult<GeneratedImageCollection> GenerateImageEdits(string imageFilePath, string prompt, int imageCount, ImageEditOptions options = null)
502{
503Argument.AssertNotNullOrEmpty(imageFilePath, nameof(imageFilePath));
504Argument.AssertNotNullOrEmpty(prompt, nameof(prompt));
505
506using FileStream imageStream = File.OpenRead(imageFilePath);
507return GenerateImageEdits(imageStream, imageFilePath, prompt, imageCount, options);
508}
509
13a9c686Jose Arriaga Maldonado1 years ago510/// <summary> Generates edited or extended images based on an original image, a prompt, and a mask. </summary>
511/// <param name="image"> The image stream to edit. Must be a valid PNG file, less than 4MB, and square. </param>
9f9f2936Jose Arriaga Maldonado2 years ago512/// <param name="imageFilename">
13a9c686Jose Arriaga Maldonado1 years ago513/// The filename associated with the image stream. The filename's extension (for example: .png) will be used to
514/// validate the format of the input image. The request may fail if the filename's extension and the actual
515/// format of the input image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago516/// </param>
517/// <param name="prompt"> A text description of the desired image. </param>
518/// <param name="mask">
13a9c686Jose Arriaga Maldonado1 years ago519/// An additional image whose fully transparent areas (i.e., where alpha is zero) indicate where the original image
520/// should be edited. Must be a valid PNG file, less than 4MB, and have the same dimensions as image.
9f9f2936Jose Arriaga Maldonado2 years ago521/// </param>
522/// <param name="maskFilename">
13a9c686Jose Arriaga Maldonado1 years ago523/// The filename associated with the mask image stream. The filename's extension (for example: .png) will be
524/// used to validate the format of the mask image. The request may fail if the filename's extension and the
525/// actual format of the mask image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago526/// </param>
13a9c686Jose Arriaga Maldonado1 years ago527/// <param name="imageCount"> The number of edited or extended images to generate. </param>
528/// <param name="options"> The options to configure the image edit. </param>
529/// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
9f9f2936Jose Arriaga Maldonado2 years ago530/// <exception cref="ArgumentNullException"> <paramref name="image"/>, <paramref name="imageFilename"/>, <paramref name="prompt"/>, <paramref name="mask"/>, or <paramref name="maskFilename"/> is null. </exception>
531/// <exception cref="ArgumentException"> <paramref name="imageFilename"/>, <paramref name="prompt"/>, or <paramref name="maskFilename"/> is an empty string, and was expected to be non-empty. </exception>
19a65a0aKrzysztof Cwalina2 years ago532public virtual async Task<ClientResult<GeneratedImageCollection>> GenerateImageEditsAsync(Stream image, string imageFilename, string prompt, Stream mask, string maskFilename, int imageCount, ImageEditOptions options = null, CancellationToken cancellationToken = default)
9f9f2936Jose Arriaga Maldonado2 years ago533{
534Argument.AssertNotNull(image, nameof(image));
535Argument.AssertNotNullOrEmpty(imageFilename, nameof(imageFilename));
536Argument.AssertNotNullOrEmpty(prompt, nameof(prompt));
537Argument.AssertNotNull(mask, nameof(mask));
538Argument.AssertNotNullOrEmpty(maskFilename, nameof(maskFilename));
539
540options ??= new();
541CreateImageEditOptions(image, imageFilename, prompt, mask, maskFilename, imageCount, ref options);
542
543using MultipartFormDataBinaryContent content = options.ToMultipartContent(image, imageFilename, mask, maskFilename);
19a65a0aKrzysztof Cwalina2 years ago544ClientResult result = await GenerateImageEditsAsync(content, content.ContentType, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
9f9f2936Jose Arriaga Maldonado2 years ago545return ClientResult.FromValue(GeneratedImageCollection.FromResponse(result.GetRawResponse()), result.GetRawResponse());
546}
547
13a9c686Jose Arriaga Maldonado1 years ago548/// <summary> Generates edited or extended images based on an original image, a prompt, and a mask. </summary>
549/// <param name="image"> The image stream to edit. Must be a valid PNG file, less than 4MB, and square. </param>
9f9f2936Jose Arriaga Maldonado2 years ago550/// <param name="imageFilename">
13a9c686Jose Arriaga Maldonado1 years ago551/// The filename associated with the image stream. The filename's extension (for example: .png) will be used to
552/// validate the format of the input image. The request may fail if the filename's extension and the actual
553/// format of the input image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago554/// </param>
555/// <param name="prompt"> A text description of the desired image. </param>
556/// <param name="mask">
13a9c686Jose Arriaga Maldonado1 years ago557/// An additional image whose fully transparent areas (i.e., where alpha is zero) indicate where the original image
558/// should be edited. Must be a valid PNG file, less than 4MB, and have the same dimensions as image.
9f9f2936Jose Arriaga Maldonado2 years ago559/// </param>
560/// <param name="maskFilename">
13a9c686Jose Arriaga Maldonado1 years ago561/// The filename associated with the mask image stream. The filename's extension (for example: .png) will be
562/// used to validate the format of the mask image. The request may fail if the filename's extension and the
563/// actual format of the mask image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago564/// </param>
13a9c686Jose Arriaga Maldonado1 years ago565/// <param name="imageCount"> The number of edited or extended images to generate. </param>
566/// <param name="options"> The options to configure the image edit. </param>
567/// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
9f9f2936Jose Arriaga Maldonado2 years ago568/// <exception cref="ArgumentNullException"> <paramref name="image"/>, <paramref name="imageFilename"/>, <paramref name="prompt"/>, <paramref name="mask"/>, or <paramref name="maskFilename"/> is null. </exception>
569/// <exception cref="ArgumentException"> <paramref name="imageFilename"/>, <paramref name="prompt"/>, or <paramref name="maskFilename"/> is an empty string, and was expected to be non-empty. </exception>
19a65a0aKrzysztof Cwalina2 years ago570public virtual ClientResult<GeneratedImageCollection> GenerateImageEdits(Stream image, string imageFilename, string prompt, Stream mask, string maskFilename, int imageCount, ImageEditOptions options = null, CancellationToken cancellationToken = default)
9f9f2936Jose Arriaga Maldonado2 years ago571{
572Argument.AssertNotNull(image, nameof(image));
573Argument.AssertNotNullOrEmpty(imageFilename, nameof(imageFilename));
574Argument.AssertNotNullOrEmpty(prompt, nameof(prompt));
575Argument.AssertNotNull(mask, nameof(mask));
576Argument.AssertNotNullOrEmpty(maskFilename, nameof(maskFilename));
577
578options ??= new();
579CreateImageEditOptions(image, imageFilename, prompt, mask, maskFilename, imageCount, ref options);
580
581using MultipartFormDataBinaryContent content = options.ToMultipartContent(image, imageFilename, mask, maskFilename);
19a65a0aKrzysztof Cwalina2 years ago582ClientResult result = GenerateImageEdits(content, content.ContentType, cancellationToken.ToRequestOptions());
9f9f2936Jose Arriaga Maldonado2 years ago583return ClientResult.FromValue(GeneratedImageCollection.FromResponse(result.GetRawResponse()), result.GetRawResponse());
584}
585
13a9c686Jose Arriaga Maldonado1 years ago586/// <summary> Generates edited or extended images based on an original image, a prompt, and a mask. </summary>
9f9f2936Jose Arriaga Maldonado2 years ago587/// <param name="imageFilePath">
13a9c686Jose Arriaga Maldonado1 years ago588/// The path of the image file to edit. Must be a valid PNG file, less than 4MB, and square. The provided file
589/// path's extension (for example: .png) will be used to validate the format of the input image. The request
590/// may fail if the file path's extension and the actual format of the input image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago591/// </param>
592/// <param name="prompt"> A text description of the desired image. </param>
593/// <param name="maskFilePath">
13a9c686Jose Arriaga Maldonado1 years ago594/// The path of the mask image file whose fully transparent areas (i.e., where alpha is zero) indicate where
595/// the original image should be edited. Must be a valid PNG file, less than 4MB, and have the same dimensions
596/// as the original image. The provided file path's extension (for example: .png) will be used to validate the
597/// format of the mask image. The request may fail if the file path's extension and the actual format of the
598/// mask image do not match.
599/// </param>
600/// <param name="imageCount"> The number of edited or extended images to generate. </param>
601/// <param name="options"> The options to configure the image edit. </param>
9f9f2936Jose Arriaga Maldonado2 years ago602/// <exception cref="ArgumentNullException"> <paramref name="imageFilePath"/>, <paramref name="prompt"/> or <paramref name="maskFilePath"/> is null. </exception>
603/// <exception cref="ArgumentException"> <paramref name="imageFilePath"/>, <paramref name="prompt"/>, or <paramref name="maskFilePath"/> is an empty string, and was expected to be non-empty. </exception>
604public virtual async Task<ClientResult<GeneratedImageCollection>> GenerateImageEditsAsync(string imageFilePath, string prompt, string maskFilePath, int imageCount, ImageEditOptions options = null)
605{
606Argument.AssertNotNullOrEmpty(imageFilePath, nameof(imageFilePath));
607Argument.AssertNotNullOrEmpty(prompt, nameof(prompt));
608Argument.AssertNotNullOrEmpty(maskFilePath, nameof(maskFilePath));
609
610using FileStream imageStream = File.OpenRead(imageFilePath);
611using FileStream maskStream = File.OpenRead(maskFilePath);
612return await GenerateImageEditsAsync(imageStream, imageFilePath, prompt, maskStream, maskFilePath, imageCount, options).ConfigureAwait(false);
613}
614
13a9c686Jose Arriaga Maldonado1 years ago615/// <summary> Generates edited or extended images based on an original image, a prompt, and a mask. </summary>
9f9f2936Jose Arriaga Maldonado2 years ago616/// <param name="imageFilePath">
13a9c686Jose Arriaga Maldonado1 years ago617/// The path of the image file to edit. Must be a valid PNG file, less than 4MB, and square. The provided file
618/// path's extension (for example: .png) will be used to validate the format of the input image. The request
619/// may fail if the file path's extension and the actual format of the input image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago620/// </param>
621/// <param name="prompt"> A text description of the desired image. </param>
622/// <param name="maskFilePath">
13a9c686Jose Arriaga Maldonado1 years ago623/// The path of the mask image file whose fully transparent areas (i.e., where alpha is zero) indicate where
624/// the original image should be edited. Must be a valid PNG file, less than 4MB, and have the same dimensions
625/// as the original image. The provided file path's extension (for example: .png) will be used to validate the
626/// format of the mask image. The request may fail if the file path's extension and the actual format of the
627/// mask image do not match.
628/// </param>
629/// <param name="imageCount"> The number of edited or extended images to generate. </param>
630/// <param name="options"> The options to configure the image edit. </param>
9f9f2936Jose Arriaga Maldonado2 years ago631/// <exception cref="ArgumentNullException"> <paramref name="imageFilePath"/>, <paramref name="prompt"/> or <paramref name="maskFilePath"/> is null. </exception>
632/// <exception cref="ArgumentException"> <paramref name="imageFilePath"/>, <paramref name="prompt"/>, or <paramref name="maskFilePath"/> is an empty string, and was expected to be non-empty. </exception>
633public virtual ClientResult<GeneratedImageCollection> GenerateImageEdits(string imageFilePath, string prompt, string maskFilePath, int imageCount, ImageEditOptions options = null)
634{
635Argument.AssertNotNullOrEmpty(imageFilePath, nameof(imageFilePath));
636Argument.AssertNotNullOrEmpty(prompt, nameof(prompt));
637Argument.AssertNotNullOrEmpty(maskFilePath, nameof(maskFilePath));
638
639using FileStream imageStream = File.OpenRead(imageFilePath);
640using FileStream maskStream = File.OpenRead(maskFilePath);
641return GenerateImageEdits(imageStream, imageFilePath, prompt, maskStream, maskFilePath, imageCount, options);
642}
643
644#endregion
645
646#region GenerateImageVariations
647
648/// <summary> Generates a variation of a given image. </summary>
13a9c686Jose Arriaga Maldonado1 years ago649/// <param name="image"> The image stream to use as the basis for the variation. Must be a valid PNG file, less than 4MB, and square. </param>
9f9f2936Jose Arriaga Maldonado2 years ago650/// <param name="imageFilename">
13a9c686Jose Arriaga Maldonado1 years ago651/// The filename associated with the image stream. The filename's extension (for example: .png) will be used to
652/// validate the format of the input image. The request may fail if the filename's extension and the actual
653/// format of the input image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago654/// </param>
13a9c686Jose Arriaga Maldonado1 years ago655/// <param name="options"> The options to configure the image variation. </param>
656/// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
9f9f2936Jose Arriaga Maldonado2 years ago657/// <exception cref="ArgumentNullException"> <paramref name="image"/> or <paramref name="imageFilename"/> is null. </exception>
658/// <exception cref="ArgumentException"> <paramref name="imageFilename"/> is an empty string, and was expected to be non-empty. </exception>
19a65a0aKrzysztof Cwalina2 years ago659public virtual async Task<ClientResult<GeneratedImage>> GenerateImageVariationAsync(Stream image, string imageFilename, ImageVariationOptions options = null, CancellationToken cancellationToken = default)
9f9f2936Jose Arriaga Maldonado2 years ago660{
661Argument.AssertNotNull(image, nameof(image));
662Argument.AssertNotNullOrEmpty(imageFilename, nameof(imageFilename));
663
664options ??= new();
665CreateImageVariationOptions(image, imageFilename, null, ref options);
666
667using MultipartFormDataBinaryContent content = options.ToMultipartContent(image, imageFilename);
19a65a0aKrzysztof Cwalina2 years ago668ClientResult result = await GenerateImageVariationsAsync(content, content.ContentType, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
9f9f2936Jose Arriaga Maldonado2 years ago669return ClientResult.FromValue(GeneratedImageCollection.FromResponse(result.GetRawResponse()).FirstOrDefault(), result.GetRawResponse());
670}
671
672/// <summary> Generates a variation of a given image. </summary>
13a9c686Jose Arriaga Maldonado1 years ago673/// <param name="image"> The image stream to use as the basis for the variation. Must be a valid PNG file, less than 4MB, and square. </param>
9f9f2936Jose Arriaga Maldonado2 years ago674/// <param name="imageFilename">
13a9c686Jose Arriaga Maldonado1 years ago675/// The filename associated with the image stream. The filename's extension (for example: .png) will be used to
676/// validate the format of the input image. The request may fail if the filename's extension and the actual
677/// format of the input image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago678/// </param>
13a9c686Jose Arriaga Maldonado1 years ago679/// <param name="options"> The options to configure the image variation. </param>
680/// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
9f9f2936Jose Arriaga Maldonado2 years ago681/// <exception cref="ArgumentNullException"> <paramref name="image"/> or <paramref name="imageFilename"/> is null. </exception>
682/// <exception cref="ArgumentException"> <paramref name="imageFilename"/> is an empty string, and was expected to be non-empty. </exception>
19a65a0aKrzysztof Cwalina2 years ago683public virtual ClientResult<GeneratedImage> GenerateImageVariation(Stream image, string imageFilename, ImageVariationOptions options = null, CancellationToken cancellationToken = default)
9f9f2936Jose Arriaga Maldonado2 years ago684{
685Argument.AssertNotNull(image, nameof(image));
686Argument.AssertNotNullOrEmpty(imageFilename, nameof(imageFilename));
687
688options ??= new();
689CreateImageVariationOptions(image, imageFilename, null, ref options);
690
691using MultipartFormDataBinaryContent content = options.ToMultipartContent(image, imageFilename);
19a65a0aKrzysztof Cwalina2 years ago692ClientResult result = GenerateImageVariations(content, content.ContentType, cancellationToken.ToRequestOptions());
9f9f2936Jose Arriaga Maldonado2 years ago693return ClientResult.FromValue(GeneratedImageCollection.FromResponse(result.GetRawResponse()).FirstOrDefault(), result.GetRawResponse());
694}
695
696/// <summary> Generates a variation of a given image. </summary>
697/// <param name="imageFilePath">
13a9c686Jose Arriaga Maldonado1 years ago698/// The path of the image file to use as the basis for the variation. Must be a valid PNG file, less than 4MB,
699/// and square. The provided file path's extension (for example: .png) will be used to validate the format of
700/// the input image. The request may fail if the file path's extension and the actual format of the input image
701/// do not match.
9f9f2936Jose Arriaga Maldonado2 years ago702/// </param>
13a9c686Jose Arriaga Maldonado1 years ago703/// <param name="options"> The options to configure the image variation. </param>
9f9f2936Jose Arriaga Maldonado2 years ago704/// <exception cref="ArgumentNullException"> <paramref name="imageFilePath"/> is null. </exception>
705/// <exception cref="ArgumentException"> <paramref name="imageFilePath"/> is an empty string, and was expected to be non-empty. </exception>
706public virtual async Task<ClientResult<GeneratedImage>> GenerateImageVariationAsync(string imageFilePath, ImageVariationOptions options = null)
707{
708Argument.AssertNotNullOrEmpty(imageFilePath, nameof(imageFilePath));
709
710using FileStream imageStream = File.OpenRead(imageFilePath);
711return await GenerateImageVariationAsync(imageStream, imageFilePath, options).ConfigureAwait(false);
712}
713
714/// <summary> Generates a variation of a given image. </summary>
715/// <param name="imageFilePath">
13a9c686Jose Arriaga Maldonado1 years ago716/// The path of the image file to use as the basis for the variation. Must be a valid PNG file, less than 4MB,
717/// and square. The provided file path's extension (for example: .png) will be used to validate the format of
718/// the input image. The request may fail if the file path's extension and the actual format of the input image
719/// do not match.
9f9f2936Jose Arriaga Maldonado2 years ago720/// </param>
13a9c686Jose Arriaga Maldonado1 years ago721/// <param name="options"> The options to configure the image variation. </param>
9f9f2936Jose Arriaga Maldonado2 years ago722/// <exception cref="ArgumentNullException"> <paramref name="imageFilePath"/> is null. </exception>
723/// <exception cref="ArgumentException"> <paramref name="imageFilePath"/> is an empty string, and was expected to be non-empty. </exception>
724public virtual ClientResult<GeneratedImage> GenerateImageVariation(string imageFilePath, ImageVariationOptions options = null)
725{
726Argument.AssertNotNullOrEmpty(imageFilePath, nameof(imageFilePath));
727
728using FileStream imageStream = File.OpenRead(imageFilePath);
729return GenerateImageVariation(imageStream, imageFilePath, options);
730}
731
732/// <summary> Generates variations of a given image. </summary>
13a9c686Jose Arriaga Maldonado1 years ago733/// <param name="image"> The image stream to use as the basis for the variation. Must be a valid PNG file, less than 4MB, and square. </param>
9f9f2936Jose Arriaga Maldonado2 years ago734/// <param name="imageFilename">
13a9c686Jose Arriaga Maldonado1 years ago735/// The filename associated with the image stream. The filename's extension (for example: .png) will be used to
736/// validate the format of the input image. The request may fail if the filename's extension and the actual
737/// format of the input image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago738/// </param>
739/// <param name="imageCount"> The number of image variations to generate. </param>
13a9c686Jose Arriaga Maldonado1 years ago740/// <param name="options"> The options to configure the image variation. </param>
741/// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
9f9f2936Jose Arriaga Maldonado2 years ago742/// <exception cref="ArgumentNullException"> <paramref name="image"/> or <paramref name="imageFilename"/> is null. </exception>
743/// <exception cref="ArgumentException"> <paramref name="imageFilename"/> is an empty string, and was expected to be non-empty. </exception>
19a65a0aKrzysztof Cwalina2 years ago744public virtual async Task<ClientResult<GeneratedImageCollection>> GenerateImageVariationsAsync(Stream image, string imageFilename, int imageCount, ImageVariationOptions options = null, CancellationToken cancellationToken = default)
9f9f2936Jose Arriaga Maldonado2 years ago745{
746Argument.AssertNotNull(image, nameof(image));
747Argument.AssertNotNullOrEmpty(imageFilename, nameof(imageFilename));
748
749options ??= new();
750CreateImageVariationOptions(image, imageFilename, imageCount, ref options);
751
752using MultipartFormDataBinaryContent content = options.ToMultipartContent(image, imageFilename);
19a65a0aKrzysztof Cwalina2 years ago753ClientResult result = await GenerateImageVariationsAsync(content, content.ContentType, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
9f9f2936Jose Arriaga Maldonado2 years ago754return ClientResult.FromValue(GeneratedImageCollection.FromResponse(result.GetRawResponse()), result.GetRawResponse());
755}
756
757/// <summary> Generates variations of a given image. </summary>
13a9c686Jose Arriaga Maldonado1 years ago758/// <param name="image"> The image stream to use as the basis for the variation. Must be a valid PNG file, less than 4MB, and square. </param>
9f9f2936Jose Arriaga Maldonado2 years ago759/// <param name="imageFilename">
13a9c686Jose Arriaga Maldonado1 years ago760/// The filename associated with the image stream. The filename's extension (for example: .png) will be used to
761/// validate the format of the input image. The request may fail if the filename's extension and the actual
762/// format of the input image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago763/// </param>
764/// <param name="imageCount"> The number of image variations to generate. </param>
13a9c686Jose Arriaga Maldonado1 years ago765/// <param name="options"> The options to configure the image variation. </param>
766/// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
9f9f2936Jose Arriaga Maldonado2 years ago767/// <exception cref="ArgumentNullException"> <paramref name="image"/> or <paramref name="imageFilename"/> is null. </exception>
768/// <exception cref="ArgumentException"> <paramref name="imageFilename"/> is an empty string, and was expected to be non-empty. </exception>
19a65a0aKrzysztof Cwalina2 years ago769public virtual ClientResult<GeneratedImageCollection> GenerateImageVariations(Stream image, string imageFilename, int imageCount, ImageVariationOptions options = null, CancellationToken cancellationToken = default)
9f9f2936Jose Arriaga Maldonado2 years ago770{
771Argument.AssertNotNull(image, nameof(image));
772Argument.AssertNotNullOrEmpty(imageFilename, nameof(imageFilename));
773
774options ??= new();
775CreateImageVariationOptions(image, imageFilename, imageCount, ref options);
776
777using MultipartFormDataBinaryContent content = options.ToMultipartContent(image, imageFilename);
19a65a0aKrzysztof Cwalina2 years ago778ClientResult result = GenerateImageVariations(content, content.ContentType, cancellationToken.ToRequestOptions());
9f9f2936Jose Arriaga Maldonado2 years ago779return ClientResult.FromValue(GeneratedImageCollection.FromResponse(result.GetRawResponse()), result.GetRawResponse());
780}
781
782/// <summary> Generates variations of a given image. </summary>
783/// <param name="imageFilePath">
13a9c686Jose Arriaga Maldonado1 years ago784/// The path of the image file to use as the basis for the variation. Must be a valid PNG file, less than 4MB,
785/// and square. The provided file path's extension (for example: .png) will be used to validate the format of
786/// the input image. The request may fail if the file path's extension and the actual format of the input image
787/// do not match.
9f9f2936Jose Arriaga Maldonado2 years ago788/// </param>
789/// <param name="imageCount"> The number of image variations to generate. </param>
13a9c686Jose Arriaga Maldonado1 years ago790/// <param name="options"> The options to configure the image variation. </param>
9f9f2936Jose Arriaga Maldonado2 years ago791/// <exception cref="ArgumentNullException"> <paramref name="imageFilePath"/> was null. </exception>
792/// <exception cref="ArgumentException"> <paramref name="imageFilePath"/> is an empty string, and was expected to be non-empty. </exception>
793public virtual async Task<ClientResult<GeneratedImageCollection>> GenerateImageVariationsAsync(string imageFilePath, int imageCount, ImageVariationOptions options = null)
794{
795Argument.AssertNotNullOrEmpty(imageFilePath, nameof(imageFilePath));
796
797using FileStream imageStream = File.OpenRead(imageFilePath);
798return await GenerateImageVariationsAsync(imageStream, imageFilePath, imageCount, options).ConfigureAwait(false);
799}
800
801/// <summary> Generates variations of a given image. </summary>
802/// <param name="imageFilePath">
13a9c686Jose Arriaga Maldonado1 years ago803/// The path of the image file to use as the basis for the variation. Must be a valid PNG file, less than 4MB,
804/// and square. The provided file path's extension (for example: .png) will be used to validate the format of
805/// the input image. The request may fail if the file path's extension and the actual format of the input image
806/// do not match.
9f9f2936Jose Arriaga Maldonado2 years ago807/// </param>
808/// <param name="imageCount"> The number of image variations to generate. </param>
13a9c686Jose Arriaga Maldonado1 years ago809/// <param name="options"> The options to configure the image variation. </param>
9f9f2936Jose Arriaga Maldonado2 years ago810/// <exception cref="ArgumentNullException"> <paramref name="imageFilePath"/> was null. </exception>
811/// <exception cref="ArgumentException"> <paramref name="imageFilePath"/> is an empty string, and was expected to be non-empty. </exception>
812public virtual ClientResult<GeneratedImageCollection> GenerateImageVariations(string imageFilePath, int imageCount, ImageVariationOptions options = null)
813{
814Argument.AssertNotNullOrEmpty(imageFilePath, nameof(imageFilePath));
815
816using FileStream imageStream = File.OpenRead(imageFilePath);
817return GenerateImageVariations(imageStream, imageFilePath, imageCount, options);
818}
819
820#endregion
821
822private void CreateImageGenerationOptions(string prompt, int? imageCount, ref ImageGenerationOptions options)
823{
824options.Prompt = prompt;
825options.N = imageCount;
826options.Model = _model;
827}
828
829private void CreateImageEditOptions(Stream image, string imageFilename, string prompt, Stream mask, string maskFilename, int? imageCount, ref ImageEditOptions options)
830{
831options.Prompt = prompt;
832options.N = imageCount;
833options.Model = _model;
834}
835
836private void CreateImageVariationOptions(Stream image, string imageFilename, int? imageCount, ref ImageVariationOptions options)
837{
838options.N = imageCount;
839options.Model = _model;
840}
841}