liveBook · Manning

LlamaIndex

As explained in Data Analysis with LLMs

LlamaIndex is an open-source software framework for building applications that connect language models to external data and retrieval systems.

LlamaIndex is a Python framework for answering questions using large collections of data. Rather than sending every file to a language model, it indexes the data, finds pieces relevant to a question, and sends those pieces with the question as context. section 10.0 section 10.6.1

Compare results across business units

Imagine a global company whose units publish results in different formats: some use text documents, while others use PowerPoint presentations. Give a question-answering program the path to a directory of files and a question about the units. LlamaIndex loads the files, builds an index, retrieves relevant data, and returns an answer. The answer should still be checked against the underlying files before relying on it. section 10.6.3

A typical pipeline has four steps:

  • Load files and split long documents into manageable pieces. section 10.6.1
  • Create an index by associating those pieces with embedding vectors, which represent their meaning. section 10.6.1
  • Compare the question’s vector with indexed vectors to find relevant pieces. section 10.6.1
  • Pass the question and retrieved pieces to a language model to generate an answer. section 10.6.1
Listing 10.4 A simple question-answering system with LlamaIndex
import argparse
import openai

from llama_index.core import VectorStoreIndex, SimpleDirectoryReader

if __name__ == '__main__':
     #1
    parser = argparse.ArgumentParser()
    parser.add_argument('datadir', type=str, help='Path to data directory')
    parser.add_argument('question', type=str, help='A question to answer')
    args = parser.parse_args()
     #2
    documents = SimpleDirectoryReader(args.datadir).load_data()
     #3
    index = VectorStoreIndex.from_documents(documents)
     #4
    engine = index.as_query_engine()
     #5
    answer = engine.query(args.question)
    print(answer)

This working Python example takes a directory path and a question as command-line inputs. SimpleDirectoryReader loads the files; VectorStoreIndex builds the index; a query engine retrieves context and answers the question. section 10.6.3

For this example, install llama-index==0.10.25; additional packages support the PDF and PowerPoint files used in the project. LlamaIndex is especially useful when an application needs indexed access to substantial or varied data. For a simple prompt-and-answer task, a provider’s lower-level Python library may be enough. Framework interfaces can change between versions. section 10.6.2 section 10.7

How Data Analysis with LLMs covers it

How deep it goes
Walks through loading, chunking, embedding, retrieval, and a short Python implementation without detailing model internals.
Examples
Uses a command-line Python program to answer questions over business-unit files, including text documents and PowerPoint presentations.
What it assumes
Assumes enough Python and terminal familiarity to run a script and install packages, but introduces embedding vectors in plain language.
What you can do after
Apply it via LlamaIndex to load a file directory, build an index, and query its contents.
How it connects
Links embedding vectors and query engines to language-model question answering, while contrasting LlamaIndex with LangChain and provider libraries.

Also explained in

Compare how 4 books explain LlamaIndex →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data Analysis with LLMs. The section links point to the parts of the book it draws on.