From 074a9ae9a506e15c5629f6733bc0ad259eb4d760 Mon Sep 17 00:00:00 2001 From: Roger Date: Wed, 8 Mar 2023 17:14:59 -0800 Subject: [PATCH] Add and update Interfaces (for mock testing, etc) and update version to 1.6 --- OpenAI_API/Chat/ChatEndpoint.cs | 2 +- OpenAI_API/Chat/IChatEndpoint.cs | 26 +++++++ OpenAI_API/Completions/ICompletionEndpoint.cs | 8 +- OpenAI_API/Embedding/IEmbeddingEndpoint.cs | 8 +- OpenAI_API/Files/IFilesEndpoint.cs | 8 +- OpenAI_API/IOpenAIAPI.cs | 10 +-- OpenAI_API/Images/IImageGenerationEndpoint.cs | 13 ++++ OpenAI_API/Images/ImageGenerationEndpoint.cs | 6 +- OpenAI_API/Model/IModelsEndpoint.cs | 8 +- OpenAI_API/Moderation/IModerationEndpoint.cs | 15 ++++ OpenAI_API/Moderation/ModerationEndpoint.cs | 76 +++++++++---------- OpenAI_API/OpenAI_API.csproj | 12 +-- README.md | 6 +- 13 files changed, 126 insertions(+), 72 deletions(-) create mode 100644 OpenAI_API/Chat/IChatEndpoint.cs create mode 100644 OpenAI_API/Images/IImageGenerationEndpoint.cs create mode 100644 OpenAI_API/Moderation/IModerationEndpoint.cs diff --git a/OpenAI_API/Chat/ChatEndpoint.cs b/OpenAI_API/Chat/ChatEndpoint.cs index fca675a..1dee60b 100644 --- a/OpenAI_API/Chat/ChatEndpoint.cs +++ b/OpenAI_API/Chat/ChatEndpoint.cs @@ -11,7 +11,7 @@ namespace OpenAI_API.Chat /// /// ChatGPT API endpoint. Use this endpoint to send multiple messages and carry on a conversation. /// - public class ChatEndpoint : EndpointBase + public class ChatEndpoint : EndpointBase, IChatEndpoint { /// /// This allows you to set default parameters for every request, for example to set a default temperature or max tokens. For every request, if you do not have a parameter set on the request but do have it set here as a default, the request will automatically pick up the default value. diff --git a/OpenAI_API/Chat/IChatEndpoint.cs b/OpenAI_API/Chat/IChatEndpoint.cs new file mode 100644 index 0000000..61c231e --- /dev/null +++ b/OpenAI_API/Chat/IChatEndpoint.cs @@ -0,0 +1,26 @@ +using OpenAI_API.Models; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace OpenAI_API.Chat +{ + /// + /// An interface for , for ease of mock testing, etc + /// + public interface IChatEndpoint + { + ChatRequest DefaultChatRequestArgs { get; set; } + + Task CreateChatCompletionAsync(ChatRequest request); + Task CreateChatCompletionAsync(ChatRequest request, int numOutputs = 5); + Task CreateChatCompletionAsync(IList messages, Model model = null, double? temperature = null, double? top_p = null, int? numOutputs = null, int? max_tokens = null, double? frequencyPenalty = null, double? presencePenalty = null, IReadOnlyDictionary logitBias = null, params string[] stopSequences); + Task CreateChatCompletionAsync(params ChatMessage[] messages); + Task CreateChatCompletionAsync(params string[] userMessages); + Conversation CreateConversation(); + Task StreamChatAsync(ChatRequest request, Action resultHandler); + IAsyncEnumerable StreamChatEnumerableAsync(ChatRequest request); + IAsyncEnumerable StreamChatEnumerableAsync(IList messages, Model model = null, double? temperature = null, double? top_p = null, int? numOutputs = null, int? max_tokens = null, double? frequencyPenalty = null, double? presencePenalty = null, IReadOnlyDictionary logitBias = null, params string[] stopSequences); + Task StreamCompletionAsync(ChatRequest request, Action resultHandler); + } +} \ No newline at end of file diff --git a/OpenAI_API/Completions/ICompletionEndpoint.cs b/OpenAI_API/Completions/ICompletionEndpoint.cs index 40d5417..1f53010 100644 --- a/OpenAI_API/Completions/ICompletionEndpoint.cs +++ b/OpenAI_API/Completions/ICompletionEndpoint.cs @@ -5,10 +5,10 @@ namespace OpenAI_API.Completions { - /// - /// Text generation is the core function of the API. You give the API a prompt, and it generates a completion. The way you “program” the API to do a task is by simply describing the task in plain english or providing a few written examples. This simple approach works for a wide range of use cases, including summarization, translation, grammar correction, question answering, chatbots, composing emails, and much more (see the prompt library for inspiration). - /// - public interface ICompletionEndpoint + /// + /// An interface for , for ease of mock testing, etc + /// + public interface ICompletionEndpoint { /// /// This allows you to set default parameters for every request, for example to set a default temperature or max tokens. For every request, if you do not have a parameter set on the request but do have it set here as a default, the request will automatically pick up the default value. diff --git a/OpenAI_API/Embedding/IEmbeddingEndpoint.cs b/OpenAI_API/Embedding/IEmbeddingEndpoint.cs index 83e228c..4a8d8c9 100644 --- a/OpenAI_API/Embedding/IEmbeddingEndpoint.cs +++ b/OpenAI_API/Embedding/IEmbeddingEndpoint.cs @@ -2,10 +2,10 @@ namespace OpenAI_API.Embedding { - /// - /// OpenAI’s text embeddings measure the relatedness of text strings by generating an embedding, which is a vector (list) of floating point numbers. The distance between two vectors measures their relatedness. Small distances suggest high relatedness and large distances suggest low relatedness. - /// - public interface IEmbeddingEndpoint + /// + /// An interface for , for ease of mock testing, etc + /// + public interface IEmbeddingEndpoint { /// /// This allows you to send request to the recommended model without needing to specify. Every request uses the model diff --git a/OpenAI_API/Files/IFilesEndpoint.cs b/OpenAI_API/Files/IFilesEndpoint.cs index 7235359..cd53fbf 100644 --- a/OpenAI_API/Files/IFilesEndpoint.cs +++ b/OpenAI_API/Files/IFilesEndpoint.cs @@ -3,10 +3,10 @@ namespace OpenAI_API.Files { - /// - /// The API endpoint for operations List, Upload, Delete, Retrieve files - /// - public interface IFilesEndpoint + /// + /// An interface for , for ease of mock testing, etc + /// + public interface IFilesEndpoint { /// /// Get the list of all files diff --git a/OpenAI_API/IOpenAIAPI.cs b/OpenAI_API/IOpenAIAPI.cs index fbcb215..7b2de67 100644 --- a/OpenAI_API/IOpenAIAPI.cs +++ b/OpenAI_API/IOpenAIAPI.cs @@ -5,15 +5,15 @@ namespace OpenAI_API { - /// - /// Entry point to the OpenAPI API, handling auth and allowing access to the various API endpoints - /// - public interface IOpenAIAPI + /// + /// An interface for , for ease of mock testing, etc + /// + public interface IOpenAIAPI { /// /// Base url for OpenAI /// for OpenAI, should be "https://api.openai.com/{0}/{1}" - /// for Azure, should be "https://(your-resource-name).openai.azure.com/openai/deployments/(deployment-id)/{1}?api-version={0}" + /// for Azure, should be "https://(your-resource-name.openai.azure.com/openai/deployments/(deployment-id)/{1}?api-version={0}" /// string ApiUrlFormat { get; set; } diff --git a/OpenAI_API/Images/IImageGenerationEndpoint.cs b/OpenAI_API/Images/IImageGenerationEndpoint.cs new file mode 100644 index 0000000..a1b2421 --- /dev/null +++ b/OpenAI_API/Images/IImageGenerationEndpoint.cs @@ -0,0 +1,13 @@ +using System.Threading.Tasks; + +namespace OpenAI_API.Images +{ + /// + /// An interface for , for ease of mock testing, etc + /// + public interface IImageGenerationEndpoint + { + Task CreateImageAsync(ImageGenerationRequest request); + Task CreateImageAsync(string input); + } +} \ No newline at end of file diff --git a/OpenAI_API/Images/ImageGenerationEndpoint.cs b/OpenAI_API/Images/ImageGenerationEndpoint.cs index e93670f..06d55a2 100644 --- a/OpenAI_API/Images/ImageGenerationEndpoint.cs +++ b/OpenAI_API/Images/ImageGenerationEndpoint.cs @@ -6,11 +6,11 @@ namespace OpenAI_API.Images { - /// + /// /// Given a prompt and/or an input image, the model will generate a new image. /// - public class ImageGenerationEndpoint : EndpointBase - { + public class ImageGenerationEndpoint : EndpointBase, IImageGenerationEndpoint + { /// /// The name of the endpoint, which is the final path segment in the API URL. For example, "image". /// diff --git a/OpenAI_API/Model/IModelsEndpoint.cs b/OpenAI_API/Model/IModelsEndpoint.cs index 83b940b..3eceb61 100644 --- a/OpenAI_API/Model/IModelsEndpoint.cs +++ b/OpenAI_API/Model/IModelsEndpoint.cs @@ -3,10 +3,10 @@ namespace OpenAI_API.Models { - /// - /// The API endpoint for querying available models - /// - public interface IModelsEndpoint + /// + /// An interface for , for ease of mock testing, etc + /// + public interface IModelsEndpoint { /// /// Get details about a particular Model from the API, specifically properties such as and permissions. diff --git a/OpenAI_API/Moderation/IModerationEndpoint.cs b/OpenAI_API/Moderation/IModerationEndpoint.cs new file mode 100644 index 0000000..891b99a --- /dev/null +++ b/OpenAI_API/Moderation/IModerationEndpoint.cs @@ -0,0 +1,15 @@ +using System.Threading.Tasks; + +namespace OpenAI_API.Moderation +{ + /// + /// An interface for , for ease of mock testing, etc + /// + public interface IModerationEndpoint + { + ModerationRequest DefaultModerationRequestArgs { get; set; } + + Task CallModerationAsync(ModerationRequest request); + Task CallModerationAsync(string input); + } +} \ No newline at end of file diff --git a/OpenAI_API/Moderation/ModerationEndpoint.cs b/OpenAI_API/Moderation/ModerationEndpoint.cs index 25e2812..1fe6069 100644 --- a/OpenAI_API/Moderation/ModerationEndpoint.cs +++ b/OpenAI_API/Moderation/ModerationEndpoint.cs @@ -6,46 +6,46 @@ namespace OpenAI_API.Moderation { - /// - /// This endpoint classifies text against the OpenAI Content Policy - /// - public class ModerationEndpoint : EndpointBase - { - /// - /// This allows you to send request to the recommended model without needing to specify. OpenAI recommends using the model - /// - public ModerationRequest DefaultModerationRequestArgs { get; set; } = new ModerationRequest() { Model = Model.TextModerationLatest }; + /// + /// This endpoint classifies text against the OpenAI Content Policy + /// + public class ModerationEndpoint : EndpointBase, IModerationEndpoint + { + /// + /// This allows you to send request to the recommended model without needing to specify. OpenAI recommends using the model + /// + public ModerationRequest DefaultModerationRequestArgs { get; set; } = new ModerationRequest() { Model = Model.TextModerationLatest }; - /// - /// The name of the endpoint, which is the final path segment in the API URL. For example, "completions". - /// - protected override string Endpoint { get { return "moderations"; } } + /// + /// The name of the endpoint, which is the final path segment in the API URL. For example, "completions". + /// + protected override string Endpoint { get { return "moderations"; } } - /// - /// Constructor of the api endpoint. Rather than instantiating this yourself, access it through an instance of as . - /// - /// - internal ModerationEndpoint(OpenAIAPI api) : base(api) { } + /// + /// Constructor of the api endpoint. Rather than instantiating this yourself, access it through an instance of as . + /// + /// + internal ModerationEndpoint(OpenAIAPI api) : base(api) { } - /// - /// Ask the API to classify the text using the default model. - /// - /// Text to classify - /// Asynchronously returns the classification result - public async Task CallModerationAsync(string input) - { - ModerationRequest req = new ModerationRequest(DefaultModerationRequestArgs.Model, input); - return await CallModerationAsync(req); - } + /// + /// Ask the API to classify the text using the default model. + /// + /// Text to classify + /// Asynchronously returns the classification result + public async Task CallModerationAsync(string input) + { + ModerationRequest req = new ModerationRequest(DefaultModerationRequestArgs.Model, input); + return await CallModerationAsync(req); + } - /// - /// Ask the API to classify the text using a custom request. - /// - /// Request to send to the API - /// Asynchronously returns the classification result - public async Task CallModerationAsync(ModerationRequest request) - { - return await HttpPost(postData: request); - } - } + /// + /// Ask the API to classify the text using a custom request. + /// + /// Request to send to the API + /// Asynchronously returns the classification result + public async Task CallModerationAsync(ModerationRequest request) + { + return await HttpPost(postData: request); + } + } } diff --git a/OpenAI_API/OpenAI_API.csproj b/OpenAI_API/OpenAI_API.csproj index 5353690..4846444 100644 --- a/OpenAI_API/OpenAI_API.csproj +++ b/OpenAI_API/OpenAI_API.csproj @@ -6,20 +6,20 @@ true OkGoDoIt (Roger Pincombe) OpenAI API - A simple C# / .NET wrapper library to use with OpenAI's GPT-3 API. Independently developed, this is not an official library and I am not affiliated with OpenAI. An OpenAI or Azure OpenAI account is required. + A simple C# / .NET wrapper library to use with OpenAI's GPT-3 API, as well as ChatGPT, DALL·E, etc. Independently developed, this is not an official library and I am not affiliated with OpenAI. An OpenAI or Azure OpenAI account is required. This library is licensed CC-0, in the public domain CC0-1.0 https://github.com/OkGoDoIt/OpenAI-API-dotnet https://github.com/OkGoDoIt/OpenAI-API-dotnet - OpenAI, AI, ML, API + OpenAI, AI, ML, API, ChatGPT, DALLE, GPT3, GPT-3, DALL-E OpenAI API - Updated to work with the current API as of February 16, 2023. Fixed several minor bugs. Now also should work with the Azure OpenAI Service, although this is untested. + Added support for ChatGPT, DALL·E image generations, and the moderation endpoint. OpenAI - 1.5 - 1.5.0.0 - 1.5.0.0 + 1.6 + 1.6.0.0 + 1.6.0.0 True README.md True diff --git a/README.md b/README.md index 2759aac..e564998 100644 --- a/README.md +++ b/README.md @@ -23,17 +23,17 @@ Console.WriteLine(result); * [Embeddings API](#embeddings) * [Moderation API](#moderation) * [Files API](#files-for-fine-tuning) - * [Image APIs](#images) + * [Image APIs (DALL�E)](#images) * [Azure](#azure) * [Additonal Documentation](#documentation) * [License](#license) ## Status -Added support for ChatGPT, DALL�E 2 image generations and the moderation endpoint. +Added support for ChatGPT, DALL�E 2 image generations, and the moderation endpoint. Now also should work with the Azure OpenAI Service, although this is untested. See the [Azure](#azure) section for further details. -Thank you [@GotMike](https://github.com/gotmike), [@stonelv](https://github.com/stonelv), [@megalon](https://github.com/megalon), [@ncface](https://github.com/ncface), [@KeithHenry](https://github.com/KeithHenry), [@gmilano](https://github.com/gmilano), [@metjuperry](https://github.com/metjuperry), and [@Alexei000](https://github.com/Alexei000) for your contributions! +Thank you [@GotMike](https://github.com/gotmike), [@megalon](https://github.com/megalon), [@stonelv](https://github.com/stonelv), [@ncface](https://github.com/ncface), [@KeithHenry](https://github.com/KeithHenry), [@gmilano](https://github.com/gmilano), [@metjuperry](https://github.com/metjuperry), and [@Alexei000](https://github.com/Alexei000) for your contributions! ## Requirements