Microsoft Semantic Kernel Integration Guide

This guide will walk you through integrating Microsoft Semantic Kernel with APIpie, enabling you to build intelligent AI agents and multi-agent systems with enterprise-ready capabilities.
What is Microsoft Semantic Kernel?
Microsoft Semantic Kernel is a model-agnostic SDK that empowers developers to build, orchestrate, and deploy AI agents and multi-agent systems. It provides:
- Agent Framework: Build modular AI agents with access to tools/plugins, memory, and planning
- Multi-Agent Systems: Orchestrate complex workflows with collaborating specialist agents
- Plugin Ecosystem: Extend with native code functions, prompt templates, OpenAPI specs, or Model Context Protocol (MCP)
- Vector DB Support: Seamlessly integrate with various vector databases for knowledge retrieval
- Multimodal Support: Process text, vision, and audio inputs
- Process Framework: Model complex business processes with a structured workflow approach
- Enterprise Ready: Built for observability, security, and stable APIs
By connecting Semantic Kernel to APIpie, you gain access to a wide range of powerful language models while leveraging Semantic Kernel's sophisticated agent orchestration capabilities.
Integration Steps
1. Create an APIpie Account
- Register here: APIpie Registration
- Complete the sign-up process.
2. Add Credit
- Add Credit: APIpie Subscription
- Add credits to your account to enable API access.
3. Generate an API Key
- API Key Management: APIpie API Keys
- Create a new API key for use with Semantic Kernel.
4. Install Semantic Kernel
Python
pip install semantic-kernel
.NET
dotnet add package Microsoft.SemanticKernel
dotnet add package Microsoft.SemanticKernel.Agents.Core
5. Configure Semantic Kernel for APIpie
Semantic Kernel supports OpenAI-compatible APIs, which makes it easy to integrate with APIpie.
Python
import os
import semantic_kernel as sk
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion
# Set up the kernel
kernel = sk.Kernel()
# Add APIpie as a chat service
api_key = os.environ.get("APIPIE_API_KEY", "your-apipie-key")
service_id = "apipie-chat"
kernel.add_service(
OpenAIChatCompletion(
service_id=service_id,
ai_model_id="gpt-4o", # Use any model available on APIpie
api_key=api_key,
endpoint="https://apipie.ai/v1"
)
)
.NET
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
// Create a kernel builder
var builder = Kernel.CreateBuilder();
// Add APIpie as the chat completion service
builder.AddOpenAIChatCompletion(
modelId: "gpt-4o", // Use any model available on APIpie
apiKey: Environment.GetEnvironmentVariable("APIPIE_API_KEY") ?? "your-apipie-key",
serviceId: "apipie-chat",
endpoint: new Uri("https://apipie.ai/v1")
);
// Build the kernel
var kernel = builder.Build();
Key Features
- Model Flexibility: Connect to any LLM with built-in support for OpenAI-compatible APIs
- Agent Building Blocks: Create agents with specific instructions, tools, and memory
- Plugin System: Extend functionality with native code functions, prompt templates, or APIs
- Memory & Embeddings: Store and retrieve contextual information for more effective agents
- Planning Capabilities: Enable agents to break down complex tasks into steps
- Streaming Support: Process responses as they're generated for responsive applications
- Multi-Modal Support: Handle text, images, and other data types in agent interactions
- Cross-Platform: Available for Python, .NET, and Java developers
Example Workflows
Application Type | What Semantic Kernel Helps You Build |
---|---|
Conversational Assistants | Chatbots with memory, specialized knowledge, and tool access |
Customer Support Systems | Agents that handle different categories of support inquiries |
Research & Analysis Tools | Multi-agent systems that collect, analyze, and synthesize data |
Enterprise Workflows | Complex business processes with human-in-the-loop capabilities |
Document Processing | Systems that understand, extract, and generate content from documents |
Using Semantic Kernel with APIpie
Basic Chat Agent (Python)
import os
import asyncio
from semantic_kernel.agents import ChatCompletionAgent
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion
async def main():
# Configure the chat service with APIpie
chat_service = OpenAIChatCompletion(
ai_model_id="gpt-4o", # Use any model available on APIpie
api_key=os.environ.get("APIPIE_API_KEY", "your-apipie-key"),
endpoint="https://apipie.ai/v1"
)
# Create a simple chat agent
agent = ChatCompletionAgent(
service=chat_service,
name="APIpie-Assistant",
instructions="You are a helpful assistant that provides concise and accurate information.",
)
# Get a response to a user message
response = await agent.get_response(messages="What is the relationship between AI and machine learning?")
print(response.content)
if __name__ == "__main__":
asyncio.run(main())
Basic Chat Agent (.NET)
using System;
using System.Threading.Tasks;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents;
class Program
{
static async Task Main()
{
// Create a kernel builder
var builder = Kernel.CreateBuilder();
// Add APIpie as the chat completion service
builder.AddOpenAIChatCompletion(
modelId: "gpt-4o", // Use any model available on APIpie
apiKey: Environment.GetEnvironmentVariable("APIPIE_API_KEY") ?? "your-apipie-key",
endpoint: new Uri("https://apipie.ai/v1")
);
// Build the kernel
var kernel = builder.Build();
// Create a chat agent
var agent = new ChatCompletionAgent()
{
Name = "APIpie-Assistant",
Instructions = "You are a helpful assistant that provides concise and accurate information.",
Kernel = kernel,
};
// Get a response to a user message
await foreach (var response in agent.InvokeAsync("What is the relationship between AI and machine learning?"))
{
Console.WriteLine(response.Message?.Content);
}
}
}
Agent with Custom Tools (Python)
import os
import asyncio
from typing import Annotated
from semantic_kernel.agents import ChatCompletionAgent
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion
from semantic_kernel.functions import kernel_function, KernelArguments
# Define a custom plugin for weather information
class WeatherPlugin:
@kernel_function(description="Get the current weather for a given location.")
def get_weather(self, location: Annotated[str, "The city and country"]) -> Annotated[str, "The current weather information"]:
# In a real implementation, this would call a weather API
return f"The weather in {location} is currently sunny with a temperature of 25°C (77°F)."
@kernel_function(description="Get the weather forecast for the next few days.")
def get_forecast(self,
location: Annotated[str, "The city and country"],
days: Annotated[int, "Number of days to forecast"] = 3) -> Annotated[str, "The weather forecast"]:
# In a real implementation, this would call a weather API
return f"The {days}-day forecast for {location} shows sunny conditions with temperatures ranging from 22°C to 28°C."
async def main():
# Configure the chat service with APIpie
chat_service = OpenAIChatCompletion(
ai_model_id="gpt-4o", # Use any model available on APIpie
api_key=os.environ.get("APIPIE_API_KEY", "your-apipie-key"),
endpoint="https://apipie.ai/v1"
)
# Create a chat agent with the weather plugin
agent = ChatCompletionAgent(
service=chat_service,
name="Weather-Assistant",
instructions="You are a helpful assistant that provides weather information when asked.",
plugins=[WeatherPlugin()],
)
# Get a response to a user message
response = await agent.get_response(messages="What's the weather like in Tokyo today?")
print(response.content)
if __name__ == "__main__":
asyncio.run(main())
Agent with Custom Tools (.NET)
using System;
using System.ComponentModel;
using System.Threading.Tasks;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents;
class Program
{
static async Task Main()
{
// Create a kernel builder
var builder = Kernel.CreateBuilder();
// Add APIpie as the chat completion service
builder.AddOpenAIChatCompletion(
modelId: "gpt-4o", // Use any model available on APIpie
apiKey: Environment.GetEnvironmentVariable("APIPIE_API_KEY") ?? "your-apipie-key",
endpoint: new Uri("https://apipie.ai/v1")
);
// Build the kernel
var kernel = builder.Build();
// Add the weather plugin
kernel.Plugins.Add(KernelPluginFactory.CreateFromType<WeatherPlugin>("WeatherPlugin"));
// Create a chat agent with the weather plugin
var agent = new ChatCompletionAgent()
{
Name = "Weather-Assistant",
Instructions = "You are a helpful assistant that provides weather information when asked.",
Kernel = kernel,
Arguments = new KernelArguments({ FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() })
};
// Get a response to a user message
await foreach (var response in agent.InvokeAsync("What's the weather like in Tokyo today?"))
{
Console.WriteLine(response.Message?.Content);
}
}
}
// Define a custom plugin for weather information
public class WeatherPlugin
{
[KernelFunction, Description("Get the current weather for a given location.")]
public string GetWeather([Description("The city and country")] string location)
{
// In a real implementation, this would call a weather API
return $"The weather in {location} is currently sunny with a temperature of 25°C (77°F).";
}
[KernelFunction, Description("Get the weather forecast for the next few days.")]
public string GetForecast(
[Description("The city and country")] string location,
[Description("Number of days to forecast")] int days = 3)
{
// In a real implementation, this would call a weather API
return $"The {days}-day forecast for {location} shows sunny conditions with temperatures ranging from 22°C to 28°C.";
}
}
Multi-Agent System with Handoffs (Python)
import os
import asyncio
from semantic_kernel.agents import ChatCompletionAgent
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion
async def main():
# Configure the chat service with APIpie
chat_service = OpenAIChatCompletion(
ai_model_id="gpt-4o", # Use any model available on APIpie
api_key=os.environ.get("APIPIE_API_KEY", "your-apipie-key"),
endpoint="https://apipie.ai/v1"
)
# Create specialized agents
billing_agent = ChatCompletionAgent(
service=chat_service,
name="BillingAgent",
instructions="You handle billing issues like charges, payment methods, cycles, fees, discrepancies, and payment failures.",
)
refund_agent = ChatCompletionAgent(
service=chat_service,
name="RefundAgent",
instructions="You assist users with refund inquiries, including eligibility, policies, processing, and status updates.",
)
# Create a triage agent that can delegate to specialized agents
triage_agent = ChatCompletionAgent(
service=chat_service,
name="TriageAgent",
instructions="Evaluate user requests and forward them to BillingAgent or RefundAgent for targeted assistance. Provide the full answer to the user containing any information from the agents.",
plugins=[billing_agent, refund_agent],
)
# Process a user request
user_query = "I was charged twice for my subscription last month and need a refund."
response = await triage_agent.get_response(messages=user_query)
print(f"User: {user_query}\n\nAgent: {response.content}")
if __name__ == "__main__":
asyncio.run(main())
Using Memory for Context-Aware Responses (Python)
import os
import asyncio
from semantic_kernel import Kernel
from semantic_kernel.agents import ChatCompletionAgent
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion, OpenAITextEmbedding
from semantic_kernel.memory import VolatileMemoryStore
async def main():
# Create a kernel
kernel = Kernel()
# Configure chat service with APIpie
chat_service = OpenAIChatCompletion(
ai_model_id="gpt-4o", # Use any model available on APIpie
api_key=os.environ.get("APIPIE_API_KEY", "your-apipie-key"),
endpoint="https://apipie.ai/v1"
)
# Configure embedding service with APIpie
embedding_service = OpenAITextEmbedding(
ai_model_id="text-embedding-3-large", # Use any embedding model available on APIpie
api_key=os.environ.get("APIPIE_API_KEY", "your-apipie-key"),
endpoint="https://apipie.ai/v1"
)
# Add services to the kernel
kernel.add_service(chat_service)
kernel.add_service(embedding_service)
# Set up memory
memory_store = VolatileMemoryStore()
kernel.register_memory(memory_store)
# Add some memories
await kernel.memory.save_information_async(
collection="user_data",
id="user_preferences",
text="The user prefers vegetarian food and enjoys hiking on weekends."
)
await kernel.memory.save_information_async(
collection="user_data",
id="user_location",
text="The user lives in Seattle, Washington."
)
# Create a chat agent with access to memory
agent = ChatCompletionAgent(
service=chat_service,
name="ContextAwareAssistant",
instructions="""You are a helpful assistant that provides personalized responses based on what you know about the user.
Before answering, check if you have relevant information about the user that could help personalize your response.""",
kernel=kernel,
)
# Query the memory and include relevant context in the response
user_query = "Can you recommend some activities for this weekend?"
# Retrieve relevant memories
memories = await kernel.memory.search_async(
collection="user_data",
query=user_query,
limit=5
)
# Build context from memories
context = "User information:\n"
for memory in memories:
context += f"- {memory.text}\n"
# Create a message with the context and query
message = f"{context}\n\nUser query: {user_query}"
# Get a response from the agent
response = await agent.get_response(messages=message)
print(f"User: {user_query}\n\nAgent: {response.content}")
if __name__ == "__main__":
asyncio.run(main())
Troubleshooting & FAQ
-
How do I handle authentication with APIpie?
Store your API key securely in environment variables and pass it to the Semantic Kernel configuration. Never hardcode API keys in your code. -
Which models work best with Semantic Kernel?
While most models available on APIpie will work with Semantic Kernel, models with strong tool use/function calling capabilities (like GPT-4o or Claude 3 Opus) work best for agent-based applications. -
How do I debug agent behavior?
Enable logging in Semantic Kernel to see detailed information about each step the agent takes. For Python, use the standard logging module; for .NET, use ILogger. -
Can I mix and match different LLM providers?
Yes, Semantic Kernel allows you to configure different services for different purposes. For example, you might use APIpie for chat completions but another provider for embeddings. -
How do I handle rate limits?
Implement retry logic in your application to handle rate limits. Semantic Kernel does not have built-in retry mechanisms, so you'll need to handle this in your code. -
Does Semantic Kernel support streaming?
Yes, Semantic Kernel supports streaming responses. Use the streaming methods provided by the library to process responses as they're generated.
For more information, see the Semantic Kernel documentation or the GitHub repository.
Support
If you encounter any issues during the integration process, please reach out on APIpie Discord for assistance.