Query Astra DB with Vector Embeddings
LlamaIndex reader that retrieves documents from Astra DB by vector similarity.
Why it matters
Retrieve relevant documents from Astra DB using vector similarity search. This asset enables efficient querying of your vector database to find information based on semantic meaning.
Outcomes
What it gets done
Initialize connection to Astra DB using API endpoint and token.
Generate query vectors using an embedding model (e.g., OpenAI).
Fetch documents from Astra DB based on a provided query vector.
Load retrieved documents for further processing within LlamaIndex.
Install
Add it to your toolbox
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/li-reader-readers-astra-db | bash Overview
Astra DB Loader
The Astra DB Loader retrieves documents from an Astra DB collection by vector similarity, given a query embedding and a result limit, and loads them into LlamaIndex. Use it when you have an existing, populated Astra DB vector collection and need to retrieve nearest-matching documents by embedding. It requires an Astra DB endpoint, token, and collection already set up.
What it does
The Astra DB Loader returns documents retrieved from Astra DB by vector similarity search. You initialize AstraDBReader with an Astra DB collection, then pass in a query vector to fetch matching documents.
When to use - and when NOT to
Use it when you already have embeddings and an Astra DB vector collection and need to pull the nearest-matching documents into LlamaIndex -- for example generating a query embedding with OpenAI and retrieving the top 5 matches. It requires an existing Astra DB endpoint, token, and collection, so it is not usable without an Astra DB account already set up and populated.
Inputs and outputs
Install with:
pip install llama-index-readers-astra-db
Initialize with your Astra DB endpoint, token, and collection, then fetch by vector:
from openai import OpenAI
api_endpoint = "https://324<...>f1c.astra.datastax.com"
token = "AstraCS:<...>"
client = OpenAI(api_key="sk-<...>")
response = client.embeddings.create(
input="Your text string goes here", model="text-embedding-ada-002"
)
query_vector = response.data[0].embedding
from llama_index.readers.astra_db import AstraDBReader
reader = AstraDBReader(
collection_name="astra_v_table",
token=token,
api_endpoint=api_endpoint,
embedding_dimension=len(query_vector),
)
documents = reader.load_data(vector=query_vector, limit=5)
collection_name, token, api_endpoint, and embedding_dimension configure the connection; load_data takes a vector and a limit on how many matching documents to return.
Who it's for
Developers building LlamaIndex pipelines that need to retrieve documents from an existing Astra DB vector collection by embedding similarity.
Source README
Astra DB Loader
pip install llama-index-readers-astra-db
The Astra DB Loader returns a set of documents retrieved from Astra DB.
The user initializes the loader with an Astra DB index. They then pass in a vector.
Usage
Here's an example usage of the AstraDBReader.
from openai import OpenAI
### Get the credentials for Astra DB
api_endpoint = "https://324<...>f1c.astra.datastax.com"
token = "AstraCS:<...>"
### EXAMPLE: OpenAI embeddings
client = OpenAI(api_key="sk-<...>")
### Call OpenAI (or generate embeddings another way)
response = client.embeddings.create(
input="Your text string goes here", model="text-embedding-ada-002"
)
### Get the embedding
query_vector = response.data[0].embedding
### Initialize the Reader object
from llama_index.readers.astra_db import AstraDBReader
### Your Astra DB Account will provide you with the endpoint URL and Token
reader = AstraDBReader(
collection_name="astra_v_table",
token=token,
api_endpoint=api_endpoint,
embedding_dimension=len(query_vector),
)
### Fetch data from the reader
documents = reader.load_data(vector=query_vector, limit=5)
This loader is designed to be used as a way to load data into LlamaIndex.
Note: Please see the AstraDB documentation here.
FAQ
Common questions
Discussion
Questions & comments · 0
Sign In Sign in to leave a comment.