Skip to content

Instantly share code, notes, and snippets.

@rippo
Last active December 13, 2024 13:19
Show Gist options
  • Save rippo/00de775cdc27d1161a0b75337cb5afc5 to your computer and use it in GitHub Desktop.
Save rippo/00de775cdc27d1161a0b75337cb5afc5 to your computer and use it in GitHub Desktop.
Auto Function Calling execution result: 
The current UTC time is Friday, December 13th, 2024 at 13:10:42 GMT. 
The weather in Boston is currently 61 degrees Fahrenheit with rain.

using System.ComponentModel;
using Microsoft.SemanticKernel;
public sealed class TimePlugin
{
[KernelFunction]
[Description("Retrieves the current time in UTC")]
public string GetCurrentUtcTime() => DateTime.UtcNow.ToString("R");
}
public sealed class WeatherPlugin
{
[KernelFunction]
[Description("Gets the current weather for the specified city")]
public string GetWeather(string cityName) =>
cityName switch
{
"Boston" => "61 and rainy",
"London" => "55 and cloudy",
"Miami" => "80 and sunny",
"Paris" => "60 and rainy",
"Tokyo" => "50 and sunny",
"Sydney" => "75 and sunny",
"Tel Aviv" => "80 and sunny",
_ => "31 and snowing",
};
}
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.OpenAI;
namespace OllamaSemanticKernelConnector;
public class Test3
{
public async Task Go()
{
// Get kernel
var kernel = GetKernel();
// Enable Automatic Function Calling
OpenAIPromptExecutionSettings executionSettings = new()
{ FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() };
// Generate and execute a plan
var result =
await kernel.InvokePromptAsync("Check current UTC time and return current weather in Boston city.",
new KernelArguments(executionSettings));
Console.WriteLine($"Auto Function Calling execution result: {result}");
}
private Kernel GetKernel()
{
var kernel = Kernel
.CreateBuilder()
.AddOllamaChatCompletion("llama3.1", new Uri("http://localhost:11434"))
//.AddOllamaChatCompletion("llama3.2", new Uri("http://localhost:11434"))
.Build();
// Import sample plugins.
kernel.ImportPluginFromType<TimePlugin>();
kernel.ImportPluginFromType<WeatherPlugin>();
return kernel;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment