了解的运作原理之后,就可以开始使用Semantic Kernel来制作应用了。

Semantic Kernel将embedding的功能封装到了Memory中,用来存储上下文信息,就好像电脑的内存一样,而LLM就像是CPU一样,我们所需要做的就是从内存中取出相关的信息交给CPU处理就好了。

内存配置

使用Memory需要注册 embedding模型,目前使用的就是 text-embedding-ada-002。同时需要为Kernel添加MemoryStore,用于存储更多的信息,这里Semantic Kernel提供了一个 VolatileMemoryStore,就是一个普通的内存存储的MemoryStore。

var kernel = Kernel.Builder.Configure(c =>
{
c.AddOpenAITextCompletionService("openai", "text-davinci-003", Environment.GetEnvironmentVariable("MY_OPEN_AI_API_KEY"));
c.AddOpenAIEmbeddingGenerationService("openai", "text-embedding-ada-002", Environment.GetEnvironmentVariable("MY_OPEN_AI_API_KEY"));
})
.WithMemoryStorage(new VolatileMemoryStore())
.Build();

信息存储

完成了基础信息的注册后,就可以往Memroy中存储信息了。

const string MemoryCollectionName = "aboutMe";

await kernel.Memory.SaveInformationAsync(MemoryCollectionName, id: "info1", text: "My name is Andrea");
await kernel.Memory.SaveInformationAsync(MemoryCollectionName, id: "info2", text: "I currently work as a tourist operator");
await kernel.Memory.SaveInformationAsync(MemoryCollectionName, id: "info3", text: "I currently live in Seattle and have been living there since 2005");
await kernel.Memory.SaveInformationAsync(MemoryCollectionName, id: "info4", text: "I visited France and Italy five times since 2015");
await kernel.Memory.SaveInformationAsync(MemoryCollectionName, id: "info5", text: "My family is from New York");

SaveInformationAsync 会将text的内容通过 embedding 模型转化为对应的文本向量,存放在的MemoryStore中。其中CollectionName如同数据库的表名,Id就是Id。

语义搜索

完成信息的存储之后,就可以用来语义搜索了。

直接使用Memory.SearchAsync方法,指定对应的Collection,同时提供相应的查询问题,查询问题也会被转化为embedding,再在MemoryStore中计算查找最相似的信息。

var questions = new[]
{
"what is my name?",
"where do I live?",
"where is my family from?",
"where have I travelled?",
"what do I do for work?",
}; foreach (var q in questions)
{
var response = await kernel.Memory.SearchAsync(MemoryCollectionName, q).FirstOrDefaultAsync();
Console.WriteLine(q + " " + response?.Metadata.Text);
} // output
/*
what is my name? My name is Andrea
where do I live? I currently live in Seattle and have been living there since 2005
where is my family from? My family is from New York
where have I travelled? I visited France and Italy five times since 2015
what do I do for work? I currently work as a tourist operator
*/

到这个时候,即便不需要进行总结归纳,光是这样的语义查找,都会很有价值。

引用存储

除了添加信息以外,还可以添加引用,像是非常有用的参考链接之类的。

const string memoryCollectionName = "SKGitHub";

var githubFiles = new Dictionary<string, string>()
{
["https://github.com/microsoft/semantic-kernel/blob/main/README.md"]
= "README: Installation, getting started, and how to contribute",
["https://github.com/microsoft/semantic-kernel/blob/main/samples/notebooks/dotnet/2-running-prompts-from-file.ipynb"]
= "Jupyter notebook describing how to pass prompts from a file to a semantic skill or function",
["https://github.com/microsoft/semantic-kernel/blob/main/samples/notebooks/dotnet/Getting-Started-Notebook.ipynb"]
= "Jupyter notebook describing how to get started with the Semantic Kernel",
["https://github.com/microsoft/semantic-kernel/tree/main/samples/skills/ChatSkill/ChatGPT"]
= "Sample demonstrating how to create a chat skill interfacing with ChatGPT",
["https://github.com/microsoft/semantic-kernel/blob/main/dotnet/src/SemanticKernel/Memory/Volatile/VolatileMemoryStore.cs"]
= "C# class that defines a volatile embedding store",
["https://github.com/microsoft/semantic-kernel/tree/main/samples/dotnet/KernelHttpServer/README.md"]
= "README: How to set up a Semantic Kernel Service API using Azure Function Runtime v4",
["https://github.com/microsoft/semantic-kernel/tree/main/samples/apps/chat-summary-webapp-react/README.md"]
= "README: README associated with a sample starter react-based chat summary webapp",
};
foreach (var entry in githubFiles)
{
await kernel.Memory.SaveReferenceAsync(
collection: memoryCollectionName,
description: entry.Value,
text: entry.Value,
externalId: entry.Key,
externalSourceName: "GitHub"
);
}

