Skip to main content

Turbopuffer

设置

¥Setup

首先,你必须注册一个 Turbopuffer 账户 此处。然后,一旦你拥有账户,你就可以创建 API 密钥。

¥First you must sign up for a Turbopuffer account here. Then, once you have an account you can create an API key.

将你的 API 密钥设置为环境变量:

¥Set your API key as an environment variable:

export TURBOPUFFER_API_KEY=<YOUR_API_KEY>

用法

¥Usage

以下是一些如何使用该类的示例。你可以根据之前指定的元数据过滤查询,但请记住,目前仅支持字符串值。

¥Here are some examples of how to use the class. You can filter your queries by previous specified metadata, but keep in mind that currently only string values are supported.

有关可接受的过滤器格式,请参阅 更多信息请点击此处

¥See here for more information on acceptable filter formats.

import { OpenAIEmbeddings } from "@langchain/openai";
import { TurbopufferVectorStore } from "@langchain/community/vectorstores/turbopuffer";

const embeddings = new OpenAIEmbeddings();

const store = new TurbopufferVectorStore(embeddings, {
apiKey: process.env.TURBOPUFFER_API_KEY,
namespace: "my-namespace",
});

const createdAt = new Date().getTime();

// Add some documents to your store.
// Currently, only string metadata values are supported.
const ids = await store.addDocuments([
{
pageContent: "some content",
metadata: { created_at: createdAt.toString() },
},
{ pageContent: "hi", metadata: { created_at: (createdAt + 1).toString() } },
{ pageContent: "bye", metadata: { created_at: (createdAt + 2).toString() } },
{
pageContent: "what's this",
metadata: { created_at: (createdAt + 3).toString() },
},
]);

// Retrieve documents from the store
const results = await store.similaritySearch("hello", 1);

console.log(results);
/*
[
Document {
pageContent: 'hi',
metadata: { created_at: '1705519164987' }
}
]
*/

// Filter by metadata
// See https://turbopuffer.com/docs/reference/query#filter-parameters for more on
// allowed filters
const results2 = await store.similaritySearch("hello", 1, {
created_at: [["Eq", (createdAt + 3).toString()]],
});

console.log(results2);

/*
[
Document {
pageContent: "what's this",
metadata: { created_at: '1705519164989' }
}
]
*/

// Upsert by passing ids
await store.addDocuments(
[
{ pageContent: "changed", metadata: { created_at: createdAt.toString() } },
{
pageContent: "hi changed",
metadata: { created_at: (createdAt + 1).toString() },
},
{
pageContent: "bye changed",
metadata: { created_at: (createdAt + 2).toString() },
},
{
pageContent: "what's this changed",
metadata: { created_at: (createdAt + 3).toString() },
},
],
{ ids }
);

// Filter by metadata
const results3 = await store.similaritySearch("hello", 10, {
created_at: [["Eq", (createdAt + 3).toString()]],
});

console.log(results3);

/*
[
Document {
pageContent: "what's this changed",
metadata: { created_at: '1705519164989' }
}
]
*/

// Remove all vectors from the namespace.
await store.delete({
deleteIndex: true,
});

API Reference:

¥Related