s
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.
Microsoft Semantic Kernel is a model-agnostic SDK that empowers developers to build, orchestrate, and deploy AI agents and multi-agent systems. It provides:
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.
pip install semantic-kernel
dotnet add package Microsoft.SemanticKernel
dotnet add package Microsoft.SemanticKernel.Agents.Core
Semantic Kernel supports OpenAI-compatible APIs, which makes it easy to integrate with APIpie.
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"
)
)
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();
| 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 |
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())
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);
}
}
}
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())
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.";
}
}
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())
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())
For more information, see the Semantic Kernel documentation or the GitHub repository.
If you encounter any issues during the integration process, please reach out on APIpie Discord for assistance.