-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
||
using Newtonsoft.Json; | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using UnityEngine; | ||
using Utilities.WebRequestRest; | ||
|
||
namespace OpenAI.Images | ||
{ | ||
/// <summary> | ||
/// Creates an image given a prompt. | ||
/// </summary> | ||
public class ImageGenerationEndpoint : BaseEndPoint | ||
{ | ||
/// <inheritdoc /> | ||
internal ImageGenerationEndpoint(OpenAIClient api) : base(api) { } | ||
|
||
/// <inheritdoc /> | ||
protected override string GetEndpoint(Engine engine = null) => $"{Api.BaseUrl}images/generations"; | ||
|
||
/// <summary> | ||
/// Creates an image given a prompt. | ||
/// </summary> | ||
/// <param name="prompt"></param> | ||
/// <param name="numberOfResults"></param> | ||
/// <param name="size"></param> | ||
/// <returns>An array of generated textures.</returns> | ||
public async Task<IReadOnlyList<Texture2D>> GenerateImageAsync(string prompt, int numberOfResults = 1, ImageSize size = ImageSize.Large) | ||
=> await GenerateImageAsync(new ImageGenerationRequest(prompt, numberOfResults, size)); | ||
|
||
/// <summary> | ||
/// Creates an image given a prompt. | ||
/// </summary> | ||
/// <param name="request"><see cref="ImageGenerationRequest"/></param> | ||
/// <returns>An array of generated textures.</returns> | ||
/// <exception cref="HttpRequestException"></exception> | ||
public async Task<IReadOnlyList<Texture2D>> GenerateImageAsync(ImageGenerationRequest request) | ||
{ | ||
var jsonContent = JsonConvert.SerializeObject(request, Api.JsonSerializationOptions); | ||
var response = await Api.Client.PostAsync(GetEndpoint(), jsonContent.ToJsonStringContent()); | ||
|
||
if (response.IsSuccessStatusCode) | ||
{ | ||
var resultAsString = await response.Content.ReadAsStringAsync(); | ||
var imageGenerationResponse = JsonConvert.DeserializeObject<ImageGenerationResponse>(resultAsString, Api.JsonSerializationOptions); | ||
|
||
if (imageGenerationResponse?.Data == null || imageGenerationResponse.Data.Count == 0) | ||
{ | ||
throw new HttpRequestException($"{nameof(GenerateImageAsync)} returned no results! HTTP status code: {response.StatusCode}. Response body: {resultAsString}"); | ||
} | ||
|
||
imageGenerationResponse.SetResponseData(response.Headers); | ||
|
||
var images = new List<Texture2D>(imageGenerationResponse.Data.Count); | ||
|
||
foreach (var imageResult in imageGenerationResponse.Data) | ||
{ | ||
images.Add(await Rest.DownloadTextureAsync(imageResult.Url)); | ||
} | ||
|
||
return images; | ||
} | ||
|
||
throw new HttpRequestException($"{nameof(GenerateImageAsync)} Failed! HTTP status code: {response.StatusCode}. Request body: {jsonContent}"); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
||
using System; | ||
using Newtonsoft.Json; | ||
|
||
namespace OpenAI.Images | ||
{ | ||
/// <summary> | ||
/// Creates an image given a prompt. | ||
/// </summary> | ||
public sealed class ImageGenerationRequest | ||
{ | ||
/// <summary> | ||
/// Constructor. | ||
/// </summary> | ||
/// <param name="prompt"> | ||
/// A text description of the desired image(s). The maximum length is 1000 characters. | ||
/// </param> | ||
/// <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. | ||
/// </param> | ||
/// <exception cref="ArgumentOutOfRangeException"></exception> | ||
public ImageGenerationRequest(string prompt, int numberOfResults, ImageSize size) | ||
{ | ||
if (prompt.Length > 1000) | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(prompt), "The maximum character length for the prompt is 1000 characters."); | ||
} | ||
|
||
Prompt = prompt; | ||
|
||
if (numberOfResults is > 10 or < 1) | ||
{ | ||
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) | ||
}; | ||
} | ||
|
||
/// <summary> | ||
/// A text description of the desired image(s). The maximum length is 1000 characters. | ||
/// </summary> | ||
[JsonProperty("prompt")] | ||
public string Prompt { get; } | ||
|
||
/// <summary> | ||
/// The number of images to generate. Must be between 1 and 10. | ||
/// </summary> | ||
[JsonProperty("n")] | ||
public int Number { get; } | ||
|
||
/// <summary> | ||
/// The size of the generated images. | ||
/// </summary> | ||
[JsonProperty("size")] | ||
public string Size { get; } | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
||
using System.Collections.Generic; | ||
using Newtonsoft.Json; | ||
|
||
namespace OpenAI.Images | ||
{ | ||
internal class ImageGenerationResponse : BaseResponse | ||
{ | ||
[JsonProperty("created")] | ||
public int Created { get; set; } | ||
|
||
[JsonProperty("data")] | ||
public List<ImageResult> Data { get; set; } | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
||
using Newtonsoft.Json; | ||
|
||
namespace OpenAI.Images | ||
{ | ||
internal class ImageResult | ||
{ | ||
[JsonProperty("url")] | ||
public string Url { get; set; } | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
||
namespace OpenAI.Images | ||
{ | ||
public enum ImageSize | ||
{ | ||
/// <summary> | ||
/// 256x256 | ||
/// </summary> | ||
Small, | ||
/// <summary> | ||
/// 512x512 | ||
/// </summary> | ||
Medium, | ||
/// <summary> | ||
/// 1024x1024 | ||
/// </summary> | ||
Large, | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,16 @@ | ||
{ | ||
"name": "OpenAI" | ||
} | ||
"name": "OpenAI", | ||
"rootNamespace": "", | ||
"references": [ | ||
"GUID:7958db66189566541a6363568aee1575" | ||
], | ||
"includePlatforms": [], | ||
"excludePlatforms": [], | ||
"allowUnsafeCode": false, | ||
"overrideReferences": false, | ||
"precompiledReferences": [], | ||
"autoReferenced": true, | ||
"defineConstraints": [], | ||
"versionDefines": [], | ||
"noEngineReferences": false | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
||
using NUnit.Framework; | ||
using OpenAI.Images; | ||
using System.Collections; | ||
using UnityEngine.TestTools; | ||
|
||
namespace OpenAI.Tests | ||
{ | ||
internal class ImagesTestFixture | ||
{ | ||
[UnityTest] | ||
public IEnumerator Test_1_GenerateImage() | ||
{ | ||
yield return AwaitTestUtilities.Await(async () => | ||
{ | ||
var api = new OpenAIClient(); | ||
var results = await api.ImageGenerationEndPoint.GenerateImageAsync("A house riding a velociraptor", 1, ImageSize.Small); | ||
Assert.IsNotNull(results); | ||
Assert.NotZero(results.Count); | ||
}); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.