Semantic Kernel:微软的 AI 智能体编排框架

文章目录

  • [Semantic Kernel:微软的 AI 智能体编排框架](#Semantic Kernel:微软的 AI 智能体编排框架)

Semantic Kernel:微软的 AI 智能体编排框架

微软开源的 AI 编排框架 Semantic Kernel,拿到了 28,000+ Star。

Semantic Kernel 是微软推出的模型无关 SDK,用于构建、编排和部署 AI 智能体及多智能体系统。搭一个聊天机器人,或者编排一组专业智能体协同处理复杂任务,都能覆盖。

项目现在已经演进为 Microsoft Agent Framework(MAF),MAF 1.0 是正式生产版本,提供稳定 API 和长期支持,支持多智能体编排、多模型接入以及跨运行时互操作。

系统要求

  • Python 3.10+
  • .NET 10.0+
  • Java JDK 17+
  • 支持 Windows、macOS、Linux

核心能力

  • 模型灵活性:内置 OpenAI、Azure OpenAI、Hugging Face、NVidia 支持,也可用 Ollama、LMStudio、ONNX 做本地部署
  • 智能体框架:每个智能体可绑定工具插件、记忆和规划能力
  • 多智能体系统:多个专业智能体协作处理复杂工作流
  • 插件生态:通过原生代码函数、提示模板、OpenAPI 规范或 MCP 协议扩展
  • 向量数据库:支持 Azure AI Search、Elasticsearch、Chroma 等
  • 多模态:文本、视觉、音频输入均可处理

安装

设置环境变量后,通过包管理器安装:

nginx 复制代码
# Python
pip install semantic-kernel

# .NET
dotnet add package Microsoft.SemanticKernel
dotnet add package Microsoft.SemanticKernel.Agents.Core

快速上手

Python 创建一个基础智能体只需要几行代码:

python 复制代码
import asyncio
from semantic_kernel.agents import ChatCompletionAgent
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion

async def main():
    agent = ChatCompletionAgent(
        service=AzureChatCompletion(),
        name="SK-Assistant",
        instructions="You are a helpful assistant.",
    )
    response = await agent.get_response(messages="Write a haiku about Semantic Kernel.")
    print(response.content)

asyncio.run(main())

给智能体挂载自定义插件,用 @kernel_function 装饰器标记函数,声明描述和参数就行:

python 复制代码
class MenuPlugin:
    @kernel_function(description="Provides the price of the requested menu item.")
    def get_item_price(
        self, menu_item: Annotated[str, "The name of the menu item."]
    ) -> Annotated[str, "Returns the price of the menu item."]:
        return "$9.99"

多智能体场景下,定义多个专业智能体,再通过一个路由智能体做分发:

python 复制代码
billing_agent = ChatCompletionAgent(
    service=AzureChatCompletion(),
    name="BillingAgent",
    instructions="You handle billing issues.",
)

refund_agent = ChatCompletionAgent(
    service=AzureChatCompletion(),
    name="RefundAgent",
    instructions="Assist users with refund inquiries.",
)

triage_agent = ChatCompletionAgent(
    service=OpenAIChatCompletion(),
    name="TriageAgent",
    instructions="Evaluate user requests and forward them.",
    plugins=[billing_agent, refund_agent],
)

用户提问进来后,TriageAgent 判断转给 BillingAgent 还是 RefundAgent,各自处理完再汇总返回。

.NET 写法

.NET 侧通过 Kernel.CreateBuilder() 配置模型服务,挂载插件后启动对话:

csharp 复制代码
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents;

var builder = Kernel.CreateBuilder();
builder.AddAzureOpenAIChatCompletion(
    Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT"),
    Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT"),
    Environment.GetEnvironmentVariable("AZURE_OPENAI_API_KEY")
);
var kernel = builder.Build();

ChatCompletionAgent agent = new()
{
    Name = "SK-Agent",
    Instructions = "You are a helpful assistant.",
    Kernel = kernel,
};

await foreach (AgentResponseItem<ChatMessageContent> response
    in agent.InvokeAsync("Write a haiku about Semantic Kernel."))
{
    Console.WriteLine(response.Message);
}

许可协议

项目采用 MIT 协议,可自由用于商业项目。

"Write a haiku about Semantic Kernel."))

{

Console.WriteLine(response.Message);

}

复制代码
**许可协议**

项目采用 MIT 协议,可自由用于商业项目。