OpenAI function calling for Sub-Question Query Engine¶
In this notebook, we showcase how to use OpenAI function calling to improve the robustness of our sub-question query engine.
The sub-question query engine is designed to accept swappable question generators that implement the BaseQuestionGenerator
interface.
To leverage the power of openai function calling API, we implemented a new OpenAIQuestionGenerator
(powered by our OpenAIPydanticProgram
)
OpenAI Question Generator¶
Unlike the default LLMQuestionGenerator
that supports generic LLMs via the completion API, OpenAIQuestionGenerator
only works with the latest OpenAI models that supports the function calling API.
The benefit is that these models are fine-tuned to output JSON objects, so we can worry less about output parsing issues.
If you're opening this Notebook on colab, you will probably need to install LlamaIndex 🦙.
%pip install llama-index-question-gen-openai
!pip install llama-index
from llama_index.question_gen.openai import OpenAIQuestionGenerator
question_gen = OpenAIQuestionGenerator.from_defaults()
Let's test it out!
from llama_index.core.tools import ToolMetadata
from llama_index.core import QueryBundle
tools = [
ToolMetadata(
name="march_22",
description=(
"Provides information about Uber quarterly financials ending March"
" 2022"
),
),
ToolMetadata(
name="june_22",
description=(
"Provides information about Uber quarterly financials ending June"
" 2022"
),
),
ToolMetadata(
name="sept_22",
description=(
"Provides information about Uber quarterly financials ending"
" September 2022"
),
),
ToolMetadata(
name="sept_21",
description=(
"Provides information about Uber quarterly financials ending"
" September 2022"
),
),
ToolMetadata(
name="june_21",
description=(
"Provides information about Uber quarterly financials ending June"
" 2022"
),
),
ToolMetadata(
name="march_21",
description=(
"Provides information about Uber quarterly financials ending March"
" 2022"
),
),
]
sub_questions = question_gen.generate(
tools=tools,
query=QueryBundle(
"Compare the fastest growing sectors for Uber in the first two"
" quarters of 2022"
),
)
sub_questions
[SubQuestion(sub_question='What were the fastest growing sectors for Uber in March 2022?', tool_name='march_22'), SubQuestion(sub_question='What were the fastest growing sectors for Uber in June 2022?', tool_name='june_22')]