Files
semantic-kernel/samples/dotnet/kernel-syntax-examples/Example06_TemplateLanguage.cs
T
af03e00a79 Feature serviceregistry (#993)
### Motivation, Context and Description

This PR introduces several changes aimed to simplify the kernel and
enhancing its extensibility for AI services management. The changes
include the following:

1. Kernel simplification and extensibility: The code responsible for AI
services management, such as registration and resolution, has been moved
out of the Kernel and KernelConfig classes into a new class called
AIServiceProvider. This restructuring promotes the principle of Single
Responsibility, ensuring that each class has a clear and focused
purpose.

2. Hosting apps customization: With the introduction of the
AIServiceProvider, hosting applications now have the flexibility to
provide their own implementation of the provider if needed. This
promotes extensibility, allowing the hosting apps to tailor the AI
service management according to their specific requirements.

3. AI services registration improvements: A new method called
WithAiService has been added, which enables the registration of any new
AI service that is not currently supported.

4. Improved kernel builder interface: The Add* methods for AI services
registration have been moved from the KernelConfig class to the
KernelBuilder. This change aligns the interface with other KernelBilder
methods, such as WithLogger and WithMemory, creating a more cohesive and
intuitive kernel builder interface.

---------

Co-authored-by: Shawn Callegari <[email protected]>
Co-authored-by: Lee Miller <[email protected]>
2023-05-22 15:21:48 -07:00

76 lines
2.6 KiB
C#

// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Threading.Tasks;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.CoreSkills;
using Microsoft.SemanticKernel.TemplateEngine;
using RepoUtils;
// ReSharper disable once InconsistentNaming
public static class Example06_TemplateLanguage
{
/// <summary>
/// Show how to invoke a Native Function written in C#
/// from a Semantic Function written in natural language
/// </summary>
public static async Task RunAsync()
{
Console.WriteLine("======== TemplateLanguage ========");
IKernel kernel = Kernel.Builder
.WithLogger(ConsoleLogger.Log)
.WithOpenAITextCompletionService("text-davinci-003", Env.Var("OPENAI_API_KEY"))
.Build();
// Load native skill into the kernel skill collection, sharing its functions with prompt templates
// Functions loaded here are available as "time.*"
kernel.ImportSkill(new TimeSkill(), "time");
// Semantic Function invoking time.Date and time.Time native functions
const string FunctionDefinition = @"
Today is: {{time.Date}}
Current time is: {{time.Time}}
Answer to the following questions using JSON syntax, including the data used.
Is it morning, afternoon, evening, or night (morning/afternoon/evening/night)?
Is it weekend time (weekend/not weekend)?
";
// This allows to see the prompt before it's sent to OpenAI
Console.WriteLine("--- Rendered Prompt");
var promptRenderer = new PromptTemplateEngine();
var renderedPrompt = await promptRenderer.RenderAsync(FunctionDefinition, kernel.CreateNewContext());
Console.WriteLine(renderedPrompt);
// Run the prompt / semantic function
var kindOfDay = kernel.CreateSemanticFunction(FunctionDefinition, maxTokens: 150);
// Show the result
Console.WriteLine("--- Semantic Function result");
var result = await kindOfDay.InvokeAsync();
Console.WriteLine(result);
/* OUTPUT:
--- Rendered Prompt
Today is: Friday, April 28, 2023
Current time is: 11:04:30 PM
Answer to the following questions using JSON syntax, including the data used.
Is it morning, afternoon, evening, or night (morning/afternoon/evening/night)?
Is it weekend time (weekend/not weekend)?
--- Semantic Function result
{
"date": "Friday, April 28, 2023",
"time": "11:04:30 PM",
"period": "night",
"weekend": "weekend"
}
*/
}
}