Use this file to discover all available pages before exploring further.
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 AlibabaCloudMySQLvector store. For a detailed listing of all alibabacloud-mysql vector store features, paramaters, and configurations, head to the langchain-alibabacloud-mysql.
Now we can instantiate the vector store with your RDS MySQL connection informations:
Initialize vector store
import osfrom langchain_alibabacloud_mysql import AlibabaCloudMySQLfrom langchain_community.embeddings import DashScopeEmbeddings# Initialize DashScope embeddings (Alibaba Cloud's embedding service)embeddings = DashScopeEmbeddings( model="text-embedding-v4", dashscope_api_key=os.environ.get("DASHSCOPE_API_KEY"),)# Or you can use OpenAI embeddings# embeddings = OpenAIEmbeddings()# Initialize vector storevector_store = AlibabaCloudMySQL( host=os.environ.get("ALIBABACLOUD_MYSQL_HOST", "localhost"), port=int(os.environ.get("ALIBABACLOUD_MYSQL_PORT", "3306")), user=os.environ.get("ALIBABACLOUD_MYSQL_USER", "root"), password=os.environ.get("ALIBABACLOUD_MYSQL_PASSWORD", ""), database=os.environ.get("ALIBABACLOUD_MYSQL_DATABASE", "test"), embedding=embeddings, table_name="langchain_vectors", distance_strategy="cosine", # or "euclidean" hnsw_m=6, # HNSW index M parameter (3-200))
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 from Model Studio, and set it in the following codes.
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.
MMR search provides diverse results by balancing relevance and diversity:
MMR search
results = vector_store.max_marginal_relevance_search( query="artificial intelligence", k=4, fetch_k=20, # Number of candidates to consider lambda_mult=0.5, # 0 = max diversity, 1 = max relevance)
Retrieval-Augmented Generation (RAG) combines vector search with language model generation to provide contextual, accurate answers based on your documents.
You can also use the vector store as a retrieval tool in an agent:
RAG agent
from langchain.agents import create_agentfrom langchain.tools import tool@tooldef retrieve_context(query: str) -> str: """Retrieve information to help answer a query.""" retrieved_docs = vector_store.similarity_search(query, k=2) return "\n\n".join( f"Source: {doc.metadata}\nContent: {doc.page_content}" for doc in retrieved_docs )tools = [retrieve_context]llm = ChatTongyi()agent = create_agent( llm, tools, system_prompt="You have access to a tool that retrieves context. Use it to help answer user queries.",)response = agent.invoke({"messages": [{"role": "user", "content": "What is task decomposition?"}]})