Skip to content

Commit

Permalink
OpenAI-DotNet 6.6.0 (RageAgainstThePixel#77)
Browse files Browse the repository at this point in the history
- Added
[`response_format`](https://platform.openai.com/docs/api-reference/images/create#images/create-response_format)
to `ImageGenerationRequest`
- Refactored Image Requests with AbstractBaseImageRequest

---------

Co-authored-by: John Korsnes <[email protected]>
  • Loading branch information
StephenHodgson and johnkors committed Apr 4, 2023
1 parent 40c3fc2 commit 841802f
Show file tree
Hide file tree
Showing 9 changed files with 307 additions and 124 deletions.
59 changes: 57 additions & 2 deletions OpenAI-DotNet-Tests/TestFixture_05_Images.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public async Task Test_1_GenerateImages()

Assert.IsNotNull(results);
Assert.NotZero(results.Count);
Assert.IsNotEmpty(results[0]);

foreach (var result in results)
{
Expand All @@ -25,7 +26,43 @@ public async Task Test_1_GenerateImages()
}

[Test]
public async Task Test_2_GenerateImageEdits()
public async Task Test_2_GenerateImages_B64_Json()
{
Assert.IsNotNull(OpenAIClient.ImagesEndPoint);

var results = await OpenAIClient.ImagesEndPoint.GenerateImageAsync("A house riding a velociraptor", 1, ImageSize.Small, responseFormat: ResponseFormat.B64_Json);

Assert.IsNotNull(results);
Assert.NotZero(results.Count);
Assert.IsNotEmpty(results[0]);

foreach (var result in results)
{
Console.WriteLine(result);
}
}

[Test]
public async Task Test_3_GenerateImageEdits()
{
Assert.IsNotNull(OpenAIClient.ImagesEndPoint);

var imageAssetPath = Path.GetFullPath("..\\..\\..\\Assets\\image_edit_original.png");
var maskAssetPath = Path.GetFullPath("..\\..\\..\\Assets\\image_edit_mask.png");

var results = await OpenAIClient.ImagesEndPoint.CreateImageEditAsync(imageAssetPath, maskAssetPath, "A sunlit indoor lounge area with a pool containing a flamingo", 1, ImageSize.Small);

Assert.IsNotNull(results);
Assert.NotZero(results.Count);

foreach (var result in results)
{
Console.WriteLine(result);
}
}

[Test]
public async Task Test_4_GenerateImageEdits_B64_Json()
{
Assert.IsNotNull(OpenAIClient.ImagesEndPoint);

Expand All @@ -44,7 +81,7 @@ public async Task Test_2_GenerateImageEdits()
}

[Test]
public async Task Test_3_GenerateImageVariations()
public async Task Test_5_GenerateImageVariations()
{
Assert.IsNotNull(OpenAIClient.ImagesEndPoint);

Expand All @@ -60,5 +97,23 @@ public async Task Test_3_GenerateImageVariations()
Console.WriteLine(result);
}
}

[Test]
public async Task Test_6_GenerateImageVariations_B64_Json()
{
Assert.IsNotNull(OpenAIClient.ImagesEndPoint);

var imageAssetPath = Path.GetFullPath("..\\..\\..\\Assets\\image_edit_original.png");

var results = await OpenAIClient.ImagesEndPoint.CreateImageVariationAsync(imageAssetPath, 1, ImageSize.Small, responseFormat: ResponseFormat.B64_Json);

Assert.IsNotNull(results);
Assert.NotZero(results.Count);

foreach (var result in results)
{
Console.WriteLine(result);
}
}
}
}
76 changes: 76 additions & 0 deletions OpenAI-DotNet/Images/AbstractBaseImageRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System;
using System.Text.Json.Serialization;

