Build5Nines.SharpVector
是一個專為 .NET 應用程式設計的記憶體中向量資料庫庫。它允許您使用向量表示來儲存、搜尋和管理文字資料。該庫是可自訂和可擴展的,支援不同的向量比較方法、預處理技術和向量化策略。
向量資料庫與生成式 AI 解決方案結合使用,增強了 LLM(大型語言模型),能夠使用 RAG(檢索增強生成)設計模式透過 AI 提示載入附加上下文資料。
雖然有許多大型資料庫可用於建立向量資料庫(例如 Azure CosmosDB、帶有 pgvector 的 PostgreSQL、Azure AI Search、Elasticsearch 等),但可嵌入到的輕量級向量資料庫的選項並不多。 。
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 維護。