SQLServer
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.
!pip install langchain-sqlserver==0.1.1
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:
# os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
# os.environ["LANGSMITH_TRACING"] = "true"
Initialization​
from langchain_sqlserver import SQLServer_VectorStore
Find your Azure SQL DB connection string in the Azure portal under your database settings
For more info: Connect to Azure SQL DB - Python
import os
import pyodbc
# Define your SQLServer Connection String
_CONNECTION_STRING = (
"Driver={ODBC Driver 18 for SQL Server};"
"Server=<YOUR_DBSERVER>.database.windows.net,1433;"
"Database=test;"
"TrustServerCertificate=yes;"
"Connection Timeout=60;"
"LongAsMax=yes;"
)
# Connection string can vary:
# "mssql+pyodbc://<username>:<password><servername>/<dbname>?driver=ODBC+Driver+18+for+SQL+Server" -> With Username and Password specified
# "mssql+pyodbc://<servername>/<dbname>?driver=ODBC+Driver+18+for+SQL+Server&Trusted_connection=yes" -> Uses Trusted connection
# "mssql+pyodbc://<servername>/<dbname>?driver=ODBC+Driver+18+for+SQL+Server" -> Uses EntraID connection
# "mssql+pyodbc://<servername>/<dbname>?driver=ODBC+Driver+18+for+SQL+Server&Trusted_connection=no" -> Uses EntraID connection
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.
!pip install langchain-openai
# Import the necessary Libraries
from langchain_openai import AzureChatOpenAI, AzureOpenAIEmbeddings
# Set your AzureOpenAI details
azure_endpoint = "https://<YOUR_ENDPOINT>.openai.azure.com/"
azure_deployment_name_embedding = "text-embedding-3-small"
azure_deployment_name_chatcompletion = "chatcompletion"
azure_api_version = "2023-05-15"
azure_api_key = "YOUR_KEY"
# Use AzureChatOpenAI for chat completions
llm = AzureChatOpenAI(
azure_endpoint=azure_endpoint,
azure_deployment=azure_deployment_name_chatcompletion,
openai_api_version=azure_api_version,
openai_api_key=azure_api_key,
)
# Use AzureOpenAIEmbeddings for embeddings
embeddings = AzureOpenAIEmbeddings(
azure_endpoint=azure_endpoint,
azure_deployment=azure_deployment_name_embedding,
openai_api_version=azure_api_version,
openai_api_key=azure_api_key,
)