Unable to connect to Cassandra (AstraDB) for vector search example from LangChain docs

119 views Asked by At

I'm trying to connect to AstraDB for my Python vector search project (for my GPT chatbot), but I'm getting a connection error. (I'm following the example in the LangChain Cassandra Vector store example and want to use AstraDB as my vector database.)

Here's my code:

    ASTRA_DB_CLIENT_ID = os.getenv("ASTRA_DB_CLIENT_ID")
    ASTRA_DB_APPLICATION_TOKEN = os.getenv("ASTRA_DB_APPLICATION_TOKEN")
    ASTRA_DB_SECURE_BUNDLE_PATH = (
        "/Users/devin.bost/proj/demos/ACI/secure-connect-vector-demo.zip"
    )
    cluster = Cluster(
        cloud={
            "secure_connect_bundle": ASTRA_DB_SECURE_BUNDLE_PATH,
        },
        auth_provider=PlainTextAuthProvider(
            ASTRA_DB_CLIENT_ID,
            ASTRA_DB_APPLICATION_TOKEN,
        ),
    )
    session = cluster.connect()

I've set my ASTRA_DB_CLIENT_ID to my particular CLIENT_ID, and I've done the same for the other environment variables.

I'm getting this error:

Traceback (most recent call last):
  File "cassandra/cluster.py", line 1703, in cassandra.cluster.Cluster.connect
  File "cassandra/cluster.py", line 1690, in cassandra.cluster.Cluster.connect
  File "cassandra/cluster.py", line 3488, in cassandra.cluster.ControlConnection.connect
  File "cassandra/cluster.py", line 3533, in cassandra.cluster.ControlConnection._reconnect_internal
cassandra.cluster.NoHostAvailable: ('Unable to connect to any servers', {'1d0ba879-1c8b-49e4-bc78-90baff9e5714-westus3.db.astra.datastax.com:29042:4cc2a061-0383-4355-85cd-699b4a5af93c': AuthenticationFailed('Failed to authenticate to 1d0ba879-1c8b-49e4-bc78-90baff9e5714-westus3.db.astra.datastax.com:29042:4cc2a061-0383-4355-85cd-699b4a5af93c: Error from server: code=0100 [Bad credentials] message="We recently improved your database security. To find out more and reconnect, see https://docs.datastax.com/en/astra/docs/manage-application-tokens.html"'), '1d0ba879-1c8b-49e4-bc78-90baff9e5714-westus3.db.astra.datastax.com:29042:a856cbcf-1faa-4098-84a0-94e221931fd8': AuthenticationFailed('Failed to authenticate to 1d0ba879-1c8b-49e4-bc78-90baff9e5714-westus3.db.astra.datastax.com:29042:a856cbcf-1faa-4098-84a0-94e221931fd8: Error from server: code=0100 [Bad credentials] message="We recently improved your database security. To find out more and reconnect, see https://docs.datastax.com/en/astra/docs/manage-application-tokens.html"'), '1d0ba879-1c8b-49e4-bc78-90baff9e5714-westus3.db.astra.datastax.com:29042:6f9150b3-c81a-4fcd-a61a-499ab11f09e8': AuthenticationFailed('Failed to authenticate to 1d0ba879-1c8b-49e4-bc78-90baff9e5714-westus3.db.astra.datastax.com:29042:6f9150b3-c81a-4fcd-a61a-499ab11f09e8: Error from server: code=0100 [Bad credentials] message="We recently improved your database security. To find out more and reconnect, see https://docs.datastax.com/en/astra/docs/manage-application-tokens.html"')})
python-BaseException

I've double-checked the credentials, ensured they're not getting passed to the object as null values, verified that the secure bundle is the correct one for the DB and that the secure bundle path is correct, and ensured that the token has sufficient permission configured. What could be the issue?

1

There are 1 answers

0
devinbost On

The issue is that when you changed out your ASTRA_DB_CLIENT_ID from "token" to your actual CLIENT_ID, you actually broke the example.

If you want to use token instead of the client ID and secret, you need to literally set that parameter as "token", like this:

auth_provider=PlainTextAuthProvider(
        "token",
        ASTRA_DB_APPLICATION_TOKEN,
),

In general, the exception you got can be thrown any time your credentials are incorrect.

Alternatively, when establishing the connection, you can use the client ID and client secret. Here's what a more complete example would look like (using your code) with this approach:

ASTRA_DB_CLIENT_ID = os.getenv("ASTRA_DB_CLIENT_ID")
ASTRA_DB_APPLICATION_TOKEN = os.getenv("ASTRA_DB_APPLICATION_TOKEN")
ASTRA_DB_SECURE_BUNDLE_PATH = (
    "/Users/devin.bost/proj/demos/ACI/secure-connect-vector-demo.zip"
)
cluster = Cluster(
    cloud={
        "secure_connect_bundle": ASTRA_DB_SECURE_BUNDLE_PATH,
    },
    auth_provider=PlainTextAuthProvider(
        ASTRA_DB_CLIENT_ID,
        ASTRA_DB_SECRET,
    ),
)
session = cluster.connect()

See this example from the official AstraDB portal with the relevant part in red: AstraDB example of establishing connection