Skip to main content
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 the langchain-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 the langchain-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

Find your Azure SQL DB connection string in the Azure portal under your database settings For more info: Connect to Azure SQL DB - Python
In this example we use Azure OpenAI to generate embeddings , however you can use different embeddings provided in LangChain. You can deploy a version of Azure OpenAI instance on Azure Portal following this guide. Once you have your instance running, make sure you have the name of your instance and key. You can find the key in the Azure Portal, under the “Keys and Endpoint” section of your instance.

Manage vector store

The langchain-community package is no longer maintained. Examples that import from langchain_community may be outdated or broken. Use with caution.

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:
For a full list of the different searches you can execute on a Azure SQL vector store, please refer to the API reference.

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.
API Reference: AzureBlobStorageFileLoader

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 and terminologies.

API reference

For detailed documentation of SQLServer Vectorstore features and configurations head to the API reference