4.3. Semantic Kernel Agents
💡 First Principle: Building agents from scratch means writing your own tool-calling loops, memory management, and planning logic. Semantic Kernel provides these as reusable components: Plugins are callable tools, Planners decompose goals into steps, and Memory persists context across sessions. Think of it as a framework that handles the "agent infrastructure" so you focus on the business logic. The exam tests whether you know Semantic Kernel uses plugins (not "tools" like OpenAI Assistants)—terminology matters.
Building on the agent concepts from Section 4.1, Semantic Kernel provides a structured framework for building enterprise-grade agents with modular, reusable components.
Why Semantic Kernel?
| Capability | Benefit |
|---|---|
| Plugin architecture | Modular, reusable functionality |
| Planners | Automatic task decomposition |
| Memory | Persistent context across sessions |
| Azure integration | Native support for Azure AI services |
đź”§ Implementation Reference: Semantic Kernel
| Item | Value |
|---|---|
| Package | semantic-kernel |
| Core Classes | Kernel, ChatCompletionAgent, KernelFunction |
| Agent Classes | ChatCompletionAgent, AgentGroupChat |
| Key Decorators | @kernel_function |
Kernel Setup
The Kernel is the central orchestrator that connects AI services with plugins and memory.
Testable Pattern:
from semantic_kernel import Kernel
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion
# Create kernel
kernel = Kernel()
# Add Azure OpenAI service
kernel.add_service(AzureChatCompletion(
deployment_name="gpt-4o",
endpoint="https://your-resource.openai.azure.com/",
api_key="your-key"
))
Creating Plugins
Plugins extend agent capabilities with custom functions. Use the @kernel_function decorator to expose methods to the AI.
Plugin Pattern:
from semantic_kernel.functions import kernel_function
class WeatherPlugin:
"""Plugin for weather-related queries."""
@kernel_function(
name="get_current_weather",
description="Get the current weather for a location"
)
def get_weather(self, location: str) -> str:
# In production, call weather API
return f"Weather in {location}: Sunny, 72°F"
@kernel_function(
name="get_forecast",
description="Get weather forecast for upcoming days"
)
def get_forecast(self, location: str, days: int = 3) -> str:
return f"{days}-day forecast for {location}: Sunny then cloudy"
# Register plugin with kernel
kernel.add_plugin(WeatherPlugin(), plugin_name="weather")
Creating Agents with Semantic Kernel
ChatCompletionAgent Pattern:
from semantic_kernel.agents import ChatCompletionAgent
# Create agent with instructions
agent = ChatCompletionAgent(
service_id="gpt-4o",
kernel=kernel,
name="WeatherAssistant",
instructions="You are a helpful weather assistant. Use the weather plugin to answer questions."
)
# Invoke agent
response = await agent.invoke("What's the weather in Seattle?")
print(response.content)
Multi-Agent Chat with Semantic Kernel
AgentGroupChat Pattern:
from semantic_kernel.agents import AgentGroupChat, ChatCompletionAgent
from semantic_kernel.agents.strategies import MaxTurnsTermination
# Create specialized agents
researcher = ChatCompletionAgent(
kernel=kernel,
name="Researcher",
instructions="You research topics and provide factual information."
)
writer = ChatCompletionAgent(
kernel=kernel,
name="Writer",
instructions="You write clear, engaging content based on research."
)
editor = ChatCompletionAgent(
kernel=kernel,
name="Editor",
instructions="You review and improve written content."
)
# Create group chat
group_chat = AgentGroupChat(
agents=[researcher, writer, editor],
termination_strategy=MaxTurnsTermination(max_turns=10)
)
# Run conversation
async for message in group_chat.invoke("Write a blog post about Azure AI"):
print(f"{message.name}: {message.content}")
⚠️ Exam Trap: Semantic Kernel uses plugins (not tools like OpenAI Assistants). The terminology matters for exam questions.