添加项目

main
bicijinlian 1 month ago
parent f0dfcb61f9
commit 63057567be

@ -0,0 +1,100 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "4fa88424",
"metadata": {},
"source": [
"语义内核简介\n",
"==========="
]
},
{
"cell_type": "markdown",
"id": "d3a87a3d",
"metadata": {},
"source": [
"语义内核是一种轻型开源开发工具包,可用于轻松生成 AI 代理并将最新的 AI 模型集成到 C#、Python 或 Java 代码库中。 它充当一个高效的中间件,可实现企业级解决方案的快速交付。"
]
},
{
"cell_type": "markdown",
"id": "a89e87bd",
"metadata": {},
"source": [
"## 面向企业"
]
},
{
"cell_type": "markdown",
"id": "3120c4a2",
"metadata": {},
"source": [
"Microsoft和其他财富 500 强公司已经利用语义内核,因为它灵活、模块化和可观察。 通过增强安全性的功能(如遥测支持和挂钩和筛选器)提供支持,因此你有信心大规模交付负责任的 AI 解决方案。\n",
"\n",
"跨 C#、Python 和 Java 的 1.0+ 版本支持意味着它可靠,致力于非中断性变更。 可以轻松扩展任何基于聊天的现有 API以支持其他形式例如语音和视频。\n",
"\n",
"语义内核设计为未来证明,可以轻松地将代码连接到随着技术的发展而演变的最新 AI 模型。 发布新模型后,只需将它们交换出来,而无需重写整个代码库。\n",
"\n",
"![企业就绪](./Assets/Images/enterprise-ready.png)"
]
},
{
"cell_type": "markdown",
"id": "92c4f3c8",
"metadata": {},
"source": [
"## 业务流程自动化"
]
},
{
"cell_type": "markdown",
"id": "bfe6b13a",
"metadata": {},
"source": [
"语义内核将提示与现有 API 组合在一起以执行操作。 通过将现有代码描述为 AI 模型,将调用它们来处理请求。 发出请求时,模型调用函数,语义内核是将模型请求转换为函数调用的中间件,并将结果传回模型。"
]
},
{
"cell_type": "markdown",
"id": "11e159fe",
"metadata": {},
"source": [
"## 模块化和可扩展"
]
},
{
"cell_type": "markdown",
"id": "6c31d657",
"metadata": {},
"source": [
"通过将现有代码添加为插件,你可以通过一组现成的连接器灵活集成 AI 服务来最大化投资。 语义内核使用 OpenAPI 规范(如 Microsoft 365 Copilot以便你可以与公司中的其他专业或低代码开发人员共享任何扩展。\n",
"![模块化和可扩展](./Assets/Images/designed-for-modular-extensibility.png)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".NET (C#)",
"language": "C#",
"name": ".net-csharp"
},
"language_info": {
"name": "polyglot-notebook"
},
"polyglot_notebook": {
"kernelInfo": {
"defaultKernelName": "csharp",
"items": [
{
"aliases": [],
"languageName": "csharp",
"name": "csharp"
}
]
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}

@ -0,0 +1,293 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "261d91b6",
"metadata": {},
"source": [
"快速入门指南\n",
"==========="
]
},
{
"cell_type": "markdown",
"id": "034c3c59",
"metadata": {},
"source": [
"只需执行几个步骤,即可在 Python、.NET 或 Java 中使用语义内核生成第一个 AI 代理。以C#为例: \n",
"\n",
"+ 安装所需的包\n",
"+ 使用 AI 创建来回对话\n",
"+ 使 AI 代理能够运行代码\n",
"+ 观看 AI 动态创建计划"
]
},
{
"cell_type": "markdown",
"id": "70d6d69f",
"metadata": {},
"source": [
"## 初始化基础设施"
]
},
{
"cell_type": "code",
"execution_count": 37,
"id": "becd4e47",
"metadata": {
"language_info": {
"name": "polyglot-notebook"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"初始化完毕!\r\n"
]
}
],
"source": [
"#!import ./ini.ipynb"
]
},
{
"cell_type": "markdown",
"id": "f942ceb2",
"metadata": {},
"source": [
"## 安装 SDK"
]
},
{
"cell_type": "markdown",
"id": "145ba5d7",
"metadata": {},
"source": [
"语义内核有多个 NuGet 包可用。 但是,对于大多数方案,通常只需要 Microsoft.SemanticKernel。\n",
"\n",
"可以使用以下命令安装它:\n",
"\n",
"```Bash\n",
" dotnet add package Microsoft.SemanticKernel\n",
"```\n",
"\n",
"有关 Nuget 包的完整列表,请参阅 [支持的语言文章](https://learn.microsoft.com/zh-cn/semantic-kernel/get-started/supported-languages?pivots=programming-language-csharp)。"
]
},
{
"cell_type": "markdown",
"id": "6e796f0a",
"metadata": {},
"source": [
"## 快速开始使用笔记本"
]
},
{
"cell_type": "markdown",
"id": "d3ecfe91",
"metadata": {},
"source": [
"C# 开发人员,可以快速开始使用笔记本。 这些笔记本提供了有关如何使用语义内核生成 AI 代理的分步指南。\n",
"![快速开始](./Assets/Images/getting_started_notebooks.png)\n",
"\n",
"要开始,请按照以下步骤操作:\n",
"\n",
"1. 克隆[语义内核存储库](https://github.com/microsoft/semantic-kernel)\n",
"2. 在 Visual Studio Code 中打开存储库\n",
"3. 导航到 _/dotnet/notebooks\n",
"4. 打开 00-getting-started.ipynb 以开始设置环境并创建第一个 AI 代理!\n",
"\n"
]
},
{
"cell_type": "markdown",
"id": "7e1ffa9a",
"metadata": {},
"source": [
"## 编写第一个控制台应用"
]
},
{
"cell_type": "markdown",
"id": "eeba44ee",
"metadata": {},
"source": [
"1. 使用以下命令创建新的 .NET 控制台项目:\n",
"\n",
" ```bash\n",
" dotnet new console\n",
" ```"
]
},
{
"cell_type": "markdown",
"id": "765fd394",
"metadata": {},
"source": [
"2. 安装以下 .NET 依赖项:\n",
"\n",
" ```baSH\n",
" dotnet add package Microsoft.SemanticKernel\n",
" dotnet add package Microsoft.Extensions.Logging\n",
" dotnet add package Microsoft.Extensions.Logging.Console\n",
" ```"
]
},
{
"cell_type": "markdown",
"id": "8c9b17d0",
"metadata": {},
"source": [
"3. 将 Program.cs 文件的内容替换为以下代码:\n",
"\n",
"```csharp\n",
" // Import packages\n",
" using Microsoft.Extensions.DependencyInjection;\n",
" using Microsoft.Extensions.Logging;\n",
" using Microsoft.SemanticKernel;\n",
" using Microsoft.SemanticKernel.ChatCompletion;\n",
" using Microsoft.SemanticKernel.Connectors.OpenAI;\n",
"\n",
" // Populate values from your OpenAI deployment\n",
" var modelId = \"\";\n",
" var endpoint = \"\";\n",
" var apiKey = \"\";\n",
"\n",
" // Create a kernel with Azure OpenAI chat completion\n",
" var builder = Kernel.CreateBuilder().AddAzureOpenAIChatCompletion(modelId, endpoint, apiKey);\n",
"\n",
" // Add enterprise components\n",
" builder.Services.AddLogging(services => services.AddConsole().SetMinimumLevel(LogLevel.Trace));\n",
"\n",
" // Build the kernel\n",
" Kernel kernel = builder.Build();\n",
" var chatCompletionService = kernel.GetRequiredService<IChatCompletionService>();\n",
"\n",
" // Add a plugin (the LightsPlugin class is defined below)\n",
" kernel.Plugins.AddFromType<LightsPlugin>(\"Lights\");\n",
"\n",
" // Enable planning\n",
" OpenAIPromptExecutionSettings openAIPromptExecutionSettings = new() \n",
" {\n",
" FunctionChoiceBehavior = FunctionChoiceBehavior.Auto()\n",
" };\n",
"\n",
" // Create a history store the conversation\n",
" var history = new ChatHistory();\n",
"\n",
" // Initiate a back-and-forth chat\n",
" string? userInput;\n",
" do {\n",
" // Collect user input\n",
" Console.Write(\"User > \");\n",
" userInput = Console.ReadLine();\n",
"\n",
" // Add user input\n",
" history.AddUserMessage(userInput);\n",
"\n",
" // Get the response from the AI\n",
" var result = await chatCompletionService.GetChatMessageContentAsync(\n",
" history,\n",
" executionSettings: openAIPromptExecutionSettings,\n",
" kernel: kernel);\n",
"\n",
" // Print the results\n",
" Console.WriteLine(\"Assistant > \" + result);\n",
"\n",
" // Add the message from the agent to the chat history\n",
" history.AddMessage(result.Role, result.Content ?? string.Empty);\n",
" } while (userInput is not null);\n",
"\n",
"```\n",
"\n",
"以下来回聊天应类似于你在控制台中看到的内容。 下面添加了函数调用,以演示 AI 如何在后台利用插件。\n",
"\n",
"| 角色 | 消息 |\n",
"| ------------------ - | ----------- |\n",
"| 🔵 用户 | 请切换灯 |\n",
"| 🔴 助手(函数调用) | LightsPlugin.GetState() |\n",
"| 🟢 工具 | off |\n",
"| 🔴 助手(函数调用) | LightsPlugin.ChangeState(true) |\n",
"| 🟢 工具 | on |\n",
"| 🔴 助手 | 灯现在打开 |\n",
"\n",
"如果有兴趣了解有关上述代码的详细信息,我们将在下一部分中将其分解。"
]
},
{
"cell_type": "markdown",
"id": "b7e846a6",
"metadata": {},
"source": [
"## 了解代码"
]
},
{
"cell_type": "markdown",
"id": "31f3881d",
"metadata": {},
"source": [
"为了更轻松地开始使用语义内核构建企业应用,我们创建了一个分步指导你完成创建内核的过程,并使用它与 AI 服务交互。\n",
"\n",
"![过程](./Assets/Images/dotnetmap.png)\n",
"\n",
"在以下部分中,我们将通过演练步骤 1、2、3、4、6、9 和 10 来解包上述示例。 构建由 AI 服务提供支持且可以运行代码的简单代理所需的一切。\n",
"+ 导入包\n",
"+ 添加 AI 服务\n",
"+ 企业组件\n",
"+ 生成内核\n",
"+ 添加内存(已跳过)\n",
"+ 添加插件\n",
"+ 创建内核参数(已跳过)\n",
"+ 创建提示(跳过)\n",
"+ 规划\n",
"+ Invoke"
]
},
{
"cell_type": "markdown",
"id": "d9b47cb5",
"metadata": {},
"source": [
"### 1导入包"
]
},
{
"cell_type": "markdown",
"id": "382cfdf5",
"metadata": {},
"source": [
"### 2添加 AI 服务"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".NET (C#)",
"language": "C#",
"name": ".net-csharp"
},
"language_info": {
"name": "polyglot-notebook"
},
"polyglot_notebook": {
"kernelInfo": {
"defaultKernelName": "csharp",
"items": [
{
"aliases": [],
"languageName": "csharp",
"name": "csharp"
}
]
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}

@ -0,0 +1,52 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "1d5afbc3",
"metadata": {},
"source": [
"深入示例\n",
"======="
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "becd4e47",
"metadata": {
"language_info": {
"name": "polyglot-notebook"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": ".NET (C#)",
"language": "C#",
"name": ".net-csharp"
},
"language_info": {
"name": "polyglot-notebook"
},
"polyglot_notebook": {
"kernelInfo": {
"defaultKernelName": "csharp",
"items": [
{
"aliases": [],
"languageName": "csharp",
"name": "csharp"
}
]
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}

@ -0,0 +1,52 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "520bf6a8",
"metadata": {},
"source": [
"支持的语言\n",
"========="
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "becd4e47",
"metadata": {
"language_info": {
"name": "polyglot-notebook"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": ".NET (C#)",
"language": "C#",
"name": ".net-csharp"
},
"language_info": {
"name": "polyglot-notebook"
},
"polyglot_notebook": {
"kernelInfo": {
"defaultKernelName": "csharp",
"items": [
{
"aliases": [],
"languageName": "csharp",
"name": "csharp"
}
]
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 242 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 75 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 MiB

@ -0,0 +1,57 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "34fd7745",
"metadata": {
"language_info": {
"name": "polyglot-notebook"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"初始化完毕!\r\n"
]
}
],
"source": [
"/** 初始化文档\n",
" 全局共用文件,包括 Nuget包引用、全局类库引用、全局文件引用、全局命名空间引用、全局变量、全局方法、全局类定义等功能;\n",
" 在业务笔记中引用,执行其它单元格之前先执行一次。\n",
"*/\n",
"Console.WriteLine(\"初始化完毕!\");"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".NET (C#)",
"language": "C#",
"name": ".net-csharp"
},
"language_info": {
"name": "polyglot-notebook"
},
"polyglot_notebook": {
"kernelInfo": {
"defaultKernelName": "csharp",
"items": [
{
"aliases": [],
"languageName": "csharp",
"name": "csharp"
}
]
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}

File diff suppressed because one or more lines are too long

@ -0,0 +1,48 @@
global using System;
global using System.Collections.Generic;
global using System.Linq;
global using System.Text;
global using System.Threading.Tasks;
global using Microsoft.Extensions.Configuration;
global using Microsoft.Extensions.Configuration.Memory;
global using Microsoft.Extensions.Configuration.UserSecrets;
global using Microsoft.Extensions.Configuration.EnvironmentVariables;
global using Microsoft.Extensions.Configuration.CommandLine;
global using Microsoft.Extensions.Configuration.Json;
global using Microsoft.Extensions.Logging;
global using Microsoft.Extensions.Logging.Configuration;
global using Microsoft.Extensions.Logging.Console;
global using Microsoft.Extensions.Logging.Debug;
global using Microsoft.Extensions.Logging.EventLog;
global using Microsoft.Extensions.Logging.EventSource;
global using Microsoft.Extensions.Options;
global using Microsoft.Extensions.DependencyInjection;
global using Microsoft.Extensions.Hosting;
global using Microsoft.SemanticKernel;
global using Microsoft.SemanticKernel.AudioToText;
global using Microsoft.SemanticKernel.ChatCompletion;
global using Microsoft.SemanticKernel.Data;
global using Microsoft.SemanticKernel.Embeddings;
global using Microsoft.SemanticKernel.Functions;
global using Microsoft.SemanticKernel.ImageToText;
global using Microsoft.SemanticKernel.Text;
global using Microsoft.SemanticKernel.Services;
global using Microsoft.SemanticKernel.PromptTemplates;
global using Microsoft.SemanticKernel.Plugins;
global using Microsoft.SemanticKernel.Plugins.Core;
global using Microsoft.SemanticKernel.Connectors;
global using Microsoft.SemanticKernel.Connectors.InMemory;
global using Microsoft.SemanticKernel.Connectors.Ollama;
global using Microsoft.SemanticKernel.Connectors.OpenAI;
global using Microsoft.SemanticKernel.Agents;
global using Microsoft.SemanticKernel.Agents.Extensions;
global using Microsoft.SemanticKernel.Agents.Chat;
global using Microsoft.SemanticKernel.Agents.Serialization;
global using Microsoft.SemanticKernel.Agents.OpenAI;

@ -0,0 +1,49 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.8" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="9.0.8" />
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="9.0.8" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="9.0.8" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.8" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="9.0.8" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.8" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.8" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.8" />
<PackageReference Include="Microsoft.Extensions.Logging.Configuration" Version="9.0.8" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="9.0.8" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="9.0.8" />
<PackageReference Include="Microsoft.Extensions.Logging.EventLog" Version="9.0.8" />
<PackageReference Include="Microsoft.Extensions.Logging.EventSource" Version="9.0.8" />
<PackageReference Include="Microsoft.Extensions.Options" Version="9.0.8" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="9.0.8" />
<PackageReference Include="Microsoft.Extensions.Options.DataAnnotations" Version="9.0.8" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
<PackageReference Include="Microsoft.SemanticKernel" Version="1.64.0" />
<PackageReference Include="Microsoft.SemanticKernel.Agents.Core" Version="1.64.0" />
<PackageReference Include="Microsoft.SemanticKernel.Agents.OpenAI" Version="1.64.0-preview" />
<PackageReference Include="Microsoft.SemanticKernel.Connectors.InMemory" Version="1.64.0-preview" />
<PackageReference Include="Microsoft.SemanticKernel.Connectors.Ollama" Version="1.64.0-alpha" />
<PackageReference Include="Microsoft.SemanticKernel.Connectors.OpenAI" Version="1.64.0" />
<PackageReference Include="Microsoft.SemanticKernel.Plugins.Core" Version="1.64.0-preview" />
<PackageReference Include="Microsoft.SemanticKernel.PromptTemplates.Handlebars" Version="1.64.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
</ItemGroup>
<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>
</Project>

@ -0,0 +1,37 @@
using System.Threading.Tasks;
using Microsoft.Extensions.AI;
namespace SemanticKernelStudy.Test
{
public class UseXunit
{
[Fact]
public void Test1()
{
Assert.True(true,"使用 xUnit 2 框架!");
}
[Fact]
public async Task Test2()
{
var kernelBuilder = Kernel.CreateBuilder();
//添加AI服务
kernelBuilder.AddOllamaChatClient("qwen3:0.6b", new Uri("http://localhost:11434/v1"), "ollamaService");
kernelBuilder.Plugins.AddFromType<TimePlugin>();
//添加企业级服务(日志、过滤器等)
kernelBuilder.Services.AddLogging(services => services.AddConsole().SetMinimumLevel(LogLevel.Trace));
Kernel kernel = kernelBuilder.Build();
FunctionResult d = await kernel.InvokePromptAsync("你好");
ChatResponse? dd = d.GetValue<ChatResponse>();
var t = dd?.Messages.First().Text;
}
}
}

@ -0,0 +1,33 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.14.36414.22
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SemanticKernelStudy.Test", "SemanticKernelStudy.Test\SemanticKernelStudy.Test.csproj", "{3DCC1341-0A80-4BE5-A211-2F8BB8B77751}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Docs", "Docs", "{208BA388-4CEE-2CBF-559C-5607C099C1F8}"
ProjectSection(SolutionItems) = preProject
Docs\01.概述.ipynb = Docs\01.概述.ipynb
Docs\02.1.快速入门指南.ipynb = Docs\02.1.快速入门指南.ipynb
Docs\02.2.深入示例.ipynb = Docs\02.2.深入示例.ipynb
Docs\02.3.支持的语言.ipynb = Docs\02.3.支持的语言.ipynb
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{3DCC1341-0A80-4BE5-A211-2F8BB8B77751}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3DCC1341-0A80-4BE5-A211-2F8BB8B77751}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3DCC1341-0A80-4BE5-A211-2F8BB8B77751}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3DCC1341-0A80-4BE5-A211-2F8BB8B77751}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {9C65038D-5C88-4AED-96A6-CAECCC526BFE}
EndGlobalSection
EndGlobal
Loading…
Cancel
Save