llamaindex -- 响应模式(Response Modes)

响应模式(Response Modes)

在处理和合成响应时,LlamaIndex 提供了多种响应模式,每种模式都有其独特的处理方式和适用场景。以下是对这些模式的详细解释和编程示例。

1. 精炼模式(refine)

概念解释
精炼模式通过依次处理每个检索到的文本块来创建和精炼答案。每次处理一个节点或检索到的文本块时,都会进行一次 LLM 调用。

详细过程

  1. 使用 text_qa_template 提示对第一个文本块进行查询。
  2. 将答案和下一个文本块(以及原始问题)一起用于 refine_template 提示的另一个查询。
  3. 重复上述过程,直到所有文本块都被处理。

如果一个文本块太大,无法适应窗口大小(考虑提示大小),则使用 TokenTextSplitter 将其拆分(允许文本块之间有一些重叠),并将新生成的文本块视为原始文本块集合的一部分(同样使用 refine_template 进行查询)。

适用场景
适用于需要更详细答案的场景。

编程示例

from llama_index import VectorStoreIndex, ServiceContext
from llama_index.response.schema import Response

# 创建索引
index = VectorStoreIndex.from_documents(documents)

# 设置服务上下文
service_context = ServiceContext.from_defaults(chunk_size=512)

# 使用精炼模式进行查询
query_engine = index.as_query_engine(
    response_mode="refine",
    service_context=service_context
)
response: Response = query_engine.query("你的问题")
print(response)
2. 紧凑模式(compact,默认模式)

概念解释
紧凑模式类似于精炼模式,但事先将文本块紧凑(连接),从而减少 LLM 调用次数。

详细过程

  1. 尽可能多地连接(打包)检索到的文本块,使其适应上下文窗口大小(考虑 text_qa_templaterefine_template 之间的最大提示大小)。
  2. 如果文本太长,无法适应一个提示,则使用 TokenTextSplitter 将其拆分为所需的部分(允许文本块之间有一些重叠)。
  3. 每个文本部分被视为一个“文本块”,并发送给精炼合成器。

适用场景
类似于精炼模式,但减少了 LLM 调用次数。

编程示例

from llama_index import VectorStoreIndex, ServiceContext
from llama_index.response.schema import Response

# 创建索引
index = VectorStoreIndex.from_documents(documents)

# 设置服务上下文
service_context = ServiceContext.from_defaults(chunk_size=512)

# 使用紧凑模式进行查询
query_engine = index.as_query_engine(
    response_mode="compact",
    service_context=service_context
)
response: Response = query_engine.query("你的问题")
print(response)
3. 树摘要模式(tree_summarize)

概念解释
树摘要模式使用 summary_template 提示尽可能多地查询 LLM,直到所有连接的文本块都被查询,生成多个答案,这些答案本身被视为文本块,递归地用于树摘要 LLM 调用,直到只剩下一个文本块和一个最终答案。

详细过程

  1. 尽可能多地连接文本块,使其适应上下文窗口大小,使用 summary_template 提示进行查询,并根据需要拆分(使用 TokenTextSplitter 和一些文本重叠)。
  2. 对每个生成的文本块/拆分进行 summary_template 查询(没有精炼查询!),并获取多个答案。
  3. 如果只有一个答案(因为只有一个文本块),则它是最终答案。
  4. 如果有多个答案,这些答案本身被视为文本块,并递归地发送给树摘要过程(连接/拆分以适应/查询)。

适用场景
适用于摘要目的。

编程示例

from llama_index import VectorStoreIndex, ServiceContext
from llama_index.response.schema import Response

# 创建索引
index = VectorStoreIndex.from_documents(documents)

# 设置服务上下文
service_context = ServiceContext.from_defaults(chunk_size=512)

# 使用树摘要模式进行查询
query_engine = index.as_query_engine(
    response_mode="tree_summarize",
    service_context=service_context
)
response: Response = query_engine.query("你的问题")
print(response)
4. 简单摘要模式(simple_summarize)

概念解释
简单摘要模式将所有文本块截断,以适应单个 LLM 提示。适用于快速摘要目的,但由于截断可能会丢失细节。

编程示例

from llama_index import VectorStoreIndex, ServiceContext
from llama_index.response.schema import Response

# 创建索引
index = VectorStoreIndex.from_documents(documents)

# 设置服务上下文
service_context = ServiceContext.from_defaults(chunk_size=512)

# 使用简单摘要模式进行查询
query_engine = index.as_query_engine(
    response_mode="simple_summarize",
    service_context=service_context
)
response: Response = query_engine.query("你的问题")
print(response)
5. 无文本模式(no_text)

概念解释
无文本模式仅运行检索器以获取将发送给 LLM 的节点,而不实际发送它们。可以通过检查 response.source_nodes 来检查这些节点。

无文本模式的主要用途是允许用户检查检索到的节点,而不必实际消耗LLM的计算资源或生成响应。这对于调试、验证检索逻辑或仅需要检索到的节点信息而不需要生成完整响应的场景非常有用。

编程示例

from llama_index import VectorStoreIndex, ServiceContext
from llama_index.response.schema import Response

# 创建索引
index = VectorStoreIndex.from_documents(documents)

# 设置服务上下文
service_context = ServiceContext.from_defaults(chunk_size=512)

# 使用无文本模式进行查询
query_engine = index.as_query_engine(
    response_mode="no_text",
    service_context=service_context
)
response: Response = query_engine.query("你的问题")
print(response.source_nodes)
6. 累积模式(accumulate)

概念解释
累积模式给定一组文本块和查询,将查询应用于每个文本块,同时将响应累积到一个数组中。返回所有响应的连接字符串。适用于需要对每个文本块单独运行相同查询的场景。

编程示例

from llama_index import VectorStoreIndex, ServiceContext
from llama_index.response.schema import Response

# 创建索引
index = VectorStoreIndex.from_documents(documents)

# 设置服务上下文
service_context = ServiceContext.from_defaults(chunk_size=512)

# 使用累积模式进行查询
query_engine = index.as_query_engine(
    response_mode="accumulate",
    service_context=service_context
)
response: Response = query_engine.query("你的问题")
print(response)
7. 紧凑累积模式(compact_accumulate)

概念解释
紧凑累积模式与累积模式相同,但会对每个 LLM 提示进行“紧凑”处理,类似于紧凑模式,并对每个文本块运行相同查询。

编程示例

from llama_index import VectorStoreIndex, ServiceContext
from llama_index.response.schema import Response

# 创建索引
index = VectorStoreIndex.from_documents(documents)

# 设置服务上下文
service_context = ServiceContext.from_defaults(chunk_size=512)

# 使用紧凑累积模式进行查询
query_engine = index.as_query_engine(
    response_mode="compact_accumulate",
    service_context=service_context
)
response: Response = query_engine.query("你的问题")
print(response)

通过这些详细的解释和编程示例,希望你能更好地理解和使用 LlamaIndex 中的响应模式。每种模式都有其独特的处理方式和适用场景,选择合适的模式可以提高查询效率和结果质量。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

需要重新演唱

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

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

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

打赏作者

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

抵扣说明:

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

余额充值