Semantic Kernel 入门系列:Memory内存

news2024/7/6 19:17:48

image

了解的运作原理之后,就可以开始使用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 了。

  

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/852269.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

视频声音怎么转换成文字?这四种转换方法很简单

将视频声音转换成文字的好处不仅仅限于方便记录、保存和查阅视频内容。它还可以大大提高视频内容的可访问性和可搜索性&#xff0c;使得非母语人士、听力障碍者等人群更容易理解视频内容&#xff0c;并且可以更快速地找到相关信息。此外&#xff0c;将视频声音转换成文字还可以…

ArcGISPro随机森林自动化调参分类预测模型展示

更改ArcGISPro的python环境变量请参考文章 ArcGISPro中如何使用机器学习脚本_Z_W_H_的博客-CSDN博客 脚本文件如下 点击运行 结果展示 负类预测概率 正类预测概率 二值化概率 文件夹&#xff08;模型验证结果&#xff09; 数据集数据库 ROC曲线 由于个人数据量太少所以…

工业4.0:欢迎来到智能制造

制造业正在经历一场被称为“工业4.0”的全新技术革命&#xff0c;这场革命将数字化、网络化、智能化和自动化技术融合在一起&#xff0c;旨在打造高质、高效、高产且可持续的智能工厂。工业4.0将彻底改变产品制造的方式&#xff0c;颠覆我们对制造业的传统认知。 什么是工业4.…

APP电话管家在各行业的应用

目前语音呼叫在各行业广泛应用&#xff0c;不管是电话销售也好&#xff0c;还是客户呼入咨询也好&#xff0c;部署呼叫中心对于业务提升&#xff0c;还是很有效率的。但是随着使用的行业越来越多&#xff0c;有些行业属性所在&#xff0c;需要有便于携带&#xff0c;企业管理可…

无涯教程-Perl - exp函数

