Skip to main content

Astra 数据库

¥Astra DB

Compatibility

仅在 Node.js 上可用。

¥Only available on Node.js.

DataStax Astra 数据库 是一个基于 Apache Cassandra 构建的无服务器向量数据库,可通过易于使用的 JSON API 轻松访问。

¥DataStax Astra DB is a serverless vector-capable database built on Apache Cassandra and made conveniently available through an easy-to-use JSON API.

设置

¥Setup

  1. 创建一个 Astra DB 账户

    ¥Create an Astra DB account.

  2. 创建一个 支持向量的数据库

    ¥Create a vector enabled database.

  3. 从数据库详细信息中获取你的 API EndpointToken

    ¥Grab your API Endpoint and Token from the Database Details.

  4. 设置以下环境变量:

    ¥Set up the following env vars:

export ASTRA_DB_APPLICATION_TOKEN=YOUR_ASTRA_DB_APPLICATION_TOKEN_HERE
export ASTRA_DB_ENDPOINT=YOUR_ASTRA_DB_ENDPOINT_HERE
export ASTRA_DB_COLLECTION=YOUR_ASTRA_DB_COLLECTION_HERE
export OPENAI_API_KEY=YOUR_OPENAI_API_KEY_HERE

其中 ASTRA_DB_COLLECTION 是你想要的集合名称

¥Where ASTRA_DB_COLLECTION is the desired name of your collection

  1. 安装 Astra TS 客户端和 LangChain 社区包

    ¥Install the Astra TS Client & the LangChain community package

npm install @langchain/openai @datastax/astra-db-ts @langchain/community @langchain/core

索引文档

¥Indexing docs

import { OpenAIEmbeddings } from "@langchain/openai";
import {
AstraDBVectorStore,
AstraLibArgs,
} from "@langchain/community/vectorstores/astradb";

const astraConfig: AstraLibArgs = {
token: process.env.ASTRA_DB_APPLICATION_TOKEN as string,
endpoint: process.env.ASTRA_DB_ENDPOINT as string,
collection: process.env.ASTRA_DB_COLLECTION ?? "langchain_test",
collectionOptions: {
vector: {
dimension: 1536,
metric: "cosine",
},
},
};

const vectorStore = await AstraDBVectorStore.fromTexts(
[
"AstraDB is built on Apache Cassandra",
"AstraDB is a NoSQL DB",
"AstraDB supports vector search",
],
[{ foo: "foo" }, { foo: "bar" }, { foo: "baz" }],
new OpenAIEmbeddings(),
astraConfig
);

// Querying docs:
const results = await vectorStore.similaritySearch("Cassandra", 1);

// or filtered query:
const filteredQueryResults = await vectorStore.similaritySearch("A", 1, {
foo: "bar",
});

API Reference:

向量类型

¥Vector Types

Astra DB 支持 cosine(默认)、dot_producteuclidean 相似性搜索;这在向量存储首次创建时作为 CreateCollectionOptions 的一部分进行定义:

¥Astra DB supports cosine (the default), dot_product, and euclidean similarity search; this is defined when the vector store is first created as part of the CreateCollectionOptions:

  vector: {
dimension: number;
metric?: "cosine" | "euclidean" | "dot_product";
};

¥Related