How to build multi-agent system with Semantic Kernel
Quick answer
Use
semantic_kernel to create multiple AI agents by defining separate skills or services within a Kernel instance. Coordinate these agents by invoking their skills programmatically, enabling a multi-agent system that collaborates on complex workflows.PREREQUISITES
Python 3.8+pip install semantic-kernelOpenAI API key (free tier works)pip install openai>=1.0
Setup
Install the semantic-kernel Python package and set your OpenAI API key as an environment variable.
- Install Semantic Kernel:
pip install semantic-kernel - Set environment variable:
export OPENAI_API_KEY='your_api_key'(Linux/macOS) orsetx OPENAI_API_KEY "your_api_key"(Windows)
pip install semantic-kernel openai Step by step
Create a Kernel instance, add multiple AI agents as services with distinct skills, and invoke them to collaborate on tasks.
import os
import semantic_kernel as sk
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion
# Initialize kernel
kernel = sk.Kernel()
# Add two AI agents with different skills
agent1 = OpenAIChatCompletion(
service_id="agent1",
api_key=os.environ["OPENAI_API_KEY"],
ai_model_id="gpt-4o-mini"
)
agent2 = OpenAIChatCompletion(
service_id="agent2",
api_key=os.environ["OPENAI_API_KEY"],
ai_model_id="gpt-4o-mini"
)
kernel.add_service(agent1)
kernel.add_service(agent2)
# Define simple skills for each agent
@kernel.skill("agent1", "greet")
def greet_agent1(input_text: str) -> str:
return f"Agent1 says: Hello, {input_text}!"
@kernel.skill("agent2", "farewell")
def farewell_agent2(input_text: str) -> str:
return f"Agent2 says: Goodbye, {input_text}!"
# Use the agents collaboratively
name = "Alice"
greeting = kernel.invoke("agent1.greet", name)
farewell = kernel.invoke("agent2.farewell", name)
print(greeting) # Agent1 says: Hello, Alice!
print(farewell) # Agent2 says: Goodbye, Alice! output
Agent1 says: Hello, Alice! Agent2 says: Goodbye, Alice!
Common variations
You can use asynchronous calls, stream responses, or switch to different AI models like gpt-4o or claude-3-5-sonnet-20241022. Also, agents can share context by passing outputs between them programmatically.
import asyncio
async def async_multi_agent():
greeting = await kernel.invoke_async("agent1.greet", "Bob")
farewell = await kernel.invoke_async("agent2.farewell", "Bob")
print(greeting)
print(farewell)
asyncio.run(async_multi_agent()) output
Agent1 says: Hello, Bob! Agent2 says: Goodbye, Bob!
Troubleshooting
- If you see authentication errors, verify your
OPENAI_API_KEYenvironment variable is set correctly. - If skills are not found, ensure you registered them with the correct
service_idand skill name. - For network issues, check your internet connection and API endpoint accessibility.
Key Takeaways
- Use
Kernelto manage multiple AI agents as services with distinct skills. - Invoke agents programmatically to build collaborative multi-agent workflows.
- Support async and streaming calls for efficient multi-agent interactions.