-
Notifications
You must be signed in to change notification settings - Fork 430
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
Support Dependency Injection #44
base: master
Are you sure you want to change the base?
Changes from all commits
e6155b4
945dd2a
71f30cc
6c5c05f
964be77
d23797c
0b98925
dafb02e
43370bd
cf46b05
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#nullable enable | ||
namespace OpenAI_API; | ||
using OpenAI_API.Completions; | ||
using OpenAI_API.Embedding; | ||
using OpenAI_API.Files; | ||
using OpenAI_API.Models; | ||
|
||
/// <summary>Entry point to the OpenAPI API, handling auth and allowing access to the various API endpoints</summary> | ||
public interface IOpenAI | ||
{ | ||
/// <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; } | ||
|
||
/// <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; } | ||
|
||
/// <summary>The API endpoint for querying available Engines/models</summary> | ||
public ModelsEndpoint 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; } | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#nullable enable | ||
namespace Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Configuration; | ||
using OpenAI_API; | ||
|
||
public static class OpenAIApiExtensions | ||
{ | ||
/// <summary>Register <see cref="IOpenAI"/> for DI services. Read configuration from appsettings <code>"openAI": { "key": "", "org": "" }</code></summary> | ||
/// <param name="services"></param> | ||
/// <param name="configuration"></param> | ||
/// <returns></returns> | ||
public static IServiceCollection AddOpenAIService(this IServiceCollection services, IConfiguration configuration) | ||
{ | ||
var section = configuration.GetSection("openAI"); | ||
if (!section.Exists()) return services; | ||
|
||
string? key = section["key"]; | ||
if (key is null) return services; | ||
|
||
string? organisation = section["org"]; | ||
return services.AddOpenAIService(new APIAuthentication(key, organisation)); | ||
} | ||
Comment on lines
+12
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With introducing DI one should maybe also think of introducing the Options Pattern. Within your Startup/Program.cs you can then use the @KeithHenry You've made a great starting point but i think this can be improved even more, do you mind if i helped you out here/there? |
||
|
||
public static IServiceCollection AddOpenAIService(this IServiceCollection services, APIAuthentication auth) | ||
{ | ||
services.AddHttpClient<IOpenAI, OpenAIAPI>(client => | ||
EndpointBase.ConfigureClient(client, auth)); | ||
|
||
return services; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An idea to give you the backwards compatibility, but it's somewhat messy - alternate constructors that create an
HttpClient
when called and configure it, but that match the old patterns.These will cause warnings for users upgrading still.