同样的,使用SearchAsync搜索就行。

string ask = "I love Jupyter notebooks, how should I get started?";
Console.WriteLine("===========================\n" +
"Query: " + ask + "\n"); var memories = kernel.Memory.SearchAsync(memoryCollectionName, ask, limit: 5, minRelevanceScore: 0.77);
var i = 0;
await foreach (MemoryQueryResult memory in memories)
{
Console.WriteLine($"Result {++i}:");
Console.WriteLine(" URL: : " + memory.Metadata.Id);
Console.WriteLine(" Title : " + memory.Metadata.Description);
Console.WriteLine(" ExternalSource: " + memory.Metadata.ExternalSourceName);
Console.WriteLine(" Relevance: " + memory.Relevance);
Console.WriteLine();
}
//output
/*
===========================
Query: I love Jupyter notebooks, how should I get started? Result 1:
URL: : https://github.com/microsoft/semantic-kernel/blob/main/samples/notebooks/dotnet/Getting-Started-Notebook.ipynb
Title : Jupyter notebook describing how to get started with the Semantic Kernel
ExternalSource: GitHub
Relevance: 0.8677381632778319 Result 2:
URL: : https://github.com/microsoft/semantic-kernel/blob/main/samples/notebooks/dotnet/2-running-prompts-from-file.ipynb
Title : Jupyter notebook describing how to pass prompts from a file to a semantic skill or function
ExternalSource: GitHub
Relevance: 0.8162989178955157 Result 3:
URL: : https://github.com/microsoft/semantic-kernel/blob/main/README.md
Title : README: Installation, getting started, and how to contribute
ExternalSource: GitHub
Relevance: 0.8083238591883483
*/

这里多使用了两个参数,一个是limit,用于限制返回信息的条数,只返回最相似的前几条数据,另外一个是minRelevanceScore,限制最小的相关度分数,这个取值范围在0.0 ~ 1.0 之间,1.0意味着完全匹配。

语义问答

将Memory的存储、搜索功能和语义技能相结合,就可以快速的打造一个实用的语义问答的应用了。

只需要将搜索到的相关信息内容填充到 prompt中,然后将内容和问题都抛给LLM,就可以等着得到一个满意的答案了。

const string MemoryCollectionName = "aboutMe";

await kernel.Memory.SaveInformationAsync(MemoryCollectionName, id: "info1", text: "My name is Andrea");
await kernel.Memory.SaveInformationAsync(MemoryCollectionName, id: "info2", text: "I currently work as a tourist operator");
await kernel.Memory.SaveInformationAsync(MemoryCollectionName, id: "info3", text: "I currently live in Seattle and have been living there since 2005");
await kernel.Memory.SaveInformationAsync(MemoryCollectionName, id: "info4", text: "I visited France and Italy five times since 2015");
await kernel.Memory.SaveInformationAsync(MemoryCollectionName, id: "info5", text: "My family is from New York"); var prompt =
"""
It can give explicit instructions or say 'I don't know' if it does not have an answer. Information about me, from previous conversations:
{{ $fact }} User: {{ $ask }}
ChatBot:
"""; var skill = kernel.CreateSemanticFunction(prompt);
var ask = "Hello, I think we've met before, remember? my name is...";
var fact = await kernel.Memory.SearchAsync(MemoryCollectionName,ask).FirstOrDefaultAsync();
var context = kernel.CreateNewContext();
context["fact"] = fact?.Metadata?.Text;
context["ask"] = ask; var resultContext =await skill.InvokeAsync(context);
resultContext.Result.Dump(); //output
/*
Hi there! Yes, I remember you. Your name is Andrea, right?
*/

