Skip to content
datastaxPublic

About

AstraPy is a Pythonic interface for DataStax Astra DB and the Data API

Topics

Resources

Code of conduct

Contributing

Stars

40 stars

Watchers

5 watching

Forks

Repository files navigation

astrapy

astrapy is the Pythonic client for interacting with DataStax Astra DB and Hyper-Converged Database.

Setup

Install with

pip install astrapy

A database is needed for the client to connect to. The database can be either an Astra DB instance or a Hyper-Converged Database (HCD) instance: follow the appropriate documentation links to get the connection parameters for your database.

Documentation

For Astra DB Serverless:

For Hyper-Converged Database (HCD):

Additional info

For technical details and further information on some specific astrapy topics (exception hierarchy, date/datetime handling, and such), please refer to:

At a glance

from astrapy import DataAPIClient
from astrapy.info import CollectionDefinition


# Connect to the DB:
client = DataAPIClient()
database = client.get_database(
    "<Astra DB API Endpoint>",
    token="<Astra DB Application Token>",
)

# Create the collection
# (with server-side embedding generation, 'vectorize')
collection_definition = (
    CollectionDefinition.builder()
    .set_vector_dimension(768)
    .set_vector_service(
        "<vectorize provider>",
        "<vectorize model name>",
        authentication={"providerKey": "API_KEY_NAME"}
    )
    .build()
)
collection = database.create_collection(
    "COLLECTION_NAME",
    definition=collection_definition,
)

collection.insert_many([
    {"name": "Apple", "score": 10, "$vectorize": "This fruit ..."},
    {"name": "Peach", "score": 5, "$vectorize": "A delicious ..."},
    {"name": "Walnut", "score": 8, "$vectorize": "Rich in ..."},
    # ...
])

# Use vectorize search and filters to find a document:
result = collection.find_one(
    {
        "$and": [
            {"score": {"$gte": 5}},
            {"name": {"$ne": "Apple"}},
        ]
    },
    sort={"$vectorize": "Thirst-quenching fruit with a somewhat sour taste"},
)

print(result)

About

AstraPy is a Pythonic interface for DataStax Astra DB and the Data API

Topics

Resources

Code of conduct

Contributing

Stars

40 stars

Watchers

5 watching

Forks

Releases

Packages

Used by

Contributors

Languages