Skip to content

Commit

Permalink
Merge pull request #14 from mule/development
Browse files Browse the repository at this point in the history
Key vault and blob storage in use
  • Loading branch information
mule authored Jul 20, 2023
2 parents 874ddb3 + c57ded4 commit 6fbfd79
Show file tree
Hide file tree
Showing 17 changed files with 630 additions and 62 deletions.
3 changes: 3 additions & 0 deletions ChatGptBlazorApp/ChatGptBlazorApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Azure.Extensions.AspNetCore.Configuration.Secrets" Version="1.2.2"/>
<PackageReference Include="Azure.Identity" Version="1.9.0"/>
<PackageReference Include="Blazored.Toast" Version="4.1.0"/>
<PackageReference Include="Markdig" Version="0.31.0"/>
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="7.0.8"/>
Expand All @@ -24,6 +26,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="7.0.0"/>
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0"/>
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="7.0.0"/>
<PackageReference Include="Microsoft.Identity.Web" Version="2.12.4"/>
Expand Down
39 changes: 30 additions & 9 deletions ChatGptBlazorApp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO.Abstractions;
using Azure.Identity;
using Azure.Storage.Blobs;
using Blazored.Toast;
using ChatGptBlazorApp.Areas.Identity.Data;
using ChatGptBlazorCore.Models;
Expand All @@ -12,6 +13,7 @@
using OpenAI.ObjectModels;
using Serilog;
using ServiceAccessLayer.AiServices;
using Spectre.Console;

// Configure Serilog logger
Log.Logger = new LoggerConfiguration()
Expand All @@ -24,6 +26,24 @@
try
{
var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddUserSecrets<Program>();
builder.Configuration.AddEnvironmentVariables();


var azureKeyVaultUriStr = builder.Configuration.GetValue<string>("AzureKeyVaultUri");
if (!string.IsNullOrEmpty(azureKeyVaultUriStr))
{
Log.Information("Adding Azure Key Vault to configuration");
var azureKeyVaultUri = new Uri(azureKeyVaultUriStr);
builder.Configuration.AddAzureKeyVault(azureKeyVaultUri, new DefaultAzureCredential());
}

AnsiConsole.WriteLine(builder.Configuration.GetDebugView());

var openAiKey = builder.Configuration["openai-api-key"] ?? builder.Configuration["OpenAIServiceOptions:ApiKey"];
var blobStorageConnectionString = builder.Configuration["BlobStorage:ConnectionString"];
var blobStorageContainerName = builder.Configuration["BlobStorage:ContainerName"];

var connectionString = builder.Configuration.GetConnectionString("ChatGptBlazorAppContextConnection") ??
throw new InvalidOperationException(
"Connection string 'ChatGptBlazorAppContextConnection' not found.");
Expand All @@ -49,7 +69,6 @@
builder.Services.AddDefaultIdentity<ChatGptBlazorAppUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ChatGptBlazorAppContext>();

builder.Configuration.AddUserSecrets<Program>();

builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme);
builder.Services.AddMicrosoftIdentityWebAppAuthentication(builder.Configuration);
Expand All @@ -59,7 +78,7 @@
// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddOpenAIService();
builder.Services.AddOpenAIService(options => { options.ApiKey = openAiKey; });
builder.Services.AddTransient<IOpenAiClient>(provider => new OpenAiClient(
provider.GetService<IOpenAIService>(),
provider.GetService<ILogger<OpenAiClient>>(), Models.Gpt_3_5_Turbo));
Expand All @@ -78,21 +97,23 @@
options.FallbackPolicy = options.DefaultPolicy;
});

var userRepo = new UserFileDb(new FileSystem(), Log.Logger, Path.Combine(dataFilesPath, "usersdb.json"));
var blobContainerClient = new BlobContainerClient(blobStorageConnectionString, blobStorageContainerName);
await blobContainerClient.CreateIfNotExistsAsync();
var userRepo = new UserBlobStorageRepo(blobContainerClient.GetBlobClient("users.json"), Log.Logger);
await userRepo.InitializeAsync(new Dictionary<Guid, User>
{
{ adminUserId, new User(adminUserId, adminUserName, "root") }
});


builder.Services.AddSingleton<IUserRepository, UserFileDb>(ctx => userRepo);
builder.Services.AddSingleton<IUserRepository, UserBlobStorageRepo>(ctx => userRepo);

var chatRepo =
new ChatSessionFileDb(new FileSystem(), Log.Logger, Path.Combine(dataFilesPath, "chatsessionsdb.json"));
await chatRepo.InitializeAsync();
var chatSessionRepo =
new ChatSessionBlobStorageRepository(blobContainerClient.GetBlobClient("chatSessions.json"), Log.Logger);
await chatSessionRepo.InitializeAsync();


builder.Services.AddSingleton<IChatSessionRepository, ChatSessionFileDb>(ctx => chatRepo);
builder.Services.AddSingleton<IChatSessionRepository, ChatSessionBlobStorageRepository>(ctx => chatSessionRepo);

var app = builder.Build();

Expand Down
4 changes: 4 additions & 0 deletions ChatGptBlazorApp/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"BlobStorage": {
"ConnectionString": "DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1;",
"ContainerName": "abotti-db-dev"
}
}
1 change: 0 additions & 1 deletion ChatGptBlazorCore/Repositories/InMemoryUserRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public InMemoryUserRepository(Guid rootUserId, string rootUserName, IDictionary<
Add(rootUser);
}


public async Task<(bool Ok, User? Result, string[] Errors)> GetByNameAsync(string userName)
{
var result = await Task.FromResult(GetByName(userName));
Expand Down
5 changes: 0 additions & 5 deletions ChatGptBlazorCore/Services/UsersService.cs

This file was deleted.

1 change: 1 addition & 0 deletions DataAccessLayer/DataAccessLayer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Azure.Storage.Blobs" Version="12.17.0"/>
<PackageReference Include="Serilog" Version="3.0.1"/>
<PackageReference Include="System.IO.Abstractions" Version="19.2.29"/>
</ItemGroup>
Expand Down
Loading

0 comments on commit 6fbfd79

Please sign in to comment.