chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
#pragma warning disable VSTHRD111 // Use ConfigureAwait(bool)
|
||||
#pragma warning disable CA1050 // Declare types in namespaces
|
||||
#pragma warning disable CA2007 // Consider calling ConfigureAwait on the awaited task
|
||||
|
||||
using System.ComponentModel;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.SemanticKernel;
|
||||
using Microsoft.SemanticKernel.ChatCompletion;
|
||||
using Microsoft.SemanticKernel.Connectors.OpenAI;
|
||||
|
||||
var config = new ConfigurationBuilder()
|
||||
.AddUserSecrets<Program>()
|
||||
.AddEnvironmentVariables()
|
||||
.Build()
|
||||
?? throw new InvalidOperationException("Configuration is not provided.");
|
||||
|
||||
ArgumentNullException.ThrowIfNull(config["OpenAI:ChatModelId"], "OpenAI:ChatModelId");
|
||||
ArgumentNullException.ThrowIfNull(config["OpenAI:ApiKey"], "OpenAI:ApiKey");
|
||||
|
||||
var kernelBuilder = Kernel.CreateBuilder().AddOpenAIChatCompletion(
|
||||
modelId: config["OpenAI:ChatModelId"]!,
|
||||
apiKey: config["OpenAI:ApiKey"]!);
|
||||
|
||||
kernelBuilder.Plugins.AddFromType<TimeInformationPlugin>();
|
||||
var kernel = kernelBuilder.Build();
|
||||
|
||||
// Get chat completion service
|
||||
var chatCompletionService = kernel.GetRequiredService<IChatCompletionService>();
|
||||
|
||||
// Enable auto function calling
|
||||
OpenAIPromptExecutionSettings openAIPromptExecutionSettings = new()
|
||||
{
|
||||
FunctionChoiceBehavior = FunctionChoiceBehavior.Auto()
|
||||
};
|
||||
|
||||
Console.WriteLine("Ask questions to use the Time Plugin such as:\n" +
|
||||
"- What time is it?");
|
||||
|
||||
ChatHistory chatHistory = [];
|
||||
string? input = null;
|
||||
while (true)
|
||||
{
|
||||
Console.Write("\nUser > ");
|
||||
input = Console.ReadLine();
|
||||
if (string.IsNullOrWhiteSpace(input))
|
||||
{
|
||||
// Leaves if the user hit enter without typing any word
|
||||
break;
|
||||
}
|
||||
chatHistory.AddUserMessage(input);
|
||||
var chatResult = await chatCompletionService.GetChatMessageContentAsync(chatHistory, openAIPromptExecutionSettings, kernel);
|
||||
Console.Write($"\nAssistant > {chatResult}\n");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A plugin that returns the current time.
|
||||
/// </summary>
|
||||
public class TimeInformationPlugin
|
||||
{
|
||||
/// <summary>
|
||||
/// Retrieves the current time in UTC.
|
||||
/// </summary>
|
||||
/// <returns>The current time in UTC. </returns>
|
||||
[KernelFunction, Description("Retrieves the current time in UTC.")]
|
||||
public string GetCurrentUtcTime()
|
||||
=> DateTime.UtcNow.ToString("R");
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
# Time Plugin - Demo Application
|
||||
|
||||
This is an example how you can easily use Plugins with the Power of Auto Function Calling from AI Models.
|
||||
|
||||
Here we have a simple Time Plugin created in C# that can be called from the AI Model to get the current time.
|
||||
|
||||
|
||||
## Semantic Kernel Features Used
|
||||
|
||||
- [Plugin](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/src/SemanticKernel.Abstractions/Functions/KernelPlugin.cs) - Creating a Plugin from a native C# Booking class to be used by the Kernel to interact with Bookings API.
|
||||
- [Chat Completion Service](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/src/SemanticKernel.Abstractions/AI/ChatCompletion/IChatCompletionService.cs) - Using the Chat Completion Service [OpenAI Connector implementation](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/src/Connectors/Connectors.OpenAI/Services/OpenAIChatCompletionService.cs) to generate responses from the LLM.
|
||||
- [Chat History](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/src/SemanticKernel.Abstractions/AI/ChatCompletion/ChatHistory.cs) Using the Chat History abstraction to create, update and retrieve chat history from Chat Completion Models.
|
||||
- [Auto Function Calling](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Concepts/ChatCompletion/OpenAI_FunctionCalling.cs) Enables the LLM to have knowledge of current importedUsing the Function Calling feature automatically call the Booking Plugin from the LLM.
|
||||
|
||||
## Prerequisites
|
||||
|
||||
- [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0).
|
||||
|
||||
### Function Calling Enabled Models
|
||||
|
||||
This sample uses function calling capable models and has been tested with the following models:
|
||||
|
||||
| Model type | Model name/id | Model version | Supported |
|
||||
| --------------- | ------------------------- | ------------------: | --------- |
|
||||
| Chat Completion | gpt-3.5-turbo | 0125 | ✅ |
|
||||
| Chat Completion | gpt-3.5-turbo-1106 | 1106 | ✅ |
|
||||
| Chat Completion | gpt-3.5-turbo-0613 | 0613 | ✅ |
|
||||
| Chat Completion | gpt-3.5-turbo-0301 | 0301 | ❌ |
|
||||
| Chat Completion | gpt-3.5-turbo-16k | 0613 | ✅ |
|
||||
| Chat Completion | gpt-4 | 0613 | ✅ |
|
||||
| Chat Completion | gpt-4-0613 | 0613 | ✅ |
|
||||
| Chat Completion | gpt-4-0314 | 0314 | ❌ |
|
||||
| Chat Completion | gpt-4-turbo | 2024-04-09 | ✅ |
|
||||
| Chat Completion | gpt-4-turbo-2024-04-09 | 2024-04-09 | ✅ |
|
||||
| Chat Completion | gpt-4-turbo-preview | 0125-preview | ✅ |
|
||||
| Chat Completion | gpt-4-0125-preview | 0125-preview | ✅ |
|
||||
| Chat Completion | gpt-4-vision-preview | 1106-vision-preview | ✅ |
|
||||
| Chat Completion | gpt-4-1106-vision-preview | 1106-vision-preview | ✅ |
|
||||
|
||||
ℹ️ OpenAI Models older than 0613 version do not support function calling.
|
||||
|
||||
## Configuring the sample
|
||||
|
||||
The sample can be configured by using the command line with .NET [Secret Manager](https://learn.microsoft.com/en-us/aspnet/core/security/app-secrets) to avoid the risk of leaking secrets into the repository, branches and pull requests.
|
||||
|
||||
### Using .NET [Secret Manager](https://learn.microsoft.com/en-us/aspnet/core/security/app-secrets)
|
||||
|
||||
```powershell
|
||||
|
||||
# OpenAI
|
||||
dotnet user-secrets set "OpenAI:ChatModelId" "gpt-3.5-turbo"
|
||||
dotnet user-secrets set "OpenAI:ApiKey" "... your api key ... "
|
||||
```
|
||||
|
||||
## Running the sample
|
||||
|
||||
After configuring the sample, to build and run the console application just hit `F5`.
|
||||
|
||||
To build and run the console application from the terminal use the following commands:
|
||||
|
||||
```powershell
|
||||
dotnet build
|
||||
dotnet run
|
||||
```
|
||||
|
||||
### Example of a conversation
|
||||
|
||||
Ask questions to use the Time Plugin such as:
|
||||
- What time is it?
|
||||
|
||||
**User** > What time is it ?
|
||||
|
||||
**Assistant** > The current time is Sun, 12 May 2024 15:53:54 GMT.
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<UserSecretsId>5ee045b0-aea3-4f08-8d31-32d1a6f8fed0</UserSecretsId>
|
||||
<NoWarn>$(NoWarn);SKEXP0001</NoWarn>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\src\Connectors\Connectors.OpenAI\Connectors.OpenAI.csproj" />
|
||||
<ProjectReference Include="..\..\..\src\SemanticKernel.Abstractions\SemanticKernel.Abstractions.csproj" />
|
||||
<ProjectReference Include="..\..\..\src\SemanticKernel.Core\SemanticKernel.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user