描述 此函数将e(自然对数底数)返回到EXPR的幂。如果省略EXPR,则给出exp($_)。 语法 以下是此函数的简单语法- exp EXPRexp返回值 此函数不返回任何值。 Perl 中的 exp函数 - 无涯教程网无涯教程网提供描述此函数将e(自然对数底数)返回到EXPR的幂。如果省略EXPR,则给出exp(…

Flowable-结束事件-终止结束事件

目录 定义图形标记XML内容视频教程 定义 当流程到达终止结束事件时&#xff0c;该流程将终止。当流程实例有多个流程分支被激活时&#xff0c;当有一个 分支到达终止结束事件时&#xff0c;所有其它流程分支也立即结束。在 flowable 中&#xff0c;当流程执行到达终止结 束事件…

轻松抓取网页内容!API助力开发者,快速数据采集

在如今这个信息爆炸的时代&#xff0c;人们需要从各种渠道获取数据来支持自己的业务需求。而对于开发者们来说&#xff0c;如何快速、准确地从互联网上抓取所需的数据也成为了一项重要的技能。而抓取网页内容 API 则是一种能够帮助开发者轻松实现数据抓取的工具。 一、什么是抓…

springboot mongo 使用

nosql对我来说&#xff0c;就是用它的变动列&#xff0c;如果列是固定的&#xff0c;我为什么不用mysql这种关系型数据库呢&#xff1f; 所以&#xff0c;现在网上搜出来的大部分&#xff0c;用实体类去接的做法&#xff0c;并不适合我的需求。 所以&#xff0c;整理记录一下…

Vue命名规范

JS文件命名 一般采用的是小驼峰命名法&#xff0c;如 pieChartHelp 第一个单词小写&#xff0c;其他单词首字母大写 Components 文件命名 一般采用的是大驼峰命名法&#xff0c;如PieChart 所有单词的首字母大写 常量命名 一般全部大写&#xff0c;每个单词使用分隔符隔开&…

python_day18_socket客户端

客户端 import socket# 创建socket对象 socket_client socket.socket() # 链接服务器 socket_client.connect(("localhost", 19999))发消息 while True:# 发消息msg input("输入&#xff1a;")if msg exit:breaksocket_client.send(msg.encode("U…

Snapdrop手机电脑互传-无需下载App

软件介绍 Snapdrop&#xff1a;浏览器中的本地文件共享。灵感来自苹果的空投。 软件访问地址&#xff1a; Snapdrop官网地址 软件开源地址&#xff1a; github 软件截图

v-if与v-show造成部分元素丢失的问题——v-if复用元素问题

问题描述 在写tab切换时遇到了一个问题&#xff0c;以下为简化后的问题所在的代码&#xff1a; <img v-if"tabIndex 2" id"t1"> <div v-if"tabIndex 2" id"t2"></div> <div v-if"tabIndex 2" id&…

嘉楠勘智k230开发板上手记录(三)--K230_RVV实战

按照K230_RVV实战.md操作 在k230_sdk目录下运行&#xff0c;Makefile里默认的toolchain路径是在/opt下的&#xff0c;需要拷贝过去 cp -r toolchain /opt/ make rt-smart-apps 进入目录 src/big/rt-smart 运行脚本 source smart-env.sh riscv64 配置环境变量 source smart-e…

MyBatis和MyBatis-plus配置多数据源和操作多数据库

一&#xff0c;学习MyBatis和MyBatis-plus&#xff1a; mybatis:官网文档:mybatis – MyBatis 3 | 简介 mybatis-plus:官网文档&#xff1a;MyBatis-Plus 二&#xff0c;MyBatis来实现多数据源的查询&#xff1a; 配置文件的格式&#xff1a; spring:datasource: db1:driv…

分布式锁 -- 分布式锁的种类和原理

锁的种类 jvm进程锁和分布式锁 jvm进程锁 说明&#xff1a;jvm进程锁可以控制jvm内部多个线程的共享资源访问。常用的有synchronized和Lock &#xff0c;异同点如下&#xff1a; 1、Lock是一个接口&#xff0c;而synchronized是Java中的关键字&#xff0c;synchronized是内…

2023年8月12日(星期六):骑行渔浦寒泉。

2023年8月12日(星期六)&#xff1a;骑行渔浦寒泉&#xff0c;早8:30到9:00&#xff0c; 大观公园门囗集合&#xff0c;9:30点准时出发 【因迟到者&#xff0c;骑行速度快者&#xff0c;可自行追赶偶遇。】 偶遇地点: 大观公园门囗集合&#xff0c;家住南&#xff0c;东&#…

中级课程——CSRF

文章目录 案例原理挖掘 案例 原理 挖掘 挖掘详情 首先就是对目标敏感部位进行抓包分析&#xff0c;比如修改信息、转账、添加信息等等。通常一个数据包HTTP请求头里边都会有一个Referer&#xff0c;这个需要特别去验证。比如放到Burpsuit Repeater里边去测试&#xff1a;去掉…

基于Java+SpringBoot+Vue的数码论坛系统设计与实现(源码+LW+部署文档等)

博主介绍&#xff1a; 大家好&#xff0c;我是一名在Java圈混迹十余年的程序员&#xff0c;精通Java编程语言&#xff0c;同时也熟练掌握微信小程序、Python和Android等技术&#xff0c;能够为大家提供全方位的技术支持和交流。 我擅长在JavaWeb、SSH、SSM、SpringBoot等框架…

灰度阈值变换之c++实现(qt + 不调包)

本章介绍灰度阈值变换的基本原理和实现方法&#xff0c;它用于产生二值图。 1.基本原理 我觉得不用过多介绍&#xff0c;就是一个普通得不能再普通的二值化操作 2.代码实现&#xff08;代码是我以前自学图像处理时写的&#xff0c;代码很粗糙没做任何优化&#xff0c;但很好理解…

海外跨境贸易全开源线上商城搭建--无货源一键铺货模式

要搭建一个海外跨境贸易全开源线上商城并实现无货源一键铺货模式&#xff0c;以下是一些步骤和注意事项&#xff1a; 1. 选择适合的开源平台&#xff1a;首先&#xff0c;需要选择一个适合的开源平台作为商城的基础架构。根据企业的需求和技术实力&#xff0c;选择一个稳定、功…