Skip to content

Commit

Permalink
fixed IConfiguration
Browse files Browse the repository at this point in the history
  • Loading branch information
danzuep committed Dec 5, 2023
1 parent 85698e0 commit 72ad315
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 44 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/_version.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,8 @@ jobs:
run: |
echo "Project name: $projectName"
shopt -s globstar
projects=""
for project in ./**/*.csproj; do
echo "Project file: $project"
echo "projects+=::$project" >> $GITHUB_ENV
done
# https://github.com/GitTools/actions/blob/main/docs/examples/github/gitversion/setup/usage-examples.md#example-1
Expand Down
2 changes: 1 addition & 1 deletion LocalGuideAI/Abstractions/IChatGptClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ namespace LocalGuideAI.Abstractions
{
internal interface IChatGptClientFactory
{
OpenAIClient Create(string? apiKey = null, string? proxyUrl = "https://aoai.hacktogether.net");
Task<OpenAIClient> CreateAsync();
}
}
22 changes: 17 additions & 5 deletions LocalGuideAI/MauiProgram.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
using Azure.AI.OpenAI;
using Azure;
using CommunityToolkit.Maui;
using CommunityToolkit.Maui;
using CommunityToolkit.Mvvm.DependencyInjection;
using LocalGuideAI.Abstractions;
using LocalGuideAI.Services;
using LocalGuideAI.ViewModels;
using LocalGuideAI.Views;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Maui.Controls;

namespace LocalGuideAI
{
Expand Down Expand Up @@ -43,7 +40,7 @@ public static T GetRequiredService<T>() where T : notnull =>

static IServiceProvider Register(this IServiceCollection services)
{
var configuration = ChatGptClientFactory.BuildConfiguration();
var configuration = BuildConfiguration();

// Services
services.AddSingleton(configuration);
Expand All @@ -60,6 +57,21 @@ static IServiceProvider Register(this IServiceCollection services)
return provider;
}

public static IConfiguration BuildConfiguration(string? prefix = "Azure")
{
var configurationBuilder = new ConfigurationBuilder();
if (DeviceInfo.Platform == DevicePlatform.WinUI)
{
#if WINDOWS
// Add configuration sources
configurationBuilder.AddUserSecrets<App>();
configurationBuilder.AddEnvironmentVariables(prefix);
#endif
}
var configuration = configurationBuilder.Build();
return configuration;
}

static void Register<TView>(this IServiceCollection services)
where TView : class
{
Expand Down
60 changes: 28 additions & 32 deletions LocalGuideAI/Services/ChatGptClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,51 +7,47 @@ namespace LocalGuideAI.Services
{
internal sealed class ChatGptClientFactory : IChatGptClientFactory
{
private readonly IConfiguration _configuration;
private readonly string? _apiKey;
private readonly string? _proxyUrl;

public ChatGptClientFactory(IConfiguration configuration)
{
_configuration = configuration;
(_apiKey, _proxyUrl) = InitializeApiValues(configuration);
}

public static IConfiguration BuildConfiguration(string? prefix = "Azure")
static (string?, string?) InitializeApiValues(IConfiguration configuration, string sectionName = "Azure")
{
var configurationBuilder = new ConfigurationBuilder();
if (DeviceInfo.Platform == DevicePlatform.WinUI)
{
#if WINDOWS
// Add configuration sources
configurationBuilder.AddUserSecrets<App>();
configurationBuilder.AddEnvironmentVariables(prefix);
#endif
}
var configuration = configurationBuilder.Build();
return configuration;
var section = configuration.GetSection(sectionName);
var apiUrl = InitializeProxyUrl(section);
var apiKey = InitializeApiKey(section);
return (apiKey, apiUrl);
}

static string TestApiConfiguration(IConfiguration configuration, string? proxyUrl, string sectionName = "Azure", string apiKey = "ApiKey")
static string? InitializeApiKey(IConfiguration section, string? apiKeyValue = null, string apiKeyName = "ApiKey")
{
var section = configuration.GetSection(sectionName);
if (string.IsNullOrWhiteSpace(section[apiKey]))
{
if (section.GetSection(apiKey).Exists())
{
System.Diagnostics.Debugger.Break();
section[apiKey] = StorageHelper.ApiKey;
}
}
else
{
StorageHelper.ApiKey = section[apiKey];
var apiKey = section[apiKeyName];
if (!string.IsNullOrWhiteSpace(apiKey))
apiKeyValue = apiKey;
if (!string.IsNullOrWhiteSpace(apiKeyValue))
StorageHelper.ApiKey = apiKeyValue;
return apiKeyValue;
}

static string? InitializeProxyUrl(IConfiguration section, string? proxyUrl = "https://aoai.hacktogether.net", string apiUrlName = "ProxyUrl")
{
var apiUrl = section[apiUrlName];
if (!string.IsNullOrWhiteSpace(apiUrl))
proxyUrl = apiUrl;
if (!string.IsNullOrWhiteSpace(proxyUrl))
StorageHelper.ApiUrl = proxyUrl;
}
ArgumentException.ThrowIfNullOrWhiteSpace(section[apiKey], nameof(apiKey));
return section[apiKey]!;
return proxyUrl;
}

public OpenAIClient Create(string? apiKey = null, string? proxyUrl = "https://aoai.hacktogether.net")
public async Task<OpenAIClient> CreateAsync()
{
apiKey ??= TestApiConfiguration(_configuration, proxyUrl);
var apiKey = await StorageHelper.GetKeyAsync() ?? _apiKey;
var proxyUrl = await StorageHelper.GetUrlAsync() ?? _proxyUrl;
ArgumentException.ThrowIfNullOrWhiteSpace(apiKey, nameof(apiKey));
// the full key is appended by "/YOUR-GITHUB-ALIAS"
AzureKeyCredential token = new(apiKey);
// the full url is appended by /v1/api
Expand Down
4 changes: 1 addition & 3 deletions LocalGuideAI/Services/ChatGptRecommendationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ public ChatGptRecommendationService(IChatGptClientFactory chatGptClientFactory)
public async IAsyncEnumerable<string> GetRecommendationAsync(string prompt, [EnumeratorCancellation] CancellationToken cancellationToken = default)
{
var chatCompletionsOptions = GetChatCompletionsOptions(prompt);
var key = await StorageHelper.GetKeyAsync();
var url = await StorageHelper.GetUrlAsync();
var chatGptClient = _chatGptClientFactory.Create(key, url);
var chatGptClient = await _chatGptClientFactory.CreateAsync();
var response = await chatGptClient.GetChatCompletionsStreamingAsync(chatCompletionsOptions, cancellationToken);
await foreach (var fragment in response)
{
Expand Down
1 change: 0 additions & 1 deletion LocalGuideAI/Views/SettingsPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using LocalGuideAI.Models;
using LocalGuideAI.Services;
using System.Windows.Input;

namespace LocalGuideAI.Views
{
Expand Down

0 comments on commit 72ad315

Please sign in to comment.