Build5Nines.SharpVector
は、.NET アプリケーション用に設計されたインメモリ ベクター データベース ライブラリです。ベクトル表現を使用してテキスト データを保存、検索、管理できます。このライブラリはカスタマイズ可能で拡張可能であり、さまざまなベクトル比較方法、前処理技術、ベクトル化戦略のサポートが可能になります。
ベクトル データベースは、RAG (検索拡張生成) デザイン パターンを使用して AI プロンプトで追加のコンテキスト データを読み込む機能を備えた LLM (大規模言語モデル) を強化する生成 AI ソリューションで使用されます。
ベクター データベースの構築に使用できる大規模なデータベース (Azure CosmosDB、pgvector を使用した PostgreSQL、Azure AI Search、Elasticsearch など) は多数ありますが、これに埋め込むことができる軽量のベクター データベースのオプションはあまりありません。任意の .NET アプリケーション。
Build5Nines SharpVector プロジェクトは、あらゆる .NET アプリケーションで使用できる軽量のインメモリ ベクター データベースを提供します。
「メモリ内のベクトル データベースには、Chris Pietschmann による優れたオープンソース プロジェクトである Build5Nines.SharpVector を使用しています。SharpVector を使用すると、ベクトル化されたデータの保存と取得が簡単になるため、サンプル RAG 実装には理想的な選択肢となります。」
— Microsoft for Azure App Service プリンシパル プロダクト マネージャー、Tulika Chaudharie 氏
Build5Nines.SharpVector
のようなインメモリ ベクター データベースは、特に高性能、低遅延、効率的なリソース使用が要求されるシナリオにおいて、従来のベクター データベース サーバーに比べていくつかの利点をもたらします。
以下に、 Build5Nines.SharpVector
役立ついくつかの使用シナリオのリストを示します。
ここでは、独自のプロジェクトでBuild5Nines.SharpVector
使用する場合の追加のドキュメントと例を含む、いくつかの役立つチュートリアル リンクを示します。
Build5Nines.SharpVector
ライブラリは、.NET プロジェクトに簡単に組み込める Nuget パッケージとして利用できます。
dotnet add package Build5Nines.SharpVector
Nuget.org の https://www.nuget.org/packages/Build5Nines.SharpVector/ で表示できます。
互換性を最大限に高めるために、 Build5Nines.SharpVector
ライブラリは、.NET から入手できるもの以外の外部依存関係を使用せずに構築されており、.NET 6 以降をターゲットにするように構築されています。
次のBuild5Nines.SharpVector
ライブラリの使用例からわかるように、わずか数行のコードで、メモリ内の Vector Database を任意の .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
クラスは、コサイン類似度、辞書語彙ストア、および基本的なテキスト プリプロセッサを備えた Bag of Words ベクトル化戦略を使用します。このライブラリには、汎用クラスと、必要に応じてカスタマイズされたベクター データベース実装を作成するための多数の拡張ポイントが含まれています。
また、 TextDataLoader
を使用すると、複数の異なるテキスト チャンク化メソッドをサポートし、テキスト ドキュメントを Vector データベースにロードするのに役立ちます。
/// 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
ロード時にチャックのメタデータを簡単に定義するために使用できるラムダ関数を受け入れます。
このリポジトリのサンプル コンソール アプリは、Build5Nines.SharpVector.dll の使用例を示しています。
JSON ファイルから映画のタイトルと説明のリストを読み込み、ユーザーがプロンプトを入力してデータベースを検索し、最も一致するものを返すことができます。
これは、実行中のテスト コンソール アプリのスクリーンショットです。
BasicMemoryVectorDatabase
同期操作と非同期操作の両方をサポートするようになりました。Async
バージョンが正常に動作する不要なクラスを削除するためにリファクタリングされました。Async
バージョンを追加しました.AddText()
および.AddTextAsync()
を呼び出すときにメタデータが必要なくなりました。IVectorSimilarityCalculator
をIVectorComparer
およびCosineVectorSimilarityCalculatorAsync
からCosineSimilarityVectorComparerAsync
にリファクタリングします。EuclideanDistanceVectorComparerAsync
を追加TId
ジェネリック型を必要としないようにMemoryVectorDatabase
修正しましたVectorSimilarity
およびSimilarity
プロパティの名前をVectorComparison
に変更します。TextDataLoader
クラスを追加して、ベクター データベースにドキュメントをロードするときにテキスト チャンクのさまざまな方法をサポートします。BasicMemoryVectorDatabase
クラスが導入されました。VectorTextResultItem.Similarity
を追加しました。これにより、使用するコードでベクター検索結果内のテキストの類似性を検査できるようになります。.Search
メソッドを更新して、検索結果のページングと類似性比較のしきい値サポートをサポートします。Build5Nines SharpVector プロジェクトは、Microsoft MVP であり HashiCorp アンバサダーである Chris Pietschmann によって管理されています。