-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add function calling based sub question generator class, and use it b…
…y default if possible (#6949)
- Loading branch information
Showing
8 changed files
with
322 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "c58e17b3-ec09-4e07-8e2e-d19a8e24dd40", | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"# OpenAI function calling for Sub-Question Query Engine" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "d5637f97-60c3-40bb-840f-fc4e217940a7", | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"In this notebook, we showcase how to use OpenAI function calling to improve the robustness of our sub-question query engine. " | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "bd3d24c8-5b2b-4acf-a9de-53134453c186", | ||
"metadata": {}, | ||
"source": [ | ||
"The sub-question query engine is designed to accept swappable question generators that implement the `BaseQuestionGenerator` interface. \n", | ||
"To leverage the power of openai function calling API, we implemented a new `OpenAIQuestionGenerator` (powered by our `OpenAIPydanticProgram`)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "afa2db97-2a46-4629-a201-d4eb99480f3d", | ||
"metadata": {}, | ||
"source": [ | ||
"## OpenAI Question Generator" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "3977e961-fb19-495f-89c5-6a283596b459", | ||
"metadata": {}, | ||
"source": [ | ||
"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. \n", | ||
"\n", | ||
"The benefit is that these models are fine-tuned to output JSON objects, so we can worry less about output parsing issues." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 54, | ||
"id": "85b9e1d3-2f60-4730-8186-7c3c30b6dae5", | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"from llama_index.question_gen.openai_generator import OpenAIQuestionGenerator" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 55, | ||
"id": "0df7f8ad-c026-4bfc-9a12-52efcb24f9d5", | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"question_gen = OpenAIQuestionGenerator.from_defaults()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "04039c8c-72df-495d-915c-09d04321bb96", | ||
"metadata": {}, | ||
"source": [ | ||
"Let's test it out!" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 56, | ||
"id": "1e40ac6c-6b66-4cf3-9dd6-de02416b7dd5", | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"from llama_index.tools import ToolMetadata\n", | ||
"from llama_index import QueryBundle" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 57, | ||
"id": "77106a07-bccf-471d-8d85-c6438772cf35", | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"tools = [\n", | ||
" ToolMetadata(\n", | ||
" name=\"march_22\",\n", | ||
" description=\"Provides information about Uber quarterly financials ending March 2022\",\n", | ||
" ),\n", | ||
" ToolMetadata(\n", | ||
" name=\"june_22\",\n", | ||
" description=\"Provides information about Uber quarterly financials ending June 2022\",\n", | ||
" ),\n", | ||
" ToolMetadata(\n", | ||
" name=\"sept_22\",\n", | ||
" description=\"Provides information about Uber quarterly financials ending September 2022\",\n", | ||
" ),\n", | ||
" ToolMetadata(\n", | ||
" name=\"sept_21\",\n", | ||
" description=\"Provides information about Uber quarterly financials ending September 2022\",\n", | ||
" ),\n", | ||
" ToolMetadata(\n", | ||
" name=\"june_21\",\n", | ||
" description=\"Provides information about Uber quarterly financials ending June 2022\",\n", | ||
" ),\n", | ||
" ToolMetadata(\n", | ||
" name=\"march_21\",\n", | ||
" description=\"Provides information about Uber quarterly financials ending March 2022\",\n", | ||
" ),\n", | ||
"]" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 58, | ||
"id": "82ed271a-bd0d-4b6a-b9e3-987d75f6a4ad", | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"sub_questions = question_gen.generate(\n", | ||
" tools=tools,\n", | ||
" query=QueryBundle(\n", | ||
" \"Compare the fastest growing sectors for Uber in the first two quarters of 2022\"\n", | ||
" ),\n", | ||
")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 59, | ||
"id": "2740e60e-c4e6-412a-b46f-70a1f3fe1231", | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"[SubQuestion(sub_question='What were the fastest growing sectors for Uber in March 2022?', tool_name='march_22'),\n", | ||
" SubQuestion(sub_question='What were the fastest growing sectors for Uber in June 2022?', tool_name='june_22')]" | ||
] | ||
}, | ||
"execution_count": 59, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"sub_questions" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.9.16" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.