query - 向量查询
query() 用于执行向量相似性搜索,以找到与查询向量最相似的 documents。
信息
仅支持在使用 SeekdbClient 连接时,才能使用该接口。关于 SeekdbClient 的详细介绍,参见 SeekdbClient。
前提条件
-
您已经安装了 seekdb-js,有关安装 seekdb-js 的详细信息,参见 快速开始。
-
您已经安装了 seekdb 服务器模式,有关安装 seekdb 服务器模式的详细信息,参见 通过 yum install 部署 seekdb。
-
您已经连接到数据库。有关连接的详细操作参见 SeekdbClient。
-
您已经创建了 collection,并插入相应的数据。有关创建 collection 和插入数据的详细操作参见 createCollection - 创建 Collection 和 add - 插入数据。
请求参数
query()
| 参数 | 取值类型 | 是否必选 | 描述 | 取值示例 |
|---|---|---|---|---|
queryEmbeddings | number[] | number[][] | 必选 | 用于批量查询的单个向量或向量列表;如果提供,则直接使用(忽略embeddingFunction);如果没有提供,则必须提供 queryText,collection 必须具有 embeddingFunction | [1.0, 2.0, 3.0] |
queryTexts | string | string[] | 可选 | 单个 vectors 或 vectors 列表;如果提供,则直接使用(忽略 embeddingFunction);如果没有提供,则必须提供 documents,同时 collection 必须具有 embeddingFunction。 | ["my query text"] |
nResults | number | 必须 | 返回相似的结果数,默认值为 10 | `3 |
where | Where | 可选 | Metadata 筛选条件。 | {"category": {"$eq": "AI"}} |
whereDocument | WhereDocument | 可选 | Document 筛选条件。 | {"$contains": "machine"} |
include | readonly ("documents" | "metadatas" | "embeddings" | "distances")[][str] | 可选 | 要包含的字段列表:["documents", "metadatas", "embeddings"]。 | ["documents", "metadatas", "embeddings"] |
distance | DistanceMetric | 可选 | 距离度量方式。 | |
approximate | boolean | 可选 | 是否使用近似搜索。 | true |
信息
-
使用的
embeddingFunction是与 collection 相关联的(在createCollection()或getCollection()期间设置)。您不能每次操作都覆盖它。 -
如果提供了
queryEmbeddings: 直接使用向量,不调用embeddingFunction。 -
如果未提供
queryEmbeddings但提供了queryTexts:-
如果集合有
embeddingFunction,将自动从文本生成查询向量。 -
如果集合没有
embeddingFunction,将抛出 ValueError。
-
-
如果既未提供
queryEmbeddings也未提供queryTexts: 将抛出 ValueError。
请求示例
import { SeekdbClient } from "seekdb";
// 1. Connect
const client = new SeekdbClient({
host: "127.0.0.1",
port: 2881,
user: "root",
password: "",
database: "test",
});
// 2. Create collection
const collection = await client.createCollection({ name: "my_collection" });
// 3. Add data (auto-vectorized)
await collection.add({
ids: ["1", "2"],
documents: ["Hello world", "seekdb is fast"],
});
// 4. Search
const results = await collection.query({
queryTexts: "Hello",
nResults: 5,
});
console.log(results);
返回参数
| 参数 | 取值类型 | 是否必选 | 描述 |
|---|---|---|---|
ids | readonly (readonly string[])[] | 必选 | 需要新增或者修改的 ID。可以是单个,也可以是数组。 |
embeddings | [List[List[List[float]]]] | 可选 | Vectors。如果提供,直接使用(忽略 embeddingFunction),如果不提供,可以提供 documents 来自动生成 vectors。 |
documents | [List[List[Dict]]] | 可选 | Documents。如果没有提供 vectors,documents 将使用 collection 的 embeddingFunction 转换为 vectors。 |
metadatas | [List[List[Dict]]] | 可选 | metadata。 |
distances | [List[List[Dict]]] | 可选 |
返回示例
{
ids: [ [ '1', '2' ] ],
distances: [ [ 0.24884326749104657, 0.9389671014508124 ] ],
documents: [ [ 'Hello world', 'seekdb is fast' ] ],
metadatas: [ [ null, null ] ],
embeddings: undefined
}