Leveraging Alibaba Cloud’s Hologres as a Vector Store for Advanced Similarity Search in LangChain Applications
Leveraging Alibaba Cloud’s Hologres as a Vector Store for Advanced Similarity Search in LangChain Applications
In today’s AI-driven applications, efficient vector search capabilities are essential for implementing semantic search, recommendation systems, and other similarity-based features. LangChain, a popular framework for building AI applications, offers integration with various vector stores. Among these, Alibaba Cloud’s Hologres stands out as a powerful option for organizations seeking high-performance vector search capabilities.
This article explores how to implement and leverage Hologres as a vector store within LangChain applications, providing you with the knowledge to build scalable AI solutions with advanced similarity search.
What is Hologres?
Hologres is a real-time analytics service provided by Alibaba Cloud that combines the benefits of both OLAP (Online Analytical Processing) and OLTP (Online Transaction Processing) databases. It’s designed to handle massive amounts of data with high concurrency and low latency, making it ideal for AI applications that require efficient vector operations.
Setting Up Hologres as a Vector Store in LangChain
To use Hologres with LangChain, you’ll need to install the necessary dependencies and set up a connection to your Hologres instance.
Prerequisites
- An Alibaba Cloud account with Hologres service enabled
- Python environment with LangChain installed
- Required Python packages:
langchain
,psycopg2
(for PostgreSQL connections)
Basic Setup
First, let’s import the required modules and establish a connection to Hologres:
from langchain_community.vectorstores import Hologres
from langchain.embeddings import OpenAIEmbeddings
# Initialize your embedding model
embedding_model = OpenAIEmbeddings()
# Create a connection string
connection_string = Hologres.connection_string_from_db_params(
host="your-hologres-instance.aliyuncs.com",
port=80,
database="your_database",
user="your_username",
password="your_password"
)
# Initialize the Hologres vector store
vector_store = Hologres(
connection_string=connection_string,
embedding_function=embedding_model,
ndims=1536, # Dimensions for OpenAI embeddings
table_name="langchain_vectors",
pre_delete_table=False # Set to True if you want to recreate the table
)
Adding Documents to Hologres
Once you’ve set up the connection, you can add documents to your vector store:
from langchain.schema import Document
# Create some sample documents
documents = [
Document(page_content="Artificial intelligence is transforming industries worldwide", metadata={"source": "tech_report", "year": 2023}),
Document(page_content="Machine learning models require large amounts of training data", metadata={"source": "research_paper", "year": 2022}),
Document(page_content="Vector databases are essential for semantic search applications", metadata={"source": "blog_post", "year": 2023})
]
# Add documents to the vector store
document_ids = vector_store.add_documents(documents)
print(f"Added {len(document_ids)} documents with IDs: {document_ids}")
Alternatively, you can create a vector store directly from documents:
# Create a new vector store from documents
vector_store = Hologres.from_documents(
documents=documents,
embedding=embedding_model,
connection_string=connection_string,
table_name="langchain_vectors"
)
Performing Similarity Search
Now that your documents are stored in Hologres, you can perform similarity searches:
Basic Similarity Search
# Perform a simple similarity search
query = "How is AI changing business?"
results = vector_store.similarity_search(query, k=2)
for doc in results:
print(f"Content: {doc.page_content}")
print(f"Metadata: {doc.metadata}")
print("---")
Similarity Search with Metadata Filtering
You can also filter results based on metadata:
# Search with metadata filter
filter_criteria = {"year": "2023"}
filtered_results = vector_store.similarity_search(
query="vector databases",
k=2,
filter=filter_criteria
)
for doc in filtered_results:
print(f"Content: {doc.page_content}")
print(f"Metadata: {doc.metadata}")
print("---")
Search with Scores
If you need the similarity scores along with the results:
# Get results with similarity scores
results_with_scores = vector_store.similarity_search_with_score(
query="machine learning data requirements",
k=3
)
for doc, score in results_with_scores:
print(f"Content: {doc.page_content}")
print(f"Similarity Score: {score}")
print("---")
Advanced Search Techniques
Hologres supports more sophisticated search strategies through LangChain:
Maximum Marginal Relevance (MMR) Search
MMR search balances relevance with diversity in search results:
# Perform MMR search
mmr_results = vector_store.max_marginal_relevance_search(
query="AI applications",
k=2,
fetch_k=10,
lambda_mult=0.5 # 0.5 balances between relevance and diversity
)
print("MMR Search Results:")
for doc in mmr_results:
print(f"Content: {doc.page_content}")
print("---")
Using Hologres as a Retriever
You can easily convert your Hologres vector store into a retriever for use in LangChain chains:
# Create a retriever from the vector store
retriever = vector_store.as_retriever(
search_type="similarity",
search_kwargs={"k": 3}
)
# Use the retriever in a chain
from langchain.chains import RetrievalQA
from langchain.llms import OpenAI
qa_chain = RetrievalQA.from_chain_type(
llm=OpenAI(),
chain_type="stuff",
retriever=retriever
)
# Ask a question
response = qa_chain.run("What are the key requirements for machine learning models?")
print(response)
Working with Pre-Generated Embeddings
If you already have embeddings for your texts, you can add them directly:
# Example with pre-generated embeddings
texts = ["This is the first document", "This is the second document"]
embeddings_list = [
[0.1, 0.2, 0.3, ..., 0.5], # First document embedding (truncated for brevity)
[0.2, 0.3, 0.4, ..., 0.6] # Second document embedding (truncated for brevity)
]
# Add the embeddings to the vector store
vector_store.add_embeddings(
texts=texts,
embeddings=embeddings_list,
metadatas=[{"source": "doc1"}, {"source": "doc2"}]
)
Managing Your Vector Store
Deleting Documents
You can delete documents from your vector store when they’re no longer needed:
# Delete specific documents by their IDs
vector_store.delete(ids=["doc_id_1", "doc_id_2"])
# Delete all documents (use with caution)
vector_store.delete()
Retrieving Documents by ID
You can retrieve specific documents using their IDs:
# Get documents by their IDs
documents = vector_store.get_by_ids(["doc_id_1", "doc_id_2"])
for doc in documents:
print(f"Retrieved: {doc.page_content}")
Performance Considerations
When working with Hologres as a vector store in production environments, keep these tips in mind:
-
Dimension Optimization: Choose the appropriate embedding dimension (
ndims
) based on your embedding model to avoid wasted space. -
Batch Processing: Use batch operations like
add_documents
rather than adding documents one at a time for better performance. -
Connection Pooling: For high-throughput applications, implement connection pooling to reduce the overhead of creating new database connections.
-
Index Management: Hologres creates appropriate indexes for vector search, but be aware of the space requirements as your collection grows.
Conclusion
Alibaba Cloud’s Hologres provides a robust and scalable solution for vector storage and similarity search within LangChain applications. By leveraging Hologres as your vector store, you can build AI applications that efficiently handle semantic search, recommendations, and other similarity-based features at scale.
The integration between LangChain and Hologres simplifies the development process while providing access to advanced search capabilities like MMR and metadata filtering. Whether you’re building a small prototype or a large-scale production system, this combination offers the flexibility and performance needed for modern AI applications.
To get started with your own implementation, sign up for an Alibaba Cloud account, provision a Hologres instance, and follow the code examples in this article to integrate it with your LangChain application.
Additional Resources
This post was originally written in my native language and then translated using an LLM. I apologize if there are any grammatical inconsistencies.