Cassandra
仅在 Node.js 上可用。
Apache Cassandra® 是一个 NoSQL、面向行、高度可扩展和高可用的数据库。
最新版本 的 Apache Cassandra 原生支持向量相似度搜索。
设置
首先,安装 Cassandra Node.js 驱动程序
- npm
- Yarn
- pnpm
npm install cassandra-driver @langchain/community @langchain/openai @langchain/core
yarn add cassandra-driver @langchain/community @langchain/openai @langchain/core
pnpm add cassandra-driver @langchain/community @langchain/openai @langchain/core
根据您的数据库提供商,连接到数据库的具体方法会有所不同。我们将创建一个文档 configConnection
,它将用作向量存储配置的一部分。
Apache Cassandra®
在 Apache Cassandra® 5.0 及更高版本中支持向量搜索。您可以使用标准连接文档,例如
const configConnection = {
contactPoints: ['h1', 'h2'],
localDataCenter: 'datacenter1',
credentials: {
username: <...> as string,
password: <...> as string,
},
};
Astra DB
Astra DB 是一个云原生的 Cassandra 即服务平台。
- 创建一个 Astra DB 帐户。
- 创建一个 启用向量的数据库。
- 为您的数据库创建 令牌。
const configConnection = {
serviceProviderArgs: {
astra: {
token: <...> as string,
endpoint: <...> as string,
},
},
};
您可以提供属性 datacenterID:
和可选的 regionName:
,而不是 endpoint:
。
索引文档
import { CassandraStore } from "langchain/vectorstores/cassandra";
import { OpenAIEmbeddings } from "@langchain/openai";
// The configConnection document is defined above
const config = {
...configConnection,
keyspace: "test",
dimensions: 1536,
table: "test",
indices: [{ name: "name", value: "(name)" }],
primaryKey: {
name: "id",
type: "int",
},
metadataColumns: [
{
name: "name",
type: "text",
},
],
};
const vectorStore = await CassandraStore.fromTexts(
["I am blue", "Green yellow purple", "Hello there hello"],
[
{ id: 2, name: "2" },
{ id: 1, name: "1" },
{ id: 3, name: "3" },
],
new OpenAIEmbeddings(),
cassandraConfig
);
查询文档
const results = await vectorStore.similaritySearch("Green yellow purple", 1);
或过滤查询
const results = await vectorStore.similaritySearch("B", 1, { name: "Bubba" });
向量类型
Cassandra 支持 cosine
(默认)、dot_product
和 euclidean
相似度搜索;这在首次创建向量存储时定义,并在构造函数参数 vectorType
中指定,例如
...,
vectorType: "dot_product",
...
索引
在版本 5 中,Cassandra 引入了存储附加索引或 SAI。这些允许在不指定分区键的情况下进行 WHERE
过滤,并允许其他运算符类型,例如非等式。您可以使用 indices
参数定义这些索引,该参数接受零个或多个字典,每个字典都包含 name
和 value
条目。
索引是可选的,但如果要在非分区列上使用过滤查询,则需要索引。
name
条目是对象名称的一部分;在名为test_table
的表上,name: "some_column"
的索引将为idx_test_table_some_column
。value
条目是创建索引的列,用(
和)
包围。对于上面的列some_column
,它将被指定为value: "(some_column)"
。- 可选的
options
条目是传递给CREATE CUSTOM INDEX
语句的WITH OPTIONS =
子句的映射。此映射上的特定条目特定于索引类型。
indices: [{ name: "some_column", value: "(some_column)" }],
高级过滤
默认情况下,过滤器使用等式 =
应用。对于具有 indices
条目的字段,您可以提供一个 operator
,其中包含索引支持的值的字符串;在这种情况下,您可以指定一个或多个过滤器,可以是单例或列表(将使用 AND
连接)。例如
{ name: "create_datetime", operator: ">", value: some_datetime_variable }
或
[
{ userid: userid_variable },
{ name: "create_datetime", operator: ">", value: some_date_variable },
];
value
可以是单个值或数组。如果它不是数组,或者 value
中只有一个元素,则生成的查询将类似于 ${name} ${operator} ?
,其中 value
绑定到 ?
。
如果 value
数组中有多个元素,则计算 name
中未加引号的 ?
的数量,并从 value
的长度中减去该数量,并将此数量的 ?
放在运算符的右侧;如果有多于一个 ?
,则它们将被封装在 (
和 )
中,例如 (?, ?, ?)
。
这有助于运算符左侧的绑定值,这对于某些函数很有用;例如,地理距离过滤器
{
name: "GEO_DISTANCE(coord, ?)",
operator: "<",
value: [new Float32Array([53.3730617,-6.3000515]), 10000],
},
数据分区和复合键
在某些系统中,您可能希望出于各种原因对数据进行分区,例如按用户或按会话。Cassandra 中的数据始终是分区的;默认情况下,此库将按第一个主键字段进行分区。您可以指定构成记录主(唯一)键的多个列,并可选择指示应作为分区键一部分的字段。例如,向量存储可以按 userid
和 collectionid
进行分区,其他字段 docid
和 docpart
使单个条目唯一
...,
primaryKey: [
{name: "userid", type: "text", partition: true},
{name: "collectionid", type: "text", partition: true},
{name: "docid", type: "text"},
{name: "docpart", type: "int"},
],
...
搜索时,您可以在过滤器中包含分区键,而无需为这些列定义 indices
;您不需要指定所有分区键,但必须首先指定键中的分区键。在上面的示例中,您可以指定 {userid: userid_variable}
和 {userid: userid_variable, collectionid: collectionid_variable}
的过滤器,但是如果您只想指定 {collectionid: collectionid_variable}
的过滤器,则必须在 indices
列表中包含 collectionid
。
其他配置选项
在配置文件中,提供了更多可选参数;它们的默认值是
...,
maxConcurrency: 25,
batchSize: 1,
withClause: "",
...
参数 | 用法 |
---|---|
maxConcurrency | 在给定时间内将发送到 Cassandra 的并发请求数。 |
batchSize | 在单个请求中发送到 Cassandra 的文档数量。当使用大于 1 的值时,您应确保批量大小不会超过 Cassandra 参数 batch_size_fail_threshold_in_kb 。批量处理未记录日志。 |
withClause | 可以使用可选的 WITH 子句创建 Cassandra 表;通常不需要这样做,但为了完整性而提供。 |