可以嵌入,搜索和緩存的客戶端矢量搜索庫。在瀏覽器和服務器端工作。
它的表現優於Openai的文本插入-ADA-002,並且比Pinecone和其他Vectordb的速度快。
我是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。
計算兩個嵌入之間的餘弦相似性。
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 ( ) ;將索引保存到持續的索引DDB數據庫中。筆記
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" ) ;