openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
achandmsft-patch-1

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/Custom/Images/ImageClient.cs

831lines · 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>
0ca4c062Jose Arriaga Maldonado1 years ago16[CodeGenType("Images")]
17[CodeGenSuppress("ImageClient", typeof(ClientPipeline), typeof(Uri))]
e0fee603Jose Arriaga Maldonado1 years ago18[CodeGenSuppress("CreateImageAsync", typeof(ImageGenerationOptions), typeof(CancellationToken))]
19[CodeGenSuppress("CreateImage", typeof(ImageGenerationOptions), typeof(CancellationToken))]
9f9f2936Jose Arriaga Maldonado2 years ago20public partial class ImageClient
21{
22private readonly string _model;
23
2ab1a942Jose Arriaga Maldonado1 years ago24// CUSTOM: Added as a convenience.
e0fee603Jose Arriaga Maldonado1 years ago25/// <summary> Initializes a new instance of <see cref="ImageClient"/>. </summary>
2ab1a942Jose Arriaga Maldonado1 years ago26/// <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>
27/// <param name="apiKey"> The API key to authenticate with the service. </param>
28/// <exception cref="ArgumentNullException"> <paramref name="model"/> or <paramref name="apiKey"/> is null. </exception>
29/// <exception cref="ArgumentException"> <paramref name="model"/> is an empty string, and was expected to be non-empty. </exception>
30public ImageClient(string model, string apiKey) : this(model, new ApiKeyCredential(apiKey), new OpenAIClientOptions())
31{
32}
33
9f9f2936Jose Arriaga Maldonado2 years ago34// CUSTOM:
35// - Added `model` parameter.
13a9c686Jose Arriaga Maldonado1 years ago36// - Used a custom pipeline.
37// - Demoted the endpoint parameter to be a property in the options class.
e0fee603Jose Arriaga Maldonado1 years ago38/// <summary> Initializes a new instance of <see cref="ImageClient"/>. </summary>
13a9c686Jose Arriaga Maldonado1 years ago39/// <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>
40/// <param name="credential"> The API key to authenticate with the service. </param>
41/// <exception cref="ArgumentNullException"> <paramref name="model"/> or <paramref name="credential"/> is null. </exception>
42/// <exception cref="ArgumentException"> <paramref name="model"/> is an empty string, and was expected to be non-empty. </exception>
43public ImageClient(string model, ApiKeyCredential credential) : this(model, credential, new OpenAIClientOptions())
44{
45}
9f9f2936Jose Arriaga Maldonado2 years ago46
47// CUSTOM:
48// - Added `model` parameter.
13a9c686Jose Arriaga Maldonado1 years ago49// - Used a custom pipeline.
50// - Demoted the endpoint parameter to be a property in the options class.
e0fee603Jose Arriaga Maldonado1 years ago51/// <summary> Initializes a new instance of <see cref="ImageClient"/>. </summary>
13a9c686Jose Arriaga Maldonado1 years ago52/// <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>
53/// <param name="credential"> The API key to authenticate with the service. </param>
54/// <param name="options"> The options to configure the client. </param>
55/// <exception cref="ArgumentNullException"> <paramref name="model"/> or <paramref name="credential"/> is null. </exception>
56/// <exception cref="ArgumentException"> <paramref name="model"/> is an empty string, and was expected to be non-empty. </exception>
57public ImageClient(string model, ApiKeyCredential credential, OpenAIClientOptions options)
58{
59Argument.AssertNotNullOrEmpty(model, nameof(model));
60Argument.AssertNotNull(credential, nameof(credential));
61options ??= new OpenAIClientOptions();
9f9f2936Jose Arriaga Maldonado2 years ago62
13a9c686Jose Arriaga Maldonado1 years ago63_model = model;
e0fee603Jose Arriaga Maldonado1 years ago64Pipeline = OpenAIClient.CreatePipeline(credential, options);
13a9c686Jose Arriaga Maldonado1 years ago65_endpoint = OpenAIClient.GetEndpoint(options);
66}
67
68// CUSTOM:
69// - Added `model` parameter.
70// - Used a custom pipeline.
71// - Demoted the endpoint parameter to be a property in the options class.
72// - Made protected.
e0fee603Jose Arriaga Maldonado1 years ago73/// <summary> Initializes a new instance of <see cref="ImageClient"/>. </summary>
13a9c686Jose Arriaga Maldonado1 years ago74/// <param name="pipeline"> The HTTP pipeline to send and receive REST requests and responses. </param>
75/// <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>
76/// <param name="options"> The options to configure the client. </param>
77/// <exception cref="ArgumentNullException"> <paramref name="pipeline"/> or <paramref name="model"/> is null. </exception>
78/// <exception cref="ArgumentException"> <paramref name="model"/> is an empty string, and was expected to be non-empty. </exception>
79protected internal ImageClient(ClientPipeline pipeline, string model, OpenAIClientOptions options)
9f9f2936Jose Arriaga Maldonado2 years ago80{
13a9c686Jose Arriaga Maldonado1 years ago81Argument.AssertNotNull(pipeline, nameof(pipeline));
9f9f2936Jose Arriaga Maldonado2 years ago82Argument.AssertNotNullOrEmpty(model, nameof(model));
13a9c686Jose Arriaga Maldonado1 years ago83options ??= new OpenAIClientOptions();
9f9f2936Jose Arriaga Maldonado2 years ago84
85_model = model;
e0fee603Jose Arriaga Maldonado1 years ago86Pipeline = pipeline;
13a9c686Jose Arriaga Maldonado1 years ago87_endpoint = OpenAIClient.GetEndpoint(options);
9f9f2936Jose Arriaga Maldonado2 years ago88}
89
90#region GenerateImages
91
13a9c686Jose Arriaga Maldonado1 years ago92/// <summary> Generates an image based on a prompt. </summary>
9f9f2936Jose Arriaga Maldonado2 years ago93/// <param name="prompt"> A text description of the desired image. </param>
13a9c686Jose Arriaga Maldonado1 years ago94/// <param name="options"> The options to configure the image generation. </param>
95/// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
9f9f2936Jose Arriaga Maldonado2 years ago96/// <exception cref="ArgumentNullException"> <paramref name="prompt"/> is null. </exception>
97/// <exception cref="ArgumentException"> <paramref name="prompt"/> is an empty string, and was expected to be non-empty. </exception>
19a65a0aKrzysztof Cwalina2 years ago98public virtual async Task<ClientResult<GeneratedImage>> GenerateImageAsync(string prompt, ImageGenerationOptions options = null, CancellationToken cancellationToken = default)
9f9f2936Jose Arriaga Maldonado2 years ago99{
100Argument.AssertNotNullOrEmpty(prompt, nameof(prompt));
101
102options ??= new();
103CreateImageGenerationOptions(prompt, null, ref options);
104
e0fee603Jose Arriaga Maldonado1 years ago105using BinaryContent content = options;
19a65a0aKrzysztof Cwalina2 years ago106ClientResult result = await GenerateImagesAsync(content, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
e0fee603Jose Arriaga Maldonado1 years ago107return ClientResult.FromValue(((GeneratedImageCollection)result).FirstOrDefault(), result.GetRawResponse());
9f9f2936Jose Arriaga Maldonado2 years ago108}
109
13a9c686Jose Arriaga Maldonado1 years ago110/// <summary> Generates an image based on a prompt. </summary>
9f9f2936Jose Arriaga Maldonado2 years ago111/// <param name="prompt"> A text description of the desired image. </param>
13a9c686Jose Arriaga Maldonado1 years ago112/// <param name="options"> The options to configure the image generation. </param>
113/// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
9f9f2936Jose Arriaga Maldonado2 years ago114/// <exception cref="ArgumentNullException"> <paramref name="prompt"/> is null. </exception>
115/// <exception cref="ArgumentException"> <paramref name="prompt"/> is an empty string, and was expected to be non-empty. </exception>
19a65a0aKrzysztof Cwalina2 years ago116public virtual ClientResult<GeneratedImage> GenerateImage(string prompt, ImageGenerationOptions options = null, CancellationToken cancellationToken = default)
9f9f2936Jose Arriaga Maldonado2 years ago117{
118Argument.AssertNotNullOrEmpty(prompt, nameof(prompt));
119
120options ??= new();
121CreateImageGenerationOptions(prompt, null, ref options);
122
e0fee603Jose Arriaga Maldonado1 years ago123using BinaryContent content = options;
19a65a0aKrzysztof Cwalina2 years ago124ClientResult result = GenerateImages(content, cancellationToken.ToRequestOptions());
e0fee603Jose Arriaga Maldonado1 years ago125return ClientResult.FromValue(((GeneratedImageCollection)result).FirstOrDefault(), result.GetRawResponse());
9f9f2936Jose Arriaga Maldonado2 years ago126}
127
13a9c686Jose Arriaga Maldonado1 years ago128/// <summary> Generates images based on a prompt. </summary>
9f9f2936Jose Arriaga Maldonado2 years ago129/// <param name="prompt"> A text description of the desired images. </param>
130/// <param name="imageCount"> The number of images to generate. </param>
13a9c686Jose Arriaga Maldonado1 years ago131/// <param name="options"> The options to configure the image generation. </param>
132/// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
9f9f2936Jose Arriaga Maldonado2 years ago133/// <exception cref="ArgumentNullException"> <paramref name="prompt"/> is null. </exception>
134/// <exception cref="ArgumentException"> <paramref name="prompt"/> is an empty string, and was expected to be non-empty. </exception>
19a65a0aKrzysztof Cwalina2 years ago135public virtual async Task<ClientResult<GeneratedImageCollection>> GenerateImagesAsync(string prompt, int imageCount, ImageGenerationOptions options = null, CancellationToken cancellationToken = default)
9f9f2936Jose Arriaga Maldonado2 years ago136{
137Argument.AssertNotNullOrEmpty(prompt, nameof(prompt));
138
139options ??= new();
140CreateImageGenerationOptions(prompt, imageCount, ref options);
141
e0fee603Jose Arriaga Maldonado1 years ago142using BinaryContent content = options;
19a65a0aKrzysztof Cwalina2 years ago143ClientResult result = await GenerateImagesAsync(content, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
e0fee603Jose Arriaga Maldonado1 years ago144return ClientResult.FromValue((GeneratedImageCollection)result, result.GetRawResponse());
9f9f2936Jose Arriaga Maldonado2 years ago145}
146
13a9c686Jose Arriaga Maldonado1 years ago147/// <summary> Generates images based on a prompt. </summary>
9f9f2936Jose Arriaga Maldonado2 years ago148/// <param name="prompt"> A text description of the desired images. </param>
149/// <param name="imageCount"> The number of images to generate. </param>
13a9c686Jose Arriaga Maldonado1 years ago150/// <param name="options"> The options to configure the image generation. </param>
151/// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
9f9f2936Jose Arriaga Maldonado2 years ago152/// <exception cref="ArgumentNullException"> <paramref name="prompt"/> is null. </exception>
153/// <exception cref="ArgumentException"> <paramref name="prompt"/> is an empty string, and was expected to be non-empty. </exception>
19a65a0aKrzysztof Cwalina2 years ago154public virtual ClientResult<GeneratedImageCollection> GenerateImages(string prompt, int imageCount, ImageGenerationOptions options = null, CancellationToken cancellationToken = default)
9f9f2936Jose Arriaga Maldonado2 years ago155{
156Argument.AssertNotNullOrEmpty(prompt, nameof(prompt));
157
158options ??= new();
159CreateImageGenerationOptions(prompt, imageCount, ref options);
160
e0fee603Jose Arriaga Maldonado1 years ago161using BinaryContent content = options;
19a65a0aKrzysztof Cwalina2 years ago162ClientResult result = GenerateImages(content, cancellationToken.ToRequestOptions());
e0fee603Jose Arriaga Maldonado1 years ago163return ClientResult.FromValue((GeneratedImageCollection)result, result.GetRawResponse());
9f9f2936Jose Arriaga Maldonado2 years ago164}
165
166#endregion
167
168#region GenerateImageEdits
169
13a9c686Jose Arriaga Maldonado1 years ago170/// <summary> Generates an edited or extended image based on an original image and a prompt. </summary>
9f9f2936Jose Arriaga Maldonado2 years ago171/// <param name="image">
13a9c686Jose Arriaga Maldonado1 years ago172/// The image stream to edit. Must be a valid PNG file, less than 4MB, and square. The image must have transparency, which
173/// will be used as the mask.
9f9f2936Jose Arriaga Maldonado2 years ago174/// </param>
175/// <param name="imageFilename">
13a9c686Jose Arriaga Maldonado1 years ago176/// The filename associated with the image stream. The filename's extension (for example: .png) will be used to
177/// validate the format of the input image. The request may fail if the filename's extension and the actual
178/// format of the input image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago179/// </param>
180/// <param name="prompt"> A text description of the desired image. </param>
13a9c686Jose Arriaga Maldonado1 years ago181/// <param name="options"> The options to configure the image edit. </param>
182/// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
9f9f2936Jose Arriaga Maldonado2 years ago183/// <exception cref="ArgumentNullException"> <paramref name="image"/>, <paramref name="imageFilename"/>, or <paramref name="prompt"/> is null. </exception>
184/// <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 ago185public virtual async Task<ClientResult<GeneratedImage>> GenerateImageEditAsync(Stream image, string imageFilename, string prompt, ImageEditOptions options = null, CancellationToken cancellationToken = default)
9f9f2936Jose Arriaga Maldonado2 years ago186{
187Argument.AssertNotNull(image, nameof(image));
188Argument.AssertNotNullOrEmpty(imageFilename, nameof(imageFilename));
189Argument.AssertNotNullOrEmpty(prompt, nameof(prompt));
190
191options ??= new();
192CreateImageEditOptions(image, imageFilename, prompt, null, null, null, ref options);
193
0ca4c062Jose Arriaga Maldonado1 years ago194using MultiPartFormDataBinaryContent content = options.ToMultipartContent(image, imageFilename, null, null);
19a65a0aKrzysztof Cwalina2 years ago195ClientResult result = await GenerateImageEditsAsync(content, content.ContentType, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
e0fee603Jose Arriaga Maldonado1 years ago196return ClientResult.FromValue(((GeneratedImageCollection)result).FirstOrDefault(), result.GetRawResponse());
9f9f2936Jose Arriaga Maldonado2 years ago197}
198
13a9c686Jose Arriaga Maldonado1 years ago199/// <summary> Generates an edited or extended image based on an original image and a prompt. </summary>
9f9f2936Jose Arriaga Maldonado2 years ago200/// <param name="image">
13a9c686Jose Arriaga Maldonado1 years ago201/// The image stream to edit. Must be a valid PNG file, less than 4MB, and square. The image must have transparency, which
202/// will be used as the mask.
9f9f2936Jose Arriaga Maldonado2 years ago203/// </param>
204/// <param name="imageFilename">
13a9c686Jose Arriaga Maldonado1 years ago205/// The filename associated with the image stream. The filename's extension (for example: .png) will be used to
206/// validate the format of the input image. The request may fail if the filename's extension and the actual
207/// format of the input image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago208/// </param>
209/// <param name="prompt"> A text description of the desired image. </param>
13a9c686Jose Arriaga Maldonado1 years ago210/// <param name="options"> The options to configure the image edit. </param>
211/// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
9f9f2936Jose Arriaga Maldonado2 years ago212/// <exception cref="ArgumentNullException"> <paramref name="image"/>, <paramref name="imageFilename"/>, or <paramref name="prompt"/> is null. </exception>
213/// <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 ago214public virtual ClientResult<GeneratedImage> GenerateImageEdit(Stream image, string imageFilename, string prompt, ImageEditOptions options = null, CancellationToken cancellationToken = default)
9f9f2936Jose Arriaga Maldonado2 years ago215{
216Argument.AssertNotNull(image, nameof(image));
217Argument.AssertNotNullOrEmpty(imageFilename, nameof(imageFilename));
218Argument.AssertNotNullOrEmpty(prompt, nameof(prompt));
219
220options ??= new();
221CreateImageEditOptions(image, imageFilename, prompt, null, null, null, ref options);
222
0ca4c062Jose Arriaga Maldonado1 years ago223using MultiPartFormDataBinaryContent content = options.ToMultipartContent(image, imageFilename, null, null);
19a65a0aKrzysztof Cwalina2 years ago224ClientResult result = GenerateImageEdits(content, content.ContentType, cancellationToken.ToRequestOptions());
e0fee603Jose Arriaga Maldonado1 years ago225return ClientResult.FromValue(((GeneratedImageCollection)result).FirstOrDefault(), result.GetRawResponse());
9f9f2936Jose Arriaga Maldonado2 years ago226}
227
13a9c686Jose Arriaga Maldonado1 years ago228/// <summary> Generates an edited or extended image based on an original image and a prompt. </summary>
9f9f2936Jose Arriaga Maldonado2 years ago229/// <param name="imageFilePath">
13a9c686Jose Arriaga Maldonado1 years ago230/// The path of the image file to edit. Must be a valid PNG file, less than 4MB, and square. The image must
231/// have transparency, which will be used as the mask. The provided file path's extension (for example: .png)
232/// will be used to validate the format of the input image. The request may fail if the file path's extension
233/// and the actual format of the input image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago234/// </param>
235/// <param name="prompt"> A text description of the desired image. </param>
13a9c686Jose Arriaga Maldonado1 years ago236/// <param name="options"> The options to configure the image edit. </param>
9f9f2936Jose Arriaga Maldonado2 years ago237/// <exception cref="ArgumentNullException"> <paramref name="imageFilePath"/> or <paramref name="prompt"/> is null. </exception>
238/// <exception cref="ArgumentException"> <paramref name="imageFilePath"/> or <paramref name="prompt"/> is an empty string, and was expected to be non-empty. </exception>
239public virtual async Task<ClientResult<GeneratedImage>> GenerateImageEditAsync(string imageFilePath, string prompt, ImageEditOptions options = null)
240{
241Argument.AssertNotNullOrEmpty(imageFilePath, nameof(imageFilePath));
242Argument.AssertNotNullOrEmpty(prompt, nameof(prompt));
243
244using FileStream imageStream = File.OpenRead(imageFilePath);
245return await GenerateImageEditAsync(imageStream, imageFilePath, prompt, options).ConfigureAwait(false);
246}
247
13a9c686Jose Arriaga Maldonado1 years ago248/// <summary> Generates an edited or extended image based on an original image and a prompt. </summary>
9f9f2936Jose Arriaga Maldonado2 years ago249/// <param name="imageFilePath">
13a9c686Jose Arriaga Maldonado1 years ago250/// The path of the image file to edit. Must be a valid PNG file, less than 4MB, and square. The image must
251/// have transparency, which will be used as the mask. The provided file path's extension (for example: .png)
252/// will be used to validate the format of the input image. The request may fail if the file path's extension
253/// and the actual format of the input image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago254/// </param>
255/// <param name="prompt"> A text description of the desired image. </param>
13a9c686Jose Arriaga Maldonado1 years ago256/// <param name="options"> The options to configure the image edit. </param>
9f9f2936Jose Arriaga Maldonado2 years ago257/// <exception cref="ArgumentNullException"> <paramref name="imageFilePath"/> or <paramref name="prompt"/> is null. </exception>
258/// <exception cref="ArgumentException"> <paramref name="imageFilePath"/> or <paramref name="prompt"/> is an empty string, and was expected to be non-empty. </exception>
259public virtual ClientResult<GeneratedImage> GenerateImageEdit(string imageFilePath, string prompt, ImageEditOptions options = null)
260{
261Argument.AssertNotNullOrEmpty(imageFilePath, nameof(imageFilePath));
262Argument.AssertNotNullOrEmpty(prompt, nameof(prompt));
263
264using FileStream imageStream = File.OpenRead(imageFilePath);
79014abcShivangiReja1 years ago265return GenerateImageEdit(imageStream, imageFilePath, prompt, options);
9f9f2936Jose Arriaga Maldonado2 years ago266}
267
13a9c686Jose Arriaga Maldonado1 years ago268/// <summary> Generates an edited or extended image based on an original image, a prompt, and a mask. </summary>
269/// <param name="image"> The image stream to edit. Must be a valid PNG file, less than 4MB, and square. </param>
9f9f2936Jose Arriaga Maldonado2 years ago270/// <param name="imageFilename">
13a9c686Jose Arriaga Maldonado1 years ago271/// The filename associated with the image stream. The filename's extension (for example: .png) will be used to
272/// validate the format of the input image. The request may fail if the filename's extension and the actual
273/// format of the input image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago274/// </param>
275/// <param name="prompt"> A text description of the desired image. </param>
276/// <param name="mask">
13a9c686Jose Arriaga Maldonado1 years ago277/// An additional image whose fully transparent areas (i.e., where alpha is zero) indicate where the original image
278/// should be edited. Must be a valid PNG file, less than 4MB, and have the same dimensions as image.
9f9f2936Jose Arriaga Maldonado2 years ago279/// </param>
280/// <param name="maskFilename">
13a9c686Jose Arriaga Maldonado1 years ago281/// The filename associated with the mask image stream. The filename's extension (for example: .png) will be
282/// used to validate the format of the mask image. The request may fail if the filename's extension and the
283/// actual format of the mask image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago284/// </param>
13a9c686Jose Arriaga Maldonado1 years ago285/// <param name="options"> The options to configure the image edit. </param>
286/// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
9f9f2936Jose Arriaga Maldonado2 years ago287/// <exception cref="ArgumentNullException"> <paramref name="image"/>, <paramref name="imageFilename"/>, <paramref name="prompt"/>, <paramref name="mask"/>, or <paramref name="maskFilename"/> is null. </exception>
288/// <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 ago289public 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 ago290{
291Argument.AssertNotNull(image, nameof(image));
292Argument.AssertNotNullOrEmpty(imageFilename, nameof(imageFilename));
293Argument.AssertNotNullOrEmpty(prompt, nameof(prompt));
294Argument.AssertNotNull(mask, nameof(mask));
295Argument.AssertNotNullOrEmpty(maskFilename, nameof(maskFilename));
296
297options ??= new();
298CreateImageEditOptions(image, imageFilename, prompt, mask, maskFilename, null, ref options);
299
0ca4c062Jose Arriaga Maldonado1 years ago300using MultiPartFormDataBinaryContent content = options.ToMultipartContent(image, imageFilename, mask, maskFilename);
19a65a0aKrzysztof Cwalina2 years ago301ClientResult result = await GenerateImageEditsAsync(content, content.ContentType, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
e0fee603Jose Arriaga Maldonado1 years ago302return ClientResult.FromValue(((GeneratedImageCollection)result).FirstOrDefault(), result.GetRawResponse());
9f9f2936Jose Arriaga Maldonado2 years ago303}
304
13a9c686Jose Arriaga Maldonado1 years ago305/// <summary> Generates an edited or extended image based on an original image, a prompt, and a mask. </summary>
306/// <param name="image"> The image stream to edit. Must be a valid PNG file, less than 4MB, and square. </param>
9f9f2936Jose Arriaga Maldonado2 years ago307/// <param name="imageFilename">
13a9c686Jose Arriaga Maldonado1 years ago308/// The filename associated with the image stream. The filename's extension (for example: .png) will be used to
309/// validate the format of the input image. The request may fail if the filename's extension and the actual
310/// format of the input image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago311/// </param>
312/// <param name="prompt"> A text description of the desired image. </param>
313/// <param name="mask">
13a9c686Jose Arriaga Maldonado1 years ago314/// An additional image whose fully transparent areas (i.e., where alpha is zero) indicate where the original image
315/// should be edited. Must be a valid PNG file, less than 4MB, and have the same dimensions as image.
9f9f2936Jose Arriaga Maldonado2 years ago316/// </param>
317/// <param name="maskFilename">
13a9c686Jose Arriaga Maldonado1 years ago318/// The filename associated with the mask image stream. The filename's extension (for example: .png) will be
319/// used to validate the format of the mask image. The request may fail if the filename's extension and the
320/// actual format of the mask image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago321/// </param>
13a9c686Jose Arriaga Maldonado1 years ago322/// <param name="options"> The options to configure the image edit. </param>
323/// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
9f9f2936Jose Arriaga Maldonado2 years ago324/// <exception cref="ArgumentNullException"> <paramref name="image"/>, <paramref name="imageFilename"/>, <paramref name="prompt"/>, <paramref name="mask"/>, or <paramref name="maskFilename"/> is null. </exception>
325/// <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 ago326public virtual ClientResult<GeneratedImage> GenerateImageEdit(Stream image, string imageFilename, string prompt, Stream mask, string maskFilename, ImageEditOptions options = null, CancellationToken cancellationToken = default)
9f9f2936Jose Arriaga Maldonado2 years ago327{
328Argument.AssertNotNull(image, nameof(image));
329Argument.AssertNotNullOrEmpty(imageFilename, nameof(imageFilename));
330Argument.AssertNotNullOrEmpty(prompt, nameof(prompt));
331Argument.AssertNotNull(mask, nameof(mask));
332Argument.AssertNotNullOrEmpty(maskFilename, nameof(maskFilename));
333
334options ??= new();
335CreateImageEditOptions(image, imageFilename, prompt, mask, maskFilename, null, ref options);
336
0ca4c062Jose Arriaga Maldonado1 years ago337using MultiPartFormDataBinaryContent content = options.ToMultipartContent(image, imageFilename, mask, maskFilename);
19a65a0aKrzysztof Cwalina2 years ago338ClientResult result = GenerateImageEdits(content, content.ContentType, cancellationToken.ToRequestOptions());
e0fee603Jose Arriaga Maldonado1 years ago339return ClientResult.FromValue(((GeneratedImageCollection)result).FirstOrDefault(), result.GetRawResponse());
9f9f2936Jose Arriaga Maldonado2 years ago340}
341
13a9c686Jose Arriaga Maldonado1 years ago342/// <summary> Generates an edited or extended image based on an original image, a prompt, and a mask. </summary>
9f9f2936Jose Arriaga Maldonado2 years ago343/// <param name="imageFilePath">
13a9c686Jose Arriaga Maldonado1 years ago344/// The path of the image file to edit. Must be a valid PNG file, less than 4MB, and square. The provided file
345/// path's extension (for example: .png) will be used to validate the format of the input image. The request
346/// may fail if the file path's extension and the actual format of the input image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago347/// </param>
348/// <param name="prompt"> A text description of the desired image. </param>
349/// <param name="maskFilePath">
13a9c686Jose Arriaga Maldonado1 years ago350/// The path of the mask image file whose fully transparent areas (i.e., where alpha is zero) indicate where
351/// the original image should be edited. Must be a valid PNG file, less than 4MB, and have the same dimensions
352/// as the original image. The provided file path's extension (for example: .png) will be used to validate the
353/// format of the mask image. The request may fail if the file path's extension and the actual format of the
354/// mask image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago355/// </param>
13a9c686Jose Arriaga Maldonado1 years ago356/// <param name="options"> The options to configure the image edit. </param>
9f9f2936Jose Arriaga Maldonado2 years ago357/// <exception cref="ArgumentNullException"> <paramref name="imageFilePath"/>, <paramref name="prompt"/> or <paramref name="maskFilePath"/> is null. </exception>
358/// <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>
359public virtual async Task<ClientResult<GeneratedImage>> GenerateImageEditAsync(string imageFilePath, string prompt, string maskFilePath, ImageEditOptions options = null)
360{
361Argument.AssertNotNullOrEmpty(imageFilePath, nameof(imageFilePath));
362Argument.AssertNotNullOrEmpty(prompt, nameof(prompt));
363Argument.AssertNotNullOrEmpty(maskFilePath, nameof(maskFilePath));
364
365using FileStream imageStream = File.OpenRead(imageFilePath);
366using FileStream maskStream = File.OpenRead(maskFilePath);
367return await GenerateImageEditAsync(imageStream, imageFilePath, prompt, maskStream, maskFilePath, options).ConfigureAwait(false);
368}
369
13a9c686Jose Arriaga Maldonado1 years ago370/// <summary> Generates an edited or extended image based on an original image, a prompt, and a mask. </summary>
9f9f2936Jose Arriaga Maldonado2 years ago371/// <param name="imageFilePath">
13a9c686Jose Arriaga Maldonado1 years ago372/// The path of the image file to edit. Must be a valid PNG file, less than 4MB, and square. The provided file
373/// path's extension (for example: .png) will be used to validate the format of the input image. The request
374/// may fail if the file path's extension and the actual format of the input image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago375/// </param>
376/// <param name="prompt"> A text description of the desired image. </param>
377/// <param name="maskFilePath">
13a9c686Jose Arriaga Maldonado1 years ago378/// The path of the mask image file whose fully transparent areas (i.e., where alpha is zero) indicate where
379/// the original image should be edited. Must be a valid PNG file, less than 4MB, and have the same dimensions
380/// as the original image. The provided file path's extension (for example: .png) will be used to validate the
381/// format of the mask image. The request may fail if the file path's extension and the actual format of the
382/// mask image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago383/// </param>
13a9c686Jose Arriaga Maldonado1 years ago384/// <param name="options"> The options to configure the image edit. </param>
9f9f2936Jose Arriaga Maldonado2 years ago385/// <exception cref="ArgumentNullException"> <paramref name="imageFilePath"/>, <paramref name="prompt"/> or <paramref name="maskFilePath"/> is null. </exception>
386/// <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>
387public virtual ClientResult<GeneratedImage> GenerateImageEdit(string imageFilePath, string prompt, string maskFilePath, ImageEditOptions options = null)
388{
389Argument.AssertNotNullOrEmpty(imageFilePath, nameof(imageFilePath));
390Argument.AssertNotNullOrEmpty(prompt, nameof(prompt));
391Argument.AssertNotNullOrEmpty(maskFilePath, nameof(maskFilePath));
392
393using FileStream imageStream = File.OpenRead(imageFilePath);
394using FileStream maskStream = File.OpenRead(maskFilePath);
395return GenerateImageEdit(imageStream, imageFilePath, prompt, maskStream, maskFilePath, options);
396}
397
13a9c686Jose Arriaga Maldonado1 years ago398/// <summary> Generates edited or extended images based on an original image and a prompt. </summary>
9f9f2936Jose Arriaga Maldonado2 years ago399/// <param name="image">
13a9c686Jose Arriaga Maldonado1 years ago400/// The image stream to edit. Must be a valid PNG file, less than 4MB, and square. The image must have transparency, which
401/// will be used as the mask.
9f9f2936Jose Arriaga Maldonado2 years ago402/// </param>
403/// <param name="imageFilename">
13a9c686Jose Arriaga Maldonado1 years ago404/// The filename associated with the image stream. The filename's extension (for example: .png) will be used to
405/// validate the format of the input image. The request may fail if the filename's extension and the actual
406/// format of the input image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago407/// </param>
408/// <param name="prompt"> A text description of the desired image. </param>
13a9c686Jose Arriaga Maldonado1 years ago409/// <param name="imageCount"> The number of edited or extended images to generate. </param>
410/// <param name="options"> The options to configure the image edit. </param>
411/// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
9f9f2936Jose Arriaga Maldonado2 years ago412/// <exception cref="ArgumentNullException"> <paramref name="image"/>, <paramref name="imageFilename"/>, or <paramref name="prompt"/> is null. </exception>
413/// <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 ago414public virtual async Task<ClientResult<GeneratedImageCollection>> GenerateImageEditsAsync(Stream image, string imageFilename, string prompt, int imageCount, ImageEditOptions options = null, CancellationToken cancellationToken = default)
9f9f2936Jose Arriaga Maldonado2 years ago415{
416Argument.AssertNotNull(image, nameof(image));
417Argument.AssertNotNullOrEmpty(imageFilename, nameof(imageFilename));
418Argument.AssertNotNullOrEmpty(prompt, nameof(prompt));
419
420options ??= new();
421CreateImageEditOptions(image, imageFilename, prompt, null, null, imageCount, ref options);
422
0ca4c062Jose Arriaga Maldonado1 years ago423using MultiPartFormDataBinaryContent content = options.ToMultipartContent(image, imageFilename, null, null);
19a65a0aKrzysztof Cwalina2 years ago424ClientResult result = await GenerateImageEditsAsync(content, content.ContentType, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
e0fee603Jose Arriaga Maldonado1 years ago425return ClientResult.FromValue((GeneratedImageCollection)result, result.GetRawResponse());
9f9f2936Jose Arriaga Maldonado2 years ago426}
427
13a9c686Jose Arriaga Maldonado1 years ago428/// <summary> Generates edited or extended images based on an original image and a prompt. </summary>
9f9f2936Jose Arriaga Maldonado2 years ago429/// <param name="image">
13a9c686Jose Arriaga Maldonado1 years ago430/// The image stream to edit. Must be a valid PNG file, less than 4MB, and square. The image must have transparency, which
431/// will be used as the mask.
9f9f2936Jose Arriaga Maldonado2 years ago432/// </param>
433/// <param name="imageFilename">
13a9c686Jose Arriaga Maldonado1 years ago434/// The filename associated with the image stream. The filename's extension (for example: .png) will be used to
435/// validate the format of the input image. The request may fail if the filename's extension and the actual
436/// format of the input image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago437/// </param>
438/// <param name="prompt"> A text description of the desired image. </param>
13a9c686Jose Arriaga Maldonado1 years ago439/// <param name="imageCount"> The number of edited or extended images to generate. </param>
440/// <param name="options"> The options to configure the image edit. </param>
441/// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
9f9f2936Jose Arriaga Maldonado2 years ago442/// <exception cref="ArgumentNullException"> <paramref name="image"/>, <paramref name="imageFilename"/>, or <paramref name="prompt"/> is null. </exception>
443/// <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 ago444public virtual ClientResult<GeneratedImageCollection> GenerateImageEdits(Stream image, string imageFilename, string prompt, int imageCount, ImageEditOptions options = null, CancellationToken cancellationToken = default)
9f9f2936Jose Arriaga Maldonado2 years ago445{
446Argument.AssertNotNull(image, nameof(image));
447Argument.AssertNotNullOrEmpty(imageFilename, nameof(imageFilename));
448Argument.AssertNotNullOrEmpty(prompt, nameof(prompt));
449
450options ??= new();
451CreateImageEditOptions(image, imageFilename, prompt, null, null, imageCount, ref options);
452
0ca4c062Jose Arriaga Maldonado1 years ago453using MultiPartFormDataBinaryContent content = options.ToMultipartContent(image, imageFilename, null, null);
19a65a0aKrzysztof Cwalina2 years ago454ClientResult result = GenerateImageEdits(content, content.ContentType, cancellationToken.ToRequestOptions());
e0fee603Jose Arriaga Maldonado1 years ago455return ClientResult.FromValue((GeneratedImageCollection)result, result.GetRawResponse());
9f9f2936Jose Arriaga Maldonado2 years ago456}
457
13a9c686Jose Arriaga Maldonado1 years ago458/// <summary> Generates edited or extended images based on an original image and a prompt. </summary>
9f9f2936Jose Arriaga Maldonado2 years ago459/// <param name="imageFilePath">
13a9c686Jose Arriaga Maldonado1 years ago460/// The path of the image file to edit. Must be a valid PNG file, less than 4MB, and square. The image must
461/// have transparency, which will be used as the mask. The provided file path's extension (for example: .png)
462/// will be used to validate the format of the input image. The request may fail if the file path's extension
463/// and the actual format of the input image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago464/// </param>
465/// <param name="prompt"> A text description of the desired image. </param>
13a9c686Jose Arriaga Maldonado1 years ago466/// <param name="imageCount"> The number of edited or extended images to generate. </param>
467/// <param name="options"> The options to configure the image edit. </param>
9f9f2936Jose Arriaga Maldonado2 years ago468/// <exception cref="ArgumentNullException"> <paramref name="imageFilePath"/> or <paramref name="prompt"/> is null. </exception>
469/// <exception cref="ArgumentException"> <paramref name="imageFilePath"/> or <paramref name="prompt"/> is an empty string, and was expected to be non-empty. </exception>
470public virtual async Task<ClientResult<GeneratedImageCollection>> GenerateImageEditsAsync(string imageFilePath, string prompt, int imageCount, ImageEditOptions options = null)
471{
472Argument.AssertNotNullOrEmpty(imageFilePath, nameof(imageFilePath));
473Argument.AssertNotNullOrEmpty(prompt, nameof(prompt));
474
475using FileStream imageStream = File.OpenRead(imageFilePath);
476return await GenerateImageEditsAsync(imageStream, imageFilePath, prompt, imageCount, options).ConfigureAwait(false);
477}
478
13a9c686Jose Arriaga Maldonado1 years ago479/// <summary> Generates edited or extended images based on an original image and a prompt. </summary>
9f9f2936Jose Arriaga Maldonado2 years ago480/// <param name="imageFilePath">
13a9c686Jose Arriaga Maldonado1 years ago481/// The path of the image file to edit. Must be a valid PNG file, less than 4MB, and square. The image must
482/// have transparency, which will be used as the mask. The provided file path's extension (for example: .png)
483/// will be used to validate the format of the input image. The request may fail if the file path's extension
484/// and the actual format of the input image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago485/// </param>
486/// <param name="prompt"> A text description of the desired image. </param>
13a9c686Jose Arriaga Maldonado1 years ago487/// <param name="imageCount"> The number of edited or extended images to generate. </param>
488/// <param name="options"> The options to configure the image edit. </param>
9f9f2936Jose Arriaga Maldonado2 years ago489/// <exception cref="ArgumentNullException"> <paramref name="imageFilePath"/> or <paramref name="prompt"/> is null. </exception>
490/// <exception cref="ArgumentException"> <paramref name="imageFilePath"/> or <paramref name="prompt"/> is an empty string, and was expected to be non-empty. </exception>
491public virtual ClientResult<GeneratedImageCollection> GenerateImageEdits(string imageFilePath, string prompt, int imageCount, ImageEditOptions options = null)
492{
493Argument.AssertNotNullOrEmpty(imageFilePath, nameof(imageFilePath));
494Argument.AssertNotNullOrEmpty(prompt, nameof(prompt));
495
496using FileStream imageStream = File.OpenRead(imageFilePath);
497return GenerateImageEdits(imageStream, imageFilePath, prompt, imageCount, options);
498}
499
13a9c686Jose Arriaga Maldonado1 years ago500/// <summary> Generates edited or extended images based on an original image, a prompt, and a mask. </summary>
501/// <param name="image"> The image stream to edit. Must be a valid PNG file, less than 4MB, and square. </param>
9f9f2936Jose Arriaga Maldonado2 years ago502/// <param name="imageFilename">
13a9c686Jose Arriaga Maldonado1 years ago503/// The filename associated with the image stream. The filename's extension (for example: .png) will be used to
504/// validate the format of the input image. The request may fail if the filename's extension and the actual
505/// format of the input image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago506/// </param>
507/// <param name="prompt"> A text description of the desired image. </param>
508/// <param name="mask">
13a9c686Jose Arriaga Maldonado1 years ago509/// An additional image whose fully transparent areas (i.e., where alpha is zero) indicate where the original image
510/// should be edited. Must be a valid PNG file, less than 4MB, and have the same dimensions as image.
9f9f2936Jose Arriaga Maldonado2 years ago511/// </param>
512/// <param name="maskFilename">
13a9c686Jose Arriaga Maldonado1 years ago513/// The filename associated with the mask image stream. The filename's extension (for example: .png) will be
514/// used to validate the format of the mask image. The request may fail if the filename's extension and the
515/// actual format of the mask image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago516/// </param>
13a9c686Jose Arriaga Maldonado1 years ago517/// <param name="imageCount"> The number of edited or extended images to generate. </param>
518/// <param name="options"> The options to configure the image edit. </param>
519/// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
9f9f2936Jose Arriaga Maldonado2 years ago520/// <exception cref="ArgumentNullException"> <paramref name="image"/>, <paramref name="imageFilename"/>, <paramref name="prompt"/>, <paramref name="mask"/>, or <paramref name="maskFilename"/> is null. </exception>
521/// <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 ago522public 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 ago523{
524Argument.AssertNotNull(image, nameof(image));
525Argument.AssertNotNullOrEmpty(imageFilename, nameof(imageFilename));
526Argument.AssertNotNullOrEmpty(prompt, nameof(prompt));
527Argument.AssertNotNull(mask, nameof(mask));
528Argument.AssertNotNullOrEmpty(maskFilename, nameof(maskFilename));
529
530options ??= new();
531CreateImageEditOptions(image, imageFilename, prompt, mask, maskFilename, imageCount, ref options);
532
0ca4c062Jose Arriaga Maldonado1 years ago533using MultiPartFormDataBinaryContent content = options.ToMultipartContent(image, imageFilename, mask, maskFilename);
19a65a0aKrzysztof Cwalina2 years ago534ClientResult result = await GenerateImageEditsAsync(content, content.ContentType, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
e0fee603Jose Arriaga Maldonado1 years ago535return ClientResult.FromValue((GeneratedImageCollection)result, result.GetRawResponse());
9f9f2936Jose Arriaga Maldonado2 years ago536}
537
13a9c686Jose Arriaga Maldonado1 years ago538/// <summary> Generates edited or extended images based on an original image, a prompt, and a mask. </summary>
539/// <param name="image"> The image stream to edit. Must be a valid PNG file, less than 4MB, and square. </param>
9f9f2936Jose Arriaga Maldonado2 years ago540/// <param name="imageFilename">
13a9c686Jose Arriaga Maldonado1 years ago541/// The filename associated with the image stream. The filename's extension (for example: .png) will be used to
542/// validate the format of the input image. The request may fail if the filename's extension and the actual
543/// format of the input image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago544/// </param>
545/// <param name="prompt"> A text description of the desired image. </param>
546/// <param name="mask">
13a9c686Jose Arriaga Maldonado1 years ago547/// An additional image whose fully transparent areas (i.e., where alpha is zero) indicate where the original image
548/// should be edited. Must be a valid PNG file, less than 4MB, and have the same dimensions as image.
9f9f2936Jose Arriaga Maldonado2 years ago549/// </param>
550/// <param name="maskFilename">
13a9c686Jose Arriaga Maldonado1 years ago551/// The filename associated with the mask image stream. The filename's extension (for example: .png) will be
552/// used to validate the format of the mask image. The request may fail if the filename's extension and the
553/// actual format of the mask image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago554/// </param>
13a9c686Jose Arriaga Maldonado1 years ago555/// <param name="imageCount"> The number of edited or extended images to generate. </param>
556/// <param name="options"> The options to configure the image edit. </param>
557/// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
9f9f2936Jose Arriaga Maldonado2 years ago558/// <exception cref="ArgumentNullException"> <paramref name="image"/>, <paramref name="imageFilename"/>, <paramref name="prompt"/>, <paramref name="mask"/>, or <paramref name="maskFilename"/> is null. </exception>
559/// <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 ago560public 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 ago561{
562Argument.AssertNotNull(image, nameof(image));
563Argument.AssertNotNullOrEmpty(imageFilename, nameof(imageFilename));
564Argument.AssertNotNullOrEmpty(prompt, nameof(prompt));
565Argument.AssertNotNull(mask, nameof(mask));
566Argument.AssertNotNullOrEmpty(maskFilename, nameof(maskFilename));
567
568options ??= new();
569CreateImageEditOptions(image, imageFilename, prompt, mask, maskFilename, imageCount, ref options);
570
0ca4c062Jose Arriaga Maldonado1 years ago571using MultiPartFormDataBinaryContent content = options.ToMultipartContent(image, imageFilename, mask, maskFilename);
19a65a0aKrzysztof Cwalina2 years ago572ClientResult result = GenerateImageEdits(content, content.ContentType, cancellationToken.ToRequestOptions());
e0fee603Jose Arriaga Maldonado1 years ago573return ClientResult.FromValue((GeneratedImageCollection)result, result.GetRawResponse());
9f9f2936Jose Arriaga Maldonado2 years ago574}
575
13a9c686Jose Arriaga Maldonado1 years ago576/// <summary> Generates edited or extended images based on an original image, a prompt, and a mask. </summary>
9f9f2936Jose Arriaga Maldonado2 years ago577/// <param name="imageFilePath">
13a9c686Jose Arriaga Maldonado1 years ago578/// The path of the image file to edit. Must be a valid PNG file, less than 4MB, and square. The provided file
579/// path's extension (for example: .png) will be used to validate the format of the input image. The request
580/// may fail if the file path's extension and the actual format of the input image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago581/// </param>
582/// <param name="prompt"> A text description of the desired image. </param>
583/// <param name="maskFilePath">
13a9c686Jose Arriaga Maldonado1 years ago584/// The path of the mask image file whose fully transparent areas (i.e., where alpha is zero) indicate where
585/// the original image should be edited. Must be a valid PNG file, less than 4MB, and have the same dimensions
586/// as the original image. The provided file path's extension (for example: .png) will be used to validate the
587/// format of the mask image. The request may fail if the file path's extension and the actual format of the
588/// mask image do not match.
589/// </param>
590/// <param name="imageCount"> The number of edited or extended images to generate. </param>
591/// <param name="options"> The options to configure the image edit. </param>
9f9f2936Jose Arriaga Maldonado2 years ago592/// <exception cref="ArgumentNullException"> <paramref name="imageFilePath"/>, <paramref name="prompt"/> or <paramref name="maskFilePath"/> is null. </exception>
593/// <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>
594public virtual async Task<ClientResult<GeneratedImageCollection>> GenerateImageEditsAsync(string imageFilePath, string prompt, string maskFilePath, int imageCount, ImageEditOptions options = null)
595{
596Argument.AssertNotNullOrEmpty(imageFilePath, nameof(imageFilePath));
597Argument.AssertNotNullOrEmpty(prompt, nameof(prompt));
598Argument.AssertNotNullOrEmpty(maskFilePath, nameof(maskFilePath));
599
600using FileStream imageStream = File.OpenRead(imageFilePath);
601using FileStream maskStream = File.OpenRead(maskFilePath);
602return await GenerateImageEditsAsync(imageStream, imageFilePath, prompt, maskStream, maskFilePath, imageCount, options).ConfigureAwait(false);
603}
604
13a9c686Jose Arriaga Maldonado1 years ago605/// <summary> Generates edited or extended images based on an original image, a prompt, and a mask. </summary>
9f9f2936Jose Arriaga Maldonado2 years ago606/// <param name="imageFilePath">
13a9c686Jose Arriaga Maldonado1 years ago607/// The path of the image file to edit. Must be a valid PNG file, less than 4MB, and square. The provided file
608/// path's extension (for example: .png) will be used to validate the format of the input image. The request
609/// may fail if the file path's extension and the actual format of the input image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago610/// </param>
611/// <param name="prompt"> A text description of the desired image. </param>
612/// <param name="maskFilePath">
13a9c686Jose Arriaga Maldonado1 years ago613/// The path of the mask image file whose fully transparent areas (i.e., where alpha is zero) indicate where
614/// the original image should be edited. Must be a valid PNG file, less than 4MB, and have the same dimensions
615/// as the original image. The provided file path's extension (for example: .png) will be used to validate the
616/// format of the mask image. The request may fail if the file path's extension and the actual format of the
617/// mask image do not match.
618/// </param>
619/// <param name="imageCount"> The number of edited or extended images to generate. </param>
620/// <param name="options"> The options to configure the image edit. </param>
9f9f2936Jose Arriaga Maldonado2 years ago621/// <exception cref="ArgumentNullException"> <paramref name="imageFilePath"/>, <paramref name="prompt"/> or <paramref name="maskFilePath"/> is null. </exception>
622/// <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>
623public virtual ClientResult<GeneratedImageCollection> GenerateImageEdits(string imageFilePath, string prompt, string maskFilePath, int imageCount, ImageEditOptions options = null)
624{
625Argument.AssertNotNullOrEmpty(imageFilePath, nameof(imageFilePath));
626Argument.AssertNotNullOrEmpty(prompt, nameof(prompt));
627Argument.AssertNotNullOrEmpty(maskFilePath, nameof(maskFilePath));
628
629using FileStream imageStream = File.OpenRead(imageFilePath);
630using FileStream maskStream = File.OpenRead(maskFilePath);
631return GenerateImageEdits(imageStream, imageFilePath, prompt, maskStream, maskFilePath, imageCount, options);
632}
633
634#endregion
635
636#region GenerateImageVariations
637
638/// <summary> Generates a variation of a given image. </summary>
13a9c686Jose Arriaga Maldonado1 years ago639/// <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 ago640/// <param name="imageFilename">
13a9c686Jose Arriaga Maldonado1 years ago641/// The filename associated with the image stream. The filename's extension (for example: .png) will be used to
642/// validate the format of the input image. The request may fail if the filename's extension and the actual
643/// format of the input image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago644/// </param>
13a9c686Jose Arriaga Maldonado1 years ago645/// <param name="options"> The options to configure the image variation. </param>
646/// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
9f9f2936Jose Arriaga Maldonado2 years ago647/// <exception cref="ArgumentNullException"> <paramref name="image"/> or <paramref name="imageFilename"/> is null. </exception>
648/// <exception cref="ArgumentException"> <paramref name="imageFilename"/> is an empty string, and was expected to be non-empty. </exception>
19a65a0aKrzysztof Cwalina2 years ago649public virtual async Task<ClientResult<GeneratedImage>> GenerateImageVariationAsync(Stream image, string imageFilename, ImageVariationOptions options = null, CancellationToken cancellationToken = default)
9f9f2936Jose Arriaga Maldonado2 years ago650{
651Argument.AssertNotNull(image, nameof(image));
652Argument.AssertNotNullOrEmpty(imageFilename, nameof(imageFilename));
653
654options ??= new();
655CreateImageVariationOptions(image, imageFilename, null, ref options);
656
0ca4c062Jose Arriaga Maldonado1 years ago657using MultiPartFormDataBinaryContent content = options.ToMultipartContent(image, imageFilename);
19a65a0aKrzysztof Cwalina2 years ago658ClientResult result = await GenerateImageVariationsAsync(content, content.ContentType, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
e0fee603Jose Arriaga Maldonado1 years ago659return ClientResult.FromValue(((GeneratedImageCollection)result).FirstOrDefault(), result.GetRawResponse());
9f9f2936Jose Arriaga Maldonado2 years ago660}
661
662/// <summary> Generates a variation of a given image. </summary>
13a9c686Jose Arriaga Maldonado1 years ago663/// <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 ago664/// <param name="imageFilename">
13a9c686Jose Arriaga Maldonado1 years ago665/// The filename associated with the image stream. The filename's extension (for example: .png) will be used to
666/// validate the format of the input image. The request may fail if the filename's extension and the actual
667/// format of the input image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago668/// </param>
13a9c686Jose Arriaga Maldonado1 years ago669/// <param name="options"> The options to configure the image variation. </param>
670/// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
9f9f2936Jose Arriaga Maldonado2 years ago671/// <exception cref="ArgumentNullException"> <paramref name="image"/> or <paramref name="imageFilename"/> is null. </exception>
672/// <exception cref="ArgumentException"> <paramref name="imageFilename"/> is an empty string, and was expected to be non-empty. </exception>
19a65a0aKrzysztof Cwalina2 years ago673public virtual ClientResult<GeneratedImage> GenerateImageVariation(Stream image, string imageFilename, ImageVariationOptions options = null, CancellationToken cancellationToken = default)
9f9f2936Jose Arriaga Maldonado2 years ago674{
675Argument.AssertNotNull(image, nameof(image));
676Argument.AssertNotNullOrEmpty(imageFilename, nameof(imageFilename));
677
678options ??= new();
679CreateImageVariationOptions(image, imageFilename, null, ref options);
680
0ca4c062Jose Arriaga Maldonado1 years ago681using MultiPartFormDataBinaryContent content = options.ToMultipartContent(image, imageFilename);
19a65a0aKrzysztof Cwalina2 years ago682ClientResult result = GenerateImageVariations(content, content.ContentType, cancellationToken.ToRequestOptions());
e0fee603Jose Arriaga Maldonado1 years ago683return ClientResult.FromValue(((GeneratedImageCollection)result).FirstOrDefault(), result.GetRawResponse());
9f9f2936Jose Arriaga Maldonado2 years ago684}
685
686/// <summary> Generates a variation of a given image. </summary>
687/// <param name="imageFilePath">
13a9c686Jose Arriaga Maldonado1 years ago688/// The path of the image file to use as the basis for the variation. Must be a valid PNG file, less than 4MB,
689/// and square. The provided file path's extension (for example: .png) will be used to validate the format of
690/// the input image. The request may fail if the file path's extension and the actual format of the input image
691/// do not match.
9f9f2936Jose Arriaga Maldonado2 years ago692/// </param>
13a9c686Jose Arriaga Maldonado1 years ago693/// <param name="options"> The options to configure the image variation. </param>
9f9f2936Jose Arriaga Maldonado2 years ago694/// <exception cref="ArgumentNullException"> <paramref name="imageFilePath"/> is null. </exception>
695/// <exception cref="ArgumentException"> <paramref name="imageFilePath"/> is an empty string, and was expected to be non-empty. </exception>
696public virtual async Task<ClientResult<GeneratedImage>> GenerateImageVariationAsync(string imageFilePath, ImageVariationOptions options = null)
697{
698Argument.AssertNotNullOrEmpty(imageFilePath, nameof(imageFilePath));
699
700using FileStream imageStream = File.OpenRead(imageFilePath);
701return await GenerateImageVariationAsync(imageStream, imageFilePath, options).ConfigureAwait(false);
702}
703
704/// <summary> Generates a variation of a given image. </summary>
705/// <param name="imageFilePath">
13a9c686Jose Arriaga Maldonado1 years ago706/// The path of the image file to use as the basis for the variation. Must be a valid PNG file, less than 4MB,
707/// and square. The provided file path's extension (for example: .png) will be used to validate the format of
708/// the input image. The request may fail if the file path's extension and the actual format of the input image
709/// do not match.
9f9f2936Jose Arriaga Maldonado2 years ago710/// </param>
13a9c686Jose Arriaga Maldonado1 years ago711/// <param name="options"> The options to configure the image variation. </param>
9f9f2936Jose Arriaga Maldonado2 years ago712/// <exception cref="ArgumentNullException"> <paramref name="imageFilePath"/> is null. </exception>
713/// <exception cref="ArgumentException"> <paramref name="imageFilePath"/> is an empty string, and was expected to be non-empty. </exception>
714public virtual ClientResult<GeneratedImage> GenerateImageVariation(string imageFilePath, ImageVariationOptions options = null)
715{
716Argument.AssertNotNullOrEmpty(imageFilePath, nameof(imageFilePath));
717
718using FileStream imageStream = File.OpenRead(imageFilePath);
719return GenerateImageVariation(imageStream, imageFilePath, options);
720}
721
722/// <summary> Generates variations of a given image. </summary>
13a9c686Jose Arriaga Maldonado1 years ago723/// <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 ago724/// <param name="imageFilename">
13a9c686Jose Arriaga Maldonado1 years ago725/// The filename associated with the image stream. The filename's extension (for example: .png) will be used to
726/// validate the format of the input image. The request may fail if the filename's extension and the actual
727/// format of the input image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago728/// </param>
729/// <param name="imageCount"> The number of image variations to generate. </param>
13a9c686Jose Arriaga Maldonado1 years ago730/// <param name="options"> The options to configure the image variation. </param>
731/// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
9f9f2936Jose Arriaga Maldonado2 years ago732/// <exception cref="ArgumentNullException"> <paramref name="image"/> or <paramref name="imageFilename"/> is null. </exception>
733/// <exception cref="ArgumentException"> <paramref name="imageFilename"/> is an empty string, and was expected to be non-empty. </exception>
19a65a0aKrzysztof Cwalina2 years ago734public virtual async Task<ClientResult<GeneratedImageCollection>> GenerateImageVariationsAsync(Stream image, string imageFilename, int imageCount, ImageVariationOptions options = null, CancellationToken cancellationToken = default)
9f9f2936Jose Arriaga Maldonado2 years ago735{
736Argument.AssertNotNull(image, nameof(image));
737Argument.AssertNotNullOrEmpty(imageFilename, nameof(imageFilename));
738
739options ??= new();
740CreateImageVariationOptions(image, imageFilename, imageCount, ref options);
741
0ca4c062Jose Arriaga Maldonado1 years ago742using MultiPartFormDataBinaryContent content = options.ToMultipartContent(image, imageFilename);
19a65a0aKrzysztof Cwalina2 years ago743ClientResult result = await GenerateImageVariationsAsync(content, content.ContentType, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
e0fee603Jose Arriaga Maldonado1 years ago744return ClientResult.FromValue((GeneratedImageCollection)result, result.GetRawResponse());
9f9f2936Jose Arriaga Maldonado2 years ago745}
746
747/// <summary> Generates variations of a given image. </summary>
13a9c686Jose Arriaga Maldonado1 years ago748/// <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 ago749/// <param name="imageFilename">
13a9c686Jose Arriaga Maldonado1 years ago750/// The filename associated with the image stream. The filename's extension (for example: .png) will be used to
751/// validate the format of the input image. The request may fail if the filename's extension and the actual
752/// format of the input image do not match.
9f9f2936Jose Arriaga Maldonado2 years ago753/// </param>
754/// <param name="imageCount"> The number of image variations to generate. </param>
13a9c686Jose Arriaga Maldonado1 years ago755/// <param name="options"> The options to configure the image variation. </param>
756/// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
9f9f2936Jose Arriaga Maldonado2 years ago757/// <exception cref="ArgumentNullException"> <paramref name="image"/> or <paramref name="imageFilename"/> is null. </exception>
758/// <exception cref="ArgumentException"> <paramref name="imageFilename"/> is an empty string, and was expected to be non-empty. </exception>
19a65a0aKrzysztof Cwalina2 years ago759public virtual ClientResult<GeneratedImageCollection> GenerateImageVariations(Stream image, string imageFilename, int imageCount, ImageVariationOptions options = null, CancellationToken cancellationToken = default)
9f9f2936Jose Arriaga Maldonado2 years ago760{
761Argument.AssertNotNull(image, nameof(image));
762Argument.AssertNotNullOrEmpty(imageFilename, nameof(imageFilename));
763
764options ??= new();
765CreateImageVariationOptions(image, imageFilename, imageCount, ref options);
766
0ca4c062Jose Arriaga Maldonado1 years ago767using MultiPartFormDataBinaryContent content = options.ToMultipartContent(image, imageFilename);
19a65a0aKrzysztof Cwalina2 years ago768ClientResult result = GenerateImageVariations(content, content.ContentType, cancellationToken.ToRequestOptions());
e0fee603Jose Arriaga Maldonado1 years ago769return ClientResult.FromValue((GeneratedImageCollection)result, result.GetRawResponse());
9f9f2936Jose Arriaga Maldonado2 years ago770}
771
772/// <summary> Generates variations of a given image. </summary>
773/// <param name="imageFilePath">
13a9c686Jose Arriaga Maldonado1 years ago774/// The path of the image file to use as the basis for the variation. Must be a valid PNG file, less than 4MB,
775/// and square. The provided file path's extension (for example: .png) will be used to validate the format of
776/// the input image. The request may fail if the file path's extension and the actual format of the input image
777/// do not match.
9f9f2936Jose Arriaga Maldonado2 years ago778/// </param>
779/// <param name="imageCount"> The number of image variations to generate. </param>
13a9c686Jose Arriaga Maldonado1 years ago780/// <param name="options"> The options to configure the image variation. </param>
9f9f2936Jose Arriaga Maldonado2 years ago781/// <exception cref="ArgumentNullException"> <paramref name="imageFilePath"/> was null. </exception>
782/// <exception cref="ArgumentException"> <paramref name="imageFilePath"/> is an empty string, and was expected to be non-empty. </exception>
783public virtual async Task<ClientResult<GeneratedImageCollection>> GenerateImageVariationsAsync(string imageFilePath, int imageCount, ImageVariationOptions options = null)
784{
785Argument.AssertNotNullOrEmpty(imageFilePath, nameof(imageFilePath));
786
787using FileStream imageStream = File.OpenRead(imageFilePath);
788return await GenerateImageVariationsAsync(imageStream, imageFilePath, imageCount, options).ConfigureAwait(false);
789}
790
791/// <summary> Generates variations of a given image. </summary>
792/// <param name="imageFilePath">
13a9c686Jose Arriaga Maldonado1 years ago793/// The path of the image file to use as the basis for the variation. Must be a valid PNG file, less than 4MB,
794/// and square. The provided file path's extension (for example: .png) will be used to validate the format of
795/// the input image. The request may fail if the file path's extension and the actual format of the input image
796/// do not match.
9f9f2936Jose Arriaga Maldonado2 years ago797/// </param>
798/// <param name="imageCount"> The number of image variations to generate. </param>
13a9c686Jose Arriaga Maldonado1 years ago799/// <param name="options"> The options to configure the image variation. </param>
9f9f2936Jose Arriaga Maldonado2 years ago800/// <exception cref="ArgumentNullException"> <paramref name="imageFilePath"/> was null. </exception>
801/// <exception cref="ArgumentException"> <paramref name="imageFilePath"/> is an empty string, and was expected to be non-empty. </exception>
802public virtual ClientResult<GeneratedImageCollection> GenerateImageVariations(string imageFilePath, int imageCount, ImageVariationOptions options = null)
803{
804Argument.AssertNotNullOrEmpty(imageFilePath, nameof(imageFilePath));
805
806using FileStream imageStream = File.OpenRead(imageFilePath);
807return GenerateImageVariations(imageStream, imageFilePath, imageCount, options);
808}
809
810#endregion
811
812private void CreateImageGenerationOptions(string prompt, int? imageCount, ref ImageGenerationOptions options)
813{
814options.Prompt = prompt;
815options.N = imageCount;
816options.Model = _model;
817}
818
819private void CreateImageEditOptions(Stream image, string imageFilename, string prompt, Stream mask, string maskFilename, int? imageCount, ref ImageEditOptions options)
820{
821options.Prompt = prompt;
822options.N = imageCount;
823options.Model = _model;
824}
825
826private void CreateImageVariationOptions(Stream image, string imageFilename, int? imageCount, ref ImageVariationOptions options)
827{
828options.N = imageCount;
829options.Model = _model;
830}
831}