namespace OpenAI.Images
{
/// <summary>
/// Abstract base image class for making requests.
/// </summary>
public abstract class AbstractBaseImageRequest
{
/// <summary>
/// Constructor.
/// </summary>
/// <param name="numberOfResults">
/// The number of images to generate. Must be between 1 and 10.
/// </param>
/// <param name="size">
/// The size of the generated images. Must be one of 256x256, 512x512, or 1024x1024.
/// </param>
/// <param name="user">
/// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
/// </param>
/// <param name="responseFormat">
/// The format in which the generated images are returned.
/// Must be one of url or b64_json.
/// <para/> Defaults to <see cref="Images.ResponseFormat.Url"/>
/// </param>
/// <exception cref="ArgumentOutOfRangeException"></exception>
protected AbstractBaseImageRequest(int numberOfResults = 1, ImageSize size = ImageSize.Large, ResponseFormat responseFormat = Images.ResponseFormat.Url, string user = null)
{
Number = numberOfResults;

Size = size switch
{
ImageSize.Small => "256x256",
ImageSize.Medium => "512x512",
ImageSize.Large => "1024x1024",
_ => throw new ArgumentOutOfRangeException(nameof(size), size, null)
};

User = user;
ResponseFormat = responseFormat switch
{
Images.ResponseFormat.Url => "url",
Images.ResponseFormat.B64_Json => "b64_json",
_ => throw new ArgumentOutOfRangeException(nameof(responseFormat), responseFormat, null)
};
}

/// <summary>
/// The number of images to generate. Must be between 1 and 10.
/// </summary>
[JsonPropertyName("n")]
public int Number { get; }

/// <summary>
/// The format in which the generated images are returned.
/// Must be one of url or b64_json.
/// <para/> Defaults to <see cref="Images.ResponseFormat.Url"/>
/// </summary>
[JsonPropertyName("response_format")]
public string ResponseFormat { get; }

/// <summary>
/// The size of the generated images. Must be one of 256x256, 512x512, or 1024x1024.
/// </summary>
[JsonPropertyName("size")]
public string Size { get; }

/// <summary>
/// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
/// </summary>
[JsonPropertyName("user")]
public string User { get; }
}
}
99 changes: 62 additions & 37 deletions OpenAI-DotNet/Images/ImageEditRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace OpenAI.Images
{
public sealed class ImageEditRequest : IDisposable
public sealed class ImageEditRequest : AbstractBaseImageRequest, IDisposable
{
/// <summary>
/// Constructor.
Expand All @@ -24,8 +24,18 @@ public sealed class ImageEditRequest : IDisposable
/// <param name="user">
/// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
/// </param>
public ImageEditRequest(string imagePath, string prompt, int numberOfResults = 1, ImageSize size = ImageSize.Large, string user = null)
: this(imagePath, null, prompt, numberOfResults, size, user)
/// <param name="responseFormat">
/// The format in which the generated images are returned.
/// Must be one of url or b64_json.
/// <para/> Defaults to <see cref="Images.ResponseFormat.Url"/>
/// </param>
public ImageEditRequest(
string imagePath,
string prompt,
int numberOfResults = 1,
ImageSize size = ImageSize.Large,
string user = null, ResponseFormat responseFormat = Images.ResponseFormat.Url)
: this(imagePath, null, prompt, numberOfResults, size, user, responseFormat)
{
}

Expand All @@ -52,7 +62,19 @@ public ImageEditRequest(string imagePath, string prompt, int numberOfResults = 1
/// <param name="user">
/// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
/// </param>
public ImageEditRequest(string imagePath, string maskPath, string prompt, int numberOfResults = 1, ImageSize size = ImageSize.Large, string user = null)
/// <param name="responseFormat">
/// The format in which the generated images are returned.
/// Must be one of url or b64_json.
/// <para/> Defaults to <see cref="ResponseFormat.Url"/>
/// </param>
public ImageEditRequest(
string imagePath,
string maskPath,
string prompt,
int numberOfResults = 1,
ImageSize size = ImageSize.Large,
string user = null,
ResponseFormat responseFormat = Images.ResponseFormat.Url)
: this(
File.OpenRead(imagePath),
Path.GetFileName(imagePath),
Expand All @@ -61,7 +83,8 @@ public ImageEditRequest(string imagePath, string maskPath, string prompt, int nu
prompt,
numberOfResults,
size,
user)
user,
responseFormat)
{
}

Expand All @@ -85,8 +108,20 @@ public ImageEditRequest(string imagePath, string maskPath, string prompt, int nu
/// <param name="user">
/// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
/// </param>
public ImageEditRequest(Stream image, string imageName, string prompt, int numberOfResults = 1, ImageSize size = ImageSize.Large, string user = null)
: this(image, imageName, null, null, prompt, numberOfResults, size, user)
/// <param name="responseFormat">
/// The format in which the generated images are returned.
/// Must be one of url or b64_json.
/// <para/> Defaults to <see cref="ResponseFormat.Url"/>
/// </param>
public ImageEditRequest(
Stream image,
string imageName,
string prompt,
int numberOfResults = 1,
ImageSize size = ImageSize.Large,
string user = null,
ResponseFormat responseFormat = Images.ResponseFormat.Url)
: this(image, imageName, null, null, prompt, numberOfResults, size, user, responseFormat)
{
}

Expand Down Expand Up @@ -115,13 +150,29 @@ public ImageEditRequest(Stream image, string imageName, string prompt, int numbe
/// <param name="user">
/// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
/// </param>
public ImageEditRequest(Stream image, string imageName, Stream mask, string maskName, string prompt, int numberOfResults = 1, ImageSize size = ImageSize.Large, string user = null)
/// <param name="responseFormat">
/// The format in which the generated images are returned.
/// Must be one of url or b64_json.
/// <para/> Defaults to <see cref="ResponseFormat.Url"/>
/// </param>
public ImageEditRequest(
Stream image,
string imageName,
Stream mask,
string maskName,
string prompt,
int numberOfResults = 1,
ImageSize size = ImageSize.Large,
string user = null,
ResponseFormat responseFormat = Images.ResponseFormat.Url)
: base(numberOfResults, size, responseFormat, user)
{
Image = image;

if (string.IsNullOrWhiteSpace(imageName))
{
imageName = "image.png";
const string defaultImageName = "image.png";
imageName = defaultImageName;
}

ImageName = imageName;
Expand All @@ -132,7 +183,8 @@ public ImageEditRequest(Stream image, string imageName, Stream mask, string mask

if (string.IsNullOrWhiteSpace(maskName))
{
maskName = "mask.png";
const string defaultMaskName = "mask.png";
maskName = defaultMaskName;
}

MaskName = maskName;
Expand All @@ -149,18 +201,6 @@ public ImageEditRequest(Stream image, string imageName, Stream mask, string mask
{
throw new ArgumentOutOfRangeException(nameof(numberOfResults), "The number of results must be between 1 and 10");
}

Number = numberOfResults;

Size = size switch
{
ImageSize.Small => "256x256",
ImageSize.Medium => "512x512",
ImageSize.Large => "1024x1024",
_ => throw new ArgumentOutOfRangeException(nameof(size), size, null)
};

User = user;
}

~ImageEditRequest() => Dispose(false);
Expand All @@ -186,21 +226,6 @@ public ImageEditRequest(Stream image, string imageName, Stream mask, string mask
/// </summary>
public string Prompt { get; }

/// <summary>
/// The number of images to generate. Must be between 1 and 10.
/// </summary>
public int Number { get; }

/// <summary>
/// The size of the generated images. Must be one of 256x256, 512x512, or 1024x1024.
/// </summary>
public string Size { get; }

/// <summary>
/// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
/// </summary>
public string User { get; }

private void Dispose(bool disposing)
{
if (disposing)
Expand Down
Loading

0 comments on commit 841802f

Please sign in to comment.