LangChain示例中AstraDB向量搜索连接失败排查求助
我在开发GPT聊天机器人的Python向量搜索项目,参照LangChain的Cassandra向量存储示例连接AstraDB时遇到了连接错误。
我的代码
import os from cassandra.cluster import Cluster from cassandra.auth import PlainTextAuthProvider 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()
错误信息
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
我已经反复检查了凭证有效性、安全包路径和权限配置,还是无法解决问题,请问可能的原因是什么?
认证参数格式错误:AstraDB安全更新后,应用令牌需单独作为认证凭证,不需要搭配客户端ID。正确的认证写法应为
PlainTextAuthProvider("token", ASTRA_DB_APPLICATION_TOKEN),第一个参数固定为"token",第二个参数是完整的应用令牌字符串,你当前代码传入客户端ID的方式不符合新规则。环境变量加载异常:即使确认设置了环境变量,也可能存在加载失败的情况。建议在代码中添加打印语句,输出
ASTRA_DB_CLIENT_ID和ASTRA_DB_APPLICATION_TOKEN的实际值,验证是否为空或与控制台中的值一致。安全包与实例不匹配:secure bundle是和特定AstraDB实例绑定的,若实例发生重建、区域迁移等操作,旧的安全包会失效。需重新从Astra控制台下载对应实例的最新安全包,并确认本地路径配置正确。
令牌权限不足:应用令牌即使有效,若未授予
db:connect、db:vector_search等必要权限,也会触发认证失败。进入Astra控制台检查该令牌的权限列表,确保包含数据库连接和向量操作的相关权限。驱动版本不兼容:旧版本的Cassandra Python驱动可能不支持AstraDB的新安全认证机制。建议升级到最新版驱动,执行
pip install --upgrade cassandra-driver即可,推荐使用4.15及以上版本。
内容的提问来源于stack exchange,提问作者devinbost

