- ทำงานระหว่างดำเนินการ
Voy อยู่ระหว่างการพัฒนาอย่างแข็งขัน เป็นผลให้ API ไม่มั่นคง โปรดทราบว่าอาจมีการเปลี่ยนแปลงก่อนการเปิดตัว 1.0 ที่จะเกิดขึ้น
แอบดูสิ่งที่เรากำลังทำอยู่:
- การแปลงข้อความในตัวใน WebAssembly: ณ ตอนนี้ Voy อาศัยไลบรารี JavaScript เช่น
transformers.js
เพื่อสร้างการฝังข้อความ ดูการใช้งานเพื่อดูรายละเอียดเพิ่มเติม- การอัปเดตดัชนี: ปัจจุบันจำเป็นต้องสร้างดัชนีอีกครั้งเมื่อมีการอัปเดตทรัพยากร
- การสนับสนุน TypeScript: เนื่องจากข้อ จำกัด ของเครื่องมือ WASM ชนิดข้อมูลที่ซับซ้อนจึงไม่ได้สร้างอัตโนมัติ
# with npm
npm i voy-search
# with Yarn
yarn add voy-search
# with pnpm
pnpm add voy-search
class Voy
คลาส Voy ห่อหุ้มดัชนีและเปิดเผยวิธีการสาธารณะทั้งหมดที่ Voy มีให้
class Voy {
/**
* By instantiating with a resource, Voy will construct the index. If the resource is
* absent, it will construct an empty index. Calling Voy.index() later on will override
* the empty index.
* @param {Resource | undefined} resource
*/
constructor ( resource ?: Resource ) ;
/**
* Index given resource. Voy.index() is designed for the use case where a Voy instance
* is instantiated without a resource. It will override the existing index. If you'd like
* to keep the existing index, you can use Voy.add() to add your resource to the index.
* @param {Resource} resource
*/
index ( resource : Resource ) : void ;
/**
* Search top k results with given query embedding.
* @param {Float32Array} query: Query Embedding
* @param {number} k: Number of items in the search result
* @returns {SearchResult}
*/
search ( query : Float32Array , k : number ) : SearchResult ;
/**
* Add given resource to the index.
* @param {Resource} resource
*/
add ( resource : Resource ) : void ;
/**
* Remove given resource from the index.
* @param {Resource} resource
*/
remove ( resource : Resource ) : void ;
/**
* Remove all resources from the index.
*/
clear ( ) : void ;
/**
* @returns {number}
*/
size ( ) : number ;
/**
* Serialize a Voy instance.
* @returns {string}
*/
serialize ( ) : string ;
/**
* Deserialize a serialized index into a Voy instance.
* @param {string} serialized_index
* @returns {Voy}
*/
static deserialize ( serialized_index : string ) : Voy ;
}
interface Resource {
embeddings : Array < {
id : string ; // id of the resource
title : string ; // title of the resource
url : string ; // url to the resource
embeddings : number [ ] ; // embeddings of the resource
} > ;
}
interface SearchResult {
neighbors : Array < {
id : string ; // id of the resource
title : string ; // title of the resource
url : string ; // url to the resource
} > ;
}
นอกจากคลาส Voy แล้ว Voy ยังส่งออกวิธีการทั้งหมดเป็นฟังก์ชั่นของแต่ละบุคคล
index(resource: Resource): SerializedIndex
มันดัชนีทรัพยากรที่กำหนดและส่งคืนดัชนีที่เป็นอนุกรม
พารามิเตอร์
interface Resource {
embeddings : Array < {
id : string ; // id of the resource
title : string ; // title of the resource
url : string ; // url to the resource
embeddings : number [ ] ; // embeddings of the resource
} > ;
}
กลับ
type SerializedIndex = string ;
search(index: SerializedIndex, query: Query, k: NumberOfResult): SearchResult
มัน deserializes ดัชนีที่กำหนดและค้นหา k
ที่ใกล้ที่สุดของการสืบค้น
พารามิเตอร์
type SerializedIndex = string ;
type Query = Float32Array ; // embeddings of the search query
type NumberOfResult = number ; // K top results to return
กลับ
interface SearchResult {
neighbors : Array < {
id : string ; // id of the resource
title : string ; // title of the resource
url : string ; // url to the resource
} > ;
}
add(index: SerializedIndex, resource: Resource): SerializedIndex
มันเพิ่มทรัพยากรให้กับดัชนีและส่งคืนดัชนีอนุกรมที่อัปเดต
พารามิเตอร์
type SerializedIndex = string ;
interface Resource {
embeddings : Array < {
id : string ; // id of the resource
title : string ; // title of the resource
url : string ; // url to the resource
embeddings : number [ ] ; // embeddings of the resource
} > ;
}
กลับ
type SerializedIndex = string ;
remove(index: SerializedIndex, resource: Resource): SerializedIndex
มันลบทรัพยากรออกจากดัชนีและส่งคืนดัชนีอนุกรมที่อัปเดต
พารามิเตอร์
type SerializedIndex = string ;
interface Resource {
embeddings : Array < {
id : string ; // id of the resource
title : string ; // title of the resource
url : string ; // url to the resource
embeddings : number [ ] ; // embeddings of the resource
} > ;
}
กลับ
type SerializedIndex = string ;
clear(index: SerializedIndex): SerializedIndex
มันจะลบรายการทั้งหมดออกจากดัชนีและส่งคืนดัชนีอนุกรมที่ว่างเปล่า
พารามิเตอร์
type SerializedIndex = string ;
กลับ
type SerializedIndex = string ;
size(index: SerializedIndex): number;
มันส่งคืนขนาดของดัชนี
พารามิเตอร์
type SerializedIndex = string ;
ณ ตอนนี้ Voy อาศัยห้องสมุดเช่น transformers.js
และ web-ai
เพื่อสร้าง embeddings สำหรับข้อความ:
import { TextModel } from "@visheratin/web-ai" ;
const { Voy } = await import ( "voy-search" ) ;
const phrases = [
"That is a very happy Person" ,
"That is a Happy Dog" ,
"Today is a sunny day" ,
] ;
const query = "That is a happy person" ;
// Create text embeddings
const model = await ( await TextModel . create ( "gtr-t5-quant" ) ) . model ;
const processed = await Promise . all ( phrases . map ( ( q ) => model . process ( q ) ) ) ;
// Index embeddings with voy
const data = processed . map ( ( { result } , i ) => ( {
id : String ( i ) ,
title : phrases [ i ] ,
url : `/path/ ${ i } ` ,
embeddings : result ,
} ) ) ;
const resource = { embeddings : data } ;
const index = new Voy ( resource ) ;
// Perform similarity search for a query embeddings
const q = await model . process ( query ) ;
const result = index . search ( q . result , 1 ) ;
// Display search result
result . neighbors . forEach ( ( result ) =>
console . log ( ` voy similarity search result: " ${ result . title } "` )
) ;
import { TextModel } from "@visheratin/web-ai" ;
const { Voy } = await import ( "voy-search" ) ;
const phrases = [
"That is a very happy Person" ,
"That is a Happy Dog" ,
"Today is a sunny day" ,
"Sun flowers are blooming" ,
] ;
const model = await ( await TextModel . create ( "gtr-t5-quant" ) ) . model ;
const processed = await Promise . all ( phrases . map ( ( q ) => model . process ( q ) ) ) ;
const data = processed . map ( ( { result } , i ) => ( {
id : String ( i ) ,
title : phrases [ i ] ,
url : `/path/ ${ i } ` ,
embeddings : result ,
} ) ) ;
const resourceA = { embeddings : data . slice ( 0 , 2 ) } ;
const resourceB = { embeddings : data . slice ( 2 ) } ;
const indexA = new Voy ( resourceA ) ;
const indexB = new Voy ( resourceB ) ;
ได้รับใบอนุญาตภายใต้
ตามตัวเลือกของคุณ
หากคุณไม่ได้ระบุอย่างชัดเจนการบริจาคใด ๆ ที่ส่งโดยเจตนาเพื่อรวมไว้ในงานโดยคุณตามที่กำหนดไว้ในใบอนุญาต Apache-2.0 จะได้รับใบอนุญาตคู่ดังกล่าวข้างต้นโดยไม่มีข้อกำหนดหรือเงื่อนไขเพิ่มเติมใด ๆ