Natural language processing (NLP) benefits greatly from semantic kernels. By expressing words as high-dimensional vectors rather than simply strings of letters, they enable computers to interpret the meaning of words in a more complex way. This enables computers to detect word associations, such as synonyms and antonyms, without the requirement for explicit rules or human supervision.
Table of Contents
What is Semantic Kernel?
Semantic Kernel (SK) is a lightweight SDK that allows AI Large Language Models (LLMs) to be integrated with traditional programming languages. The SK extensible programming model combines natural language semantic functions, conventional code native functions, and embeddings-based memory to unlock new possibilities and provide value to AI-powered systems.
SDK that allows you to quickly combine AI prompts with traditional programming languages such as C# and Python.
Orchestrating AI with Semantic Kernel
The real power of Semantic Kernel derives from its ability to mix various components. You may design sophisticated pipelines that employ AI to automate complicated operations by combining several AI models, native functions, and memory within Semantic Kernel.
For example, you might utilize Semantic Kernel to build a pipeline that assists a user in sending an email to their marketing staff. With memory, you might obtain project information and then utilize planner to autogenerate the next stages (for example, grounding the user’s request with Microsoft Graph data, generating a response with GPT-4, and sending the email). Finally, in your custom app, you may display a success message to the user.

The steps described outline the process of interacting with the Semantic Kernel in an AI application. Here’s a breakdown of each step:
Step | Component | Description |
---|---|---|
1 | Ask | The user or developer sends a goal or request to the Semantic Kernel. |
2 | Kernel | Orchestrates the user’s request and runs a pipeline or chain of functions defined by the developer. Provides a common context for data sharing between functions. |
2.1 | Memories | Allows developers to recall and store context in vector databases, simulating memory within the AI application. |
2.2 | Planner | Enables the automatic creation of chains or pipelines to address new or novel user needs. Can utilize existing plugins to create additional steps. |
2.3 | Connectors | Provides out-of-the-box connectors to retrieve additional data or perform autonomous actions. Developers can use pre-built connectors or create custom connectors for their own services. |
2.4 | Custom functions | Developers can create custom functions that run inside the Semantic Kernel. These can be semantic functions (LLM prompts) or native code (C# or Python) to add new AI capabilities and integrate existing apps and services. |
3 | Response | The generated response is sent back to the user to indicate that the requested task or goal has been completed. |
How to use Semantic Kernel
You can run the Semantic Kernel getting started guidelines in C# or Python in only a few steps. After finishing the tutorials, you’ll be able to…
- Configure your local machine to run Semantic Kernel
- Run prompts from the kernel.
- Make prompts dynamic with variables.
- Create prompt chains.
- Automatically create new chains with the planner
- Store and retrieve memory with embeddings.
Requirements to run the guides.
Before running the guides in C#, make sure you have the following installed on your local machine. If you are using the Python guides, you just need git
and python
.
git
or the GitHub application- VSCode vs. Visual Studio
- An OpenAI key obtained using the Azure OpenAI Service or OpenAI
- .Net 7 SDK – C# notebook documentation
- The Polyglot Notebook in VS Code – for notebook instructions
C#
1. Create a new console app.
2. Add the semantic kernel nuget
#r "nuget: Microsoft.SemanticKernel, *-*"
3. Copy the code from here into the app Program.cs
file.
4. Replace the configuration placeholders for API key and other params with your key and settings.
5. Run with F5
or dotnet run
Python
1. Install the pip package
pip install semantic-kernel
2. Create a new script e.g. hello-world.py
.
3. Store your API key and settings in an .env
file as described here.
4. Copy the code from here into the hello-world.py
script.
5. Run the python script.
Download and run the Guides.
- Use your web browser to visit aka.ms/sk/repo on GitHub.
- Clone or fork the repo to your local machine.
- While the repository is open in VS Code, navigate to the
semantic-kernel/samples/notebooks
folder. - Choose either the .Net or Python folder based on your preferred programming language.
- Open the
00-getting-started.ipynb
notebook. - Activate each code snippet with the “play” button on the left hand side. If you need help running the
00-getting-started.ipynb
notebook, you can watch the video below. - Repeat for the remaining notebooks.
Getting started with C# notebook
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.
// Load some helper functions, e.g. to load values from settings.json
#!import config/Settings.cs
2: Import Semantic Kernel SDK from NuGet
// Import Semantic Kernel
#r "nuget: Microsoft.SemanticKernel, 0.14.547.1-preview"
3: Instantiate the Kernel
using Microsoft.SemanticKernel;
// Set Simple kernel instance
IKernel kernel = KernelBuilder.Create();
// Configure AI service credentials used by the kernel
var (useAzureOpenAI, model, azureEndpoint, apiKey, orgId) = Settings.LoadFromFile();
if (useAzureOpenAI)
kernel.Config.AddAzureTextCompletionService(model, azureEndpoint, apiKey);
else
kernel.Config.AddOpenAITextCompletionService(model, apiKey, orgId);
4: Load and Run a Skill
// Load the Skills Directory
var skillsDirectory = Path.Combine(System.IO.Directory.GetCurrentDirectory(), "..", "..", "skills");
// Load the FunSkill from the Skills Directory
var funSkillFunctions = kernel.ImportSemanticSkillFromDirectory(skillsDirectory, "FunSkill");
// Run the Function called Joke
var result = await funSkillFunctions["Joke"].InvokeAsync("time travel to dinosaur age");
// Return the result to the Notebook
Console.WriteLine(result);
Getting started with Python notebook
1. Import Semantic Kernel SDK from pypi.org
!python -m pip install semantic-kernel==0.2.7.dev0
import semantic_kernel as sk
kernel = sk.Kernel()
Using OpenAI
2. Add your Open AI Key key to a .env
file in the same folder (org Id only if you have multiple orgs):
OPENAI_API_KEY="sk-..."
OPENAI_ORG_ID=""
and add OpenAI Text Completion to the kernel:
from semantic_kernel.connectors.ai.open_ai import OpenAITextCompletion
api_key, org_id = sk.openai_settings_from_dot_env()
kernel.add_text_completion_service("dv", OpenAITextCompletion("text-davinci-003", api_key, org_id))
Using Azure OpenAI
3. Add your Azure Open AI Service key settings to a .env
file in the same folder:
AZURE_OPENAI_API_KEY="..."
AZURE_OPENAI_ENDPOINT="https://..."
AZURE_OPENAI_DEPLOYMENT_NAME="..."
and add Azure OpenAI Text Completion to the kernel:
from semantic_kernel.connectors.ai.open_ai import AzureTextCompletion
deployment, api_key, endpoint = sk.azure_openai_settings_from_dot_env()
kernel.add_text_completion_service("dv", AzureTextCompletion(deployment, endpoint, api_key))
Run a Semantic Function
4. Load a Skill and run a semantic function:
skill = kernel.import_semantic_skill_from_directory("../../skills", "FunSkill")
joke_function = skill["Joke"]
print(joke_function("time travel to dinosaur age"))
Also Read: Mr. Ranedeer: The AI Tutor That Can Help You Learn Anything
This article is to help you learn the semantic kernel. We trust that it has been helpful to you. Please feel free to share your thoughts and feedback in the comment section below.