You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

294 lines
8.2 KiB
Plaintext

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

{
"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
}