Master Semantic Kernel Python Example: Build AI Apps Now

Logan Jul 13, 2026

The Semantic Kernel is a powerful Python library that enables semantic search, natural language processing, and knowledge graph creation. It's designed to help developers build applications that understand and interpret human language more efficiently. Here, we'll explore a practical example of using the Semantic Kernel in Python.

All Important Python Functions for Beginners (Complete Cheat Sheet)
All Important Python Functions for Beginners (Complete Cheat Sheet)

Before diving into the example, let's ensure we have the necessary prerequisites. You'll need to install the Semantic Kernel Python package using pip:

Python Cheatsheet
Python Cheatsheet

pip install semantic-kernel

Setting Up a New Project

First, let's import the required modules and set up a new project:

what python can do diagram with icons and symbols
what python can do diagram with icons and symbols

```python from semantic_kernel import Kernel from semantic_kernel '(text) import text_splitter kernel = Kernel() ```

In this example, we'll use the Sentence Transformers model 'all-MiniLM-L6-v2' for encoding text into high-dimensional vectors. This model is part of the hugging face transformer library, which is integrated with the Semantic Kernel.

Preprocessing Text Data

Python Data Structures Cheat Sheet for Beginners (Lists, Tuples, Sets & Dictionaries)
Python Data Structures Cheat Sheet for Beginners (Lists, Tuples, Sets & Dictionaries)

To prepare our data, we'll use the TextSplitter to break down long texts into smaller chunks. This is useful for creating a vector database with the Semantic Kernel:

```python splitter = text_splitter() long_text = "Your long text here..." small_texts = splitter.split_text(long_text) ```

Creating and Querying a Vector Database

Now, let's create a simple vector database using the Semantic Kernel and query it:

a book cover with the title'basic guide to python sytax '
a book cover with the title'basic guide to python sytax '

```python vector_db = kernel.create_vector_database(small_texts) query = "What is your long text about?" result = kernel.query_vector_database(query, k=1) print(result[0][0].text.replace("\n", " ")) ```

This will return the most relevant chunk of text from your input based on the query. You can adjust the number of results (k) returned by the query function.

Extracting Information from Text

The Semantic Kernel also provides tools for extracting structured information from unstructured text. Let's extract entities and relations using the spaCy pipeline:

Shortcut to learn Python.[Cheatsheet]
Shortcut to learn Python.[Cheatsheet]

First, install spaCy with the English language model:

pip install spacy python -m spacy download en_core_web_sm

Extracting Entities

Python Cheat Sheet for Beginners
Python Cheat Sheet for Beginners
a poster with the words python and symbols
a poster with the words python and symbols
the four basic python data structures are shown in this screenshoter's guide
the four basic python data structures are shown in this screenshoter's guide
Ultimate Python Cheat Sheet for Beginner
Ultimate Python Cheat Sheet for Beginner
Python Learning Path for Beginners 2026 πŸš€
Python Learning Path for Beginners 2026 πŸš€
Python For Everything – Top Libraries & What They’re Used For
Python For Everything – Top Libraries & What They’re Used For
a computer screen with the words python string method
a computer screen with the words python string method
30-Day Python Learning Plan for Beginners | Complete Python Roadmap to Learn Coding Fast
30-Day Python Learning Plan for Beginners | Complete Python Roadmap to Learn Coding Fast
Python array sorting guide #python #programming #tutorial
Python array sorting guide #python #programming #tutorial
Python Data Structures Cheat Sheet ✨
Python Data Structures Cheat Sheet ✨
PYTHON BASICS
PYTHON BASICS
Python Cheat Sheet πŸš€ | Beginner to Advanced | Save This for Coding & Interviews
Python Cheat Sheet πŸš€ | Beginner to Advanced | Save This for Coding & Interviews
a computer screen showing the different types of commands and symbols for each type of device
a computer screen showing the different types of commands and symbols for each type of device
a poster with the words string method in python
a poster with the words string method in python
"6 Python List Methods Every Beginner Should Know"
"6 Python List Methods Every Beginner Should Know"
an image of a computer screen with the text'any in python'on it
an image of a computer screen with the text'any in python'on it
Python syntax cheat sheet infographic
Python syntax cheat sheet infographic
a poster showing the different types of people
a poster showing the different types of people
Python Unit 5 Cheat Sheet 🐍 | File Handling & OOP Basics (AKTU)
Python Unit 5 Cheat Sheet 🐍 | File Handling & OOP Basics (AKTU)
Python VS R Language
Python VS R Language

Now, let's extract entities from a text using the spaCy pipeline:

```python import spacy nlp = spacy.load("en_core_web_sm") doc = nlp("Your text with entities here...") for ent in doc.ents: print(ent.text, ent.label_) ```

Extracting Relations

To extract relations between entities, we can use the Rule-based Matching feature provided by spaCy:

```python from spacy.matcher import Matcher nlp = spacy.load("en_core_web_sm") doc = nlp("Your text with relations here...") matcher = Matcher(nlp.vocab) pattern = [{"POS": "NOUN"}, {"POS": "ADP", "TEXT": "of"}, {"POS": "NOUN"}] matcher.add("ORG_SAME_CONTEXT", pattern) doc = matcher(doc) matches = [match for match in doc if matcher(doc.start, doc.end) in pattern] for match in matches: print(match[0]_...[0]._.text, "-->", match[-1]_.text) ```

This example demonstrates how to use the Semantic Kernel to create a simple knowledge graph by extracting entities and relations from text. You can combine these techniques to build powerful NLP applications in Python.

Embracing the Semantic Kernel in your projects can significantly enhance your application's ability to understand and interpret human language. By doing so, you're opening doors to more intuitive user interfaces, improved data analysis, and efficient natural language processing use cases. So, why not give it a try and see what new insights the Semantic Kernel can unlock for your Python projects?