使用 LlamaIndex 和输出解析模块生成结构化输出
在现代数据处理和分析中,生成结构化输出对于确保数据的准确性和可读性至关重要。LlamaIndex 提供了与多个框架的输出解析模块集成,使你能够轻松地将非结构化数据转换为结构化格式。本文将介绍如何使用 LlamaIndex 和输出解析模块来生成结构化输出。
输出解析模块的作用
LlamaIndex 支持与其他框架的输出解析模块集成,这些模块可以用于以下两种方式:
- 提供格式化指令:通过
output_parser.format
为任何提示或查询提供格式化指令。 - 解析 LLM 输出:通过
output_parser.parse
解析 LLM 的输出。
使用 Guardrails 进行输出解析
Guardrails 是一个开源的 Python 包,用于规范、验证和修正输出模式。以下是一个使用 Guardrails 的代码示例:
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
from llama_index.output_parsers.guardrails import GuardrailsOutputParser
from llama_index.llms.openai import OpenAI
# 加载文档,构建索引
documents = SimpleDirectoryReader("../paul_graham_essay/data").load_data()
index = VectorStoreIndex(documents, chunk_size=512)
# 定义查询 / 输出规范
rail_spec = """
<rail version="0.1">
<output>
<list name="points" description="Bullet points regarding events in the author's life.">
<object>
<string name="explanation" format="one-line" on-fail-one-line="noop" />
<string name="explanation2" format="one-line" on-fail-one-line="noop" />
<string name="explanation3" format="one-line" on-fail-one-line="noop" />
</object>
</list>
</output>
<prompt>
Query string here.
@xml_prefix_prompt
{output_schema}
@json_suffix_prompt_v2_wo_none
</prompt>
</rail>
"""
# 定义输出解析器
output_parser = GuardrailsOutputParser.from_rail_string(
rail_spec, llm=OpenAI()
)
# 将输出解析器附加到 LLM
llm = OpenAI(output_parser=output_parser)
# 获取结构化响应
query_engine = index.as_query_engine(llm=llm)
response = query_engine.query(
"What are the three items the author did growing up?",
)
print(response)
输出结果:
{'points': [{'explanation': 'Writing short stories', 'explanation2': 'Programming on an IBM 1401', 'explanation3': 'Using microcomputers'}]}
使用 Langchain 进行输出解析
Langchain 也提供了输出解析模块,你可以在 LlamaIndex 中使用它们。以下是一个使用 Langchain 的代码示例:
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
from llama_index.core.output_parsers import LangchainOutputParser
from llama_index.llms.openai import OpenAI
from langchain.output_parsers import StructuredOutputParser, ResponseSchema
# 加载文档,构建索引
documents = SimpleDirectoryReader("../paul_graham_essay/data").load_data()
index = VectorStoreIndex.from_documents(documents)
# 定义输出模式
response_schemas = [
ResponseSchema(
name="Education",
description="Describes the author's educational experience/background.",
),
ResponseSchema(
name="Work",
description="Describes the author's work experience/background.",
),
]
# 定义输出解析器
lc_output_parser = StructuredOutputParser.from_response_schemas(
response_schemas
)
output_parser = LangchainOutputParser(lc_output_parser)
# 将输出解析器附加到 LLM
llm = OpenAI(output_parser=output_parser)
# 获取结构化响应
query_engine = index.as_query_engine(llm=llm)
response = query_engine.query(
"What are a few things the author did growing up?",
)
print(str(response))
输出结果:
{'Education': 'Before college, the author wrote short stories and experimented with programming on an IBM 1401.', 'Work': 'The author also worked with microcomputers during this time.'}
结语
通过 LlamaIndex 和输出解析模块,你可以轻松地将非结构化数据转换为结构化格式,从而提高数据处理的准确性和效率。无论是使用 Guardrails 还是 Langchain,LlamaIndex 都提供了强大的工具来满足你的需求。
希望这篇博客文章能帮助你更好地理解并应用 LlamaIndex 和输出解析模块。如果你有任何问题或需要进一步的帮助,请随时联系我们。祝你在数据处理的道路上越走越远!