From e66551753485b0e993da36a81aaeedba0d3a03b0 Mon Sep 17 00:00:00 2001 From: Vin Kamat <592952+VinKamat@users.noreply.github.com> Date: Sat, 17 May 2025 09:37:43 -0500 Subject: [PATCH] Add WeatherApi URL config instructions --- Apps/WadeTheMeteorologist.cs | 4 ++-- Core/WeatherService.cs | 13 +++++++------ Program.cs | 6 +++++- README.md | 7 ++++--- appsettings.json | 5 +++++ 5 files changed, 23 insertions(+), 12 deletions(-) create mode 100644 appsettings.json diff --git a/Apps/WadeTheMeteorologist.cs b/Apps/WadeTheMeteorologist.cs index 639d860..eab7ec7 100644 --- a/Apps/WadeTheMeteorologist.cs +++ b/Apps/WadeTheMeteorologist.cs @@ -12,13 +12,13 @@ namespace GenAI.Reference.Samples.Apps { internal class WadeTheMeteorologist { - internal static async Task Execute(string weatherApiKey, Kernel kernel) + internal static async Task Execute(string weatherApiKey, string weatherApiUrl, Kernel kernel) { var rule = new Rule("[bold white on green4]:: Wade - the AI Meteorlogist ::[/]"); AnsiConsole.Write(rule); Console.WriteLine(); - var weatherService = new WeatherService(weatherApiKey); + var weatherService = new WeatherService(weatherApiKey, weatherApiUrl); kernel.ImportPluginFromObject(weatherService); var settings = new OpenAIPromptExecutionSettings() { ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions }; diff --git a/Core/WeatherService.cs b/Core/WeatherService.cs index ab9a012..5364472 100644 --- a/Core/WeatherService.cs +++ b/Core/WeatherService.cs @@ -1,4 +1,4 @@ -using Microsoft.SemanticKernel; +using Microsoft.SemanticKernel; namespace SKAgentApp.Core { @@ -7,19 +7,20 @@ namespace SKAgentApp.Core /// internal class WeatherService { - private string _weatherApiKey; + private readonly string _weatherApiKey; + private readonly string _weatherApiUrl; - public WeatherService(string weatherApiKey) + public WeatherService(string weatherApiKey, string weatherApiUrl) { - this._weatherApiKey = weatherApiKey; + _weatherApiKey = weatherApiKey; + _weatherApiUrl = weatherApiUrl; } // Add a method a kernel function to get detailed weather report data from weatherapi.com [KernelFunction] public async Task GetWeather(string location) { - // TODO: api url should be in App Settings and retrieved from there via Configuration - string apiUrl = $"https://api.weatherapi.com/v1/current.json?key={_weatherApiKey}&q={location}&aqi=no"; + string apiUrl = $"{_weatherApiUrl}?key={_weatherApiKey}&q={location}&aqi=no"; using (var client = new HttpClient()) { HttpResponseMessage response = await client.GetAsync(apiUrl); diff --git a/Program.cs b/Program.cs index c1d62fa..96f8d10 100644 --- a/Program.cs +++ b/Program.cs @@ -4,9 +4,13 @@ using Microsoft.SemanticKernel; var builder = new ConfigurationBuilder() + .AddJsonFile("appsettings.json", optional: true) .AddUserSecrets(); var configuration = builder.Build(); +string weatherApiUrl = configuration["WeatherApi:Url"] + ?? throw new ArgumentNullException(nameof(weatherApiUrl), "The WeatherApi Url is not configured."); + string weatherApiKey = configuration["AI:WeatherApi:ApiKey"] ?? throw new ArgumentNullException(nameof(weatherApiKey), "The weatherApi Key is not set as a user secret."); @@ -25,7 +29,7 @@ Kernel kernel = kernelBuilder.Build(); // #1 Wade, the Meteorologist -// await WadeTheMeteorologist.Execute(weatherApiKey, kernel); +// await WadeTheMeteorologist.Execute(weatherApiKey, weatherApiUrl, kernel); // #2 TaxImpactAnalyzer - AgentGroupChat await TaxImpactAnalyzer.Execute(kernel); diff --git a/README.md b/README.md index 44de0c3..e09999f 100644 --- a/README.md +++ b/README.md @@ -30,12 +30,13 @@ GenAI.Reference.Samples has 2 samples: ### 2. Configure User Secrets -Add your Azure OpenAI API Key and Endpoint to the user secrets: +Add your Azure OpenAI API Key, Endpoint, and Weather API settings to the user secrets: ```bash -dotnet user-secrets set "AI:EASub:AzureOpenAI:Endpoint" "" +dotnet user-secrets set "AI:EASub:AzureOpenAI:Endpoint" "" dotnet user-secrets set "AI:EASub:AzureOpenAI:ApiKey" "" dotnet user-secrets set "AI:WeatherApi:ApiKey" "" +dotnet user-secrets set "WeatherApi:Url" "https://api.weatherapi.com/v1/current.json" ``` @@ -96,4 +97,4 @@ Contains the SP (System Prompt) instructions for the `TaxImpactAnalyzerAgent`. T Contains the SP (System Prompt) instructions for the `TaxImpactReviewerAgent`. This agent reviews the Tax Impact Analysis provided by the `TaxImpactAnalyzerAgent` and asks the user key questions to provide a recommendation. ### WeatherService.cs -Provides weather data from weatherapi.com using Semantic Kernel's KernelFunction invokation \ No newline at end of file +Provides weather data from weatherapi.com using Semantic Kernel's KernelFunction invokation diff --git a/appsettings.json b/appsettings.json new file mode 100644 index 0000000..a314698 --- /dev/null +++ b/appsettings.json @@ -0,0 +1,5 @@ +{ + "WeatherApi": { + "Url": "https://api.weatherapi.com/v1/current.json" + } +}