Files
microsoft--semantic-kernel/dotnet/samples/Demos/OllamaFunctionCalling/Program.cs
T
wehub-resource-sync b957a53def
CodeQL / Analyze (csharp) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:21:23 +08:00

48 lines
1.4 KiB
C#

using System;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.Ollama;
using OllamaFunctionCalling;
var builder = Kernel.CreateBuilder();
var modelId = "llama3.2";
var endpoint = new Uri("http://localhost:11434");
builder.Services.AddOllamaChatCompletion(modelId, endpoint);
builder.Plugins
.AddFromType<MyTimePlugin>()
.AddFromObject(new MyLightPlugin(turnedOn: true))
.AddFromObject(new MyAlarmPlugin("11"));
var kernel = builder.Build();
var chatCompletionService = kernel.GetRequiredService<IChatCompletionService>();
var settings = new OllamaPromptExecutionSettings { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() };
Console.WriteLine("""
Ask questions or give instructions to the copilot such as:
- Change the alarm to 8
- What is the current alarm set?
- Is the light on?
- Turn the light off please.
- Set an alarm for 6:00 am.
""");
Console.Write("> ");
string? input = null;
while ((input = Console.ReadLine()) is not null)
{
Console.WriteLine();
try
{
ChatMessageContent chatResult = await chatCompletionService.GetChatMessageContentAsync(input, settings, kernel);
Console.Write($"\n>>> Result: {chatResult}\n\n> ");
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}\n\n> ");
}
}