Azure SQL provides a dedicated Vector data type that simplifies the creation, storage, and querying of vector embeddings directly within a relational database. This eliminates the need for separate vector databases and related integrations, increasing the security of your solutions while reducing the overall complexity.Azure SQL is a robust service that combines scalability, security, and high availability, providing all the benefits of a modern database solution. It leverages a sophisticated query optimizer and enterprise features to perform vector similarity searches alongside traditional SQL queries, enhancing data analysis and decision-making. Read more on using Intelligent applications with Azure SQL Database This notebook shows you how to leverage this integrated SQL vector database to store documents and perform vector search queries using Cosine (cosine distance), L2 (Euclidean distance), and IP (inner product) to locate documents close to the query vectors
Setup
Install thelangchain-sqlserver python package.
The code lives in an integration package called:langchain-sqlserver.
Credentials
There are no credentials needed to run this notebook, just make sure you downloaded thelangchain-sqlserver package
If you want to get best in-class automated tracing of your model calls you can also set your LangSmith API key by uncommenting below:
Initialization
Manage vector store
Add items to vector store
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. Performing a simple similarity search can be done as follows:Filtering Support
The vectorstore supports a set of filters that can be applied against the metadata fields of the documents.This feature enables developers and data analysts to refine their queries, ensuring that the search results are accurately aligned with their needs. By applying filters based on specific metadata attributes, users can limit the scope of their searches, concentrating only on the most relevant data subsets.Similarity Search with Score
If you want to execute a similarity search and receive the corresponding scores you can run:Similarity Search when you already have embeddings you want to search on
Delete items from vector store
Delete Row by ID
Drop Vector Store
Load a Document from Azure Blob Storage
Below is example of loading a file from Azure Blob Storage container into the SQL Vector store after splitting the document into chunks. Azure Blog Storage is Microsoft’s object storage solution for the cloud. Blob Storage is optimized for storing massive amounts of unstructured data.Query directly
Usage for retrieval-augmented generation
Use Case 1: Q&A System based on the Story Book
The Q&A function allows users to ask specific questions about the story, characters, and events, and get concise, context-rich answers. This not only enhances their understanding of the books but also makes them feel like they’re part of the magical universe.Query by turning into retriever
The LangChain Vector store simplifies building sophisticated Q&A systems by enabling efficient similarity searches to find the top 10 relevant documents based on the user’s query. The retriever is created from the vector_store, and the question-answer chain is built using the create_stuff_documents_chain function. A prompt template is crafted using the ChatPromptTemplate class, ensuring structured and context-rich responses. Often in Q&A applications it’s important to show users the sources that were used to generate the answer. LangChain’s built-in create_retrieval_chain will propagate retrieved source documents to the output under the “context” key: Read more about LangChain RAG tutorials & the terminologies mentioned above hereAPI reference
For detailed documentation of SQLServer Vectorstore features and configurations head to the API reference: https://python.langchain.com/api_reference/sqlserver/index.htmlRelated
- Vector store conceptual guide
- Vector store how-to guides
Connect these docs programmatically to Claude, VSCode, and more via MCP for real-time answers.