RAG with Azure AI Search

Nov 15 2024 · Python 3.12, Microsoft Azure, JupyterLab

Lesson 01: RAG with Azure AI Search

Demo 02

Episode complete

Play next episode

Next
Transcript

In this demo, you’ll run a basic RAG. The starter project for this lesson is a Jupyter Notebook — a special file that allows you to create cells of either markdown content or Python scripts.

Jupyter Notebooks work with other programming languages and various platforms. For instance, you can install Jupyter as a Python module on your computer and run it from the terminal. This will open a web interface in a browser and provide you with an environment that allows you to execute Jupyter Notebooks.

For this module, however, you’ll learn how to run the notebooks with Visual Studio Code.

Install Visual Studio Code if you don’t have it already. In the Extensions window, search for Jupyter and Python and install them.

Then, open the starter project for this module in Visual Studio Code. Press Ctrl or Cmd + Shift + P and search for the Python: Create Environment command. Click it and select Venv to create a virtual environment in your workspace.

Open the 01-rag-with-azure-ai-search.ipynb-starter.ipynb file then go to the top right corner and click Select Kernel. Choose the newly created Python environment. In a few steps, you’ll be able to run your RAG app!

Open the terminal within Visual Studio Code to install ipykernel. Run the following command to install:

python3.11 -m pip install ipykernel -U --user --force-reinstall

You’re ready to execute the commands in your notebook now. In the notebook, each rectangular box is called a cell. A cell could either contain markdown text, or code you can execute. To the left of a cell is a small triangle you can click to execute or debug the script in that cell.

At the very top, there are buttons to:

  • Create a new cell, to add a Code cell or markdown cell.
  • Run all the cells.
  • Restart the kernel.
  • Clear all outputs.
  • Show variables.
  • Show or hide the outline in the left sidebar.
  • Open an overflow menu which shows extra actions you can take in your notebook.

In the first cell, there are packages you’ll need for your app. Uncomment the code and execute the cell by clicking the play button to the left of the cell.

The packages include:

  • azure-search-documents: This allows you to contact your Azure AI Search service to manage the documents.
  • azure-identity: This provides the ability to authenticate with Azure services.
  • python-dotenv: This provides the ability to manage environment variables and data in a Python program.

The --quiet option prevents the installation process from spitting out logs to your cell output. Each program cell has an output that displays the output of a cell after it’s run.

In the next cell, replace the value of AZURE_SEARCH_SERVICE_URL with the URL of the search service you created earlier and replace the value for AZURE_SEARCH_SERVICE_API_KEY with the Primary admin key, found under the Settings/Keys menu.

You’ll be creating an index of television shows, hence the name television-shows for the INDEX_NAME variable! An index is akin to a database table or collection. In it, you’ll store your data you want to search on later. Execute this cell too.

In the next cell, under “Create a Search Schema”, you create an Azure credential and an index client to help you manage indexes in your search service. You then create several searchable fields for your index. The search fields point to the attributes of your index you’ll want to search on. Execute this cell.

In the next cell, you put it all together to create a search index. Execute this cell.

In the following cell, you define a few television shows to index. Click the button to execute this cell too.

Run the following cell to create a search client and upload the documents to Azure AI Search.

All is set for your queries now. Uncomment the code in the next cell to run an empty query. By setting the search_text to *, you won’t be searching for anything in particular. Do this as a form of control to ensure your setup is correct. Execute this cell and check out the results.

The output reveals a list of documents with the same score. This confirms that the search didn’t match any specific documents. All of the results are therefore equally a match.

In the next cell, however, the search text is “drama”. This indicates a specific category of television shows you want to retrieve. Execute this cell and observe the results.

This time, the query matches four documents. Each of these documents has a score that indicates how relevant they are to the query. That’s it, you’ve built a RAG with Azure AI Search. Congratulations!

Note: In the next two cells, delete the index and confirm it afterward. This is necessary to ensure that you always clean up after yourself when using such billable services — and avoid any unexpected costs.

Follow to the next segment to conclude this lesson…

See forum comments
Cinema mode Download course materials from Github
Previous: Instruction 02 Next: Conclusion