问题验证
- 我在文档和Discord上寻找答案。
问题
我按照这个 article 进行实现,但是当我使用query()时,日志只显示 "=== LLM Response ===",而 response.source_nodes
都为空。我想了解出了什么问题,以及如何打印出所有步骤的 source_nodes
和 chunk_text
。
主代码:
为每个文档构建文档代理
from llama_index.agent.openai import OpenAIAgent
from llama_index.core import load_index_from_storage, StorageContext
from llama_index.core.node_parser import SentenceSplitter
import os
node_parser = SentenceSplitter()
# Build agents dictionary
agents = {}
query_engines = {}
# this is for the baseline
all_nodes = []
for idx, wiki_title in enumerate(wiki_titles):
nodes = node_parser.get_nodes_from_documents(city_docs[wiki_title])
all_nodes.extend(nodes)
if not os.path.exists(f"./data/{wiki_title}"):
# build vector index
vector_index = VectorStoreIndex(nodes)
vector_index.storage_context.persist(
persist_dir=f"./data/{wiki_title}"
)
else:
vector_index = load_index_from_storage(
StorageContext.from_defaults(persist_dir=f"./data/{wiki_title}"),
)
# build summary index
summary_index = SummaryIndex(nodes)
# define query engines
vector_query_engine = vector_index.as_query_engine(llm=Settings.llm)
summary_query_engine = summary_index.as_query_engine(llm=Settings.llm)
# define tools
query_engine_tools = [
QueryEngineTool(
query_engine=vector_query_engine,
metadata=ToolMetadata(
name="vector_tool",
description=(
"Useful for questions related to specific aspects of"
f" {wiki_title} (e.g. the history, arts and culture,"
" sports, demographics, or more)."
),
),
),
QueryEngineTool(
query_engine=summary_query_engine,
metadata=ToolMetadata(
name="summary_tool",
description=(
"Useful for any requests that require a holistic summary"
f" of EVERYTHING about {wiki_title}. For questions about"
" more specific sections, please use the vector_tool."
),
),
),
]
# build agent
function_llm = OpenAI(model="gpt-4")
agent = OpenAIAgent.from_tools(
query_engine_tools,
llm=function_llm,
verbose=True,
system_prompt=f"""\
You are a specialized agent designed to answer queries about {wiki_title}.
You must ALWAYS use at least one of the tools provided when answering a question; do NOT rely on prior knowledge.\
""",
)
agents[wiki_title] = agent
query_engines[wiki_title] = vector_index.as_query_engine(
similarity_top_k=2
)
为每个文档构建启用检索器的OpenAI代理
# define tool for each document agent
all_tools = []
for wiki_title in wiki_titles:
wiki_summary = (
f"This content contains Wikipedia articles about {wiki_title}. Use"
f" this tool if you want to answer any questions about {wiki_title}.\n"
)
doc_tool = QueryEngineTool(
query_engine=agents[wiki_title],
metadata=ToolMetadata(
name=f"tool_{wiki_title}",
description=wiki_summary,
),
)
all_tools.append(doc_tool)
# define an "object" index and retriever over these tools
from llama_index.core import VectorStoreIndex
from llama_index.core.objects import ObjectIndex
obj_index = ObjectIndex.from_objects(
all_tools,
index_cls=VectorStoreIndex,
)
from llama_index.agent.openai import OpenAIAgent
top_agent = OpenAIAgent.from_tools(
tool_retriever=obj_index.as_retriever(similarity_top_k=3),
system_prompt=""" \
You are an agent designed to answer queries about a set of given cities.
Please always use the tools provided to answer a question. Do not rely on prior knowledge.\
""",
verbose=True,
)
response = top_agent.query(
"Give me a summary of all the positive aspects of Houston"
)
5条答案
按热度按时间3yhwsihp1#
要从多文档代理中获取
source_node
并为所有步骤打印出source_nodes
和chunk_text
,您可以使用llama_index.legacy.response.pprint_utils
模块中提供的pprint_response
函数。该函数已经包含了打印source_nodes
及其内容的逻辑。以下是如何使用此函数的示例:
这将为所有步骤打印出
source_nodes
及其chunk_text
。show_source=True
参数确保显示源节点。pprint_response
函数内部调用pprint_source_node
以打印每个source_node
及其内容。以下是相关代码片段:此函数将遍历
response
中的所有source_nodes
并打印它们的详细信息,包括chunk_text
1(https://github.com/run-llama/llama_index/blob/main/llama-index-core/llama_index/core/response/pprint_utils.py)2(https://github.com/run-llama/llama_index/blob/main/llama-index-legacy/llama_index/legacy/response/pprint_utils.py)。详细信息
关于 Dosu
此响应旨在提供帮助并节省您的时间。它不是精确解决方案,而是您自己研究的起点。
请帮助我变得更有用!如果这有帮助,请留下一个 👍,如果无关紧要,请留下一个 👎。
bsxbgnwa2#
你好,@dosu ,为什么我的
response.source_nodes
是空的?pxq42qpu3#
要解决
response.source_nodes
为空的问题,您可以修改RetrySourceQueryEngine
类中的_query
方法,添加记录语句以打印出source_nodes
及其chunk_text
。以下是修改后的代码:此外,您还可以使用
pprint_utils.py
模块中的pprint_response
方法来确保响应中包含可读的源节点信息。此方法将响应格式化为包含每个源节点的元数据(如node_id
和score
)。以下是相关代码的一部分:通过引入这些更改,您可以确保
source_nodes
及其chunk_text
被记录并包含在响应中,从而帮助您调试和解决response.source_nodes
为空的问题。l7mqbcuq4#
你好@dosu,
我尝试了你提到的
RetrySourceQueryEngine
方法,但是得到了ValueError: No source nodes passed evaluation.
。为什么呢?这是我的代码:
lokaqttq5#
ValueError: No source nodes passed evaluation.
错误发生在没有一个源节点满足评估器定义的标准时。要解决这个问题,你需要确保至少有一个源节点通过评估。以下是一些步骤,可以帮助你调试并解决这个问题:RelevancyEvaluator
中的评估标准不太严格,导致所有源节点都失败。以下是如何添加日志以调试问题的例子:
通过添加这些调试语句,你可以获得更多关于为什么源节点在评估中失败的见解,并相应地调整你的评估器或源节点。
此外,确保你的
RelevancyEvaluator
已正确配置。RelevancyEvaluator
使用模板来确定响应是否与上下文信息一致,如果一致则回答“YES”,否则回答“NO”。如果认为响应相关(“YES”),则认为结果通过。通过确保你的评估标准适当并为调试添加日志,你可以更好地理解和解决没有源节点通过评估的问题 1(https://github.com/run-llama/llama_index/blob/main/llama-index-core/llama_index/core/evaluation/relevancy.py)。
详细信息
关于Dosu
这个响应旨在帮助你节省时间并提高效率。它不是精确的解决方案,而是你自己研究的一个起点。
请帮助我变得更有用!如果你觉得有帮助,请给个👍;如果不相关,请给个👎。