跳到主内容

记录检索器跟踪

注意

即使您未以正确的格式记录检索器跟踪,系统也不会中断,数据仍将被记录。但是,数据将无法以检索器步骤特有的方式呈现。

许多 LLM 应用需要从向量数据库、知识图谱或其他类型的索引中查找文档。检索器跟踪是一种记录检索器所检索文档的方式。LangSmith 为跟踪中的检索步骤提供了特殊的渲染方式,以便更容易理解和诊断检索问题。为了使检索步骤正确渲染,需要采取以下几个小步骤。

  1. 使用 run_type="retriever" 注解检索器步骤。
  2. 从检索器步骤返回一个 Python 字典列表或 TypeScript 对象列表。每个字典应包含以下键:
    • page_content:文档的文本内容。
    • type:这应始终为“Document”。
    • metadata:一个 Python 字典或 TypeScript 对象,包含有关文档的元数据。此元数据将显示在跟踪中。

以下代码片段展示了如何在 Python 和 TypeScript 中记录检索步骤。

from langsmith import traceable

def _convert_docs(results):
return [
{
"page_content": r,
"type": "Document",
"metadata": {"foo": "bar"}
}
for r in results
]

@traceable(run_type="retriever")
def retrieve_docs(query):
# Foo retriever returning hardcoded dummy documents.
# In production, this could be a real vector datatabase or other document index.
contents = ["Document contents 1", "Document contents 2", "Document contents 3"]
return _convert_docs(contents)

retrieve_docs("User query")

下图展示了检索器步骤在跟踪中的渲染方式。每个文档的内容及其元数据都将显示出来。


此页面有帮助吗?


您可以在 GitHub 上留下详细反馈 在 GitHub 上.