78 使用 LlamaIndex 和输出解析模块生成结构化输出 (Langchain或Guardrails)

使用 LlamaIndex 和输出解析模块生成结构化输出

在现代数据处理和分析中,生成结构化输出对于确保数据的准确性和可读性至关重要。LlamaIndex 提供了与多个框架的输出解析模块集成,使你能够轻松地将非结构化数据转换为结构化格式。本文将介绍如何使用 LlamaIndex 和输出解析模块来生成结构化输出。

输出解析模块的作用

LlamaIndex 支持与其他框架的输出解析模块集成,这些模块可以用于以下两种方式:

  1. 提供格式化指令:通过 output_parser.format 为任何提示或查询提供格式化指令。
  2. 解析 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 和输出解析模块。如果你有任何问题或需要进一步的帮助,请随时联系我们。祝你在数据处理的道路上越走越远!

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

需要重新演唱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值