Integrating Azure Cognitive Search With Azure OpenAI and Langchain

Shweta Lodha
3 min readMay 25, 2023

In this article, I’ll guide you through all the steps which are required to utilize Azure search(non-semantic) with OpenAI and Langchain.

Before getting started, here are the few prerequisites which need to be satisfied.

Prerequisites

  • An Azure account with an active subscription. If you do not have one, you can Create an account for free.
  • An instance of Azure Cognitive Search service (any tier, any region). You can either create a new instance or can use existing one from your active Azure subscription.

Create A Cognitive Search Index

A very first thing, we need is an search instance. Once the instance is created, we need an index and for index we need data.

Here you can add index by clicking on a button named Add index or you can use button named Import data to import your existing data.

In my case, I’m already having data inside Azure blob, so I’m pulling that. Here is the glimpse of this action:

Import Required Packages

Let’s go ahead and import the required packages. Here are the ones:

import os
from langchain.retrievers import AzureCognitiveSearchRetriever
from langchain.embeddings import OpenAIEmbeddings
from langchain.vectorstores import Chroma
from langchain.llms import AzureOpenAI
from langchain.chains import RetrievalQA

Set The Key Parameters

Here is the list of key parameters which we need to grab from Azure.

os.environ["AZURE_COGNITIVE_SEARCH_SERVICE_NAME"] = "search-ins"
os.environ["AZURE_COGNITIVE_SEARCH_INDEX_NAME"] ="azureblob-index"
os.environ["AZURE_COGNITIVE_SEARCH_API_KEY"] = "RNLiWV6g4LiOPujrZVGge6NDLV4oVouwySgaEQKSpbAzSeB6TMMS"

Get Relevant Docs Using Azure Cognitive Search

As we have all the documents indexed in Azure Cognitive Search under content column…

--

--