向量存储是一个轻巧有效的矢量数据库,可将文档向量存储在浏览器的索引eddb中。此软件包允许您使用矢量嵌入在文本文档上执行语义相似性搜索。语义搜索是指理解文本文档和查询的含义和上下文的能力,从而实现了更准确和相关的搜索结果。向量存储利用OpenAI嵌入将文本文档转换为向量,并提供了一个基于余弦相似性搜索相似文档的接口。
余弦相似性是在内部产品空间中两个非零向量之间相似性的量度。它被定义为两个向量之间的角度的余弦。余弦值的值范围为-1至1,其中1表示完全相似性,0表示没有相似性,-1表示完全差异。
在此软件包中,余弦相似性用于测量文档向量与查询向量之间的相似性。余弦相似性评分是使用向量的点产物计算的,除以其幅度的乘积。
最近使用的(LRU)机制用于管理存储大小,并在存储大小超过指定限制时自动删除文档。文档由他们的命中柜台(上升)和时间戳(上升)进行排序。首先将其命中率和最古老的时间戳的文档首先删除,直到存储尺寸低于极限为止。
使用NPM安装软件包:
npm i vector-storage这是如何使用vectorstorage类的基本示例:
import { VectorStorage } from "vector-storage" ;
// Create an instance of VectorStorage
const vectorStore = new VectorStorage ( { openAIApiKey : "your-openai-api-key" } ) ;
// Add a text document to the store
await vectorStore . addText ( "The quick brown fox jumps over the lazy dog." , {
category : "example" ,
} ) ;
// Perform a similarity search
const results = await vectorStore . similaritySearch ( {
query : "A fast fox leaps over a sleepy hound." ,
} ) ;
// Display the search results
console . log ( results ) ; 用于管理IndexedDB中文档向量的主要类。
创建一个新实例的矢量storage实例。
选项:包含以下属性的对象:
interface IVSOptions {
openAIApiKey : string ; // The OpenAI API key used for generating embeddings.
maxSizeInMB ?: number ; // The maximum size of the storage in megabytes. Defaults to 2GB
debounceTime ?: number ; // The debounce time in milliseconds for saving to IndexedDB. Defaults to 0.
openaiModel ?: string ; // The OpenAI model used for generating embeddings. Defaults to 'text-embedding-ada-002'.
}将文本文档添加到商店并返回创建的文档。
将多个文本文档添加到商店,并返回创建文档的数组。
在存储的文档上执行相似性搜索,并返回一系列匹配文档。
参数:包含以下属性的对象:
IVSDocument接口表示存储在矢量数据库中的文档对象。它包含以下属性:
interface IVSDocument {
hits ?: number ; // The number of hits (accesses) for the document. Omit if the value is 0.
metadata : object ; // The metadata associated with the document for filtering.
text : string ; // The text content of the document.
timestamp : number ; // The timestamp indicating when the document was added to the store.
vectorMag : number ; // The magnitude of the document vector.
vector : number [ ] ; // The vector representation of the document.
} 欢迎对该项目的贡献!如果您想做出贡献,请按照以下步骤:
请确保您的代码遵循项目的编码样式,并在提交拉动请求之前通过所有测试。如果您发现任何错误或有改进的建议,请随时在Github上打开问题。
该项目已根据MIT许可获得许可。有关完整许可文本,请参见许可证文件。
版权(C)Nitai Aharoni。版权所有。