Back to Semantic Kernel

00 Getting Started

dotnet/notebooks/00-getting-started.ipynb

latest2.0 KB
Original Source

Watch the Getting Started Quick Start Video

[!IMPORTANT] You will need an .NET 10 SDK and Polyglot to get started with this notebook using .NET Interactive.

Step 1: Configure your AI service credentials

Use this notebook first, to choose whether to run these notebooks with OpenAI or Azure OpenAI, and to save your credentials in the configuration file.

C#
// Load some helper functions, e.g. to load values from settings.json
#!import config/Settings.cs 

Step 2: Import Semantic Kernel SDK from NuGet

C#
// Import Semantic Kernel
#r "nuget: Microsoft.SemanticKernel, 1.23.0"

Step 3: Instantiate the Kernel

C#
using Microsoft.SemanticKernel;
using Kernel = Microsoft.SemanticKernel.Kernel;

//Create Kernel builder
var builder = Kernel.CreateBuilder();
C#
// Configure AI service credentials used by the kernel
var (useAzureOpenAI, model, azureEndpoint, apiKey, orgId) = Settings.LoadFromFile();

if (useAzureOpenAI)
    builder.AddAzureOpenAIChatCompletion(model, azureEndpoint, apiKey);
else
    builder.AddOpenAIChatCompletion(model, apiKey, orgId);

var kernel = builder.Build();

Step 4: Load and Run a Plugin

C#
// FunPlugin directory path
var funPluginDirectoryPath = Path.Combine(System.IO.Directory.GetCurrentDirectory(), "..", "..", "prompt_template_samples", "FunPlugin");

// Load the FunPlugin from the Plugins Directory
var funPluginFunctions = kernel.ImportPluginFromPromptDirectory(funPluginDirectoryPath);

// Construct arguments
var arguments = new KernelArguments() { ["input"] = "time travel to dinosaur age" };

// Run the Function called Joke
var result = await kernel.InvokeAsync(funPluginFunctions["Joke"], arguments);

// Return the result to the Notebook
Console.WriteLine(result);