How to use LlamaIndex with OpenAI
Quick answer
Use
LlamaIndex to create an index from your documents and query it by connecting with the OpenAI API client. Initialize OpenAI with your API key, build a GPTVectorStoreIndex from documents, and then run queries to get AI-powered answers.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install openai>=1.0pip install llama-index
Setup
Install the required packages and set your OpenAI API key as an environment variable.
pip install openai llama-index Step by step
This example shows how to create a simple document index with LlamaIndex and query it using the OpenAI SDK v1 client.
import os
from openai import OpenAI
from llama_index import GPTVectorStoreIndex, SimpleDirectoryReader
# Initialize OpenAI client
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
# Load documents from a local directory
documents = SimpleDirectoryReader("./data").load_data()
# Create an index from documents
index = GPTVectorStoreIndex.from_documents(documents, client=client)
# Query the index
query = "What is the main topic of the documents?"
response = index.query(query)
print("Answer:", response.response) output
Answer: The main topic of the documents is about AI integration with LlamaIndex and OpenAI.
Common variations
You can customize the model by passing parameters to the OpenAI client or use different index types like GPTListIndex. Async usage is possible but requires adapting to async SDK patterns.
import os
from openai import OpenAI
from llama_index import GPTListIndex, SimpleDirectoryReader
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
# Using GPTListIndex instead of GPTVectorStoreIndex
documents = SimpleDirectoryReader("./data").load_data()
index = GPTListIndex.from_documents(documents, client=client)
response = index.query("Summarize the documents.")
print("Summary:", response.response) output
Summary: The documents provide an overview of integrating LlamaIndex with OpenAI for document querying.
Troubleshooting
- If you get authentication errors, verify your
OPENAI_API_KEYenvironment variable is set correctly. - If document loading fails, check the path and file formats supported by
SimpleDirectoryReader. - For slow responses, consider limiting
max_tokensor using smaller models.
Key Takeaways
- Use the latest
OpenAISDK v1 client withLlamaIndexfor seamless integration. - Load documents with
SimpleDirectoryReaderand create an index for efficient querying. - Customize index types and models to fit your use case and performance needs.