ComponentsVector storesTairTairTair is a cloud native in-memory database service developed by Alibaba Cloud.
It provides rich data models and enterprise-grade capabilities to support your real-time online scenarios while maintaining full compatibility with open source Redis. Tair also introduces persistent memory-optimized instances that are based on the new non-volatile memory (NVM) storage medium.This notebook shows how to use functionality related to the Tair vector database.To run, you should have a Tair instance up and running.from langchain.embeddings.fake import FakeEmbeddingsfrom langchain.text_splitter import CharacterTextSplitterfrom langchain.vectorstores import Tairfrom langchain.document_loaders import TextLoaderloader = TextLoader("../../../extras/modules/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)    ---------------------------------------------------------------------------    FileNotFoundError                         Traceback (most recent call last)    File /opt/homebrew/lib/python3.10/site-packages/langchain/document_loaders/text.py:40, in TextLoader.load(self)         39 try:    ---> 40     with open(self.file_path, encoding=self.encoding) as f:         41         text = f.read()    FileNotFoundError: [Errno 2] No such file or directory: '../../../state_of_the_union.txt'        The above exception was the direct cause of the following exception:    RuntimeError                              Traceback (most recent call last)    Cell In[30], line 4          1 from langchain.document_loaders import TextLoader          3 loader = TextLoader("../../../state_of_the_union.txt")    ----> 4 documents = loader.load()          5 text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)          6 docs = text_splitter.split_documents(documents)    File /opt/homebrew/lib/python3.10/site-packages/langchain/document_loaders/text.py:56, in TextLoader.load(self)         54         raise RuntimeError(f"Error loading {self.file_path}") from e         55 except Exception as e:    ---> 56     raise RuntimeError(f"Error loading {self.file_path}") from e         58 metadata = {"source": self.file_path}         59 return [Document(page_content=text, metadata=metadata)]    RuntimeError: Error loading ../../../state_of_the_union.txtConnect to Tair using the TAIR_URL environment variable export TAIR_URL="redis://{username}:{password}@{tair_address}:{tair_port}"or the keyword argument tair_url.Then store documents and embeddings into Tair.tair_url = "redis://localhost:6379"# drop first if index already existsTair.drop_index(tair_url=tair_url)vector_store = Tair.from_documents(docs, embeddings, tair_url=tair_url)    ---------------------------------------------------------------------------    NameError                                 Traceback (most recent call last)    Cell In[6], line 6          3 # drop first if index already exists          4 Tair.drop_index(tair_url=tair_url)    ----> 6 vector_store = Tair.from_documents(docs, embeddings, tair_url=tair_url)    NameError: name 'docs' is not definedQuery similar documents.query = "What did the president say about Ketanji Brown Jackson"docs = vector_store.similarity_search(query)docs[0]    Document(page_content='We’re going after the criminals who stole billions in relief money meant for small businesses and millions of Americans.  \n\nAnd tonight, I’m announcing that the Justice Department will name a chief prosecutor for pandemic fraud. \n\nBy the end of this year, the deficit will be down to less than half what it was before I took office.  \n\nThe only president ever to cut the deficit by more than one trillion dollars in a single year. \n\nLowering your costs also means demanding more competition. \n\nI’m a capitalist, but capitalism without competition isn’t capitalism. \n\nIt’s exploitation—and it drives up prices. \n\nWhen corporations don’t have to compete, their profits go up, your prices go up, and small businesses and family farmers and ranchers go under. \n\nWe see it happening with ocean carriers moving goods in and out of America. \n\nDuring the pandemic, these foreign-owned companies raised prices by as much as 1,000% and made record profits.', metadata={'source': '../../../state_of_the_union.txt'})Tair Hybrid Search Index build# drop first if index already existsTair.drop_index(tair_url=tair_url)vector_store = Tair.from_documents(docs, embeddings, tair_url=tair_url, index_params={"lexical_algorithm":"bm25"})Tair Hybrid Searchquery = "What did the president say about Ketanji Brown Jackson"# hybrid_ratio: 0.5 hybrid search, 0.9999 vector search, 0.0001 text searchkwargs = {"TEXT" : query, "hybrid_ratio" : 0.5}docs = vector_store.similarity_search(query, **kwargs)docs[0]PreviousSupabase (Postgres)NextTencent Cloud VectorDB