
🚜 开发中
Voy正在积极开发中。因此,API尚不稳定。请注意,在即将发布的1.0版本之前可能会有破坏性更改。
以下是我们正在进行的工作预览:
- WebAssembly中内置文本转换:目前,voy依赖于JavaScript库(如
transformers.js)来生成文本嵌入。详情请参阅使用方法。- 索引更新:当前在资源更新时需要重建索引。
- TypeScript支持:由于WASM工具的限制,复杂数据类型无法自动生成。
# 使用npm npm i voy-search # 使用Yarn yarn add voy-search # 使用pnpm pnpm add voy-search
class VoyVoy类封装了一个索引,并公开了Voy提供的所有公共方法。
class Voy { /** * 通过实例化一个资源,Voy将构建索引。如果资源不存在, * 它将构建一个空索引。稍后调用Voy.index()将覆盖空索引。 * @param {Resource | undefined} resource */ constructor(resource?: Resource); /** * 索引给定资源。Voy.index()设计用于在没有资源的情况下实例化Voy实例的场景。 * 它将覆盖现有索引。如果您想保留现有索引,可以使用Voy.add()将资源添加到索引中。 * @param {Resource} resource */ index(resource: Resource): void; /** * 使用给定的查询嵌入搜索前k个结果。 * @param {Float32Array} query: 查询嵌入 * @param {number} k: 搜索结果中的项目数 * @returns {SearchResult} */ search(query: Float32Array, k: number): SearchResult; /** * 将给定资源添加到索引中。 * @param {Resource} resource */ add(resource: Resource): void; /** * 从索引中移除给定资源。 * @param {Resource} resource */ remove(resource: Resource): void; /** * 从索引中移除所有资源。 */ clear(): void; /** * @returns {number} */ size(): number; /** * 序列化Voy实例。 * @returns {string} */ serialize(): string; /** * 将序列化的索引反序列化为Voy实例。 * @param {string} serialized_index * @returns {Voy} */ static deserialize(serialized_index: string): Voy; } interface Resource { embeddings: Array<{ id: string; // 资源的id title: string; // 资源的标题 url: string; // 资源的url embeddings: number[]; // 资源的嵌入 }>; } interface SearchResult { neighbors: Array<{ id: string; // 资源的id title: string; // 资源的标题 url: string; // 资源的url }>; }
除了Voy类,Voy还将所有实例方法作为独立函数导出。
index(resource: Resource): SerializedIndex它索引给定的资源并返回序列化的索引。
参数
interface Resource { embeddings: Array<{ id: string; // 资源的id title: string; // 资源的标题 url: string; // 资源的url embeddings: number[]; // 资源的嵌入 }>; }
返回值
type SerializedIndex = string;
search(index: SerializedIndex, query: Query, k: NumberOfResult): SearchResult它反序列化给定的索引并搜索查询的k个最近邻。
参数
type SerializedIndex = string; type Query = Float32Array; // 搜索查询的嵌入 type NumberOfResult = number; // 返回的K个顶部结果
返回值
interface SearchResult { neighbors: Array<{ id: string; // 资源的id title: string; // 资源的标题 url: string; // 资源的url }>; }
add(index: SerializedIndex, resource: Resource): SerializedIndex它将资源添加到索引中并返回更新后的序列化索引。
参数
type SerializedIndex = string; interface Resource { embeddings: Array<{ id: string; // 资源的id title: string; // 资源的标题 url: string; // 资源的url embeddings: number[]; // 资源的嵌入向量 }>; }
返回值
type SerializedIndex = string;
remove(index: SerializedIndex, resource: Resource): SerializedIndex它从索引中移除资源并返回更新后的序列化索引。
参数
type SerializedIndex = string; interface Resource { embeddings: Array<{ id: string; // 资源的id title: string; // 资源的标题 url: string; // 资源的url embeddings: number[]; // 资源的嵌入向量 }>; }
返回值
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 = [ "那是一个非常快乐的人", "那是一只快乐的狗", "今天是个阳光明媚的日子", ]; const query = "那是一个快乐的人"; // 创建文本嵌入 const model = await (await TextModel.create("gtr-t5-quant")).model; const processed = await Promise.all(phrases.map((q) => model.process(q))); // 使用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); // 对查询嵌入进行相似度搜索 const q = await model.process(query); const result = index.search(q.result, 1); // 显示搜索结果 result.neighbors.forEach((result) => console.log(`✨ voy相似度搜索结果: "${result.title}"`) );
import { TextModel } from "@visheratin/web-ai"; const { Voy } = await import("voy-search"); const phrases = [ "那是一个非常快乐的人", "那是一只快乐的狗", "今天是个阳光明媚的日子", "向日葵正在盛开", ]; 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);
根据以下两种许可证之一授权
由您选择。
<a href="https://reflect.app" target="_blank"><img src="https://avatars.githubusercontent.com/u/73365487?s=64&v=4"></a> <a href="https://github.com/markhughes" target="_blank"><img src="https://avatars.githubusercontent.com/u/1357323?s=64&v=4"></a>
除非您明确声明,否则您有意提交以包含在作品中的任何贡献,如Apache-2.0许可证中所定义,均应按上述方式双重许可,无任何附加条款或条件。


企业专属的AI法律顾问
iTerms是法大大集团旗下法律子品牌,基于最先进的大语言模型(LLM)、专业的法律知识库和强大的智能体架构,帮助企业扫清合规障碍,筑牢风控防线,成为您企业专属的AI法律顾问。


稳定高效的流量提升解决方案,助力品牌曝光
稳定高效的流量提升解决方案,助力品牌曝光


最新版Sora2模型免费使用,一键生成无水印视频
最新版Sora2模型免费使用,一键生成无水印视频


实时语音翻译/同声传译工具
Transly是一个多场景的AI大语言模型驱动的同声传译、专业翻译助手,它拥有超精准的音频识别翻译能力,几乎零延迟的使用体验和支持多国语言可以让你带它走遍全球,无论你是留学生、商务人士、韩剧美剧爱好者,还是出国游玩、多国会议、跨国追星等等,都可以满足你所有需要同传的场景需求,线上线下通用,扫除语言障碍,让全世界的语言交流不再有国界。


选题、配图、成文,一站式创作,让内容运营更高效
讯飞绘文,一个AI集成平台,支持写作、选题、配图、排版和发布。高效生成适用于各类媒体的定制内容,加速品牌传播,提升内容营销效果。


AI辅助编程,代码自动修复
Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。


最强AI数据分析助手
小浣熊家族Raccoon,您的AI智能助手,致力于通过先进的人工智能技术,为用户提供高效、便捷的智能服务。无论是日常咨询还是专业问题解答,小浣熊都能以快速、准确的响应满足您的需求,让您的生活更加智能便捷。


像人一样思考的AI智能体
imini 是一款超级AI智能体,能根据人类指令,自主思考、自主完成、并且交付结果的AI智能体。


AI数字人视频创作平台
Keevx 一款开箱即用的AI数字人视频创作平台,广泛适用于电商广告、企业培训与社媒宣传,让全球企业与个人创作者无需拍摄剪辑,就能快速生成多语言、高质量的专业视频。


一站式AI创作平台
提供 AI 驱动的图片、视频生成及数字人等功能,助力创意创作
最新AI工具、AI资讯
独家AI资源、AI项目落地

微信扫一扫关注公众号