-
Notifications
You must be signed in to change notification settings - Fork 433
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into feature/chat
- Loading branch information
Showing
14 changed files
with
818 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using OpenAI_API.Models; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace OpenAI_API.Images | ||
{ | ||
/// <summary> | ||
/// Given a prompt and/or an input image, the model will generate a new image. | ||
/// </summary> | ||
public class ImageGenerationEndpoint : EndpointBase | ||
{ | ||
/// <summary> | ||
/// The name of the endpoint, which is the final path segment in the API URL. For example, "image". | ||
/// </summary> | ||
protected override string Endpoint { get { return "images/generations"; } } | ||
|
||
/// <summary> | ||
/// Constructor of the api endpoint. Rather than instantiating this yourself, access it through an instance of <see cref="OpenAIAPI"/> as <see cref="OpenAIAPI.ImageGenerations"/>. | ||
/// </summary> | ||
/// <param name="api"></param> | ||
internal ImageGenerationEndpoint(OpenAIAPI api) : base(api) { } | ||
|
||
/// <summary> | ||
/// Ask the API to Creates an image given a prompt. | ||
/// </summary> | ||
/// <param name="input">A text description of the desired image(s)</param> | ||
/// <returns>Asynchronously returns the image result. Look in its <see cref="Data.Url"/> </returns> | ||
public async Task<ImageResult> CreateImageAsync(string input) | ||
{ | ||
ImageGenerationRequest req = new ImageGenerationRequest(prompt: input); | ||
return await CreateImageAsync(req); | ||
} | ||
|
||
/// <summary> | ||
/// Ask the API to Creates an image given a prompt. | ||
/// </summary> | ||
/// <param name="request">Request to be send</param> | ||
/// <returns>Asynchronously returns the image result. Look in its <see cref="Data.Url"/> </returns> | ||
public async Task<ImageResult> CreateImageAsync(ImageGenerationRequest request) | ||
{ | ||
return await HttpPost<ImageResult>(postData: request); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using Newtonsoft.Json; | ||
using OpenAI_API.Models; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace OpenAI_API.Images | ||
{ | ||
/// <summary> | ||
/// Represents a request to the Images API. Mostly matches the parameters in <see href="https://platform.openai.com/docs/api-reference/images/create">the OpenAI docs</see>, although some have been renames or expanded into single/multiple properties for ease of use. | ||
/// </summary> | ||
public class ImageGenerationRequest | ||
{ | ||
/// <summary> | ||
/// A text description of the desired image(s). The maximum length is 1000 characters. | ||
/// </summary> | ||
[JsonProperty("prompt")] | ||
public string Prompt { get; set; } | ||
|
||
/// <summary> | ||
/// How many different choices to request for each prompt. Defaults to 1. | ||
/// </summary> | ||
[JsonProperty("n")] | ||
public int? NumOfImages { get; set; } = 1; | ||
|
||
/// <summary> | ||
/// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. Optional. | ||
/// </summary> | ||
[JsonProperty("user")] | ||
public string User { get; set; } | ||
|
||
/// <summary> | ||
/// The size of the generated images. Must be one of 256x256, 512x512, or 1024x1024. Defauls to 1024x1024 | ||
/// </summary> | ||
[JsonProperty("size"), JsonConverter(typeof(ImageSize.ImageSizeJsonConverter))] | ||
public ImageSize Size { get; set; } | ||
|
||
/// <summary> | ||
/// The format in which the generated images are returned. Must be one of url or b64_json. Defaults to Url. | ||
/// </summary> | ||
[JsonProperty("response_format"), JsonConverter(typeof(ImageResponseFormat.ImageResponseJsonConverter))] | ||
public ImageResponseFormat ResponseFormat { get; set; } | ||
|
||
/// <summary> | ||
/// Cretes a new, empty <see cref="ImageGenerationRequest"/> | ||
/// </summary> | ||
public ImageGenerationRequest() | ||
{ | ||
|
||
} | ||
|
||
/// <summary> | ||
/// Creates a new <see cref="ImageGenerationRequest"/> with the specified parameters | ||
/// </summary> | ||
/// <param name="prompt">A text description of the desired image(s). The maximum length is 1000 characters.</param> | ||
/// <param name="numOfImages">How many different choices to request for each prompt. Defaults to 1.</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.</param> | ||
public ImageGenerationRequest( | ||
string prompt, | ||
int? numOfImages = 1, | ||
ImageSize size = null, | ||
string user = null, | ||
ImageResponseFormat responseFormat = null) | ||
{ | ||
this.Prompt = prompt; | ||
this.NumOfImages = numOfImages; | ||
this.User = user; | ||
this.Size = size ?? ImageSize._1024; | ||
this.ResponseFormat = responseFormat ?? ImageResponseFormat.Url; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using Newtonsoft.Json; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace OpenAI_API.Images | ||
{ | ||
/// <summary> | ||
/// Represents available response formats for image generation endpoints | ||
/// </summary> | ||
public class ImageResponseFormat | ||
{ | ||
private ImageResponseFormat(string value) { Value = value; } | ||
|
||
private string Value { get; set; } | ||
|
||
/// <summary> | ||
/// Requests an image that is 256x256 | ||
/// </summary> | ||
public static ImageResponseFormat Url { get { return new ImageResponseFormat("url"); } } | ||
/// <summary> | ||
/// Requests an image that is 512x512 | ||
/// </summary> | ||
public static ImageResponseFormat B64_json { get { return new ImageResponseFormat("b64_json"); } } | ||
|
||
|
||
/// <summary> | ||
/// Gets the string value for this response format to pass to the API | ||
/// </summary> | ||
/// <returns>The response format as a string</returns> | ||
public override string ToString() | ||
{ | ||
return Value; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the string value for this response format to pass to the API | ||
/// </summary> | ||
/// <param name="value">The ImageResponseFormat to convert</param> | ||
public static implicit operator String(ImageResponseFormat value) { return value; } | ||
|
||
internal class ImageResponseJsonConverter : JsonConverter<ImageResponseFormat> | ||
{ | ||
public override ImageResponseFormat ReadJson(JsonReader reader, Type objectType, ImageResponseFormat existingValue, bool hasExistingValue, JsonSerializer serializer) | ||
{ | ||
return new ImageResponseFormat(reader.ReadAsString()); | ||
} | ||
|
||
public override void WriteJson(JsonWriter writer, ImageResponseFormat value, JsonSerializer serializer) | ||
{ | ||
writer.WriteValue(value.ToString()); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using Newtonsoft.Json; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace OpenAI_API.Images | ||
{ | ||
/// <summary> | ||
/// Represents an image result returned by the Image API. | ||
/// </summary> | ||
public class ImageResult : ApiResultBase | ||
{ | ||
/// <summary> | ||
/// List of results of the embedding | ||
/// </summary> | ||
[JsonProperty("data")] | ||
public List<Data> Data { get; set; } | ||
|
||
/// <summary> | ||
/// Gets the url or base64-encoded image data of the first result, or null if there are no results | ||
/// </summary> | ||
/// <returns></returns> | ||
public override string ToString() | ||
{ | ||
if (Data?.Count > 0) | ||
{ | ||
return Data[0].Url ?? Data[0].Base64Data; | ||
} | ||
else | ||
{ | ||
return null; | ||
} | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Data returned from the Image API. | ||
/// </summary> | ||
public class Data | ||
{ | ||
/// <summary> | ||
/// The url of the image result | ||
/// </summary> | ||
[JsonProperty("url")] | ||
|
||
public string Url { get; set; } | ||
|
||
/// <summary> | ||
/// The base64-encoded image data as returned by the API | ||
/// </summary> | ||
[JsonProperty("b64_json")] | ||
public string Base64Data { get; set; } | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using Newtonsoft.Json; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace OpenAI_API.Images | ||
{ | ||
/// <summary> | ||
/// Represents available sizes for image generation endpoints | ||
/// </summary> | ||
public class ImageSize | ||
{ | ||
private ImageSize(string value) { Value = value; } | ||
|
||
private string Value { get; set; } | ||
|
||
/// <summary> | ||
/// Requests an image that is 256x256 | ||
/// </summary> | ||
public static ImageSize _256 { get { return new ImageSize("256x256"); } } | ||
/// <summary> | ||
/// Requests an image that is 512x512 | ||
/// </summary> | ||
public static ImageSize _512 { get { return new ImageSize("512x512"); } } | ||
/// <summary> | ||
/// Requests and image that is 1024x1024 | ||
/// </summary> | ||
public static ImageSize _1024 { get { return new ImageSize("1024x1024"); } } | ||
|
||
/// <summary> | ||
/// Gets the string value for this size to pass to the API | ||
/// </summary> | ||
/// <returns>The size as a string</returns> | ||
public override string ToString() | ||
{ | ||
return Value; | ||
} | ||
|
||
|
||
|
||
/// <summary> | ||
/// Gets the string value for this size to pass to the API | ||
/// </summary> | ||
/// <param name="value">The ImageSize to convert</param> | ||
public static implicit operator String(ImageSize value) { return value; } | ||
|
||
internal class ImageSizeJsonConverter : JsonConverter<ImageSize> | ||
{ | ||
public override void WriteJson(JsonWriter writer, ImageSize value, JsonSerializer serializer) | ||
{ | ||
writer.WriteValue(value.ToString()); | ||
} | ||
|
||
public override ImageSize ReadJson(JsonReader reader, Type objectType, ImageSize existingValue, bool hasExistingValue, JsonSerializer serializer) | ||
{ | ||
return new ImageSize(reader.ReadAsString()); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using OpenAI_API.Models; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace OpenAI_API.Moderation | ||
{ | ||
/// <summary> | ||
/// This endpoint classifies text against the OpenAI Content Policy | ||
/// </summary> | ||
public class ModerationEndpoint : EndpointBase | ||
{ | ||
/// <summary> | ||
/// This allows you to send request to the recommended model without needing to specify. OpenAI recommends using the <see cref="Model.TextModerationLatest"/> model | ||
/// </summary> | ||
public ModerationRequest DefaultModerationRequestArgs { get; set; } = new ModerationRequest() { Model = Model.TextModerationLatest }; | ||
|
||
/// <summary> | ||
/// The name of the endpoint, which is the final path segment in the API URL. For example, "completions". | ||
/// </summary> | ||
protected override string Endpoint { get { return "moderations"; } } | ||
|
||
/// <summary> | ||
/// Constructor of the api endpoint. Rather than instantiating this yourself, access it through an instance of <see cref="OpenAIAPI"/> as <see cref="OpenAIAPI.Moderation"/>. | ||
/// </summary> | ||
/// <param name="api"></param> | ||
internal ModerationEndpoint(OpenAIAPI api) : base(api) { } | ||
|
||
/// <summary> | ||
/// Ask the API to classify the text using the default model. | ||
/// </summary> | ||
/// <param name="input">Text to classify</param> | ||
/// <returns>Asynchronously returns the classification result</returns> | ||
public async Task<ModerationResult> CallModerationAsync(string input) | ||
{ | ||
ModerationRequest req = new ModerationRequest(DefaultModerationRequestArgs.Model, input); | ||
return await CallModerationAsync(req); | ||
} | ||
|
||
/// <summary> | ||
/// Ask the API to classify the text using a custom request. | ||
/// </summary> | ||
/// <param name="request">Request to send to the API</param> | ||
/// <returns>Asynchronously returns the classification result</returns> | ||
public async Task<ModerationResult> CallModerationAsync(ModerationRequest request) | ||
{ | ||
return await HttpPost<ModerationResult>(postData: request); | ||
} | ||
} | ||
} |
Oops, something went wrong.