The Semantic Kernel SDK can be imported from the following nuget feed:
#r "nuget: Microsoft.SemanticKernel, 1.0.0-beta1"
After adding the nuget package, you can instantiate the kernel in a few ways, depending on your use case.
using Microsoft.SemanticKernel;
// Simple instance
IKernel kernel_1 = KernelBuilder.Create();
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
// Inject your logger
// see Microsoft.Extensions.Logging.ILogger @ https://learn.microsoft.com/dotnet/core/extensions/logging
ILoggerFactory myLoggerFactory = NullLoggerFactory.Instance;
IKernel kernel_2 = new KernelBuilder()
.WithLoggerFactory(myLoggerFactory)
.Build();
When using the kernel for AI requests, the kernel needs some settings like URL and credentials to the AI models.
The SDK currently supports OpenAI, Azure OpenAI and HuggingFace. It's also possible to create your own connector and use AI provider of your choice.
If you need an Azure OpenAI key, go here.
Kernel.Builder
.WithAzureChatCompletionService(
"my-finetuned-model", // Azure OpenAI *Deployment Name*
"https://contoso.openai.azure.com/", // Azure OpenAI *Endpoint*
"...your Azure OpenAI Key...", // Azure OpenAI *Key*
serviceId: "Azure_curie" // alias used in the prompt templates' config.json
)
.WithOpenAIChatCompletionService(
"gpt-3.5-turbo", // OpenAI Model Name
"...your OpenAI API Key...", // OpenAI API key
"...your OpenAI Org ID...", // *optional* OpenAI Organization ID
serviceId: "OpenAI_davinci" // alias used in the prompt templates' config.json
);
When working with multiple backends and multiple models, the first backend defined is also the "default" used in these scenarios:
The default can be set programmatically:
Kernel.Builder
.WithOpenAIChatCompletionService(
"gpt-3.5-turbo", // OpenAI Model Name
"...your OpenAI API Key...", // OpenAI API key
"...your OpenAI Org ID...", // *optional* OpenAI Organization ID
"OpenAI_davinci", // alias used in the prompt templates' config.json
true // This flag specifies that this service is the default one.
);
Great, now that you're familiar with setting up the Semantic Kernel, let's see how we can use it to run prompts.