semantic kernel
什么是semantic kernel ?
semantic kernel是一种模型不可替代的SDK,它使开发人员能够构建,协调和部署AI代理和多代理系统。无论您是构建简单的聊天机器人还是复杂的多代理工作流程, semantic kernel可以通过企业级可靠性和灵活性提供所需的工具。
系统要求
- Python :3.10+
- .NET :.NET 8.0+
- Java :JDK 17+
- 操作系统支持:Windows,MacOS,Linux
关键功能
- 模型灵活性:连接到任何对Openai,Azure Openai,拥抱脸,NVIDIA等的内置支持的LLM
- 代理框架:构建具有访问工具/插件,内存和计划功能的模块化AI代理
- 多代理系统:与合作专家协调复杂的工作流程
- 插件生态系统:使用本机代码功能,提示模板,OpenAPI规格或模型上下文协议(MCP)扩展
- 向量数据库支持:与Azure AI搜索,Elasticsearch,Chroma等无缝集成
- 多模式支持:过程文本,视觉和音频输入
- 本地部署:与Ollama,Lmstudio或ONNX一起运行
- 流程框架:使用结构化工作流程方法建模复杂的业务流程
- 企业准备:构建供可观察性,安全性和稳定的API
安装
首先,为您的AI服务设置环境变量:
Azure Openai:
export AZURE_OPENAI_API_KEY=AAA....
或直接Openai:
export OPENAI_API_KEY=sk-...
Python
pip install semantic-kernel
。网
dotnet add package Microsoft.SemanticKernel dotnet add package Microsoft.SemanticKernel.Agents.Core
爪哇
有关说明,请参见Smantic-Kernel-Java构建。
Quickstart
基本代理-Python
创建一个响应用户提示的简单助手:
semantic kernel.")
print(response.content)
asyncio.run(main())
# Output:
# Language\’s essence,
# Semantic threads intertwine,
# Meaning\’s core revealed.\”>
import asyncio from semantic_kernel . agents import ChatCompletionAgent from semantic_kernel . connectors . ai . open_ai import AzureChatCompletion async def main (): # Initialize a chat agent with basic instructions agent = ChatCompletionAgent ( service = AzureChatCompletion (), name = \"SK-Assistant\" , instructions = \"You are a helpful assistant.\" , ) # Get a response to a user message response = await agent . get_response ( messages = \"Write a haiku about semantic kernel .\" ) print ( response . content ) asyncio . run ( main ()) # Output: # Language\'s essence, # Semantic threads intertwine, # Meaning\'s core revealed.
基本代理 – .NET
semantic kernel."))
{
Console.WriteLine(response.Message);
}
// Output:
// Language\’s essence,
// Semantic threads intertwine,
// Meaning\’s core revealed.\”>
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 ) ; } // Output: // Language\'s essence, // Semantic threads intertwine, // Meaning\'s core revealed.
插件的代理-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 MenuPlugin : @ kernel_function ( description = \"Provides a list of specials from the menu.\" ) def get_specials ( self ) -> Annotated [ str , \"Returns the specials from the menu.\" ]: return \"\"\" Special Soup: Clam Chowder Special Salad: Cobb Salad Special Drink: Chai Tea \"\"\" @ 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\" class MenuItem ( BaseModel ): price : float name : str async def main (): # Configure structured output format settings = OpenAIChatPromptExecutionSettings () settings . response_format = MenuItem # Create agent with plugin and settings agent = ChatCompletionAgent ( service = AzureChatCompletion (), name = \"SK-Assistant\" , instructions = \"You are a helpful assistant.\" , plugins = [ MenuPlugin ()], arguments = KernelArguments ( settings ) ) response = await agent . get_response ( messages = \"What is the price of the soup special?\" ) print ( response . content ) # Output: # The price of the Clam Chowder, which is the soup special, is $9.99. asyncio . run ( main ())
插件的代理 – .net
using System . ComponentModel ; using Microsoft . SemanticKernel ; using Microsoft . SemanticKernel . Agents ; using Microsoft . SemanticKernel . ChatCompletion ; 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 ( ) ; kernel . Plugins . Add ( KernelPluginFactory . CreateFromType < MenuPlugin > ( ) ) ; ChatCompletionAgent agent = new ( ) { Name = \"SK-Assistant\" , Instructions = \"You are a helpful assistant.\" , Kernel = kernel , Arguments = new KernelArguments ( new PromptExecutionSettings ( ) { FunctionChoiceBehavior = FunctionChoiceBehavior . Auto ( ) } ) } ; await foreach ( AgentResponseItem < ChatMessageContent > response in agent . InvokeAsync ( \"What is the price of the soup special?\" ) ) { Console . WriteLine ( response . Message ) ; } sealed class MenuPlugin { [ KernelFunction , Description ( \"Provides a list of specials from the menu.\" ) ] public string GetSpecials ( ) => \"\"\" Special Soup: Clam Chowder Special Salad: Cobb Salad Special Drink: Chai Tea \"\"\" ; [ KernelFunction , Description ( \"Provides the price of the requested menu item.\" ) ] public string GetItemPrice ( [ Description ( \"The name of the menu item.\" ) ] string menuItem ) => \"$9.99\" ; }
多代理系统-Python
建立一个可以协作的专业代理系统:
import asyncio from semantic_kernel . agents import ChatCompletionAgent , ChatHistoryAgentThread from semantic_kernel . connectors . ai . open_ai import AzureChatCompletion , OpenAIChatCompletion billing_agent = ChatCompletionAgent ( service = AzureChatCompletion (), name = \"BillingAgent\" , instructions = \"You handle billing issues like charges, payment methods, cycles, fees, discrepancies, and payment failures.\" ) refund_agent = ChatCompletionAgent ( service = AzureChatCompletion (), name = \"RefundAgent\" , instructions = \"Assist users with refund inquiries, including eligibility, policies, processing, and status updates.\" , ) triage_agent = ChatCompletionAgent ( service = OpenAIChatCompletion (), 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 ], ) thread : ChatHistoryAgentThread = None async def main () -> None : print ( \"Welcome to the chat bot! \\n Type \'exit\' to exit. \\n Try to get some billing or refund help.\" ) while True : user_input = input ( \"User:> \" ) if user_input . lower (). strip () == \"exit\" : print ( \" \\n \\n Exiting chat...\" ) return False response = await triage_agent . get_response ( messages = user_input , thread = thread , ) if response : print ( f\"Agent :> { response } \" ) # Agent :> I understand that you were charged twice for your subscription last month, and I\'m here to assist you with resolving this issue. Here’s what we need to do next: # 1. **Billing Inquiry**: # - Please provide the email address or account number associated with your subscription, the date(s) of the charges, and the amount charged. This will allow the billing team to investigate the discrepancy in the charges. # 2. **Refund Process**: # - For the refund, please confirm your subscription type and the email address associated with your account. # - Provide the dates and transaction IDs for the charges you believe were duplicated. # Once we have these details, we will be able to: # - Check your billing history for any discrepancies. # - Confirm any duplicate charges. # - Initiate a refund for the duplicate payment if it qualifies. The refund process usually takes 5-10 business days after approval. # Please provide the necessary details so we can proceed with resolving this issue for you. if __name__ == \"__main__\" : asyncio . run ( main ())
接下来去哪里
- 尝试我们的入门指南或了解建筑物的代理商
- ?探索超过100个详细样品
- 了解核心semantic kernel概念
API参考
- C#API参考
- Python API参考
故障排除
常见问题
- 身份验证错误:检查您的API密钥环境变量是否正确设置
- 模型可用性:验证您的Azure OpenAI部署或OpenAI模型访问
得到帮助
- 检查我们的GitHub问题是否已知问题
- 搜索Discord社区以寻找解决方案
- 在寻求帮助时包括您的SDK版本和完整错误消息
加入社区
我们欢迎您对SK社区的贡献和建议!参与的最简单方法之一是在GitHub存储库中进行讨论。欢迎错误报告和修复程序!
对于新功能,组件或扩展程序,请在发送PR之前与我们讨论问题。这是为了避免拒绝,因为我们可能会朝着不同的方向发展核心,同时也考虑对更大生态系统的影响。
了解更多并开始:
-
阅读文档
-
了解如何为该项目做出贡献
-
在GitHub讨论中提出问题
-
在不和谐社区中提出问题
-
参加正常的办公时间和SK社区活动
-
在我们的博客上关注团队
贡献者的名声墙
行为守则
该项目采用了Microsoft开源的行为代码。有关更多信息,请参阅《行为代码常见问题》或与OpenCode@microsoft.com联系,并提供任何其他问题或评论。
执照
版权(C)Microsoft Corporation。版权所有。
根据MIT许可获得许可。
