From adce2a7fe3879c79e5fb280532e7b4968c760da0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Kna=CC=88pel?= Date: Thu, 9 Mar 2023 08:17:05 +0100 Subject: [PATCH 1/4] #68 IMPLEMENT missing IAPIAuthentication interface --- OpenAI_API/APIAuthentication.cs | 2 +- OpenAI_API/IAPIAuthentication.cs | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 OpenAI_API/IAPIAuthentication.cs diff --git a/OpenAI_API/APIAuthentication.cs b/OpenAI_API/APIAuthentication.cs index 4346281..fe916ad 100644 --- a/OpenAI_API/APIAuthentication.cs +++ b/OpenAI_API/APIAuthentication.cs @@ -9,7 +9,7 @@ namespace OpenAI_API /// /// Represents authentication to the OpenAPI API endpoint /// - public class APIAuthentication + public class APIAuthentication : IAPIAuthentication { /// /// The API key, required to access the API endpoint. diff --git a/OpenAI_API/IAPIAuthentication.cs b/OpenAI_API/IAPIAuthentication.cs new file mode 100644 index 0000000..855365b --- /dev/null +++ b/OpenAI_API/IAPIAuthentication.cs @@ -0,0 +1,26 @@ +using System.Threading.Tasks; + +namespace OpenAI_API +{ + /// + /// Represents authentication to the OpenAPI API endpoint + /// + public interface IAPIAuthentication + { + /// + /// The API key, required to access the API endpoint. + /// + string ApiKey { get; set; } + + /// + /// The Organization ID to count API requests against. This can be found at https://beta.openai.com/account/org-settings. + /// + string OpenAIOrganization { get; set; } + + /// + /// Tests the api key against the OpenAI API, to ensure it is valid. This hits the models endpoint so should not be charged for usage. + /// + /// if the api key is valid, or if empty or not accepted by the OpenAI API. + Task ValidateAPIKey(); + } +} \ No newline at end of file From 13ca016161f701f280a38fbaea46c0a33a7ae793 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Kna=CC=88pel?= Date: Thu, 9 Mar 2023 08:18:12 +0100 Subject: [PATCH 2/4] #68 CHANGE OpenAIAPI by usage of the interfaces of the endpoints --- OpenAI_API/IOpenAIAPI.cs | 10 +++++----- OpenAI_API/OpenAIAPI.cs | 16 ++++++++-------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/OpenAI_API/IOpenAIAPI.cs b/OpenAI_API/IOpenAIAPI.cs index 7b2de67..bcf81ee 100644 --- a/OpenAI_API/IOpenAIAPI.cs +++ b/OpenAI_API/IOpenAIAPI.cs @@ -25,26 +25,26 @@ public interface IOpenAIAPI /// /// The API authentication information to use for API calls /// - APIAuthentication Auth { get; set; } + IAPIAuthentication Auth { get; set; } /// /// 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). /// - CompletionEndpoint Completions { get; } + ICompletionEndpoint Completions { get; } /// /// The API lets you transform text into 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. /// - EmbeddingEndpoint Embeddings { get; } + IEmbeddingEndpoint Embeddings { get; } /// /// The API endpoint for querying available Engines/models /// - ModelsEndpoint Models { get; } + IModelsEndpoint Models { get; } /// /// The API lets you do operations with files. You can upload, delete or retrieve files. Files can be used for fine-tuning, search, etc. /// - FilesEndpoint Files { get; } + IFilesEndpoint Files { get; } } } \ No newline at end of file diff --git a/OpenAI_API/OpenAIAPI.cs b/OpenAI_API/OpenAIAPI.cs index f415410..6159b4e 100644 --- a/OpenAI_API/OpenAIAPI.cs +++ b/OpenAI_API/OpenAIAPI.cs @@ -29,7 +29,7 @@ public class OpenAIAPI : IOpenAIAPI /// /// The API authentication information to use for API calls /// - public APIAuthentication Auth { get; set; } + public IAPIAuthentication Auth { get; set; } /// /// Creates a new entry point to the OpenAPI API, handling auth and allowing access to the various API endpoints @@ -65,37 +65,37 @@ public static OpenAIAPI ForAzure(string YourResourceName, string deploymentId, A /// /// 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 CompletionEndpoint Completions { get; } + public ICompletionEndpoint Completions { get; } /// /// The API lets you transform text into 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 EmbeddingEndpoint Embeddings { get; } + public IEmbeddingEndpoint Embeddings { get; } /// /// Text generation in the form of chat messages. This interacts with the ChatGPT API. /// - public ChatEndpoint Chat { get; } + public IChatEndpoint Chat { get; } /// /// Classify text against the OpenAI Content Policy. /// - public ModerationEndpoint Moderation { get; } + public IModerationEndpoint Moderation { get; } /// /// The API endpoint for querying available Engines/models /// - public ModelsEndpoint Models { get; } + public IModelsEndpoint Models { get; } /// /// The API lets you do operations with files. You can upload, delete or retrieve files. Files can be used for fine-tuning, search, etc. /// - public FilesEndpoint Files { get; } + public IFilesEndpoint Files { get; } /// /// The API lets you do operations with images. You can Given a prompt and/or an input image, the model will generate a new image. /// - public ImageGenerationEndpoint ImageGenerations { get; } + public IImageGenerationEndpoint ImageGenerations { get; } } } From 17763fd30dce80ab64b1940ac697f18e92abb849 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Kna=CC=88pel?= Date: Thu, 9 Mar 2023 08:18:54 +0100 Subject: [PATCH 3/4] #68 EXTEND IOpenAIAPI by the ChatEndpoint --- OpenAI_API/IOpenAIAPI.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/OpenAI_API/IOpenAIAPI.cs b/OpenAI_API/IOpenAIAPI.cs index bcf81ee..6bf1b9d 100644 --- a/OpenAI_API/IOpenAIAPI.cs +++ b/OpenAI_API/IOpenAIAPI.cs @@ -1,3 +1,4 @@ +using OpenAI_API.Chat; using OpenAI_API.Completions; using OpenAI_API.Embedding; using OpenAI_API.Files; @@ -36,6 +37,11 @@ public interface IOpenAIAPI /// The API lets you transform text into 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. /// IEmbeddingEndpoint Embeddings { get; } + + /// + /// Text generation in the form of chat messages. This interacts with the ChatGPT API. + /// + IChatEndpoint Chat { get; } /// /// The API endpoint for querying available Engines/models From 5471771d9286519026921f7db2a89b0c18d3da57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Kna=CC=88pel?= Date: Thu, 9 Mar 2023 08:19:42 +0100 Subject: [PATCH 4/4] #68 TEST AuthTests fixed auth tests --- OpenAI_Tests/AuthTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/OpenAI_Tests/AuthTests.cs b/OpenAI_Tests/AuthTests.cs index e791c81..32da514 100644 --- a/OpenAI_Tests/AuthTests.cs +++ b/OpenAI_Tests/AuthTests.cs @@ -67,14 +67,14 @@ public void testHelper() OpenAI_API.APIAuthentication defaultAuth = OpenAI_API.APIAuthentication.Default; OpenAI_API.APIAuthentication manualAuth = new OpenAI_API.APIAuthentication("pk-testAA"); OpenAI_API.OpenAIAPI api = new OpenAI_API.OpenAIAPI(); - OpenAI_API.APIAuthentication shouldBeDefaultAuth = api.Auth; + OpenAI_API.IAPIAuthentication shouldBeDefaultAuth = api.Auth; Assert.IsNotNull(shouldBeDefaultAuth); Assert.IsNotNull(shouldBeDefaultAuth.ApiKey); Assert.AreEqual(defaultAuth.ApiKey, shouldBeDefaultAuth.ApiKey); OpenAI_API.APIAuthentication.Default = new OpenAI_API.APIAuthentication("pk-testAA"); api = new OpenAI_API.OpenAIAPI(); - OpenAI_API.APIAuthentication shouldBeManualAuth = api.Auth; + OpenAI_API.IAPIAuthentication shouldBeManualAuth = api.Auth; Assert.IsNotNull(shouldBeManualAuth); Assert.IsNotNull(shouldBeManualAuth.ApiKey); Assert.AreEqual(manualAuth.ApiKey, shouldBeManualAuth.ApiKey);