The `llama-index-question-gen-openai` package provides an integration for LlamaIndex, enabling the generation of sub-questions using OpenAI's function calling API. It leverages the fine-tuned capabilities of the latest OpenAI models to output structured JSON objects, aiming to reduce output parsing issues compared to generic LLM question generators. The current version is 0.3.1, and it is part of the actively developed LlamaIndex ecosystem with frequent updates.
pip install llama-index-question-gen-openaiVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to initialize `OpenAIQuestionGenerator` and use it within a LlamaIndex application to generate sub-questions. It requires an `OPENAI_API_KEY` and sets up a basic `QueryEngineTool` that the generator can reference. The generated sub-questions are tailored to the provided tools and original query.
Ensure you are using a compatible OpenAI model (e.g., `gpt-3.5-turbo-0613` or newer) when initializing `OpenAIQuestionGenerator`.
Always `pip install` the specific `llama-index-*-*` integration package you intend to use (e.g., `pip install llama-index-question-gen-openai`).
Before running your application, ensure `OPENAI_API_KEY` is properly configured in your environment. For quick testing, you can directly set `os.environ["OPENAI_API_KEY"]`.
Shorten tool descriptions or consider moving extensive details into the prompt itself rather than solely relying on tool metadata. Evaluate if fewer, more broadly defined tools can achieve the same goal without hitting the limit.
Implement robust error handling with `try-except` blocks around API calls. Consider using a retry mechanism with exponential backoff (e.g., via the `tenacity` library) to manage transient network issues and rate limits. Verify your API key and monitor usage in your OpenAI dashboard.
You need to install the package explicitly using pip: `pip install llama-index-question-gen-openai`.
Upgrade your LlamaIndex and all its integration packages to their latest versions (`pip install --upgrade llama-index llama-index-question-gen-openai`) to ensure compatibility with `openai` v1.x. Alternatively, if you must use an older LlamaIndex, downgrade your `openai` package to a `0.x.x` version (e.g., `pip install openai==0.28`).
Update your code to initialize the OpenAI client by passing the API parameters directly: `from llama_index.llms.openai import OpenAI; llm = OpenAI(api_key='YOUR_API_KEY', api_base='YOUR_API_BASE')` or ensure `OPENAI_API_KEY` and `OPENAI_API_BASE` are set as environment variables.
Set your OpenAI API key as an environment variable (e.g., `export OPENAI_API_KEY='your_key_here'` in your shell or `os.environ['OPENAI_API_KEY'] = 'your_key_here'` in Python before initializing the LLM). Alternatively, pass the `api_key` directly when creating the OpenAI LLM instance: `from llama_index.llms.openai import OpenAI; llm = OpenAI(api_key='your_key_here')`.