Files
semantic-kernel/samples/dotnet/kernel-syntax-examples/Example05_InlineFunctionDefinition.cs
T
Devis Lucato e8dced1f7c Fix typos and code style (#741)
* Fix filename containing a space (`ApiKeyAuthenticationHandler .cs`)
* Fix typos
* Fix code style
* Add missing rule for local constants
* Add commented code to automate code style checks. These don't work
with .NET Standard 2.0, so we'll need some extra research.
* Fix one incorrect ILogger naming
* Add some TODOs about unused code
* Fix xmldocs
2023-05-01 17:45:35 +00:00

54 lines
1.8 KiB
C#

// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Threading.Tasks;
using Microsoft.SemanticKernel;
using RepoUtils;
// ReSharper disable once InconsistentNaming
public static class Example05_InlineFunctionDefinition
{
public static async Task RunAsync()
{
Console.WriteLine("======== Inline Function Definition ========");
/*
* Example: normally you would place prompt templates in a folder to separate
* C# code from natural language code, but you can also define a semantic
* function inline if you like.
*/
IKernel kernel = new KernelBuilder().WithLogger(ConsoleLogger.Log).Build();
// OpenAI settings
kernel.Config.AddOpenAITextCompletionService("text-davinci-003", Env.Var("OPENAI_API_KEY"));
// Function defined using few-shot design pattern
const string FunctionDefinition = @"
Generate a creative reason or excuse for the given event.
Be creative and be funny. Let your imagination run wild.
Event: I am running late.
Excuse: I was being held ransom by giraffe gangsters.
Event: I haven't been to the gym for a year
Excuse: I've been too busy training my pet dragon.
Event: {{$input}}
";
var excuseFunction = kernel.CreateSemanticFunction(FunctionDefinition, maxTokens: 100, temperature: 0.4, topP: 1);
var result = await excuseFunction.InvokeAsync("I missed the F1 final race");
Console.WriteLine(result);
result = await excuseFunction.InvokeAsync("sorry I forgot your birthday");
Console.WriteLine(result);
var fixedFunction = kernel.CreateSemanticFunction($"Translate this date {DateTimeOffset.Now:f} to French format", maxTokens: 100);
result = await fixedFunction.InvokeAsync();
Console.WriteLine(result);
}
}