Build5Nines.SharpVector
是专为 .NET 应用程序设计的内存中矢量数据库库。它允许您使用矢量表示来存储、搜索和管理文本数据。该库是可定制和可扩展的,支持不同的矢量比较方法、预处理技术和矢量化策略。
矢量数据库与生成式 AI 解决方案结合使用,增强了 LLM(大型语言模型),能够使用 RAG(检索增强生成)设计模式通过 AI 提示加载附加上下文数据。
虽然有许多大型数据库可用于构建矢量数据库(例如 Azure CosmosDB、PostgreSQL w/ pgvector、Azure AI Search、Elasticsearch 等),但可嵌入到的轻量级矢量数据库的选项并不多。任何 .NET 应用程序。
Build5Nines SharpVector 项目提供了一个轻量级内存中矢量数据库,可在任何 .NET 应用程序中使用。
“对于内存向量数据库,我们使用 Build5Nines.SharpVector,这是 Chris Pietschmann 的一个出色的开源项目。SharpVector 可以轻松存储和检索向量化数据,使其成为我们示例 RAG 实现的理想选择。”
— Tulika Chaudharie,Microsoft Azure 应用服务首席产品经理
与传统矢量数据库服务器相比,像Build5Nines.SharpVector
这样的内存矢量数据库具有多种优势,特别是在可能需要高性能、低延迟和高效资源利用的场景中。
以下列出了Build5Nines.SharpVector
可能有用的几种使用场景:
以下是一些有用的教程链接,其中包含有关在您自己的项目中使用Build5Nines.SharpVector
其他文档和示例:
Build5Nines.SharpVector
库作为 Nuget 包提供,可以轻松包含到您的 .NET 项目中:
dotnet add package Build5Nines.SharpVector
您可以在 Nuget.org 上查看它:https://www.nuget.org/packages/Build5Nines.SharpVector/
为了获得最大的兼容性, Build5Nines.SharpVector
库的构建不使用除 .NET 提供的外部依赖项之外的任何外部依赖项,并且它是针对 .NET 6 及更高版本而构建的。
正如您通过以下Build5Nines.SharpVector
库的示例用法所看到的,只需几行代码,您就可以将内存中矢量数据库嵌入到任何 .NET 应用程序中:
// Create a Vector Database with metadata of type string
var vdb = new BasicMemoryVectorDatabase ( ) ;
// The Metadata is declared using generics, so you can store whatever data you need there.
// Load Vector Database with some sample text data
// Text is the movie description, and Metadata is the movie title with release year in this example
vdb . AddText ( " Iron Man (2008) is a Marvel Studios action, adventure, and sci-fi movie about Tony Stark (Robert Downey Jr.), a billionaire inventor and weapons developer who is kidnapped by terrorists and forced to build a weapon. Instead, Tony uses his ingenuity to build a high-tech suit of armor and escape, becoming the superhero Iron Man. He then returns to the United States to refine the suit and use it to fight crime and terrorism. " , " Iron Man (2008) " ) ;
vdb . AddText ( " The Lion King is a 1994 Disney animated film about a young lion cub named Simba who is the heir to the throne of an African savanna. " , " The Lion King (1994) " ) ;
vdb . AddText ( " Aladdin is a 2019 live-action Disney adaptation of the 1992 animated classic of the same name about a street urchin who finds a magic lamp and uses a genie's wishes to become a prince so he can marry Princess Jasmine. " , " Alladin (2019) " ) ;
vdb . AddText ( " The Little Mermaid is a 2023 live-action adaptation of Disney's 1989 animated film of the same name. The movie is about Ariel, the youngest of King Triton's daughters, who is fascinated by the human world and falls in love with Prince Eric. " , " The Little Mermaid " ) ;
vdb . AddText ( " Frozen is a 2013 Disney movie about a fearless optimist named Anna who sets off on a journey to find her sister Elsa, whose icy powers have trapped their kingdom in eternal winter. " , " Frozen (2013) " ) ;
// Perform a Vector Search
var result = vdb . Search ( newPrompt , pageCount : 5 ) ; // return the first 5 results
if ( result . HasResults )
{
Console . WriteLine ( " Similar Text Found: " ) ;
foreach ( var item in result . Texts )
{
Console . WriteLine ( item . Metadata ) ;
Console . WriteLine ( item . Text ) ;
}
}
Build5Nines.SharpVector.BasicMemoryVectorDatabase
类使用词袋向量化策略,具有余弦相似度、字典词汇存储和基本文本预处理器。该库包含通用类和大量扩展点,可以在需要时使用它创建定制的矢量数据库实现。
此外, TextDataLoader
可用于帮助将文本文档加载到矢量数据库中,并支持多种不同的文本分块方法:
/// Paragraph Chunking
var loader = new TextDataLoader < int , string > ( vdb ) ;
loader . AddDocument ( document , new TextChunkingOptions < string >
{
Method = TextChunkingMethod . Paragraph ,
RetrieveMetadata = ( chunk ) => {
// add some basic metadata since this can't be null
return " { chuckSize: " " + chunk . Length + " " } " ;
}
} ) ;
/// Sentence Chunking
var loader = new TextDataLoader < int , string > ( vdb ) ;
loader . AddDocument ( document , new TextChunkingOptions < string >
{
Method = TextChunkingMethod . Sentence ,
RetrieveMetadata = ( chunk ) => {
// add some basic metadata since this can't be null
return " { chuckSize: " " + chunk . Length + " " } " ;
}
} ) ;
/// Fixed Length Chunking
var loader = new TextDataLoader < int , string > ( vdb ) ;
loader . AddDocument ( document , new TextChunkingOptions < string >
{
Method = TextChunkingMethod . FixedLength ,
ChunkSize = 150 ,
RetrieveMetadata = ( chunk ) => {
// add some basic metadata since this can't be null
return " { chuckSize: " " + chunk . Length + " " } " ;
}
} ) ;
RetrieveMetadata
接受一个 lambda 函数,该函数可用于在加载卡盘时轻松定义卡盘的元数据。
此存储库中的示例控制台应用程序显示了 Build5Nines.SharpVector.dll 的示例用法
它从 JSON 文件加载电影标题和描述列表,然后允许用户输入提示来搜索数据库并返回最佳匹配。
这是测试控制台应用程序运行的屏幕截图:
BasicMemoryVectorDatabase
现在支持同步和异步操作。Async
版本可以正常工作。Async
版本的类以支持多线程.AddText()
和.AddTextAsync()
时不再需要元数据IVectorSimilarityCalculator
重构为IVectorComparer
并将CosineVectorSimilarityCalculatorAsync
为CosineSimilarityVectorComparerAsync
EuclideanDistanceVectorComparerAsync
MemoryVectorDatabase
以不再需要未使用的TId
泛型类型VectorSimilarity
和Similarity
属性重命名为VectorComparison
TextDataLoader
类,以在将文档加载到矢量数据库时提供对不同文本分块方法的支持。BasicMemoryVectorDatabase
类作为基本向量数据库实现,该实现使用词袋向量化策略、余弦相似度、字典词汇存储和基本文本预处理器。VectorTextResultItem.Similarity
以便使用代码可以检查矢量搜索结果中文本的相似性。.Search
方法,支持搜索结果分页和相似度比较的阈值支持Build5Nines SharpVector 项目由 Microsoft MVP、HashiCorp 大使 Chris Pietschmann 维护。