(已弃用)查询引擎 + Pydantic 输出#
提示
本指南参考了一种已弃用的在 RAG 工作流中提取结构化输出的方法。请查看我们的结构化输出入门指南了解更多详情。
使用 index.as_query_engine()
及其底层的 RetrieverQueryEngine
,我们可以支持结构化的 Pydantic 输出,而无需额外的 LLM 调用(与典型的输出解析器不同)。
每个查询引擎都支持在 RetrieverQueryEngine
中使用以下 response_mode
实现集成的结构化响应:
refine
compact
tree_summarize
accumulate
(Beta 版,需要额外解析才能转换为对象)compact_accumulate
(Beta 版,需要额外解析才能转换为对象)
在底层,这取决于您设置的 LLM,它使用 OpenAIPydanitcProgam
或 LLMTextCompletionProgram
。如果存在中间 LLM 响应(即在 refine
或 tree_summarize
的多次 LLM 调用期间),Pydantic 对象会作为 JSON 对象注入到下一个 LLM 提示词中。
使用模式#
首先,您需要定义要提取的对象。
from typing import List
from pydantic import BaseModel
class Biography(BaseModel):
"""Data model for a biography."""
name: str
best_known_for: List[str]
extra_info: str
然后,您创建查询引擎。
query_engine = index.as_query_engine(
response_mode="tree_summarize", output_cls=Biography
)
最后,您可以获取响应并检查输出。
response = query_engine.query("Who is Paul Graham?")
print(response.name)
# > 'Paul Graham'
print(response.best_known_for)
# > ['working on Bel', 'co-founding Viaweb', 'creating the programming language Arc']
print(response.extra_info)
# > "Paul Graham is a computer scientist, entrepreneur, and writer. He is best known for ..."
模块#
详细用法请参阅下面的笔记本