3 个版本
0.1.3 | 2023年9月14日 |
---|---|
0.1.2 | 2023年9月14日 |
0.1.1 | 2023年9月14日 |
#1224 in 网页编程
3.5MB
1.5K SLoC
Victor
Web-optimized vector database (written in Rust)。
特性
- Rust API (使用原生文件系统或瞬态内存文件系统)
- Web API (使用 私有源文件系统)
- 非常高效的向量存储格式
- 对于具有1536维度的向量,我们的表示消耗1.5 KB,而使用JSON天真编码将消耗20.6 KB。
- 存储空间低时使用PCA进行向量压缩
JS 示例
安装
npm install victor-db
使用
import { Db } from "victor";
const db = await Db.new();
const content = "My content!";
const tags = ["these", "are", "tags"];
const embedding = new Float64Array(/* your embedding here */);
// write to victor
await db.insert(content, embedding, tags);
// read the 10 closest results from victor that are tagged with "tags"
// (only 1 will be returned because we only inserted one embedding)
const result = await db.search(embedding, ["tags"], 10);
assert(result[0].content == content);
// clear database
await db.clear();
请参阅 www/
以获取更完整的示例,包括从 OpenAI 获取嵌入。
Rust 示例
安装
cargo add victor-db
使用
use std::path::PathBuf;
use victor_db::native::Db;
let _ = std::fs::create_dir("./victor_test_data");
let mut victor = Db::new(PathBuf::from("./victor_test_data"));
victor.clear_db().await.unwrap();
victor
.write(
"Test Vector 1",
vec![1.0, 0.0, 0.0],
vec!["Test".to_string()],
)
.await;
victor
.write(
"Test Vector 2",
vec![0.0, 1.0, 0.0],
vec!["Test".to_string()],
)
.await;
// read the 10 closest results from victor that are tagged with "tags"
// (only 2 will be returned because we only inserted two embeddings)
let nearest = victor
.find_nearest_neighbors(vec![0.9, 0.0, 0.0], vec!["Test".to_string()], 10)
.await
.first()
.unwrap()
.content
.clone();
assert_eq!(nearest, "Test Vector 1".to_string());
此示例也位于 /examples
目录中。如果您已克隆此存储库,则可以使用以下命令运行它:cargo run --example native_filesystem
。
黑客攻击
-
Victor是用Rust编写的,并用wasm-pack编译为wasm。
安装wasm pack 使用
cargo install wasm-pack
或npm i -g wasm-pack
(https://wasm.rust-lang.net.cn/wasm-pack/installer/) -
构建Victor 使用
wasm-pack build
-
设置示例项目,该项目位于
www/
。如果您使用nvm,则可以直接运行
cd www/ && nvm use
然后,运行
npm i
。 -
从
www/
,使用npm run start
启动示例项目。
架构
相关代码位于 src/packed_vector.rs
。
用户
依赖项
~5–9.5MB
~172K SLoC