Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#68 OpenApi Endpoint Properties & Missing ChatEndpoint #69

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion OpenAI_API/APIAuthentication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace OpenAI_API
/// <summary>
/// Represents authentication to the OpenAPI API endpoint
/// </summary>
public class APIAuthentication
public class APIAuthentication : IAPIAuthentication
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If APIAuthentication is not meant to be inherited please add the keyword sealed so it's explicitly defined. This comes with some performance benefits: see this PR from the dotnet runtime: dotnet/runtime#49944

{
/// <summary>
/// The API key, required to access the API endpoint.
Expand Down
26 changes: 26 additions & 0 deletions OpenAI_API/IAPIAuthentication.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Threading.Tasks;

namespace OpenAI_API
{
/// <summary>
/// Represents authentication to the OpenAPI API endpoint
/// </summary>
public interface IAPIAuthentication
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion POCO's (Plain old C# objects) shouldn't have an Interface. However this isn't a poco as it's having a method within it to validate the key.

Personally i'd extract the Validate logic to a service which can validate stuff (for now the api-key).
If you want this APIAuthentication to be available through DI, one should look into using the Options Pattern as this is meant to fix that problem.

{
/// <summary>
/// The API key, required to access the API endpoint.
/// </summary>
string ApiKey { get; set; }

/// <summary>
/// The Organization ID to count API requests against. This can be found at https://beta.openai.com/account/org-settings.
/// </summary>
string OpenAIOrganization { get; set; }

/// <summary>
/// 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.
/// </summary>
/// <returns><see langword="true"/> if the api key is valid, or <see langword="false"/> if empty or not accepted by the OpenAI API.</returns>
Task<bool> ValidateAPIKey();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation of this method does a couple of things. Maybe extract that logic to definitive classes which each do their own thing?

Also this method should end in Async since it's an asynchronous operation.
I'd change the name of the method to: IsApiKeyValidAsync(); so it reads nicely in if/switch-statements

}
}
16 changes: 11 additions & 5 deletions OpenAI_API/IOpenAIAPI.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using OpenAI_API.Chat;
using OpenAI_API.Completions;
using OpenAI_API.Embedding;
using OpenAI_API.Files;
Expand Down Expand Up @@ -25,26 +26,31 @@ public interface IOpenAIAPI
/// <summary>
/// The API authentication information to use for API calls
/// </summary>
APIAuthentication Auth { get; set; }
IAPIAuthentication Auth { get; set; }

/// <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>
CompletionEndpoint Completions { get; }
ICompletionEndpoint Completions { get; }

/// <summary>
/// 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.
/// </summary>
EmbeddingEndpoint Embeddings { get; }
IEmbeddingEndpoint Embeddings { get; }

/// <summary>
/// Text generation in the form of chat messages. This interacts with the ChatGPT API.
/// </summary>
IChatEndpoint Chat { get; }

/// <summary>
/// The API endpoint for querying available Engines/models
/// </summary>
ModelsEndpoint Models { get; }
IModelsEndpoint Models { get; }

/// <summary>
/// The API lets you do operations with files. You can upload, delete or retrieve files. Files can be used for fine-tuning, search, etc.
/// </summary>
FilesEndpoint Files { get; }
IFilesEndpoint Files { get; }
}
}
16 changes: 8 additions & 8 deletions OpenAI_API/OpenAIAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class OpenAIAPI : IOpenAIAPI
/// <summary>
/// The API authentication information to use for API calls
/// </summary>
public APIAuthentication Auth { get; set; }
public IAPIAuthentication Auth { get; set; }

/// <summary>
/// Creates a new entry point to the OpenAPI API, handling auth and allowing access to the various API endpoints
Expand Down Expand Up @@ -65,37 +65,37 @@ public static OpenAIAPI ForAzure(string YourResourceName, string deploymentId, A
/// <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 CompletionEndpoint Completions { get; }
public ICompletionEndpoint Completions { get; }

/// <summary>
/// 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.
/// </summary>
public EmbeddingEndpoint Embeddings { get; }
public IEmbeddingEndpoint Embeddings { get; }

/// <summary>
/// Text generation in the form of chat messages. This interacts with the ChatGPT API.
/// </summary>
public ChatEndpoint Chat { get; }
public IChatEndpoint Chat { get; }

/// <summary>
/// Classify text against the OpenAI Content Policy.
/// </summary>
public ModerationEndpoint Moderation { get; }
public IModerationEndpoint Moderation { get; }

/// <summary>
/// The API endpoint for querying available Engines/models
/// </summary>
public ModelsEndpoint Models { get; }
public IModelsEndpoint Models { get; }

/// <summary>
/// The API lets you do operations with files. You can upload, delete or retrieve files. Files can be used for fine-tuning, search, etc.
/// </summary>
public FilesEndpoint Files { get; }
public IFilesEndpoint Files { get; }

/// <summary>
/// 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.
/// </summary>
public ImageGenerationEndpoint ImageGenerations { get; }
public IImageGenerationEndpoint ImageGenerations { get; }

}
}
4 changes: 2 additions & 2 deletions OpenAI_Tests/AuthTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down