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

设置

Install the langchain-sqlserver python package. The code lives in an integration package called:langchain-sqlserver.

凭证

There are no credentials needed to run this notebook, just make sure you downloaded the langchain-sqlserver package 如果您希望获得一流的模型调用自动追踪功能,还可以通过取消注释以下代码来设置 LangSmith API 密钥:

初始化

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.

管理向量存储

向向量存储添加项目

查询向量存储

一旦创建了向量存储并添加了相关文档,您很可能希望在链或智能体运行期间对其进行查询。 可以按以下方式执行简单的相似度搜索:

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.

带分数的相似度搜索

如果您想执行相似度搜索并获取对应分数,可以运行:
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 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:AzureBlobStorageContainerLoader

直接查询

用于检索增强生成

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.

转换为检索器进行查询

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 参考

For detailed documentation of SQLServer Vectorstore features and configurations head to the API reference: https://python.langchain.com/api_reference/sqlserver/index.html