Skip to content

Commit

Permalink
com.openai.unity 5.1.1 (#114)
Browse files Browse the repository at this point in the history
- mark some image endpoint functions obsolete in favor of image request types
- updated docs
  • Loading branch information
StephenHodgson authored Nov 7, 2023
1 parent 81ff003 commit 4ae9838
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 18 deletions.
12 changes: 8 additions & 4 deletions Documentation~/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,8 @@ Creates an image given a prompt.

```csharp
var api = new OpenAIClient();
var results = await api.ImagesEndPoint.GenerateImageAsync("A house riding a velociraptor", 1, ImageSize.Small);
var request = new ImageGenerationRequest("A house riding a velociraptor", Model.DallE_3);
var results = await api.ImagesEndPoint.GenerateImageAsync(request);

foreach (var (path, texture) in results)
{
Expand All @@ -597,7 +598,8 @@ Creates an edited or extended image given an original image and a prompt.

```csharp
var api = new OpenAIClient();
var results = await api.ImagesEndPoint.CreateImageEditAsync(Path.GetFullPath(imageAssetPath), Path.GetFullPath(maskAssetPath), "A sunlit indoor lounge area with a pool containing a flamingo", 1, ImageSize.Small);
var request = new ImageEditRequest(Path.GetFullPath(imageAssetPath), Path.GetFullPath(maskAssetPath), "A sunlit indoor lounge area with a pool containing a flamingo", size: ImageSize.Small);
var results = await api.ImagesEndPoint.CreateImageEditAsync(request);

foreach (var (path, texture) in results)
{
Expand All @@ -614,7 +616,8 @@ Creates a variation of a given image.

```csharp
var api = new OpenAIClient();
var results = await api.ImagesEndPoint.CreateImageVariationAsync(Path.GetFullPath(imageAssetPath), 1, ImageSize.Small);
var request = new ImageVariationRequest(imageTexture, size: ImageSize.Small);
var results = await api.ImagesEndPoint.CreateImageVariationAsync(request);

foreach (var (path, texture) in results)
{
Expand All @@ -629,7 +632,8 @@ Alternatively, the endpoint can directly take a Texture2D with Read/Write enable

```csharp
var api = new OpenAIClient();
var results = await api.ImagesEndPoint.CreateImageVariationAsync(imageTexture, 1, ImageSize.Small);
var request = new ImageVariationRequest(imageTexture, size: ImageSize.Small);
var results = await api.ImagesEndPoint.CreateImageVariationAsync(request);
// imageTexture is of type Texture2D
foreach (var (path, texture) in results)
{
Expand Down
2 changes: 1 addition & 1 deletion Runtime/Files/FilesEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace OpenAI.Files
{
/// <summary>
/// Files are used to upload documents that can be used with features like Fine-tuning.<br/>
/// <see href="https://platform.openai.com/docs/api-reference/fine-tunes"/>
/// <see href="https://platform.openai.com/docs/api-reference/files"/>
/// </summary>
public sealed class FilesEndpoint : OpenAIBaseEndpoint
{
Expand Down
4 changes: 2 additions & 2 deletions Runtime/Images/AbstractBaseImageRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ public abstract class AbstractBaseImageRequest
/// <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"/>
/// <para/> Defaults to <see cref="ResponseFormat.Url"/>
/// </param>
/// <exception cref="ArgumentOutOfRangeException"></exception>
[Preserve]
protected AbstractBaseImageRequest(Model model = null, int numberOfResults = 1, ImageSize size = ImageSize.Large, ResponseFormat responseFormat = Images.ResponseFormat.Url, string user = null)
protected AbstractBaseImageRequest(Model model = null, int numberOfResults = 1, ImageSize size = ImageSize.Large, ResponseFormat responseFormat = ResponseFormat.Url, string user = null)
{
Model = string.IsNullOrWhiteSpace(model?.Id) ? Models.Model.DallE_2 : model;
Number = numberOfResults;
Expand Down
2 changes: 1 addition & 1 deletion Runtime/Images/ImageEditRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public sealed class ImageEditRequest : AbstractBaseImageRequest, IDisposable
/// <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"/>
/// <para/> Defaults to <see cref="ResponseFormat.Url"/>
/// </param>
/// <param name="model">
/// The model to use for image generation.
Expand Down
5 changes: 5 additions & 0 deletions Runtime/Images/ImagesEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Unity.Collections;
using UnityEngine;
using Utilities.Async;
using Utilities.WebRequestRest;
Expand Down Expand Up @@ -106,6 +107,7 @@ public async Task<IReadOnlyDictionary<string, Texture2D>> GenerateImageAsync(Ima
/// <returns>
/// A dictionary of file urls and the preloaded <see cref="Texture2D"/> that were downloaded.
/// </returns>
[Obsolete]
public async Task<IReadOnlyDictionary<string, Texture2D>> CreateImageEditAsync(
string image,
string mask,
Expand Down Expand Up @@ -150,6 +152,7 @@ public async Task<IReadOnlyDictionary<string, Texture2D>> CreateImageEditAsync(
/// <returns>
/// A dictionary of file urls and the preloaded <see cref="Texture2D"/> that were downloaded.
/// </returns>
[Obsolete]
public async Task<IReadOnlyDictionary<string, Texture2D>> CreateImageEditAsync(
Texture2D image,
Texture2D mask,
Expand Down Expand Up @@ -218,6 +221,7 @@ public async Task<IReadOnlyDictionary<string, Texture2D>> CreateImageEditAsync(I
/// </param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
/// <returns>A dictionary of file urls and the preloaded <see cref="Texture2D"/> that were downloaded.</returns>
[Obsolete]
public async Task<IReadOnlyDictionary<string, Texture2D>> CreateImageVariationAsync(
string imagePath,
int numberOfResults = 1,
Expand Down Expand Up @@ -248,6 +252,7 @@ public async Task<IReadOnlyDictionary<string, Texture2D>> CreateImageVariationAs
/// </param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
/// <returns>A dictionary of file urls and the preloaded <see cref="Texture2D"/> that were downloaded.</returns>
[Obsolete]
public async Task<IReadOnlyDictionary<string, Texture2D>> CreateImageVariationAsync(
Texture2D texture,
int numberOfResults = 1,
Expand Down
2 changes: 1 addition & 1 deletion Runtime/OpenAIClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ protected override void ValidateAuthentication()

/// <summary>
/// Files are used to upload documents that can be used with features like Fine-tuning.<br/>
/// <see href="https://platform.openai.com/docs/api-reference/fine-tunes"/>
/// <see href="https://platform.openai.com/docs/api-reference/files"/>
/// </summary>
public FilesEndpoint FilesEndpoint { get; }

Expand Down
23 changes: 15 additions & 8 deletions Tests/TestFixture_05_Images.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

using NUnit.Framework;
using OpenAI.Images;
using OpenAI.Models;
using System;
using System.IO;
using System.Threading.Tasks;
using OpenAI.Models;
using UnityEditor;
using UnityEngine;

Expand Down Expand Up @@ -55,7 +55,8 @@ public async Task Test_3_CreateImageEdit_Path()
Assert.IsNotNull(api.ImagesEndPoint);
var imageAssetPath = AssetDatabase.GUIDToAssetPath("230fd778637d3d84d81355c8c13b1999");
var maskAssetPath = AssetDatabase.GUIDToAssetPath("0be6be2fad590cc47930495d2ca37dd6");
var results = await api.ImagesEndPoint.CreateImageEditAsync(Path.GetFullPath(imageAssetPath), Path.GetFullPath(maskAssetPath), "A sunlit indoor lounge area with a pool containing a flamingo", 1, ImageSize.Small);
var request = new ImageEditRequest(Path.GetFullPath(imageAssetPath), Path.GetFullPath(maskAssetPath), "A sunlit indoor lounge area with a pool containing a flamingo", size: ImageSize.Small);
var results = await api.ImagesEndPoint.CreateImageEditAsync(request);

Assert.IsNotNull(results);
Assert.NotZero(results.Count);
Expand All @@ -76,7 +77,8 @@ public async Task Test_4_CreateImageEdit_Texture()
var image = AssetDatabase.LoadAssetAtPath<Texture2D>(imageAssetPath);
var maskAssetPath = AssetDatabase.GUIDToAssetPath("0be6be2fad590cc47930495d2ca37dd6");
var mask = AssetDatabase.LoadAssetAtPath<Texture2D>(maskAssetPath);
var results = await api.ImagesEndPoint.CreateImageEditAsync(image, mask, "A sunlit indoor lounge area with a pool containing a flamingo", 1, ImageSize.Small);
var request = new ImageEditRequest(image, mask, "A sunlit indoor lounge area with a pool containing a flamingo", size: ImageSize.Small);
var results = await api.ImagesEndPoint.CreateImageEditAsync(request);

Assert.IsNotNull(results);
Assert.NotZero(results.Count);
Expand All @@ -97,7 +99,8 @@ public async Task Test_5_CreateImageEdit_Texture_B64_Json()
var image = AssetDatabase.LoadAssetAtPath<Texture2D>(imageAssetPath);
var maskAssetPath = AssetDatabase.GUIDToAssetPath("0be6be2fad590cc47930495d2ca37dd6");
var mask = AssetDatabase.LoadAssetAtPath<Texture2D>(maskAssetPath);
var results = await api.ImagesEndPoint.CreateImageEditAsync(image, mask, "A sunlit indoor lounge area with a pool containing a flamingo", 1, ImageSize.Small, responseFormat: ResponseFormat.B64_Json);
var request = new ImageEditRequest(image, mask, "A sunlit indoor lounge area with a pool containing a flamingo", size: ImageSize.Small, responseFormat: ResponseFormat.B64_Json);
var results = await api.ImagesEndPoint.CreateImageEditAsync(request);

Assert.IsNotNull(results);
Assert.NotZero(results.Count);
Expand All @@ -116,7 +119,8 @@ public async Task Test_6_CreateImageEdit_MaskAsTransparency()
Assert.IsNotNull(api.ImagesEndPoint);
var maskAssetPath = AssetDatabase.GUIDToAssetPath("0be6be2fad590cc47930495d2ca37dd6");
var mask = AssetDatabase.LoadAssetAtPath<Texture2D>(maskAssetPath);
var results = await api.ImagesEndPoint.CreateImageEditAsync(mask, null, "A sunlit indoor lounge area with a pool containing a flamingo", 1, ImageSize.Small);
var request = new ImageEditRequest(mask, null, "A sunlit indoor lounge area with a pool containing a flamingo", size: ImageSize.Small);
var results = await api.ImagesEndPoint.CreateImageEditAsync(request);

Assert.IsNotNull(results);
Assert.NotZero(results.Count);
Expand All @@ -134,7 +138,8 @@ public async Task Test_7_CreateImageVariation_Path()
var api = new OpenAIClient(OpenAIAuthentication.Default.LoadFromEnvironment());
Assert.IsNotNull(api.ImagesEndPoint);
var imageAssetPath = AssetDatabase.GUIDToAssetPath("230fd778637d3d84d81355c8c13b1999");
var results = await api.ImagesEndPoint.CreateImageVariationAsync(Path.GetFullPath(imageAssetPath), 1, ImageSize.Small);
var request = new ImageVariationRequest(Path.GetFullPath(imageAssetPath), size: ImageSize.Small);
var results = await api.ImagesEndPoint.CreateImageVariationAsync(request);

Assert.IsNotNull(results);
Assert.NotZero(results.Count);
Expand All @@ -153,7 +158,8 @@ public async Task Test_8_CreateImageVariation_Texture()
Assert.IsNotNull(api.ImagesEndPoint);
var imageAssetPath = AssetDatabase.GUIDToAssetPath("230fd778637d3d84d81355c8c13b1999");
var image = AssetDatabase.LoadAssetAtPath<Texture2D>(imageAssetPath);
var results = await api.ImagesEndPoint.CreateImageVariationAsync(image, 1, ImageSize.Small);
var request = new ImageVariationRequest(image, size: ImageSize.Small);
var results = await api.ImagesEndPoint.CreateImageVariationAsync(request);

Assert.IsNotNull(results);
Assert.NotZero(results.Count);
Expand All @@ -172,7 +178,8 @@ public async Task Test_9_CreateImageVariation_Texture_B64_Json()
Assert.IsNotNull(api.ImagesEndPoint);
var imageAssetPath = AssetDatabase.GUIDToAssetPath("230fd778637d3d84d81355c8c13b1999");
var image = AssetDatabase.LoadAssetAtPath<Texture2D>(imageAssetPath);
var results = await api.ImagesEndPoint.CreateImageVariationAsync(image, 1, ImageSize.Small, responseFormat: ResponseFormat.B64_Json);
var request = new ImageVariationRequest(image, size: ImageSize.Small, responseFormat: ResponseFormat.B64_Json);
var results = await api.ImagesEndPoint.CreateImageVariationAsync(request);

Assert.IsNotNull(results);
Assert.NotZero(results.Count);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"displayName": "OpenAI",
"description": "A OpenAI package for the Unity Game Engine to use GPT-4, GPT-3.5, GPT-3 and Dall-E though their RESTful API (currently in beta).\n\nIndependently developed, this is not an official library and I am not affiliated with OpenAI.\n\nAn OpenAI API account is required.",
"keywords": [],
"version": "5.1.0",
"version": "5.1.1",
"unity": "2021.3",
"documentationUrl": "https://github.com/RageAgainstThePixel/com.openai.unity#documentation",
"changelogUrl": "https://github.com/RageAgainstThePixel/com.openai.unity/releases",
Expand Down

0 comments on commit 4ae9838

Please sign in to comment.