调试管道
了解如何调试和解决 Haystack 管道的问题。
您有几种调试管道的可用选项
检查组件输出
要查看特定管道组件的输出,请在执行管道时添加include_outputs_from 参数。将其放在输入字典之后,并将其设置为您希望包含在结果中的组件的名称。
例如,以下是如何打印以下组件的输出:PromptBuilder 在此管道中
from haystack import Pipeline, Document
from haystack.utils import Secret
from haystack.components.generators.chat import OpenAIChatGenerator
from haystack.components.builders.chat_prompt_builder import ChatPromptBuilder
from haystack.dataclasses import ChatMessage
# Documents
documents = [Document(content="Joe lives in Berlin"), Document(content="Joe is a software engineer")]
# Define prompt template
prompt_template = [
ChatMessage.from_system("You are a helpful assistant."),
ChatMessage.from_user(
"Given these documents, answer the question.\nDocuments:\n"
"{% for doc in documents %}{{ doc.content }}{% endfor %}\n"
"Question: {{query}}\nAnswer:"
)
]
# Define pipeline
p = Pipeline()
p.add_component(instance=ChatPromptBuilder(template=prompt_template, required_variables={"query", "documents"}), name="prompt_builder")
p.add_component(instance=OpenAIChatGenerator(api_key=Secret.from_env_var("OPENAI_API_KEY")), name="llm")
p.connect("prompt_builder", "llm.messages")
# Define question
question = "Where does Joe live?"
# Execute pipeline
result = p.run({"prompt_builder": {"documents": documents, "query": question}},
include_outputs_from="prompt_builder")
# Print result
print(result)
日志
根据您的调试需求调整日志格式。有关详细信息,请参阅我们的 日志记录 文档。
实时管道日志记录
使用 Haystack 的 LoggingTracer 日志记录,以实时检查通过管道的数据。
此功能在实验和原型制作期间特别有用,因为您无需预先设置任何追踪后端。
以下是启用此追踪器的方法。在此示例中,我们添加了颜色标签(可选),以突出显示组件的名称和输入
import logging
from haystack import tracing
from haystack.tracing.logging_tracer import LoggingTracer
logging.basicConfig(format="%(levelname)s - %(name)s - %(message)s", level=logging.WARNING)
logging.getLogger("haystack").setLevel(logging.DEBUG)
tracing.tracer.is_content_tracing_enabled = True # to enable tracing/logging content (inputs/outputs)
tracing.enable_tracing(LoggingTracer(tags_color_strings={"haystack.component.input": "\x1b[1;31m", "haystack.component.name": "\x1b[1;34m"}))
以下是运行管道时生成的日志的样子
跟踪
为了获得管道性能的更大图景,请尝试使用 Langfuse 进行追踪。
我们的 追踪 页面提供了更多关于 Haystack 其他追踪解决方案的信息。
监控工具
查看 Haystack 管道可用的追踪和监控 集成,例如 Arize AI 或 Arize Phoenix。
更新于 6 个月前
