مكتبة البحث على جانب العميل التي يمكن أن تضمن ، والبحث ، وذاكرة التخزين المؤقت. يعمل على المتصفح وجانب الخادم.
يتفوق على Openai من embedding-ada-002 من Openai وأسرع بكثير من Pinecone وغيرها من Vectordbs.
أنا مؤسس SearchBase.App ونحن بحاجة إلى ذلك لمنتجاتنا وعملائنا. سنستخدم هذه المكتبة في الإنتاج. يمكنك التأكد من أنه سيتم الحفاظ عليه وتحسينه.
الكثير من التحسينات قادمة!
هدفنا هو بناء بحث متجه سريع وسريع للغاية يعمل مع بضع مئات إلى آلاف المتجهات. ~ 1K ناقلات لكل مستخدم يغطي 99 ٪ من حالات الاستخدام.
سنبقي في البداية الأشياء بسيطة للغاية و 100 مللي ثانية
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 ( ) ;احفظ الفهرس إلى قاعدة بيانات INSTINGEDDB المستمرة. ملحوظة
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" ) ;