Creating a Simple RAG in Python with AzureOpenAI and LlamaIndex
/ 2 min read
During a recent stream, I explored the process of ingesting a PDF using LlamaIndex and AzureOpenAI. This blog post will guide you through the steps to accomplish this task.
The objective was straightforward: answer questions based on information contained in PDF files stored in a folder. Here’s the process we’ll follow:
- Embed the PDF data: Convert PDFs into a format comprehensible for AI
- Initialise AzureOpenAI and provide it with the embedded data
- Ask a question and analyse the response
While the LlamaIndex documentation provides an excellent guide, I’ve included some visual aids to enhance clarity.
Setting Up the Environment
First, let’s install the necessary packages:
Note that AzureOpenAI is not included in the llama-index
package and must be installed separately.
Configuring AzureOpenAI
Navigate to Azure AI Studio and deploy your chosen model.
Once deployed, configure LlamaIndex to use it with the following settings:
BUT.
Azure openAI resources unfortunately differ from standard openAI resources as you can’t generate embeddings unless you use an embedding model.
This means that we need a separate embedding model for generating embeddings. Let’s do that.
Deploy an embedding model that starts with text-embedding-*
, since in this case we’re working exclusively with text.
Configure LlamaIndex to use the embedding model:
Implementing the RAG System
With the setup complete, let’s load PDFs into the data folder and query the model:
To run the script in our virtual environment:
And we get a response of
Future Improvements
We can make it better by embedding the data once and storing it in a database like Azure CosmosDB, which is a MongoDB at its core. This means we get rid of repetitive embedding overhead every time we run the script, allowing the LLM to access pre-embedded data directly from the database. I’ll cover this in my future posts.
And here is the repo with this and any future code.
Next: Creating a Simple RAG in Python with AzureOpenAI and LlamaIndex. Part 2.