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
- Create an Astra DB account.
- Create a vector enabled database.
- Grab your
API Endpoint
andToken
from the Database Details. - 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
- Install the Astra TS Client & the LangChain community package
- npm
- Yarn
- pnpm
npm install @langchain/openai @datastax/astra-db-ts @langchain/community @langchain/core
yarn add @langchain/openai @datastax/astra-db-ts @langchain/community @langchain/core
pnpm add @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:
- OpenAIEmbeddings from
@langchain/openai
- AstraDBVectorStore from
@langchain/community/vectorstores/astradb
- AstraLibArgs from
@langchain/community/vectorstores/astradb
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";
};
Related
- Vector store conceptual guide
- Vector store how-to guides