forked from Azure-Samples/cosmosdb-chatgpt
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathProgram.cs
78 lines (68 loc) · 2.61 KB
/
Program.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using Cosmos.Chat.GPT.Options;
using Cosmos.Chat.GPT.Services;
using Microsoft.Extensions.Options;
var builder = WebApplication.CreateBuilder(args);
builder.RegisterConfiguration();
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.RegisterServices();
var app = builder.Build();
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.MapBlazorHub();
app.MapFallbackToPage("/_Host");
await app.RunAsync();
static class ProgramExtensions
{
public static void RegisterConfiguration(this WebApplicationBuilder builder)
{
builder.Services.AddOptions<CosmosDb>()
.Bind(builder.Configuration.GetSection(nameof(CosmosDb)));
builder.Services.AddOptions<OpenAi>()
.Bind(builder.Configuration.GetSection(nameof(OpenAi)));
}
public static void RegisterServices(this IServiceCollection services)
{
services.AddSingleton<CosmosDbService, CosmosDbService>((provider) =>
{
var cosmosDbOptions = provider.GetRequiredService<IOptions<CosmosDb>>();
if (cosmosDbOptions is null)
{
throw new ArgumentException($"{nameof(IOptions<CosmosDb>)} was not resolved through dependency injection.");
}
else
{
return new CosmosDbService(
endpoint: cosmosDbOptions.Value?.Endpoint ?? String.Empty,
key: cosmosDbOptions.Value?.Key ?? String.Empty,
databaseName: cosmosDbOptions.Value?.Database ?? String.Empty,
containerName: cosmosDbOptions.Value?.Container ?? String.Empty
);
}
});
services.AddSingleton<OpenAiService, OpenAiService>((provider) =>
{
var openAiOptions = provider.GetRequiredService<IOptions<OpenAi>>();
if (openAiOptions is null)
{
throw new ArgumentException($"{nameof(IOptions<OpenAi>)} was not resolved through dependency injection.");
}
else
{
return new OpenAiService(
endpoint: openAiOptions.Value?.Endpoint ?? String.Empty,
key: openAiOptions.Value?.Key ?? String.Empty,
deploymentName: openAiOptions.Value?.Deployment ?? String.Empty,
maxConversationTokens: openAiOptions.Value?.MaxConversationTokens ?? String.Empty
);
}
});
services.AddSingleton<ChatService>();
}
}