voy
1.0.0
?正在进行中
Voy正在积极发展。结果,API不稳定。请注意,在即将发布的1.0版本之前可能会发生破坏更改。
偷看我们正在从事的工作:
- WebAssembly中的内置文本转换:截至目前,Voy依靠JavaScript库(例如
transformers.js
来生成文本嵌入。有关更多详细信息,请参见用法。- 索引更新:当前需要在发生资源更新时重新构建索引。
- 打字稿支持:由于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
它值得考虑给定的索引并搜索查询最近的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
等库生成文本的嵌入:
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许可证中定义)应为双重许可,如上所述,没有任何其他条款或条件。