埋め込み、検索、キャッシュできるクライアント側のベクトル検索ライブラリ。ブラウザとサーバー側で動作します。
OpenaiのText-embedding-Ada-002を上回り、Pineconeや他のVectordbsよりもはるかに高速です。
私はSearchBase.Appの創設者であり、製品と顧客にこれが必要でした。このライブラリを制作で使用します。あなたはそれが維持され、改善されることを確認することができます。
たくさんの改善が来ています!
私たちの目標は、数百から数千のベクトルで動作する非常にシンプルで高速なベクトル検索を構築することです。ユーザーあたりの〜1Kベクターは、ユースケースの99%をカバーしています。
当初、物事を非常にシンプルで100msサブに保ちます
npm i client-vector-searchこのライブラリは、埋め込みおよびベクトル検索用のプラグアンドプレイソリューションを提供します。使いやすく、効率的で多用途なように設計されています。クイックスタートガイドは次のとおりです。
import { getEmbedding , EmbeddingIndex } from 'client-vector-search' ;
// getEmbedding is an async function, so you need to use 'await' or '.then()' to get the result
const embedding = await getEmbedding ( "Apple" ) ; // Returns embedding as number[]
// Each object should have an 'embedding' property of type number[]
const initialObjects = [
{ id : 1 , name : "Apple" , embedding : embedding } ,
{ id : 2 , name : "Banana" , embedding : await getEmbedding ( "Banana" ) } ,
{ id : 3 , name : "Cheddar" , embedding : await getEmbedding ( "Cheddar" ) } ,
{ id : 4 , name : "Space" , embedding : await getEmbedding ( "Space" ) } ,
{ id : 5 , name : "database" , embedding : await getEmbedding ( "database" ) } ,
] ;
const index = new EmbeddingIndex ( initialObjects ) ; // Creates an index
// The query should be an embedding of type number[]
const queryEmbedding = await getEmbedding ( 'Fruit' ) ; // Query embedding
const results = await index . search ( queryEmbedding , { topK : 5 } ) ; // Returns top similar objects
// specify the storage type
await index . saveIndex ( 'indexedDB' ) ;
const results = await index . search ( [ 1 , 2 , 3 ] , {
topK : 5 ,
useStorage : 'indexedDB' ,
// storageOptions: { // use only if you overrode the defaults
// indexedDBName: 'clientVectorDB',
// indexedDBObjectStoreName: 'ClientEmbeddingStore',
// },
} ) ;
console . log ( results ) ;
await index . deleteIndexedDB ( ) ; // if you overrode default, specify db name NextJSプロジェクト内でそれを使用するには、次を含めるためにnext.config.jsファイルを更新する必要があります。
module . exports = {
// Override the default webpack configuration
webpack : ( config ) => {
// See https://webpack.js.org/configuration/resolve/#resolvealias
config . resolve . alias = {
... config . resolve . alias ,
sharp$ : false ,
"onnxruntime-node$" : false ,
} ;
return config ;
} ,
} ; モデルを使用する前に、埋め込みを生成する前にモデルを初期化できます。これにより、モデルが使用する前にモデルがロードされ、より良いUXが提供されます。
import { initializeModel } from "client-vector-search"
. . .
useEffect ( ( ) => {
try {
initializeModel ( ) ;
} catch ( e ) {
console . log ( e ) ;
}
} , [ ] ) ; このガイドは、図書館の主な機能の段階的なウォークスルーを提供します。文字列の埋め込みの生成から、オブジェクトの追加、更新、削除など、インデックスで操作を実行することまで、すべてをカバーします。また、インデックスをデータベースに保存し、その中の検索操作を実行する方法に関する指示も含まれています。
参照ドキュメントが作成されるまで、このガイドではすべての方法とその使用法を見つけることができます。各ステップには、問題のメソッドの使用法を説明するために、コードスニペットが添付されています。すべてがどのように機能するかをよりよく理解するために、自分の環境の例をフォローして試してみてください。
始めましょう!
getEmbeddingメソッドを使用して、特定の文字列の埋め込みを生成します。
const embedding = await getEmbedding ( "Apple" ) ; // Returns embedding as number[]注:
getEmbeddingは非同期です。必ずawaitようにしてください。
2つの埋め込み間のコサインの類似性を計算します。
const similarity = cosineSimilarity ( embedding1 , embedding2 , 6 ) ;注:両方の埋め込みは同じ長さである必要があります。
オブジェクトの最初の配列でインデックスを作成します。各オブジェクトには、「埋め込み」プロパティが必要です。
const initialObjects = [ ... ] ;
const index = new EmbeddingIndex ( initialObjects ) ;インデックスにオブジェクトを追加します。
const objectToAdd = { id : 6 , name : 'Cat' , embedding : await getEmbedding ( 'Cat' ) } ;
index . add ( objectToAdd ) ;インデックス内の既存のオブジェクトを更新します。
const vectorToUpdate = { id : 6 , name : 'Dog' , embedding : await getEmbedding ( 'Dog' ) } ;
index . update ( { id : 6 } , vectorToUpdate ) ;インデックスからオブジェクトを削除します。
index . remove ( { id : 6 } ) ;インデックスからオブジェクトを取得します。
const vector = index . get ( { id : 1 } ) ;クエリの埋め込みでインデックスを検索します。
const queryEmbedding = await getEmbedding ( 'Fruit' ) ;
const results = await index . search ( queryEmbedding , { topK : 5 } ) ;インデックス全体をコンソールに印刷します。
index . printIndex ( ) ;インデックスを永続的なindexedDBデータベースに保存します。注記
await index . saveIndex ( "indexedDB" , { DBName : "clientVectorDB" , objectStoreName : "ClientEmbeddingStore" } )indexedDBで検索操作を実行します。
const results = await index . search ( queryEmbedding , {
topK : 5 ,
useStorage : "indexedDB" ,
storageOptions : { // only if you want to override the default options, defaults are below
indexedDBName : 'clientVectorDB' ,
indexedDBObjectStoreName : 'ClientEmbeddingStore'
}
} ) ;
-- -
### Delete Database
To delete an entire database .
`` ` ts
await IndexedDbManager . deleteIndexedDB ( "clientVectorDB" ) ;データベースからオブジェクトストアを削除します。
await IndexedDbManager . deleteIndexedDBObjectStore ( "clientVectorDB" , "ClientEmbeddingStore" ) ;特定のオブジェクトストアからすべてのオブジェクトを取得する。
const allObjects = await IndexedDbManager . getAllObjectsFromIndexedDB ( "clientVectorDB" , "ClientEmbeddingStore" ) ;