? Travail en cours
Voy est en cours de développement actif. En conséquence, l'API n'est pas stable. Veuillez noter qu'il pourrait y avoir des changements de rupture avant la prochaine version 1.0.
Un aperçu de ce sur quoi nous travaillons:
- Transformation de texte intégrée dans WebAssembly: Pour l'instant, VOY s'appuie sur des bibliothèques JavaScript comme
transformers.js
pour générer des intégres de texte. Voir l'utilisation pour plus de détails.- Mise à jour de l'index: Actuellement, il est nécessaire de reconstruire l'index lorsqu'une mise à jour des ressources se produit.
- Prise en charge de typeScript: En raison de la limitation de l'outillage WASM, les types de données complexes ne sont pas générés automatiquement.
# with npm
npm i voy-search
# with Yarn
yarn add voy-search
# with pnpm
pnpm add voy-search
class Voy
La classe Voy résume un index et expose toutes les méthodes publiques que Voy a à offrir.
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
} > ;
}
Outre la classe VOY, VOY exporte également toutes les méthodes d'instance en tant que fonctions individuelles.
index(resource: Resource): SerializedIndex
Il index la ressource donnée et renvoie un indice sérialisé.
Paramètres
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
} > ;
}
Retour
type SerializedIndex = string ;
search(index: SerializedIndex, query: Query, k: NumberOfResult): SearchResult
Il désérialise l'index donné et recherche les k
voisins les plus proches de la requête.
Paramètre
type SerializedIndex = string ;
type Query = Float32Array ; // embeddings of the search query
type NumberOfResult = number ; // K top results to return
Retour
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
Il ajoute des ressources à l'index et renvoie un index sérialisé mis à jour.
Paramètre
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
} > ;
}
Retour
type SerializedIndex = string ;
remove(index: SerializedIndex, resource: Resource): SerializedIndex
Il supprime les ressources de l'index et renvoie un index sérialisé mis à jour.
Paramètre
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
} > ;
}
Retour
type SerializedIndex = string ;
clear(index: SerializedIndex): SerializedIndex
Il supprime tous les éléments de l'index et renvoie un index sérialisé vide.
Paramètre
type SerializedIndex = string ;
Retour
type SerializedIndex = string ;
size(index: SerializedIndex): number;
Il renvoie la taille de l'index.
Paramètre
type SerializedIndex = string ;
À partir de maintenant, Voy s'appuie sur des bibliothèques comme transformers.js
et web-ai
pour générer des intégres pour le texte:
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 ) ;
Sous licence sous l'un ou l'autre des
à votre option.
À moins que vous ne soyez explicitement indiqué autrement, toute contribution intentionnellement soumise pour inclusion dans les travaux par vous, telle que définie dans la licence Apache-2.0, doit être autorisée à double licence comme ci-dessus, sans aucune condition supplémentaire.