Alibaba Cloud MySQL is a fully managed relational database service that provides high availability, scalability, and security.
Alibaba Cloud MySQL provides deep integration for enterprise-level vector data processing. It natively supports storing and computing vector data of up to 16,383 dimensions. The service integrates mainstream vector operation functions and uses a highly optimized Hierarchical Navigable Small World (HNSW) algorithm to deliver efficient approximate nearest neighbor searches. This feature also supports creating indexes on full-dimension vector columns.This guide provides a quick overview for getting started with the alibabacloud-mysql vector store. For a detailed listing of all alibabacloud-mysql vector store features, paramaters, and configurations, head to the langchain-alibabacloud-mysql.
Setup
To access the alibabacloud-mysql vector store, you’ll need to create an Alibaba Cloud RDS for MySQL instance with minor version 8.0.36 or higher, open the vector feature, make it accessible, and install thelangchain-alibabacloud-mysql integration package.
Credentials
To connect to your Alibaba Cloud RDS MySQL instance, you’ll need to set the following environment variables:ALIBABACLOUD_MYSQL_HOST: Your RDS MySQL host addressALIBABACLOUD_MYSQL_PORT: MySQL port (default: 3306)ALIBABACLOUD_MYSQL_USER: MySQL usernameALIBABACLOUD_MYSQL_PASSWORD: MySQL passwordALIBABACLOUD_MYSQL_DATABASE: Database name
Installation
The LangChain alibabacloud-mysql integration lives in thelangchain-alibabacloud-mysql package:
Instantiation
Now we can instantiate the vector store with your RDS MySQL connection informations:Initialize vector store
To instantiate the vector store, you need to provide an embedding model. You can use DashScope embeddings (recommended for Alibaba Cloud) or other embedding models (OpenAI, etc.) integrated into LangChain.
If you choose to use dashscope model, you can get your api key here, and set it in the following codes.
Manage vector store
Add items
Add documents
Update items
Update document by ID
Delete items
Delete documents by IDs
Query vector store
Once your vector store has been created and the relevant documents have been added you will most likely wish to query it during the running of your chain or agent.Directly
Performing a simple similarity search can be done as follows:Similarity search
Similarity search with scores
By turning into retriever
You can also transform the vector store into a retriever for easier usage in your chains.Create retriever
Features
Alibaba Cloud MySQL vector store supports most standard vector store features:| Feature | Supported |
|---|---|
| Delete by ID | ✅ |
| Filtering | ✅ |
| Search by Vector | ✅ |
| Search with score | ✅ |
| Async | ✅ |
| Passes Standard Tests | ✅ |
| Multi Tenancy | ❌ |
| IDs in add Documents | ✅ |
Metadata filtering
You can filter search results by metadata using dictionary-style filters:Filter by metadata
$eq: Equal to$ne: Not equal to$gt: Greater than$gte: Greater than or equal to$lt: Less than$lte: Less than or equal to$in: In list$nin: Not in list$like: LIKE pattern matching
Maximal Marginal Relevance (MMR) search
MMR search provides diverse results by balancing relevance and diversity:MMR search
Batch operations
Efficiently add multiple documents at once:Batch add documents
Get documents by IDs
Retrieve specific documents by their IDs:Get by IDs
Count and clear
Get the total number of vectors or clear all data:Count and clear
Async operations
AlibabaCloud MySQL vector store supports async operations for all major methods:aadd_texts()- Add texts asynchronouslyaadd_documents()- Add documents asynchronouslyasimilarity_search()- Similarity search asynchronouslyasimilarity_search_with_score()- Similarity search with scores asynchronouslyamax_marginal_relevance_search()- MMR search asynchronouslyadelete()- Delete vectors asynchronouslyaget_by_ids()- Get documents by IDs asynchronouslyaclear()- Clear all vectors asynchronouslyacount()- Count vectors asynchronouslyaclose()- Close connection pool asynchronously
Usage for retrieval-augmented generation
Retrieval-Augmented Generation (RAG) combines vector search with language model generation to provide contextual, accurate answers based on your documents.Basic RAG workflow
Here’s a complete example of building a RAG application with Alibaba Cloud MySQL:RAG example
Using retriever with agents
You can also use the vector store as a retrieval tool in an agent:RAG agent