优化搜索过程

由于这种场景太常见了,所以Semantic Kernel中直接提供了一个技能TextMemorySkill,通过Function调用的方式简化了搜索的过程。

// .. SaveInformations 

// TextMemorySkill provides the "recall" function
kernel.ImportSkill(new TextMemorySkill()); var prompt =
"""
It can give explicit instructions or say 'I don't know' if it does not have an answer. Information about me, from previous conversations:
{{ recall $ask }} User: {{ $ask }}
ChatBot:
"""; var skill = kernel.CreateSemanticFunction(prompt);
var ask = "Hello, I think we've met before, remember? my name is..."; var context = kernel.CreateNewContext();
context["ask"] = ask;
context[TextMemorySkill.CollectionParam] = MemoryCollectionName; var resultContext =await skill.InvokeAsync(context);
resultContext.Result.Dump();
// output
/*
Hi there! Yes, I remember you. Your name is Andrea, right?
*/

这里直接使用 recall 方法,将问题传给了 TextMemorySkill,搜索对应得到结果,免去了手动搜索注入得过程。

内存的持久化

VolatileMemoryStore本身也是易丢失的,往往使用到内存的场景,其中的信息都是有可能长期存储的,起码并不会即刻过期。那么将这些信息的 embedding 能够长期存储起来,也是比较划算的事情。毕竟每一次做 embedding的转化也是需要调接口,需要花钱的。

Semantic Kernel库中包含了SQLite、Qdrant和CosmosDB的实现,自行扩展的话,也只需要实现 IMemoryStore 这个接口就可以了。

至于未来,可能就是专用的 Vector Database 了。


参考资料:

  1. https://learn.microsoft.com/en-us/semantic-kernel/concepts-sk/memories
  2. https://github.com/microsoft/semantic-kernel/blob/main/samples/notebooks/dotnet/6-memory-and-embeddings.ipynb
  3. https://github.com/johnmaeda/SK-Recipes/blob/main/e4-memories/notebook.ipynb
  4. https://learn.microsoft.com/en-us/semantic-kernel/concepts-ai/vectordb

Semantic Kernel 入门系列:🥑Memory内存的更多相关文章

  1. [转]oracle学习入门系列之五内存结构、数据库结构、进程

    原文地址:http://www.2cto.com/database/201505/399285.html 1 Oracle数据库结构 关于这个话题,网上一搜绝对一大把,更别提书籍上出现的了,还有很多大 ...

  2. How The Kernel Manages Your Memory.内核是如何管理内存的

    原文标题:How The Kernel Manages Your Memory 原文地址:http://duartes.org/gustavo/blog/ [注:本人水平有限,只好挑一些国外高手的精彩 ...

  3. 【ABAP系列】SAP ABAP模块-memory内存数据传输的例子

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP模块-memor ...

  4. linux入门系列12--磁盘管理之分区、格式化与挂载

    前面系列文章讲解了VI编辑器.常用命令.防火墙及网络服务管理,本篇将讲解磁盘管理相关知识. 本文将会介绍大量的Linux命令,其中有一部分在"linux入门系列5--新手必会的linux命令 ...

  5. Docker入门系列(一):目标和安排

    Docker入门系列(一) 这个系列的教程来源于docker的官方文档,此文档的目的在于一步一步学习docker的使用方法. 这一系列的教程有如下几篇文档: docker安装启动 构建第一个docke ...

  6. Go语言入门系列(五)之指针和结构体的使用

    Go语言入门系列前面的文章: Go语言入门系列(二)之基础语法总结 Go语言入门系列(三)之数组和切片 Go语言入门系列(四)之map的使用 1. 指针 如果你使用过C或C++,那你肯定对指针这个概念 ...

  7. ABP入门系列(1)——学习Abp框架之实操演练

    作为.Net工地搬砖长工一名,一直致力于挖坑(Bug)填坑(Debug),但技术却不见长进.也曾热情于新技术的学习,憧憬过成为技术大拿.从前端到后端,从bootstrap到javascript,从py ...

  8. Jenkins入门系列之——02第二章 Jenkins安装与配置

    2014-12-08:已不再担任SCM和CI的职位,Jenkins的文章如无必要不会再维护. 写的我想吐血,累死了. 网页看着不爽的,自己去下载PDF.有问题请留言! Jenkins入门系列之——03 ...

  9. 快速入门系列--WebAPI--03框架你值得拥有

    接下来进入的是俺在ASP.NET学习中最重要的WebAPI部分,在现在流行的互联网场景下,WebAPI可以和HTML5.单页应用程序SPA等技术和理念很好的结合在一起.所谓ASP.NET WebAPI ...

  10. Maven入门系列(二)--设置中央仓库的方法

    原文地址:http://www.codeweblog.com/maven入门系列-二-设置中央仓库的方法/ Maven仓库放在我的文档里好吗?当然不好,重装一次电脑,意味着一切jar都要重新下载和发布 ...

