Skip to content

Commit 5727164

Browse files
committed
Add WeatherPlugin and update CoreKernelService
- Added WeatherPlugin with GetWeather method using OpenMeteoClient. - Registered WeatherPlugin in _customNativePlugins dictionary. - Updated ChatWithSkSystemPromptTemplate for TextMemoryPlugin.Recall. - Introduced WeatherPluginExtensions for Markdown conversion and weather code descriptions. - Update Sqlite vector store with latest SK docs + image descriptions.
1 parent d05578b commit 5727164

File tree

4 files changed

+139
-1
lines changed

4 files changed

+139
-1
lines changed
0 Bytes
Binary file not shown.

SkPluginLibrary/CoreKernelService.KernelBuilder.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ private Dictionary<string, object> GetCustomNativePlugins()
109109
_customNativePlugins.TryAdd(nameof(KernelChatPlugin), chatWithSkPlugin);
110110
var langchainChatPlugin = new LangchainChatPlugin();
111111
_customNativePlugins.TryAdd(nameof(LangchainChatPlugin), langchainChatPlugin);
112+
var weatherPlugin = new WeatherPlugin();
113+
_customNativePlugins.TryAdd(nameof(WeatherPlugin), weatherPlugin);
112114
return _customNativePlugins;
113115
}
114116

