Skip to main content

Astra DB

Compatibility

Only available on Node.js.

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. Create an Astra DB account.
  2. Create a vector enabled database.
  3. 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

Where ASTRA_DB_COLLECTION is the desired name of your collection

  1. Install the Astra TS Client & the LangChain community package
npm install @langchain/openai @datastax/astra-db-ts @langchain/community

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 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";
};

Was this page helpful?


You can also leave detailed feedback on GitHub.