Skip to main content
Open In ColabOpen on GitHub

Tair

Tair 是由 Alibaba Cloud 开发的云原生内存数据库服务。 它提供了丰富的数据模型和企业级功能,以支持您的实时在线场景,同时保持与开源 Redis 的完全兼容。Tair 还引入了基于新型非易失性内存(NVM)存储介质的持久内存优化实例。

本笔记本展示了如何使用与Tair向量数据库相关的功能。

你需要安装 langchain-community 使用 pip install -qU langchain-community 来使用这个集成

要运行,你应该有一个Tair实例启动并运行。

from langchain_community.embeddings.fake import FakeEmbeddings
from langchain_community.vectorstores import Tair
from langchain_text_splitters import CharacterTextSplitter
from langchain_community.document_loaders import TextLoader

loader = TextLoader("../../how_to/state_of_the_union.txt")
documents = loader.load()
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
docs = text_splitter.split_documents(documents)

embeddings = FakeEmbeddings(size=128)
API Reference:TextLoader

使用TAIR_URL环境变量连接到Tair

export TAIR_URL="redis://{username}:{password}@{tair_address}:{tair_port}"

或关键字参数 tair_url

然后将文档和嵌入存储到Tair中。

tair_url = "redis://localhost:6379"

# drop first if index already exists
Tair.drop_index(tair_url=tair_url)

vector_store = Tair.from_documents(docs, embeddings, tair_url=tair_url)

查询相似文档。

query = "What did the president say about Ketanji Brown Jackson"
docs = vector_store.similarity_search(query)
docs[0]

Tair混合搜索索引构建

# drop first if index already exists
Tair.drop_index(tair_url=tair_url)

vector_store = Tair.from_documents(
docs, embeddings, tair_url=tair_url, index_params={"lexical_algorithm": "bm25"}
)

Tair混合搜索

query = "What did the president say about Ketanji Brown Jackson"
# hybrid_ratio: 0.5 hybrid search, 0.9999 vector search, 0.0001 text search
kwargs = {"TEXT": query, "hybrid_ratio": 0.5}
docs = vector_store.similarity_search(query, **kwargs)
docs[0]

这个页面有帮助吗?