Skip to content

Commit

Permalink
Add and update Interfaces (for mock testing, etc) and update version …
Browse files Browse the repository at this point in the history
…to 1.6
  • Loading branch information
OkGoDoIt committed Mar 9, 2023
1 parent 1adcae8 commit 074a9ae
Show file tree
Hide file tree
Showing 13 changed files with 126 additions and 72 deletions.
2 changes: 1 addition & 1 deletion OpenAI_API/Chat/ChatEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace OpenAI_API.Chat
/// <summary>
/// ChatGPT API endpoint. Use this endpoint to send multiple messages and carry on a conversation.
/// </summary>
public class ChatEndpoint : EndpointBase
public class ChatEndpoint : EndpointBase, IChatEndpoint
{
/// <summary>
/// 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.
Expand Down
26 changes: 26 additions & 0 deletions OpenAI_API/Chat/IChatEndpoint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using OpenAI_API.Models;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace OpenAI_API.Chat
{
/// <summary>
/// An interface for <see cref="ChatEndpoint"/>, for ease of mock testing, etc
/// </summary>
public interface IChatEndpoint
{
ChatRequest DefaultChatRequestArgs { get; set; }

Task<ChatResult> CreateChatCompletionAsync(ChatRequest request);
Task<ChatResult> CreateChatCompletionAsync(ChatRequest request, int numOutputs = 5);
Task<ChatResult> CreateChatCompletionAsync(IList<ChatMessage> 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<string, float> logitBias = null, params string[] stopSequences);
Task<ChatResult> CreateChatCompletionAsync(params ChatMessage[] messages);
Task<ChatResult> CreateChatCompletionAsync(params string[] userMessages);
Conversation CreateConversation();
Task StreamChatAsync(ChatRequest request, Action<ChatResult> resultHandler);
IAsyncEnumerable<ChatResult> StreamChatEnumerableAsync(ChatRequest request);
IAsyncEnumerable<ChatResult> StreamChatEnumerableAsync(IList<ChatMessage> 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<string, float> logitBias = null, params string[] stopSequences);
Task StreamCompletionAsync(ChatRequest request, Action<int, ChatResult> resultHandler);
}
}
8 changes: 4 additions & 4 deletions OpenAI_API/Completions/ICompletionEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

namespace OpenAI_API.Completions
{
/// <summary>
/// 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).
/// </summary>
public interface ICompletionEndpoint
/// <summary>
/// An interface for <see cref="CompletionEndpoint"/>, for ease of mock testing, etc
/// </summary>
public interface ICompletionEndpoint
{
/// <summary>
/// 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.
Expand Down
8 changes: 4 additions & 4 deletions OpenAI_API/Embedding/IEmbeddingEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace OpenAI_API.Embedding
{
/// <summary>
/// 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.
/// </summary>
public interface IEmbeddingEndpoint
/// <summary>
/// An interface for <see cref="EmbeddingEndpoint"/>, for ease of mock testing, etc
/// </summary>
public interface IEmbeddingEndpoint
{
/// <summary>
/// This allows you to send request to the recommended model without needing to specify. Every request uses the <see cref="Model.AdaTextEmbedding"/> model
Expand Down
8 changes: 4 additions & 4 deletions OpenAI_API/Files/IFilesEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

namespace OpenAI_API.Files
{
/// <summary>
/// The API endpoint for operations List, Upload, Delete, Retrieve files
/// </summary>
public interface IFilesEndpoint
/// <summary>
/// An interface for <see cref="FilesEndpoint"/>, for ease of mock testing, etc
/// </summary>
public interface IFilesEndpoint
{
/// <summary>
/// Get the list of all files
Expand Down
10 changes: 5 additions & 5 deletions OpenAI_API/IOpenAIAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@

namespace OpenAI_API
{
/// <summary>
/// Entry point to the OpenAPI API, handling auth and allowing access to the various API endpoints
/// </summary>
public interface IOpenAIAPI
/// <summary>
/// An interface for <see cref="OpenAIAPI"/>, for ease of mock testing, etc
/// </summary>
public interface IOpenAIAPI
{
/// <summary>
/// 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}"
/// </summary>
string ApiUrlFormat { get; set; }

Expand Down
13 changes: 13 additions & 0 deletions OpenAI_API/Images/IImageGenerationEndpoint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Threading.Tasks;

namespace OpenAI_API.Images
{
/// <summary>
/// An interface for <see cref="ImageGenerationEndpoint"/>, for ease of mock testing, etc
/// </summary>
public interface IImageGenerationEndpoint
{
Task<ImageResult> CreateImageAsync(ImageGenerationRequest request);
Task<ImageResult> CreateImageAsync(string input);
}
}
6 changes: 3 additions & 3 deletions OpenAI_API/Images/ImageGenerationEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

namespace OpenAI_API.Images
{
/// <summary>
/// <summary>
/// Given a prompt and/or an input image, the model will generate a new image.
/// </summary>
public class ImageGenerationEndpoint : EndpointBase
{
public class ImageGenerationEndpoint : EndpointBase, IImageGenerationEndpoint
{
/// <summary>
/// The name of the endpoint, which is the final path segment in the API URL. For example, "image".
/// </summary>
Expand Down
8 changes: 4 additions & 4 deletions OpenAI_API/Model/IModelsEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

namespace OpenAI_API.Models
{
/// <summary>
/// The API endpoint for querying available models
/// </summary>
public interface IModelsEndpoint
/// <summary>
/// An interface for <see cref="ModelsEndpoint"/>, for ease of mock testing, etc
/// </summary>
public interface IModelsEndpoint
{
/// <summary>
/// Get details about a particular Model from the API, specifically properties such as <see cref="Model.OwnedBy"/> and permissions.
Expand Down
15 changes: 15 additions & 0 deletions OpenAI_API/Moderation/IModerationEndpoint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Threading.Tasks;

namespace OpenAI_API.Moderation
{
/// <summary>
/// An interface for <see cref="ModerationEndpoint"/>, for ease of mock testing, etc
/// </summary>
public interface IModerationEndpoint
{
ModerationRequest DefaultModerationRequestArgs { get; set; }

Task<ModerationResult> CallModerationAsync(ModerationRequest request);
Task<ModerationResult> CallModerationAsync(string input);
}
}
76 changes: 38 additions & 38 deletions OpenAI_API/Moderation/ModerationEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,46 @@

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>
/// This endpoint classifies text against the OpenAI Content Policy
/// </summary>
public class ModerationEndpoint : EndpointBase, IModerationEndpoint
{
/// <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>
/// 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>
/// 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 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);
}
}
/// <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);
}
}
}
12 changes: 6 additions & 6 deletions OpenAI_API/OpenAI_API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>OkGoDoIt (Roger Pincombe)</Authors>
<Product>OpenAI API</Product>
<Description>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.</Description>
<Description>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.</Description>
<Copyright>This library is licensed CC-0, in the public domain</Copyright>
<PackageLicenseExpression>CC0-1.0</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/OkGoDoIt/OpenAI-API-dotnet</PackageProjectUrl>
<RepositoryUrl>https://github.com/OkGoDoIt/OpenAI-API-dotnet</RepositoryUrl>
<PackageTags>OpenAI, AI, ML, API</PackageTags>
<PackageTags>OpenAI, AI, ML, API, ChatGPT, DALLE, GPT3, GPT-3, DALL-E</PackageTags>
<Title>OpenAI API</Title>
<PackageReleaseNotes>
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.
</PackageReleaseNotes>
<PackageId>OpenAI</PackageId>
<Version>1.5</Version>
<AssemblyVersion>1.5.0.0</AssemblyVersion>
<FileVersion>1.5.0.0</FileVersion>
<Version>1.6</Version>
<AssemblyVersion>1.6.0.0</AssemblyVersion>
<FileVersion>1.6.0.0</FileVersion>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<PackageReadmeFile>README.md</PackageReadmeFile>
<ProduceReferenceAssembly>True</ProduceReferenceAssembly>
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 074a9ae

Please sign in to comment.