title: 记录检索器追踪
sidebarTitle: 记录检索器追踪
即使没有按规范格式记录检索器追踪数据,系统也不会报错,信息仍会被写入。但界面上将无法以检索步骤的专属方式展示这些数据。
许多 LLM 应用需要从向量数据库、知识图谱或其它索引中查找文档。检索器追踪用于记录检索器返回的文档。LangSmith 会对追踪中的检索步骤提供专门的呈现方式,帮助你更易理解并排查检索问题。要让检索步骤正确渲染,需要完成以下几步:
-
为检索步骤添加
run_type="retriever" 注解。
-
检索步骤需返回由 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")
下图展示了追踪中检索步骤的呈现方式,每篇文档会同时显示内容与元数据。