Retriever 트레이스 기록하기
Retriever 트레이스 기록하기
RAG 파이프라인의 문서 수준 가시성을 위해 LangSmith 트레이스에 검색(retrieval) 단계를 기록하는 방법을 알려드릴게요.
많은 LLM 애플리케이션은 검색 증강 생성(RAG) 파이프라인의 일부로 벡터 데이터베이스, 지식 그래프, 또는 다른 인덱스에서 문서를 검색합니다. LangSmith는 retriever 단계에 대한 전용 렌더링을 제공하며, 이를 통해 검색된 문서를 더 쉽게 검사하고 검색 문제를 진단할 수 있습니다.
참고: 이 단계들은 선택 사항입니다. 건너뛰어도 retriever 데이터는 여전히 기록되지만, LangSmith는 retriever 특정 형식으로 렌더링하지 않습니다.
출처: 문서
본문
retriever 특정 렌더링을 활성화하려면 다음 두 단계를 완료하세요.
run_type을 retriever로 설정
traceable 데코레이터(Python) 또는 traceable 래퍼(TypeScript)에 run_type="retriever"를 전달합니다. 이는 LangSmith에게 단계를 검색 실행으로 취급하고 LangSmith UI에서 retriever 특정 렌더링을 적용하라고 알려줍니다:
from langsmith import traceable
@traceable(run_type="retriever")
def retrieve_docs(query):
...
RunTree API를 traceable 대신 사용한다면 RunTree 객체를 만들 때 run_type="retriever"를 전달하세요.
기대 형식으로 문서 반환
retriever 함수에서 dict 목록(Python) 또는 객체 목록(TypeScript)을 반환합니다. 목록의 각 항목은 검색된 문서를 나타내며 다음 필드를 포함해야 합니다:
| Field | Type | Description |
|---|---|---|
page_content |
string | The text content of the retrieved document. |
type |
string | Must always be "Document". |
metadata |
object | Key-value pairs with metadata about the document, such as source URL, chunk ID, or score. This metadata is displayed alongside the document in the trace. |
다음 예제들은 두 요구사항 모두 적용된 완전한 retriever 구현을 보여줍니다:
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):
# Returning hardcoded placeholder documents.
# In production, replace with a real vector database or document index.
contents = ["Document contents 1", "Document contents 2", "Document contents 3"]
return _convert_docs(contents)
retrieve_docs("User query")
import { traceable } from "langsmith/traceable";
interface Document {
page_content: string;
type: string;
metadata: { foo: string };
}
function convertDocs(results: string[]): Document[] {
return results.map((r) => ({
page_content: r,
type: "Document",
metadata: { foo: "bar" }
}));
}
const retrieveDocs = traceable((query: string): Document[] => {
// Returning hardcoded placeholder documents.
// In production, replace with a real vector database or document index.
const contents = ["Document contents 1", "Document contents 2", "Document contents 3"];
return convertDocs(contents);
}, {
name: "retrieveDocs",
run_type: "retriever"
});
await retrieveDocs("User query");
LangSmith UI에서 각 검색 문서와 그 내용·메타데이터를 찾을 수 있습니다.
관련 자료
- 트레이싱용 코드에 주석 달기:
traceable,RunTree, REST API를 포함한 모든 트레이싱 방법 개요. - LLM 호출 기록하기: LLM 단계에 대한 유사한 커스텀 로깅 요구사항.
더 알아보기
- 트레이싱용 코드에 주석 달기 — 트레이싱 방법 개요.
- LLM 호출 기록하기 — LLM 단계 로깅.