语义核心:让您的LLM应用更强大

什么是语义核心?

ChatGPT中文站
Semantic Kernel flow

语义核心(SK)是一款创新且轻量级的软件开发工具包(SDK),可方便地将AI大型语言模型(LLM)与传统编程语言进行集成。SK可扩展的编程模型将自然语言语义函数与传统代码原生函数和基于嵌入式内存相结合。这种强大的组合可以释放新的潜力,并为AI应用增加价值。

SK概念的出现是因为需要将AI大型语言模型(LLMs)的能力与传统编程语言相结合,而不会影响任何一方的表达能力。SK SDK为开发人员提供了一组工具,可以让他们在应用程序中利用LLMs的能力,同时仍然能够编写传统代码。

为什么需要语义内核?

语义核心平台是开发人员寻求在现有应用程序中集成人工智能的完美解决方案。凭借其灵活的架构,SK使开发人员可以轻松添加新的人工智能功能,而无需担心管理基础架构。通过利用SK的威力,开发人员可以专注于构建其业务逻辑和创建新功能,而平台则负责其余部分。无论您是要在应用程序中添加自然语言处理、计算机视觉还是其他前沿的人工智能功能,SK都可以为您提供支持。它是开源的。

语义核心的主要特点

SK的关键特性包括其可扩展性,使开发人员可以向系统添加新的自然语言语义功能;基于嵌入的记忆,使系统能够记住概念之间的上下文和关系;以及支持链接,允许在单个操作中合并多个语义功能。

核心功能:

  • 规划者:根据用户的目标自动生成并执行复杂任务。
  • 技能/插件:可在不同应用程序中重复使用的自定义组件。
  • 记忆:将上下文和嵌入存储在内存或其他存储器中。
  • 连接器:面向数据源(如Microsoft Graph)的开箱即用连接器
  • 自定义函数:自定义逻辑
  • 链接:链接请求以建立管道。

语义内核支持C#和Python。

使用案例SK有很多使用案例和场景,如建立由LLM驱动的复杂用户流程和管道。

常见用例:

  • 允许组织查询自己数据的聊天机器人
  • 文档摘要
  • 客户服务自动化

最强大的SK替代品是LangChain(欢迎来到LangChain - ?? LangChain 0.0.194)。目前,LangChain拥有更多的集成和插件。但是,如果您专注于.NET领域,则语义内核是您构建LLM应用程序的好选择。

示例

让我们开发一个可以让我们向PDF文件提问的应用程序。我们将使用一种常用的测试样本来查询PDF文件——bitcoin.pdf。

  1. 初始化语义内核
var configBuilder = new ConfigurationBuilder()
.AddJsonFile(“appsettings.json”, optional: true, reloadOnChange: true)
.AddJsonFile(“appsettings.development.json”, optional: true, reloadOnChange: true)
.Build();

//Initialize SK
var embeddingOptions = configBuilder.GetSection("Embedding").Get<AzureOpenAIOptions>();
var completionOptions = configBuilder.GetSection("Completion").Get<AzureOpenAIOptions>();

var kernel = Kernel.Builder
.WithAzureTextEmbeddingGenerationService(embeddingOptions.DeploymentId, embeddingOptions.Endpoint,
embeddingOptions.Key)
.WithAzureChatCompletionService(completionOptions.DeploymentId, completionOptions.Endpoint, completionOptions.Key)
.WithMemoryStorage(new VolatileMemoryStore())
.Build();

2. 解析PDF文件并初始化SK内存。

//Parse PDF files and initialize SK memory
var pdfFiles = Directory.GetFiles(Directory.GetCurrentDirectory(), “*.pdf”);

foreach (var pdfFileName in pdfFiles)
{
using var pdfDocument = UglyToad.PdfPig.PdfDocument.Open(pdfFileName);
foreach (var pdfPage in pdfDocument.GetPages())
{
var pageText = ContentOrderTextExtractor.GetText(pdfPage);
var paragraphs = new List<string>();

//We need to break long content to smaller pieces
if (pageText.Length > MAX_CONTENT_ITEM_SIZE)
{
var lines = TextChunker.SplitPlainTextLines(pageText, MAX_CONTENT_ITEM_SIZE);
paragraphs = TextChunker.SplitPlainTextParagraphs(lines, MAX_CONTENT_ITEM_SIZE);
}
else
{
paragraphs.Add(pageText);
}
foreach (var paragraph in paragraphs)
{
var id = pdfFileName + pdfPage.Number + paragraphs.IndexOf(paragraph);
await kernel.Memory.SaveInformationAsync(memoryCollectionName, paragraph, id);
}
}
}

kernel.ImportSkill(new TextMemorySkill(), nameof(TextMemorySkill));

//Initialize skill that will be used for prompt

var promptTemplate = await File.ReadAllTextAsync("prompt.txt");

kernel.CreateSemanticFunction(promptTemplate,
"Query",
"QuerySkill",
maxTokens: 2048);

3. 提问

提示:

{{textmemoryskill.recall $input}}
— -
You are an intelligent assistant helping Contoso Inc employee.
Use ‘you’ to refer to the individual asking the questions even if they ask with ‘I’.
Answer the following question using only the data provided in the sources below.
If you cannot answer using the sources below, say you don’t know.

Question: {{$input}}
var query = “How is the work by \”R.C. Merkle\” used in this paper?”;

//Set memory for query
var contextVariables = new ContextVariables(query);
contextVariables.Set("collection", memoryCollectionName);

var result = await kernel.RunAsync(contextVariables, kernel.Skills.GetFunction("QuerySkill", "Query"));

if (result.ErrorOccurred)
{
Console.WriteLine($"Error: {result.LastErrorDescription}");
}
else
{
Console.WriteLine(result.Result);
}

4. 响应

The work by R.C. Merkle is referenced in the paper as “Protocols for public key cryptosystems” 
and is mentioned as a source in the list of references.
However, it is not specified how it is used in the paper.

源代码:DevRainSolutions/semantic-kernel-chat-demo(github.com)

资源

  • Github:语义内核
  • 使用语义内核提高技能 - LinkedIn Learning(免费): https://www.linkedin.com/learning/building-skills-with-semantic-kernel/building-skills
  • 微软开发博客:语义内核 | 语义内核团队为开发人员提供的最新消息(microsoft.com)
  • 微软文档:什么是语义内核?| 微软学习

2023-10-20 16:58:41 AI中文站翻译自原文