-
Notifications
You must be signed in to change notification settings - Fork 430
/
Copy pathOpenAIApiExtensions.cs
31 lines (26 loc) · 1.14 KB
/
OpenAIApiExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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));
}
public static IServiceCollection AddOpenAIService(this IServiceCollection services, APIAuthentication auth)
{
services.AddHttpClient<IOpenAI, OpenAIAPI>(client =>
EndpointBase.ConfigureClient(client, auth));
return services;
}
}