如果您想快速了解语义内核如何与您的应用程序集成,请从克隆存储库开始:
[hidecontent type="logged" desc="隐藏内容:登录后可查看"]
git clone https://github.com/microsoft/semantic-kernel.git
请注意:
下面是一个快速示例,说明如何从 C# 控制台应用程序使用语义内核。
创建一个新项目,以 .NET 6 或更新版本为目标,并添加 Microsoft.SemanticKernel
nuget 包:
dotnet add package Microsoft.SemanticKernel --prerelease
有关最新版本和更多说明,请参阅nuget.org 。
将以下代码复制并粘贴到您的项目中,并准备好您的 Azure OpenAI 密钥(您可以 在此处创建一个)。
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.KernelExtensions;
using Microsoft.SemanticKernel.Configuration;
var kernel = Kernel.Builder.Build();
// For Azure Open AI details please see
// https://learn.microsoft.com/azure/cognitive-services/openai/quickstart?pivots=rest-api
kernel.Config.AddAzureOpenAITextCompletion(
"davinci-azure", // Alias used by the kernel
"text-davinci-003", // Azure OpenAI *Deployment ID*
"https://contoso.openai.azure.com/", // Azure OpenAI *Endpoint*
"...your Azure OpenAI Key..." // Azure OpenAI *Key*
);
string summarizePrompt = @"
{{$input}}
Give me the a TLDR in 5 words.";
string haikuPrompt = @"
{{$input}}
Write a futuristic haiku about it.";
var summarize = kernel.CreateSemanticFunction(summarizePrompt);
var haikuWriter = kernel.CreateSemanticFunction(haikuPrompt);
string inputText = @"
1) A robot may not injure a human being or, through inaction,
allow a human being to come to harm.
2) A robot must obey orders given it by human beings except where
such orders would conflict with the First Law.
3) A robot must protect its own existence as long as such protection
does not conflict with the First or Second Law.";
var output = await kernel.RunAsync(inputText, summarize, haikuWriter);
Console.WriteLine(output);
// Output => Robots protect us all
// No harm to humans they bring
// Peaceful coexistence
[/hidecontent]