chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<NoWarn>$(NoWarn);CA2007,CA2208,CS1591,CA1024,IDE0009,IDE0055,IDE0073,IDE0211,VSTHRD111,SKEXP0001</NoWarn>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Net.Compilers.Toolset" />
|
||||
<ProjectReference Include="..\..\..\src\Connectors\Connectors.Ollama\Connectors.Ollama.csproj" />
|
||||
<ProjectReference Include="..\..\..\src\SemanticKernel.Abstractions\SemanticKernel.Abstractions.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,32 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using System.ComponentModel;
|
||||
using Microsoft.SemanticKernel;
|
||||
|
||||
namespace OllamaFunctionCalling;
|
||||
|
||||
/// <summary>
|
||||
/// Class that represents a controllable alarm.
|
||||
/// </summary>
|
||||
public class MyAlarmPlugin
|
||||
{
|
||||
private string _hour;
|
||||
|
||||
public MyAlarmPlugin(string providedHour)
|
||||
{
|
||||
this._hour = providedHour;
|
||||
}
|
||||
|
||||
[KernelFunction, Description("Sets an alarm at the provided time")]
|
||||
public string SetAlarm(string time)
|
||||
{
|
||||
this._hour = time;
|
||||
return GetCurrentAlarm();
|
||||
}
|
||||
|
||||
[KernelFunction, Description("Get current alarm set")]
|
||||
public string GetCurrentAlarm()
|
||||
{
|
||||
return $"Alarm set for {_hour}";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using System.ComponentModel;
|
||||
using Microsoft.SemanticKernel;
|
||||
|
||||
namespace OllamaFunctionCalling;
|
||||
|
||||
/// <summary>
|
||||
/// Class that represents a controllable light bulb.
|
||||
/// </summary>
|
||||
[Description("Represents a light bulb")]
|
||||
public class MyLightPlugin(bool turnedOn = false)
|
||||
{
|
||||
private bool _turnedOn = turnedOn;
|
||||
|
||||
[KernelFunction, Description("Returns whether this light is on")]
|
||||
public bool IsTurnedOn() => _turnedOn;
|
||||
|
||||
[KernelFunction, Description("Turn on this light")]
|
||||
public void TurnOn() => _turnedOn = true;
|
||||
|
||||
[KernelFunction, Description("Turn off this light")]
|
||||
public void TurnOff() => _turnedOn = false;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using Microsoft.SemanticKernel;
|
||||
|
||||
namespace OllamaFunctionCalling;
|
||||
|
||||
/// <summary>
|
||||
/// Simple plugin that just returns the time.
|
||||
/// </summary>
|
||||
public class MyTimePlugin
|
||||
{
|
||||
[KernelFunction, Description("Get the current time")]
|
||||
public DateTimeOffset Time() => DateTimeOffset.Now;
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
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> ");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
# Ollama Function Calling
|
||||
|
||||
This example illustrates how to use `Ollama Connector` with a Function Calling enabled Small Language Model
|
||||
|
||||
- Best results with llama3.1 or higher
|
||||
|
||||
## Configuring
|
||||
|
||||
- Configure the `modelId` to the model you want to use, (currently llama3.1 or related models are supported)
|
||||
Reference in New Issue
Block a user