Astra DB
DataStax Astra DB 是一个无服务器的人工智能就绪数据库,它基于
Apache Cassandra®
构建,并通过易于使用的 JSON API 提供便捷的服务。
请查看 DataStax 提供的教程。
安装与设置
安装以下 Python 包
pip install "langchain-astradb>=0.6,<0.7"
创建数据库(如果需要)并获取连接密钥。设置以下变量
ASTRA_DB_API_ENDPOINT="API_ENDPOINT"
ASTRA_DB_APPLICATION_TOKEN="TOKEN"
向量存储
这里显示了一些典型的初始化模式
from langchain_astradb import AstraDBVectorStore
vector_store = AstraDBVectorStore(
embedding=my_embedding,
collection_name="my_store",
api_endpoint=ASTRA_DB_API_ENDPOINT,
token=ASTRA_DB_APPLICATION_TOKEN,
)
from astrapy.info import VectorServiceOptions
vector_store_vectorize = AstraDBVectorStore(
collection_name="my_vectorize_store",
api_endpoint=ASTRA_DB_API_ENDPOINT,
token=ASTRA_DB_APPLICATION_TOKEN,
collection_vector_service_options=VectorServiceOptions(
provider="nvidia",
model_name="NV-Embed-QA",
),
)
from astrapy.info import (
CollectionLexicalOptions,
CollectionRerankOptions,
RerankServiceOptions,
VectorServiceOptions,
)
vector_store_hybrid = AstraDBVectorStore(
collection_name="my_hybrid_store",
api_endpoint=ASTRA_DB_API_ENDPOINT,
token=ASTRA_DB_APPLICATION_TOKEN,
collection_vector_service_options=VectorServiceOptions(
provider="nvidia",
model_name="NV-Embed-QA",
),
collection_lexical=CollectionLexicalOptions(analyzer="standard"),
collection_rerank=CollectionRerankOptions(
service=RerankServiceOptions(
provider="nvidia",
model_name="nvidia/llama-3.2-nv-rerankqa-1b-v2",
),
),
)
API 参考:AstraDBVectorStore
类 AstraDBVectorStore
的显著特性
- 原生异步 API;
- 搜索中的元数据过滤;
- MMR(最大边际相关性)搜索;
- 服务器端嵌入计算(在 Astra DB 术语中称为“向量化”);
- 从现有预填充的 Astra DB 集合中自动检测其设置;
- 混合搜索(向量 + BM25,然后进行重排序步骤);
- 支持非 Astra Data API(例如,自托管的 HCD 部署);
在示例笔记本中了解更多。
请查看 DataStax 提供的示例。
聊天消息历史记录
from langchain_astradb import AstraDBChatMessageHistory
message_history = AstraDBChatMessageHistory(
session_id="test-session",
api_endpoint=ASTRA_DB_API_ENDPOINT,
token=ASTRA_DB_APPLICATION_TOKEN,
)
API 参考:AstraDBChatMessageHistory
请查看用法示例。
LLM 缓存
from langchain.globals import set_llm_cache
from langchain_astradb import AstraDBCache
set_llm_cache(AstraDBCache(
api_endpoint=ASTRA_DB_API_ENDPOINT,
token=ASTRA_DB_APPLICATION_TOKEN,
))
API 参考:set_llm_cache | AstraDBCache
在示例笔记本中了解更多(滚动到 Astra DB 部分)。
语义 LLM 缓存
from langchain.globals import set_llm_cache
from langchain_astradb import AstraDBSemanticCache
set_llm_cache(AstraDBSemanticCache(
embedding=my_embedding,
api_endpoint=ASTRA_DB_API_ENDPOINT,
token=ASTRA_DB_APPLICATION_TOKEN,
))
API 参考:set_llm_cache | AstraDBSemanticCache
在示例笔记本中了解更多(滚动到相应部分)。
文档加载器
from langchain_astradb import AstraDBLoader
loader = AstraDBLoader(
collection_name="my_collection",
api_endpoint=ASTRA_DB_API_ENDPOINT,
token=ASTRA_DB_APPLICATION_TOKEN,
)
API 参考:AstraDBLoader
在示例笔记本中了解更多。
自查询检索器
from langchain_astradb import AstraDBVectorStore
from langchain.retrievers.self_query.base import SelfQueryRetriever
vector_store = AstraDBVectorStore(
embedding=my_embedding,
collection_name="my_store",
api_endpoint=ASTRA_DB_API_ENDPOINT,
token=ASTRA_DB_APPLICATION_TOKEN,
)
retriever = SelfQueryRetriever.from_llm(
my_llm,
vector_store,
document_content_description,
metadata_field_info
)
API 参考:AstraDBVectorStore | SelfQueryRetriever
在示例笔记本中了解更多。
存储
from langchain_astradb import AstraDBStore
store = AstraDBStore(
collection_name="my_kv_store",
api_endpoint=ASTRA_DB_API_ENDPOINT,
token=ASTRA_DB_APPLICATION_TOKEN,
)
API 参考:AstraDBStore
请查看 AstraDBStore 的 API 参考。
字节存储
from langchain_astradb import AstraDBByteStore
store = AstraDBByteStore(
collection_name="my_kv_store",
api_endpoint=ASTRA_DB_API_ENDPOINT,
token=ASTRA_DB_APPLICATION_TOKEN,
)
API 参考:AstraDBByteStore
请查看 AstraDBByteStore 的 API 参考。