Microsoft's AI orchestration SDK for building agents, multi-agent systems, and AI workflows with Python. Supports OpenAI, Azure, Hugging Face, and 20+ LLM providers.
Install and use Microsoft's Semantic Kernel SDK to build AI agents, multi-agent systems, and orchestrated AI workflows in Python.
Semantic Kernel is a flexible agent framework that enables:
Install the base package:
```bash
pip install --upgrade semantic-kernel
```
Install with optional integrations:
```bash
pip install --upgrade semantic-kernel[hugging_face]
pip install --upgrade semantic-kernel[all]
```
**Requirements:**
Create a `.env` file in your project root or set environment variables:
```bash
OPENAI_API_KEY=sk-...
OPENAI_CHAT_MODEL_ID=gpt-4
AZURE_OPENAI_API_KEY=...
AZURE_OPENAI_ENDPOINT=https://your-resource.openai.azure.com/
AZURE_OPENAI_CHAT_DEPLOYMENT_NAME=gpt-4
```
Or pass configuration directly to the service constructor:
```python
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion
chat_service = AzureChatCompletion(
api_key="your-api-key",
endpoint="https://your-resource.openai.azure.com/",
deployment_name="gpt-4",
)
```
Semantic Kernel supports:
Use the `Kernel` to invoke templated prompts with variable substitution:
```python
import asyncio
from semantic_kernel import Kernel
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion
from semantic_kernel.functions import KernelArguments
kernel = Kernel()
kernel.add_service(OpenAIChatCompletion())
prompt = """
Summarize the following text in exactly {{$num_words}} words:
{{$text}}
"""
async def main():
result = await kernel.invoke_prompt(
prompt,
arguments=KernelArguments(
num_words=10,
text="Semantic Kernel is a flexible AI orchestration framework..."
)
)
print(result)
asyncio.run(main())
```
**Use this when:** You need templated prompts with variable injection, or want centralized AI service management.
Call AI services directly without the Kernel abstraction for low-level control:
```python
import asyncio
from semantic_kernel.connectors.ai.open_ai import (
OpenAIChatCompletion,
OpenAIChatPromptExecutionSettings
)
from semantic_kernel.contents import ChatHistory
async def main():
service = OpenAIChatCompletion()
settings = OpenAIChatPromptExecutionSettings(temperature=0.7)
chat_history = ChatHistory(system_message="You are a helpful assistant.")
chat_history.add_user_message("Write a haiku about AI agents.")
response = await service.get_chat_message_content(
chat_history=chat_history,
settings=settings
)
print(response.content)
asyncio.run(main())
```
**Use this when:** You need full control over chat history, settings, or streaming responses.
Create agents with custom Python functions as tools and structured outputs:
```python
import asyncio
from typing import Annotated
from pydantic import BaseModel
from semantic_kernel.agents import ChatCompletionAgent
from semantic_kernel.connectors.ai.open_ai import (
AzureChatCompletion,
OpenAIChatPromptExecutionSettings
)
from semantic_kernel.functions import kernel_function, KernelArguments
class RestaurantPlugin:
@kernel_function(description="Get today's menu specials")
def get_specials(self) -> Annotated[str, "Returns menu specials"]:
return "Today's specials: Lobster Bisque ($12), Caesar Salad ($8)"
@kernel_function(description="Get price for a menu item")
def get_price(
self, item: Annotated[str, "Menu item name"]
) -> Annotated[str, "Returns price"]:
prices = {"Lobster Bisque": "$12", "Caesar Salad": "$8"}
return prices.get(item, "Item not found")
class MenuItem(BaseModel):
name: str
price: float
async def main():
settings = OpenAIChatPromptExecutionSettings()
settings.response_format = MenuItem # Force structured output
agent = ChatCompletionAgent(
service=AzureChatCompletion(),
name="Restaurant-Assistant",
instructions="You help customers with menu questions.",
plugins=[RestaurantPlugin()],
arguments=KernelArguments(settings=settings)
)
response = await agent.get_response("What's the soup special price?")
print(response.content)
asyncio.run(main())
```
**Use this when:** You need AI agents that can call external tools/APIs or return structured data (Pydantic models).
Coordinate multiple specialized agents to collaborate on complex tasks:
```python
import asyncio
from semantic_kernel.agents import (
ChatCompletionAgent,
GroupChatOrchestration,
RoundRobinGroupChatManager
)
from semantic_kernel.agents.runtime import InProcessRuntime
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion
async def main():
# Define specialized agents
writer = ChatCompletionAgent(
name="Writer",
instructions="Generate creative marketing copy.",
service=AzureChatCompletion(),
)
reviewer = ChatCompletionAgent(
name="Reviewer",
instructions="Critically evaluate copy and suggest improvements.",
service=AzureChatCompletion(),
)
# Set up group chat with round-robin orchestration
group_chat = GroupChatOrchestration(
members=[writer, reviewer],
manager=RoundRobinGroupChatManager(max_rounds=3),
)
runtime = InProcessRuntime()
runtime.start()
result = await group_chat.invoke(
task="Create a tagline for an eco-friendly water bottle.",
runtime=runtime,
)
final_output = await result.get()
print(f"Final Tagline: {final_output}")
await runtime.stop_when_idle()
asyncio.run(main())
```
**Use this when:** You need multiple AI agents to iteratively collaborate (e.g., writer → reviewer → editor workflows).
Model structured business workflows with the Process Framework (separate from agents):
```python
from semantic_kernel.processes import Process, ProcessStep
class DataValidationStep(ProcessStep):
async def execute(self, context):
# Validation logic
pass
```
Connect to vector stores for retrieval-augmented generation (RAG):
```python
from semantic_kernel.connectors.memory.azure_ai_search import AzureAISearchMemoryStore
```
Stream LLM responses token-by-token:
```python
async for message in service.get_streaming_chat_message_contents(chat_history, settings):
print(message.content, end="", flush=True)
```
1. **Async-First Design**: All LLM calls use `asyncio` — wrap code in `async def main()` and run with `asyncio.run(main())`
2. **Rate Limits**: Respect provider rate limits (especially OpenAI) — implement retry logic or use built-in retry settings
3. **Token Costs**: Monitor token usage with `response.metadata["usage"]` — long chat histories increase costs
4. **Plugin Security**: Never expose sensitive operations in plugins without validation — treat plugins as external APIs
5. **Structured Outputs**: Use Pydantic models + `response_format` for reliable JSON extraction (requires OpenAI models with JSON mode)
**Use Semantic Kernel when:**
**Consider alternatives when:**
**Issue**: `ImportError: No module named 'semantic_kernel'`
**Fix**: Run `pip install --upgrade semantic-kernel` in your virtual environment
**Issue**: Authentication errors with Azure OpenAI
**Fix**: Verify `AZURE_OPENAI_API_KEY` and `AZURE_OPENAI_ENDPOINT` are set correctly. Check Azure portal for correct deployment name.
**Issue**: Agents not calling plugins
**Fix**: Ensure plugin methods have `@kernel_function` decorator and clear `description` parameters. Use function calling–capable models (GPT-4, GPT-3.5-turbo).
**Issue**: Structured outputs return plain text
**Fix**: Verify your model supports JSON mode (OpenAI GPT-4/3.5-turbo). Set `response_format=YourPydanticModel` in `OpenAIChatPromptExecutionSettings`.
1. **Start simple**: Begin with Pattern 1 (prompt engineering) to learn the Kernel API
2. **Add tools**: Experiment with Pattern 3 to give agents custom capabilities
3. **Scale to multi-agent**: Use Pattern 4 for complex workflows requiring specialization
4. **Explore examples**: Clone the [Semantic Kernel repo](https://github.com/microsoft/semantic-kernel) and run samples locally
5. **Join the community**: [Discord](https://aka.ms/SKDiscord) | [GitHub Discussions](https://github.com/microsoft/semantic-kernel/discussions)
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/semantic-kernel-python-integration/raw