SkPluginLibrary/CoreKernelService.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,8 @@ public static async Task SaveNewSKPdf()
241241
"""
242242
You are a Semantic Kernel Expert and a helpful and friendly Instructor. Use the [Semantic Kernel CONTEXT] below to answer the user's questions.
243243
244-
[Semantic Kernel CONTEXT]
244+
# Semantic Kernel CONTEXT
245+
245246
{{TextMemoryPlugin.Recall input=$input collection=$collection relevance=$relevance limit=$limit}}
246247
247248
""";
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
using System.Text;
2+
using System.Text.Json;
3+
using OpenMeteo;
4+
5+
namespace SkPluginLibrary.Plugins;
6+
7+
public class WeatherPlugin()
8+
{
9+
private OpenMeteoClient _client = new();
10+
11+
[KernelFunction, Description("Gets the current weather for a location.")]
12+
public async Task<string> GetWeather(
13+
[Description(
14+
"Pass US Zipcode, UK Postcode, Canada Postalcode, IP address, Latitude/Longitude (decimal degree) or city name to get current weather for that location")]
15+
string location)
16+
{
17+
location = Uri.EscapeDataString(location);
18+
var weatherData = await _client.QueryAsync(location, new WeatherForecastOptions() { Temperature_Unit = TemperatureUnitType.fahrenheit, Current = CurrentOptions.All, Daily = DailyOptions.All});
19+
var weather = weatherData.ToMarkdown(location);
20+
return weather;
21+
}
22+
}
23+
internal static class WeatherPluginExtensions
24+
{
25+
public static string ToMarkdown(this WeatherForecast weatherForecast, string location)
26+
{
27+
var weather = weatherForecast.Current;
28+
var weatherForecastCurrentUnits = weatherForecast.CurrentUnits!;
29+
var sb = new StringBuilder();
30+
sb.AppendLine($"The weather in {location} is:");
31+
sb.AppendLine($"- Weather Condition: {weather.Weathercode?.WeathercodeDescription()}");
32+
sb.AppendLine($"- Temperature: {weather.Temperature}{weatherForecastCurrentUnits.Temperature}");
33+
sb.AppendLine($"- Feels Like: {weather.Apparent_temperature}{weatherForecastCurrentUnits.Apparent_temperature}");
34+
sb.AppendLine($"- Wind Speed: {weather.Windspeed_10m} {weatherForecastCurrentUnits.Windspeed_10m}");
35+
sb.AppendLine($"- Wind Direction: {weather.Winddirection_10m} {weatherForecastCurrentUnits.Winddirection_10m}");
36+
sb.AppendLine($"- Humidity: {weather.Relativehumidity_2m} {weatherForecastCurrentUnits.Relativehumidity_2m}");
37+
sb.AppendLine($"- Precipitation: {weather.Precipitation} {weatherForecastCurrentUnits.Precipitation}");
38+
sb.AppendLine($"- Pressure: {weather.Pressure_msl} {weatherForecastCurrentUnits.Pressure_msl}");
39+
sb.AppendLine("");
40+
sb.AppendLine($"{weatherForecast.ConvertWeatherForecastToMarkdownTable()}");
41+
return sb.ToString();
42+
}
43+
public static string ConvertWeatherForecastToMarkdownTable(this WeatherForecast forecast)
44+
{
45+
var sb = new StringBuilder();
46+
sb.AppendLine("| Date | Condition | Max Temp (°F) | Min Temp (°F) | Apparent Max Temp (°F) | Apparent Min Temp (°F) | Precipitation (mm) | Wind Speed (mph) | Sunrise | Sunset |");
47+
sb.AppendLine("|------------|---------------|---------------|---------------|------------------------|------------------------|--------------------|------------------|----------|---------|");
48+
49+
for (int i = 0; i < forecast.Daily.Time.Length; i++)
50+
{
51+
sb.AppendLine($"| {forecast.Daily.Time[i]} | {forecast.Daily.Weathercode?[i].WeathercodeDescription()} | {forecast.Daily.Temperature_2m_max?[i]} | {forecast.Daily.Temperature_2m_min?[i]} | {forecast.Daily.Apparent_temperature_max?[i]} | {forecast.Daily.Apparent_temperature_min?[i]} | {forecast.Daily.Precipitation_sum?[i]} | {forecast.Daily.Windspeed_10m_max?[i]} | {(forecast.Daily.Sunrise?[i])?[11..]} | {(forecast.Daily.Sunset?[i])?[11..]} |");
52+
}
53+
54+
return sb.ToString();
55+
}
56+
public static float ToFahrenheit(this float? celsius)
57+
{
58+
if (celsius == null)
59+
return 0;
60+
return celsius.Value * 9 / 5 + 32;
61+
}
62+
63+
public static string WeathercodeDescription(this float weathercode)
64+
{
65+
if (weathercode == null)
66+
return "Invalid weathercode";
67+
var code = (int)weathercode;
68+
return code.WeathercodeDescription();
69+
}
70+
public static string WeathercodeDescription(this int weathercode)
71+
{
72+
//var weathercode = current.Weathercode;
73+
switch (weathercode)
74+
{
75+
case 0:
76+
return "Clear sky";
77+
case 1:
78+
return "Mainly clear";
79+
case 2:
80+
return "Partly cloudy";
81+
case 3:
82+
return "Overcast";
83+
case 45:
84+
return "Fog";
85+
case 48:
86+
return "Depositing rime Fog";
87+
case 51:
88+
return "Light drizzle";
89+
case 53:
90+
return "Moderate drizzle";
91+
case 55:
92+
return "Dense drizzle";
93+
case 56:
94+
return "Light freezing drizzle";
95+
case 57:
96+
return "Dense freezing drizzle";
97+
case 61:
98+
return "Slight rain";
99+
case 63:
100+
return "Moderate rain";
101+
case 65:
102+
return "Heavy rain";
103+
case 66:
104+
return "Light freezing rain";
105+
case 67:
106+
return "Heavy freezing rain";
107+
case 71:
108+
return "Slight snow fall";
109+
case 73:
110+
return "Moderate snow fall";
111+
case 75:
112+
return "Heavy snow fall";
113+
case 77:
114+
return "Snow grains";
115+
case 80:
116+
return "Slight rain showers";
117+
case 81:
118+
return "Moderate rain showers";
119+
case 82:
120+
return "Violent rain showers";
121+
case 85:
122+
return "Slight snow showers";
123+
case 86:
124+
return "Heavy snow showers";
125+
case 95:
126+
return "Thunderstorm";
127+
case 96:
128+
return "Thunderstorm with light hail";
129+
case 99:
130+
return "Thunderstorm with heavy hail";
131+
default:
132+
return "Invalid weathercode";
133+
}
134+
}
135+
}

0 commit comments

Comments
 (0)