Skip to content

ChatLunaLab/luna-vdb

Repository files navigation

@chatluna/luna-vdb

轻量级,本地的 Wasm 向量数据库。

npm npm node version github top language

特性

  • Rust + WebAssembly,浏览器和 Node.js 均可使用
  • 简单,灵活的 API,涵盖向量索引、增量更新、搜索与清空。使用 ID 标注文档。
  • 支持序列化/反序列化
  • TypeScript 类型支持

快速开始

安装

yarn install @chatluna/luna-vdb

API

import { LunaVDB } from "@chatluna/luna-vdb";

const engine = new LunaVDB();

// 初始化一组向量
engine.index({
  embeddings: [
    { id: "cat", embeddings: [0.8, 0.7, 0.6] },
    { id: "dog", embeddings: [0.4, 0.3, 0.2] },
  ],
});

// 搜索最相近的两个向量
const result = engine.search(new Float32Array([0.75, 0.65, 0.55]), 2);
result.neighbors.forEach(({ id, distance }) => {
  console.log(`${id}: ${distance}`);
});

// 增量更新
engine.add({
  embeddings: [{ id: "bird", embeddings: [0.2, 0.1, 0.9] }],
});

// 删除向量或清空索引
engine.remove(["dog"]);
// engine.clear();

// 序列化以便持久化
const snapshot = engine.serialize(); // 返回 ArrayBuffer
const restored = LunaVDB.deserialize(snapshot);

console.log(restored.size()); // => 当前向量数量

在浏览器环境中以相同方式使用,只需通过 import/await import 加载打包后的 WASM 模块即可。

感谢

About

轻量级 Wasm 向量数据库

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published