随机推荐

  1. 前端实现文件上传——angular版本+ant design

    html代码 <nz-upload *ngIf="uploadParams.parserTypeId==3 || uploadParams.parserTypeId==4" ...

  2. 3-XSS漏洞

    1.XSS原理 跨站脚本攻击是一种针对网站应用程序的安全漏洞攻击技术,是代码注入的一种.它允许恶意用户将代码注入网页,其他用户在浏览网页时会受到影响.恶意用户利用xss代码攻击成功后,可能得到很高的权 ...

  3. canal对接阿里云kafka

    一.前提条件 已经有mysql实例 自建或者云上都可以 已经在阿里云的kakfa创建topic 默认不自动创建topic 二.配置要监控的instance 先修改example的配置文件 conf/e ...

  4. 智汇成城 ,创赢未来 | AI+产业峰会智慧城市专场在深成功举办!

    11月4日下午,由福田区人才工作局指导,广州英码信息科技有限公司和共达地创新技术(深圳)有限公司联合主办,深圳市人工智能行业协会承办的AI+产业峰会之智慧城市专场活动在深圳市南山区成功举办. &quo ...

  5. 谈恋爱要做什么事?基于auto.js自动发早安给女朋友

    谈恋爱要做什么事?除了用心之外,每天早安晚安必然是少不了的.但是每天都发免不了会忘, 为了避免遗忘,引起不必要的尴尬,我们可以做个自动化脚本来做这件事. 1 auto.js 是什么? Auto.JS是 ...

  6. 利用MRT批量处理MOD13Q1原始数据拼接的方法

    一.在存放原始数据的文件夹里,新建立一个result文件夹.新拼接的文件夹处理过后会在这个文件夹里出现 二.利用MRT点点点保存一个prm文件在存放原始数据的文件夹里 具体操作步骤参考 青灯常伴古佛 ...

  7. vs2010 Windows程序打包成安装包方法

    1.  在vs2010 选择"新建项目"--"其他项目类型"--"Visual Studio Installerà"安装项目": ...

  8. 记录一次线上gitlab11.x升级gitlab14.x版本操作

    前言:gitlab11升级到14还是有挺多需要注意的坑,也算是做一次积累吧,升级前后,gitalb的WEB界面也变化了很多,升级过程需要注意的地方我放到最后说明,挺关键的 一.首先需要下载好要升级的包 ...

  9. JS兼容问题总结

    JS兼容问题总结 "标准浏览器"和"低版本浏览器(IE)"兼容写法 一.浏览器卷去的高度和宽度 var scrollTop = document.documen ...

  10. 如何规避MyBatis使用过程中带来的全表更新风险

    作者:京东零售 贾玉西 一.前言 程序员A: MyBatis用过吧? 程序员B: 用过 程序员A: 好巧,我也用过,那你遇到过什么风险没?比如全表数据被更新或者删除了. 程序员B: 咔,还没遇到过,这 ...