Using LlamaIndex Part 1 — OpenAI

Michael Ruminer
5 min readJan 4, 2025

--

A black and white AI generated “photo” of a close up of a llama eye

I have started to experiment with LlamaIndex for use in Retrieval Augmented Generation (RAG) document parsing and indexing. My results were mixed on the simple page provided. This is part 1, where I make a short post on LlamaIndex with OpenAI as the LLM component. I expect part 2 to be LlamaIndex with Ollama and Llama3–8b as the LLM components.

This is a very short chunk of code. I also used the LlamaIndex Parse browser-based tool to see if I received different outputs. As one would expect, I did not. You can access the browser-based tool by opening a LlamaIndex account and choosing the “Parse” tool in your dashboard. You’ll need an account if you plan to use the code I provide and you will also need to generate an API key from your LlamaIndex dashboard. One of the great things about LlamaIndex is that for a paid tool it is generous in its free usage; 1000 credits PER DAY. In “accurate” mode, it is 1 credit per page; in “premium” mode, it is 15 credits per page. For my simple one page example the output between the two did not differ.

First the small snippet of code.

# pip install llama-index-embeddings-openai llama-index-llms-openai
# pip install llama-index-core llama-parse llama-index-readers-file

from llama_parse import LlamaParse
from llama_index.core import VectorStoreIndex
from dotenv import load_dotenv


load_dotenv()

parser = LlamaParse(result_type="markdown", verbose=True)

file_name = "ssi-page-5.pdf"
extra_info = {"file_name": file_name}

with open(f"../../sample_docs/{file_name}", "rb") as file_to_parse:
# must provide extra_info with file_name key when passing file object
documents = parser.load_data(file_to_parse, extra_info=extra_info)
# to manually check the MD output uncomment the below
# print(documents[0].text)

# index the parsed documents
index = VectorStoreIndex.from_documents(documents)

# generate a query engine for the index
query_engine = index.as_query_engine()

# provide the query and output the results
query = "what are the principles of SSI?"
response = query_engine.query(query)
print(response)

You can find this code and a few sample documents, including the document used in this code in my LlamaIndex_Test Github repo with the code specifically under the Test0 folder.

Note that I don’t set an LLM or an embedding model. LlamaIndex uses OpenAI as the default LLM and OpenAI’s text-embedding-ada-002. You will need an OpenAI API key to go along with the LlamaIndex key. My code loads them from the .env to environmental variables and if they are named appropriately those variables will be found by default. Below is a .env example.

OPENAI_API_KEY=YOUR_API_KEY
LLAMA_CLOUD_API_KEY=YOUR_API_KEY

In the code above I am using a single-page PDF, “ssi-page-5.pdf”. It is page 5 of the larger document, “Self-Sovereign Identity A Systematic Review Mapping and Taxonomy.pdf”. If you plan to send LlamaParse a larger document but use the API properties to tell it only to parse a subset of pages from the document keep in mind that LlamaParse starts at page 0. The first time I tried this I had an off-by-one issue because I assumed page 1 of the document was, you know, page 1. It was page 0. This is understandable from a programming standpoint but caught me off guard anyway.

a screenshot of part of the Parse browser-based tool showing the target pages entry box and a note above it that indicates it is a 0 based page count

In the example code, I opened a file directly but LlamaIndex provides a directory reader with filters, if you desire to use that instead. The results I got back on the LLM query were spot on as would be expected on a single page of context with a well-outlined section pertinent to my simple query.

You don’t really need the creation of the vector index, query engine and query/response to test out LlamaIndex parsing. Just uncomment line 23 in the above code (line 19 in the repo code) comment out everything below it and get the parsed output.

Premium Mode and Auto Mode and Less than Expected Outcomes

In the code, I didn’t try out premium mode or auto mode. I intend to make a separate post about auto mode. I did try them in the LlamaIndex Parse tool. In both, I expected the image at the top of the page to get output as an image in the “Images” tab of the Parse output tool, but it didn’t.

Screenshot that shows the “images” tab of the Parse tool and shows that it reads “no images found in the document”

The image at the top of the page is below as a screen capture.

Image from the top of the page in the parsed document

This was disappointing. I’m not sure why this did not provide the expected outcome.

There you have it. A simple bit of code to parse using LlamaIndex. What makes it different from other parsers I have tried (all open source) is that it spits out the results in markdown, if desired, which is better than the usual plain text I received in other tools. The markdown provides the LLM more context even if in my simple case it was not of value. The other is that in theory, it will better parse images, tables etc., but as I explained I did not get that result. :-( I’ll continue to experiment with it, especially on more complicated pages such as ones that contain a table and in auto mode via code.

You can find part 2 as “AI RAG with LlamaIndex, Local Embedding, and Ollama Llama 3.1 8b”. The example in part 2 uses LlamaParse auto mode.

--

--

Michael Ruminer
Michael Ruminer

Written by Michael Ruminer

My most recent posts are on AI, especially from the perspective of a, currently, non-AI tech worker. did:web:manicprogrammer.github.io

No responses yet