OpenAI 函数调用在子问题查询引擎中的应用¶
在本笔记本中,我们将展示如何利用 OpenAI 函数调用功能来提升子问题查询引擎的鲁棒性。
子问题查询引擎设计为可接受实现 BaseQuestionGenerator 接口的可替换问题生成器。
为充分发挥 OpenAI 函数调用 API 的能力,我们实现了新型 OpenAIQuestionGenerator(由我们的 OpenAIPydanticProgram 驱动)
OpenAI 问题生成器¶
与默认的 LLMQuestionGenerator 通过补全 API 支持通用大语言模型不同,OpenAIQuestionGenerator 仅适用于支持函数调用 API 的最新 OpenAI 模型。
其优势在于这些模型经过微调可输出 JSON 对象,因此我们无需过多担心输出解析问题。
如果您在 Colab 上打开此 Notebook,可能需要安装 LlamaIndex 🦙。
In [ ]:
Copied!
%pip install llama-index-question-gen-openai
%pip install llama-index-question-gen-openai
In [ ]:
Copied!
!pip install llama-index
!pip install llama-index
In [ ]:
Copied!
from llama_index.question_gen.openai import OpenAIQuestionGenerator
from llama_index.question_gen.openai import OpenAIQuestionGenerator
In [ ]:
Copied!
question_gen = OpenAIQuestionGenerator.from_defaults()
question_gen = OpenAIQuestionGenerator.from_defaults()
让我们来测试一下!
In [ ]:
Copied!
from llama_index.core.tools import ToolMetadata
from llama_index.core import QueryBundle
from llama_index.core.tools import ToolMetadata
from llama_index.core import QueryBundle
In [ ]:
Copied!
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"
),
),
]
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"
),
),
]
In [ ]:
Copied!
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 = question_gen.generate(
tools=tools,
query=QueryBundle(
"Compare the fastest growing sectors for Uber in the first two"
" quarters of 2022"
),
)
In [ ]:
Copied!
sub_questions
sub_questions
Out[ ]:
[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')]