This package provides the OpenAI Large Language Model (LLM) integration for LlamaIndex (version 0.7.5). LlamaIndex is a data framework designed to connect LLMs with your private or domain-specific data, enabling applications like RAG (Retrieval Augmented Generation). This integration allows LlamaIndex users to leverage various OpenAI models for text completion, chat generation, streaming responses, and structured outputs within their LlamaIndex applications. The library is actively maintained and releases are tied to the broader LlamaIndex ecosystem updates.
pip install llama-index-llms-openaiVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to initialize the OpenAI LLM and perform basic text completion and chat interactions. It assumes your OpenAI API key is set as an environment variable (`OPENAI_API_KEY`). You can explicitly pass the `api_key` argument during initialization if preferred. It also shows how to use ChatMessage from `llama_index.core.llms` for chat interactions.
Update your import statements from `from llamaindex import OpenAI` to `from llama_index.llms.openai import OpenAI`.
Migrate from `ServiceContext` to `Settings`. Instead of `service_context = ServiceContext.from_defaults(llm=OpenAI())`, use `from llama_index.core import Settings; Settings.llm = OpenAI()`. Explicitly set your LLM and embedding model.
Set the `OPENAI_API_KEY` environment variable before running your application (e.g., `export OPENAI_API_KEY='sk-...'`). Alternatively, pass the API key directly to the `OpenAI` constructor: `llm = OpenAI(api_key='sk-...', model='gpt-3.5-turbo')`.
If not installing the full `llama-index` package, explicitly install `pip install llama-index-core llama-index-llms-openai`.
Initialize the LLM with the desired model, e.g., `llm = OpenAI(model="gpt-4o")` or `llm = OpenAI(model="gpt-3.5-turbo")`.
Upgrade your Python environment to version 3.10 or newer. If you must use an older Python version (e.g., 3.9), you will need to downgrade your LlamaIndex installation to a version compatible with Python 3.9.
Set the `OPENAI_API_KEY` environment variable (e.g., `os.environ["OPENAI_API_KEY"] = "sk-..."`) or pass the `api_key` directly to the `OpenAI` class constructor when initializing the LLM.
Verify your internet connection, check if the `api_base` URL (if customized for proxies or local models) is correct and accessible, ensure no firewall is blocking access, and confirm the OpenAI service status. If using Azure OpenAI, ensure `api_type`, `api_version`, and `api_base` are correctly set.
Ensure `llama-index-llms-openai` is installed (`pip install llama-index-llms-openai`). If it's already installed, try upgrading both `llama-index` and `llama-index-llms-openai` to compatible versions (`pip install --upgrade llama-index llama-index-llms-openai`). Restart your Python environment after installation.
Update your import statement to use `from llama_index.core.llms import LLM` if you need the base LLM class, or directly import the concrete `OpenAI` class using `from llama_index.llms.openai import OpenAI`.