Kernel Memory学习(1)

1、背景

RAG是现在大模型中很重要的一个功能,虽然可以自己使用一系列方法实现RAG功能,但效果可能不尽如意。而Kernel Memory就是一个RAG框架,可以实现你理想中的RAG。
有点类似:你可以手动写html、js、路由解析(route mapping)等等功能,实现一个网站功能,但现在你可以直接使用ASP.NET MVC框架或者Spring Boot框架,这样就能够聚焦于业务或者界面美化,而不用过多的去关注高级功能。当然,若要深入研究,这些框架也是能通过接口实现相关功能。Kernel Memory就是这样的RAG框架
kernel Memory的官网介绍是:

RAG architecture: index and query any data using LLM and natural language, track sources, show citations, asynchronous memory patterns.

2、具体实现

全部的代码如下

using Microsoft.KernelMemory;
using Microsoft.KernelMemory.AI.Ollama;
using Microsoft.KernelMemory.DocumentStorage.DevTools;
using Microsoft.KernelMemory.FileSystem.DevTools;
using Microsoft.KernelMemory.MemoryDb.Qdrant;
using Microsoft.KernelMemory.MemoryStorage.DevTools;
using OllamaSharp;

//【注意】:虽然都是大模型,但也会分是嵌入模型还是生成式模型,还是两者都支持
//以本例为例,all-minilm是一个嵌入模型,其功能就是将文本生成嵌入向量,不支持类似ChatGPT的对话功能
//而llama3.2是一个大模型,支持嵌入和生成式对话

//用于实现对话的大模型
var modelName = "llama3.2:3b";
//用于对数据进行向量化的大模型
//var embeddingModelName = "all-minilm";
//var embeddingModelName = "llama3.2:3b";
var ollamaEndpoint = "http://localhost:11434";

var ollamaApiClient = new OllamaApiClient(new Uri(ollamaEndpoint), modelName);
var ollamaModelConfig = new OllamaModelConfig() { ModelName = modelName };
var textEmbeddingGenerator = new OllamaTextEmbeddingGenerator(ollamaApiClient, ollamaModelConfig);

//KernelMemoryBuilderBuildOptions kernelMemoryBuilderBuildOptions = new()
//{
//    AllowMixingVolatileAndPersistentData = true
//};

var memory = new KernelMemoryBuilder()
   .WithOllamaTextGeneration(modelName, ollamaEndpoint)
   .WithOllamaTextEmbeddingGeneration(modelName, ollamaEndpoint)
#pragma warning disable KMEXP03
#region 持久化存储方式1(Qdrant)
   //下面的两句是将数据持久化的存储了(方式1)
   //.WithQdrantMemoryDb(new QdrantConfig { Endpoint = "http://127.0.0.1:6333" })
   //.WithSimpleFileStorage(new SimpleFileStorageConfig { StorageType= FileSystemTypes.Disk,Directory="temp_file_dir" })
#endregion

#region 持久化存储方式2(Disk)
   //方式2(持久化存储在硬盘上)
   //.WithSimpleVectorDb(new SimpleVectorDbConfig { StorageType = FileSystemTypes.Disk, Directory = "vector_path" })
   //.WithSimpleFileStorage(new SimpleFileStorageConfig { StorageType = FileSystemTypes.Disk,Directory="file_store" })
#endregion

#region 非持久化存储(内存方式-默认的实现方式)
   //默认就是如下的实现
   .AddIngestionMemoryDb(new SimpleVectorDb(SimpleVectorDbConfig.Volatile, textEmbeddingGenerator))
   .WithSimpleFileStorage(new SimpleFileStorageConfig { StorageType = FileSystemTypes.Volatile })
#endregion

#pragma warning restore KMEXP03
   .Build<MemoryServerless>();

var text = "北京动物园有10只猴子, 8只老虎, 6头大象, 4匹马, 100头狼,以及99头骆驼.\n\n" +
           "门票价格如下:\n\n" +
           "成人: 100元/人\n" +
           "儿童: 50元/人\n" +
           "联系方式: 13813818188\n" +
           "地址: 北京市海淀区西直门附近";

var text2 = "门票价格: 外星人: 200元/人";


await memory.ImportTextAsync(text, "doc01");
await memory.ImportTextAsync(text2, "doc02");

var query = Console.ReadLine();

while (!string.IsNullOrEmpty(query))
{
    var answer = await memory.AskAsync(query);

    Console.WriteLine("回答:"+answer);

    query = Console.ReadLine();
}

3、具体效果

在这里插入图片描述

在这里插入图片描述

4、其他说明

1、Kernel Memory的存储分为两种内容,一个是结构化数据,另外一个是文件,因此,要么两类数据全部是临时数据,要么就是全部持久化数据。若两种文件使用不同的存储方式,例如。数据使用Qdrant存储,而文件存储在内存中,即,一个是内存存储(volatile ),另外一个是非内存(持久化)(persistent )存储的的,否则会报一个错误:

Microsoft.KernelMemory.ConfigurationException:"Using a persistent memory store(PostgresMemory) with a volatile document store (SimpleFileStorage Volatile) will lead toduplicate memory records over multiple executions. Set up Kernel Memory to use a persistentdocument store like Azure Blobs, AWS S3, SimpleFileStorage on disk, etc. Otherwise. useKernelMemoryBuilderBuildOptions.AllowMixingVolatileAndPersistentData to suppress thisexception when invoking kernelMemoryBuilder.Build().

在这里插入图片描述
可以通过一下代码进行注明

//声明一个允许不同的设置
KernelMemoryBuilderBuildOptions kernelMemoryBuilderBuildOptions = new()
{
    AllowMixingVolatileAndPersistentData = true
};

//将设置赋值给 KernelMemory
var memory = new KernelMemoryBuilder()
   .WithOllamaTextGeneration(modelName, ollamaEndpoint)
   .WithOllamaTextEmbeddingGeneration(modelName, ollamaEndpoint)
   .Build<MemoryServerless>(kernelMemoryBuilderBuildOptions );
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值