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



