트레이스에 메타데이터와 태그 추가하기
트레이스에 메타데이터와 태그 추가하기
LangSmith는 트레이스와 함께 임의의 메타데이터와 태그를 보낼 수 있게 지원해요. 태그는 트레이스를 분류하거나 라벨링하는 데 쓰는 문자열이고, 메타데이터는 트레이스에 대한 추가 정보를 담는 키-값 쌍의 딕셔너리랍니다. 실행 환경, 실행한 사용자, 내부 상관관계 ID 같은 정보를 트레이스에 연결할 때 둘 다 유용해요.
출처: 문서
본문
LangSmith는 트레이스와 함께 임의의 메타데이터와 태그를 보낼 수 있게 지원합니다.
태그는 트레이스를 분류하거나 라벨링하는 데 사용할 수 있는 문자열입니다. 메타데이터는 트레이스에 대한 추가 정보를 저장하는 데 사용할 수 있는 키-값 쌍의 딕셔너리입니다.
둘 다 실행된 환경, 시작한 사용자, 내부 상관관계 ID와 같은 추가 정보를 트레이스에 연결하는 데 유용합니다. 태그와 메타데이터에 대한 자세한 내용은 Concepts 페이지를 참고하세요. 메타데이터와 태그로 트레이스와 런을 쿼리하는 방법은 트레이스 필터링 페이지를 참고하세요.
import openai
import langsmith as ls
from langsmith.wrappers import wrap_openai
client = openai.Client()
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"}
]
# You can set metadata & tags **statically** when decorating a function
# Use the @traceable decorator with tags and metadata
# Ensure that the LANGSMITH_TRACING environment variables are set for @traceable to work
@ls.traceable(
run_type="llm",
name="OpenAI Call Decorator",
tags=["my-tag"],
metadata={"my-key": "my-value"}
)
def call_openai(
messages: list[dict], model: str = "gpt-5.4-mini"
) -> str:
# You can also dynamically set metadata on the parent run:
rt = ls.get_current_run_tree()
rt.metadata["some-conditional-key"] = "some-val"
rt.tags.extend(["another-tag"])
return client.chat.completions.create(
model=model,
messages=messages,
).choices[0].message.content
call_openai(
messages,
# To add at **invocation time**, when calling the function.
# via the langsmith_extra parameter
langsmith_extra={"tags": ["my-other-tag"], "metadata": {"my-other-key": "my-value"}}
)
# or you can dynamically set default metadata for runs in the given scope
# tracing_context doesn't create a span itself, but it does initialize the
# context for child spans that are created.
with ls.tracing_context(metadata={"default-key": "default-value"}):
call_openai(messages)
# Alternatively, you can use the trace context manager
# This creates a new span with the given metadata and tags
with ls.trace(
name="OpenAI Call Trace",
run_type="llm",
inputs={"messages": messages},
tags=["my-tag"],
metadata={"my-key": "my-value"},
) as rt:
chat_completion = client.chat.completions.create(
model="gpt-5.4-mini",
messages=messages,
)
rt.metadata["some-conditional-key"] = "some-val"
rt.end(outputs={"output": chat_completion})
# You can use the same techniques with the wrapped client
patched_client = wrap_openai(
client, tracing_extra={"metadata": {"my-key": "my-value"}, "tags": ["a-tag"]}
)
chat_completion = patched_client.chat.completions.create(
model="gpt-5.4-mini",
messages=messages,
langsmith_extra={
"tags": ["my-other-tag"],
"metadata": {"my-other-key": "my-value"},
},
)
import OpenAI from "openai";
import { traceable, getCurrentRunTree } from "langsmith/traceable";
import { wrapOpenAI } from "langsmith/wrappers";
const client = wrapOpenAI(new OpenAI());
const messages: OpenAI.Chat.ChatCompletionMessageParam[] = [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "Hello!" },
];
const traceableCallOpenAI = traceable(
async (messages: OpenAI.Chat.ChatCompletionMessageParam[]) => {
const completion = await client.chat.completions.create({
model: "gpt-5.4-mini",
messages,
});
const runTree = getCurrentRunTree();
runTree.extra.metadata = {
...runTree.extra.metadata,
someKey: "someValue",
};
runTree.tags = [...(runTree.tags ?? []), "runtime-tag"];
return completion.choices[0].message.content;
},
{
run_type: "llm",
name: "OpenAI Call Traceable",
tags: ["my-tag"],
metadata: { "my-key": "my-value" },
}
);
// Call the traceable function
await traceableCallOpenAI(messages);
팁: LangSmith Deployments: Agent Server 배포에서 호출별로 메타데이터를 동적으로 추가하려면 factory function에서
tracing_context를 사용하는 것을 권장합니다. 예시는 배포된 에이전트에서 트레이싱 커스터마이즈하기를 참고하세요.
더 알아보기
- 태그와 메타데이터의 개념은 Concepts 페이지를 참고하세요.
- 메타데이터와 태그로 트레이스를 필터링하는 방법은 Filter traces 페이지를 확인해 보세요.