What is Semantic Kernel function
Semantic Kernel function is a reusable AI skill or operation defined within the semantic_kernel framework that encapsulates prompts, logic, or code to perform specific tasks. It enables modular, composable AI workflows by abstracting complex prompt engineering and AI calls into callable functions.Semantic Kernel function is a modular AI skill in the semantic_kernel framework that encapsulates prompts or logic to perform specific tasks within AI workflows.How it works
A Semantic Kernel function acts like a programmable AI skill that you define by writing a prompt template or code logic. It is registered in the semantic_kernel environment and can be invoked with parameters. This abstraction lets you build complex AI applications by composing multiple functions, similar to how software functions encapsulate reusable logic. The kernel manages context, memory, and AI model calls behind the scenes.
Think of it as creating a custom AI "tool" that you can call repeatedly with different inputs, enabling modular and maintainable AI workflows.
Concrete example
Below is a Python example showing how to create and invoke a simple Semantic Kernel function that summarizes text using OpenAI's GPT-4o model via the semantic_kernel SDK.
import os
import semantic_kernel as sk
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion
# Initialize kernel and add OpenAI chat service
kernel = sk.Kernel()
kernel.add_service(OpenAIChatCompletion(
service_id="chat",
api_key=os.environ["OPENAI_API_KEY"],
ai_model_id="gpt-4o"
))
# Define a semantic function with a prompt template
prompt_template = """
Summarize the following text in one sentence:
{{input}}
"""
summary_function = kernel.create_semantic_function(
prompt_template=prompt_template,
skill_name="TextSkills",
function_name="Summarize"
)
# Invoke the function with input text
input_text = "Semantic Kernel enables modular AI programming by encapsulating prompts as functions."
result = summary_function.invoke(input_text)
print("Summary:", result) Summary: Semantic Kernel allows modular AI programming by wrapping prompts into reusable functions.
When to use it
Use Semantic Kernel functions when building AI applications that require modular, reusable, and composable AI skills. They are ideal for scenarios like chatbots, automation workflows, or multi-step reasoning where you want to encapsulate prompt logic and reuse it with different inputs. Avoid if you only need simple one-off prompt calls without reuse or composition.
Key terms
| Term | Definition |
|---|---|
| Semantic Kernel function | A reusable AI skill defined by a prompt or code within the semantic_kernel framework. |
| Kernel | The core orchestrator managing AI services, memory, and function invocation. |
| Skill | A collection of related Semantic Kernel functions grouped by purpose. |
| Prompt template | A text template with placeholders used to generate AI prompts dynamically. |
Key Takeaways
- Semantic Kernel functions encapsulate AI prompts or logic as reusable skills for modular AI workflows.
- They enable composing complex AI applications by invoking functions with parameters and managing context.
- Use them to build maintainable, scalable AI solutions with clear separation of concerns.