.ipynb
.pdf
Azure Cognitive Search
 Contents 
Azure Cognitive Search
Install Azure Cognitive Search SDK
Import required libraries
Configure OpenAI settings
Configure vector store settings
Create embeddings and vector store instances
Insert text and embeddings into vector store
Perform a vector similarity search
Perform a Hybrid Search
Azure Cognitive Search#
Install Azure Cognitive Search SDK#
!pip install --index-url=https://pkgs.dev.azure.com/azure-sdk/public/_packaging/azure-sdk-for-python/pypi/simple/ azure-search-documents==11.4.0a20230509004
!pip install azure-identity
Import required libraries#
import os, json
import openai
from dotenv import load_dotenv
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.schema import BaseRetriever
from langchain.vectorstores.azuresearch import AzureSearch
Configure OpenAI settings#
Configure the OpenAI settings to use Azure OpenAI or OpenAI
# Load environment variables from a .env file using load_dotenv():
load_dotenv()
openai.api_type = "azure"
openai.api_base = "YOUR_OPENAI_ENDPOINT"
openai.api_version = "2023-05-15"
openai.api_key = "YOUR_OPENAI_API_KEY"
model: str = "text-embedding-ada-002"
Configure vector store settings#
Set up the vector store settings using environment variables:
vector_store_address: str = 'YOUR_AZURE_SEARCH_ENDPOINT'
vector_store_password: str = 'YOUR_AZURE_SEARCH_ADMIN_KEY'
index_name: str = "langchain-vector-demo"
Create embeddings and vector store instances#
Create instances of the OpenAIEmbeddings and AzureSearch classes:
embeddings: OpenAIEmbeddings = OpenAIEmbeddings(model=model, chunk_size=1)
rtdocs_stable/api.python.langchain.com/en/stable/modules/indexes/vectorstores/examples/azuresearch.html