A Developer’s Guide to Getting Started with LlamaIndex
LlamaIndex is a tool that focuses on the ‘R’ of RAG (for retrieval) to help enrich an LLM prompt with your data. Here's how devs can use it.
Apr 13th, 2024 5:00am by

Photo by Sung Jin Cho on Unsplash.
Getting Started with LlamaIndex
Let’s get straight into LlamaIndex. Fortunately, there is a quick start that promises a start with “5 lines of code.” Now I’ve done several local LLM installations, but for this post, I’ll tamely use my OpenAI key and burn some credits. I use Visual Studio Code when I want to run Python briefly, which will add a bit of flotsam to the post, but the same touch points will get covered however you enter. On my Mac, I’ll just check up on my Homebrew install of Python3. So opening my Warp terminal, I’ll start with:
>brew install python3
I then start VS in this otherwise empty folder. I installed the Python extension, then I followed good practice and made a project-specific virtual environment from the command palette, using Python: Create environment. I then chose Venv. This ends by confirming that I’m using the Python I just installed:
OK, now I’d better go back to the LlamaIndex instructions and use pip to install the lama-index package as required, in my virtual environment within VS Code using an active terminal (so not in Warp I’m afraid):

I’ll need to tell the environment about my OpenAI key. Given the nature of the virtual environment running under an IDE, it is safest to stick this in the launch.json file that VS Code makes when it runs a project:
..
"configurations":
[
{
"name": "Python Debugger: Current File",
"type": "debugpy",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"env":
{
"OPENAI_API_KEY": "XXXX"
}
}
]
..
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
documents = SimpleDirectoryReader("data").load_data()
index = VectorStoreIndex.from_documents(documents)
query_engine = index.as_query_engine()
response = query_engine.query("What did the author do growing up?")
print(response)
To confirm that I did actually use OpenAI, here are my stats from my account activity:
So what is this code doing? It has embedded the new text into a vector store and supplied an index (hence the call to VectorStoreIndex) and this is retrieved at query time and added to the context window as English just before the call goes through to GPT-3.5. Hence the term enrich we saw earlier.
By adding two lines of logging code, I was able to extract lots of dense REST calls, but also this useful tidbit from the llama_index package:
DEBUG:llama_index.core.indices.utils:> Top 2 nodes:
> [Node 167d0eb4-7dba-4b93-85ec-3f5779b32daa] [Similarity score: 0.819982]
"What I Worked On February 2021 Before college the two main things
I worked on, outside of school..."
> [Node ee847bc2-d56a-4c26-afd7-c4bee9a3d116] [Similarity score: 0.811733]
"I remember taking the boys to the coast on a sunny day in 2015 and
figuring out how to deal with ..."
So we will run this extra query, with this additional purposely vague question:
..
response = query_engine.query("Who is Blessed?")
print(response)
Adonis is Blessed.
Interesting. Let’s grab the one area in the sonnets where Adonis is mentioned:
“Blessed are you whose worthiness gives scope, Being had to triumph, being lacked to hope. What is your substance, whereof are you made, That millions of strange shadows on you tend? Since every one, hath every one, one shade, And you but one, can every shadow lend: Describe Adonis and the counterfeit, Is poorly imitated after you, On Helen’s cheek all art of beauty set, And you in Grecian tires are painted new: Speak of the spring, and foison of the year, The one doth shadow of your beauty show, The other as your bounty doth appear, And you in every blessed shape we know.”
This is confirmed by looking at the log nodes, like the ones we saw earlier:
DEBUG:llama_index.core.indices.utils:> Top 2 nodes:
> [Node 38e29f53-3656-4b55-ab6b-08acf898f122] [Similarity score: 0.766188]
"Blessed are you whose worthiness gives scope, Being had to triumph,
being lacked to hope. What i..."
> [Node 16d55fda-34ac-42cf-9b08-66d2c6944302] [Similarity score: 0.730936]
"And other strains of woe, which now seem woe, Compared with loss of thee,
will not seem so. Some..."
YOUTUBE.COM/THENEWSTACK
Tech moves fast, don't miss an episode. Subscribe to our YouTube
channel to stream all our podcasts, interviews, demos, and more.

