Picture credit — undraw.co

Semantic Kernel concepts — My notes (part-1)

@Anil's Notes
9 min readJun 30, 2024

Earlier, I was primarily focused on LangChain and related frameworks. However, my interest in Semantic Kernel has recently grown as I’ve started concentrating more on the Microsoft ecosystem. I see frameworks as tools to solve business problems and keen to learn.

Here, I will begin sharing my Notion notes that I take while learning & my initial basic example that I will build upon to learn, as it may help someone else who is in the same track.

Introduction to Semantic Kernel

Semantic Kernel is an open-source SDK developed by Microsoft to quickly integrate large language models (LLMs) & workflows into existing or new AI applications. It’s designed to orchestrate AI workflows based on natural language queries and commands, making it easier for developers to leverage the power of LLMs in their projects.

Key Features for Python (or C# or Java) Developers

  1. Simplified LLM Integration: Like LangChain, Semantic Kernel abstracts away much of the complexity of working with LLMs, providing a structured approach to integrating models like OpenAI’s GPT into your Python applications.
  2. Intuitive Plugin Architecture: Semantic Kernel uses a plugin-based approach that supports both semantic functions (AI-powered) and native functions (traditional Python code). This allows for seamless integration of AI capabilities with existing Python codebases.
  3. Advanced Planning Capabilities: Semantic Kernel includes a built-in Planner component that can automatically create sequences of actions to address complex user needs. This feature enables more dynamic and flexible AI workflows compared to LangChain’s more static chain approach.
  4. Robust Memory Management: With advanced features for storing context and embeddings, Semantic Kernel enhances the contextual understanding and performance of AI applications in Python.
  5. Customization and Extensibility: The framework allows for easy creation of custom plugins and functions in Python, enabling you to tailor AI capabilities to your specific needs.

Semantic Kernel vs. LangChain for Python Developers

Source — https://github.com/formulahendry/semantic-kernel-vs-langchain

While both frameworks aim to simplify working with LLMs, there are some key differences:

  1. Planning vs. Chaining: Semantic Kernel’s Planner offers a more dynamic approach to workflow creation compared to LangChain’s predefined chains. This can be advantageous for complex, adaptive AI workflows in Python.
  2. Plugin System: Semantic Kernel’s plugin architecture might feel more intuitive for Python developers used to object-oriented programming, as it clearly separates AI-powered and traditional Python functions.
  3. Memory Handling: While both offer memory capabilities, Semantic Kernel’s approach to memory management is designed to be more robust and scalable, which can be beneficial for larger Python applications.
  4. Ecosystem Integration: LangChain has a larger community and more integrations with various tools and services. However, Semantic Kernel offers tighter integration with Microsoft and Azure services, which can be advantageous if you’re using these in your Python projects.
  5. Learning Curve: LangChain might have a better learning curve for Python developers due to its larger community and more extensive documentation. However, Semantic Kernel’s architecture could be more intuitive for developers coming from an object-oriented background.
Source — https://github.com/microsoft/semantic-kernel

Semantic Kernel Components

Kernel

  • The central orchestrator of Semantic Kernel.
  • Manages services, functions, and plugins.
  • Provides a unified interface for working with AI models and other services.
  • Acts as the main entry point for creating and executing AI-powered workflows.
  • Handles the lifecycle of AI services and ensures proper initialization and configuration.

AI Services

  • Connectors to AI models like OpenAI’s GPT.
  • Examples include OpenAIChatCompletion, AzureOpenAITextCompletion, etc.
  • Managed by the Kernel for easy integration and swapping.
  • Abstracts the complexities of interacting with different AI models and providers.
  • Allows for easy switching between different AI services without changing application logic.

Semantic Functions

  • AI-powered functions defined by natural language prompts.
  • Created and managed by the Kernel.
  • Used to generate text, answer questions, or perform other AI tasks.
  • Allow developers to leverage AI capabilities without writing complex code.
  • Can be chained together to create more complex AI workflows.

Native Functions

  • Traditional code functions that can be integrated into Semantic Kernel workflows.
  • Can be used alongside semantic functions for complex operations.
  • Provide a way to incorporate existing code and business logic into AI-powered applications.
  • Enable hybrid workflows that combine AI and traditional programming approaches.

Plugins (Skills)

  • Collections of related functions (both semantic and native).
  • Can be loaded into the Kernel to extend its capabilities.
  • Promote modularity and reusability in your AI applications.
  • Allow for organizing and sharing common functionalities across different projects.
  • Can be developed independently and easily integrated into existing Semantic Kernel applications.

Memory

  • System for storing and retrieving information.
  • Supports semantic search and context management.
  • Can use different storage backends (e.g., volatile memory, databases).
  • Enhances AI applications with persistent knowledge and context awareness.
  • Enables more coherent and context-aware interactions in long-running applications.

Planner

  • Automatically creates sequences of actions to achieve complex goals.
  • Uses available functions and plugins to construct plans.
  • Enables dynamic, goal-oriented AI workflows.
  • Reduces the need for manual orchestration of complex tasks.
  • Allows for more flexible and adaptable AI applications that can handle novel situations.
  • BasicPlanner, SequentialPlanner, ActionPlanner, StepwisePlanner

Prompt Template Engine

  • Manages the creation and rendering of prompt templates.
  • Supports variable substitution and complex prompt structures.
  • Enables the creation of reusable and parameterized prompts.
  • Helps maintain consistency in AI interactions across an application.
  • Allows for easy modification and optimization of prompts without changing code.

Connectors

  • Interfaces to external services and data sources.
  • Allow Semantic Kernel to interact with various APIs and databases.
  • Enable integration of AI capabilities with existing systems and data sources.
  • Provide a standardized way to incorporate external data and services into AI workflows.
  • Can be extended to support new services and data sources as needed.

Context

  • Holds variables and state during function execution.
  • Allows for data passing between functions in a workflow.
  • Maintains the current state of an AI interaction or workflow.
  • Enables more complex, multi-step AI operations with shared state.
  • Provides a way to manage and access relevant information throughout an AI workflow.

Embeddings

  • Used for semantic search and similarity comparisons.
  • Integrated with the memory system for efficient information retrieval.
  • Enable more nuanced and context-aware information retrieval.
  • Allow for finding semantically similar content, not just exact matches.
  • Enhance the ability of AI applications to understand and work with unstructured data.

Configuration

  • Manages settings for the Kernel and its components.
  • Allows for easy customization of Semantic Kernel behavior.
  • Provides a centralized way to control various aspects of the AI application.
  • Enables easy switching between different environments (e.g., development, production).
  • Allows for fine-tuning of AI services and other components without code changes.

Lets Practice (Basic example):

Lets learn these components in practice by building a sample Yeezy sneaker bot.

Problem Statement:
We want to create a Yeezy sneaker chatbot that can assist customers with information about Yeezy sneakers, help them find the right pair, size information. The bot should be able to understand natural language queries, access a knowledge base about Yeezy sneakers, and provide personalized recommendations based on user preferences.

Lets break down the problem statement in to basic initial PoC to first write a connector to GPT model, prompt config with chat history added to it.

import asyncio
from pprint import pprint
from semantic_kernel import Kernel
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion
from semantic_kernel.prompt_template import PromptTemplateConfig,InputVariable
from semantic_kernel.contents.chat_history import ChatHistory
from semantic_kernel.functions import KernelArguments


# Initialize the kernel
kernel = Kernel()

# Configure AI service (replace with your actual API key)
service_id = "chat-gpt"
openai_api_key = "REPLACE_ME"
kernel.add_service(
OpenAIChatCompletion(
service_id=service_id,
api_key=openai_api_key,
ai_model_id="gpt-3.5-turbo"
)
)

# Define the request settings
req_settings = kernel.get_prompt_execution_settings_from_service_id(service_id)
req_settings.max_tokens = 2000
req_settings.temperature = 0.7
req_settings.top_p = 0.8

# YeezyInfoPrompt
yeezy_info_prompt = """
You are a Yeezy sneaker expert chatbot. Provide detailed and accurate information about Yeezy sneakers, including personalized recommendations based on the user's preferences and previous interactions.

Chat History:
{{$history}}

User: {{$input}}
ChatBot: Let me provide you with personalized information and recommendations based on our conversation:
"""


prompt_template_config = PromptTemplateConfig(
template=yeezy_info_prompt,
name="yeezy_info_prompt_template",
template_format="semantic-kernel",
input_variables=[
InputVariable(name="input", description="The user input", is_required=True),
InputVariable(name="history", description="The conversation history", is_required=True),
],
execution_settings=req_settings,
)

chatbot = kernel.add_function(
function_name="yeezy_chatbot_with_history",
plugin_name="yeezy_info_plugin",
prompt_template_config=prompt_template_config,
)

chat_history = ChatHistory()

async def chat(input_text, verbose=True):
# Save new message in the context variables
context = KernelArguments(input=input_text, history=chat_history)

# Process the user message and get an answer
answer = await kernel.invoke(chatbot, context)

# Show the response
pprint(f"User input: {input_text}")
pprint(f"ChatBot response: {answer}")

# Append the new interaction to the chat history
chat_history.add_user_message(input_text)
chat_history.add_assistant_message(str(answer))

async def main():
await chat("What can you tell me about Yeezy 700 v2 Vanta?")
await chat("I prefer comfortable sneakers. Any recommendations?")
await chat("Please recommend men sizing information for yeezy 700 v2 Vanta")

await main()

Output:

User input: What can you tell me about Yeezy 700 v2 Vanta?

ChatBot response: The Yeezy 700 v2 Vanta is a stylish and versatile sneaker designed by Kanye West in collaboration with Adidas. It features a sleek all-black colorway with a mix of materials such as suede, leather, and mesh on the upper. The shoe is known for its chunky silhouette and comfortable Boost cushioning technology in the midsole, providing excellent support and comfort for all-day wear.

The Yeezy 700 v2 Vanta is a popular choice among sneaker enthusiasts for its sleek design and high-quality construction. It's a great option for both casual and athleisure outfits, adding a touch of luxury and streetwear flair to any look.

If you're a fan of black sneakers with a modern and edgy aesthetic, the Yeezy 700 v2 Vanta would be a great addition to your collection. Its versatile colorway makes it easy to style with a variety of outfits, from streetwear to casual wear. The Boost cushioning also ensures a comfortable and supportive fit, making it ideal for daily wear or long walks.

Do you have any specific preferences or questions about the Yeezy 700 v2 Vanta or any other Yeezy sneakers? Feel free to ask!


User input: I prefer comfortable sneakers. Any recommendations?

ChatBot response: If you prioritize comfort in your sneakers, Yeezy offers several models that are known for their comfort features, including the Boost cushioning technology. Here are some Yeezy sneaker recommendations that are both stylish and comfortable:

Yeezy Boost 350 V2: The Yeezy Boost 350 V2 is a popular silhouette known for its lightweight construction and responsive Boost cushioning. The Primeknit upper provides a snug and comfortable fit, making it a great option for all-day wear.

Yeezy Boost 700: The Yeezy Boost 700 features a chunky design with a full-length Boost midsole for maximum comfort and support. The mix of materials on the upper offers a premium feel, while the overall design is both stylish and comfortable.

Yeezy Boost 380: The Yeezy Boost 380 showcases a futuristic design with a unique patterned Primeknit upper and a chunky Boost midsole for cushioning. The sock-like fit and comfortable materials make it a great choice for those seeking comfort in their sneakers.

These Yeezy models are designed with comfort in mind, incorporating innovative cushioning technologies and materials to provide a comfortable and supportive fit. If you have any specific preferences or questions about these recommendations or any other Yeezy sneakers, feel free to ask!


User input: Please recommend men sizing information for yeezy 700 v2 Vanta

ChatBot response: For men's sizing in the Yeezy 700 v2 Vanta, it is recommended to go true to size for a snug and comfortable fit. The Yeezy 700 v2 silhouette typically runs true to size, so selecting your regular size should provide you with the best fit. However, some individuals with wide feet may prefer to go up half a size for a roomier feel.

If you have the opportunity to try on the Yeezy 700 v2 Vanta in person before purchasing, it's always a good idea to do so to ensure the perfect fit for your feet. Keep in mind that Yeezy sizing can vary slightly between different models, so trying them on is the best way to determine your ideal size.

If you have any more questions or need further assistance regarding sizing or any other Yeezy sneakers, feel free to ask!

Code explanation:

This code snippet demonstrates a basic Yeezy sneaker chatbot built with Semantic Kernel. It leverages OpenAI’s GPT model to provide personalized sneaker information. The chatbot maintains conversation history, allowing for context-aware responses. Key components include the Kernel for orchestration, a custom prompt template for Yeezy expertise, and asynchronous functions for efficient processing. The chat function handles user inputs, generates responses, and updates the conversation history. The main function simulates a three-turn conversation, demonstrating the bot’s ability to provide information, understand preferences, and offer sizing recommendations for Yeezy sneakers.

Next, I will write about custom plugins (with search integration), planners, memory and how they all fit together — continuing to implement the Yeezy bot problem statement.

Keep Learning!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

@Anil's Notes
@Anil's Notes

Written by @Anil's Notes

Thoughts I add here are my personal notes and learnings only

No responses yet

Write a response