추적된 함수 내에서 현재 런(span)에 접근하기

추적된 함수 내에서 현재 런(span)에 접근하기

가끔은 추적된 함수 안에서 현재 런(span)에 접근하고 싶을 때가 있어요. UUID나 태그, 기타 정보를 현재 런에서 뽑아내야 할 때 유용하답니다. Python/TypeScript SDK에서 get_current_run_tree/getCurrentRunTree 함수를 호출하면 간단하게 접근할 수 있어요.

출처: 문서

본문

경우에 따라 추적된 함수 내에서 현재 런(span)에 접근하고 싶을 때가 있습니다. 이는 현재 런에서 UUID, 태그 또는 기타 정보를 추출하는 데 유용할 수 있습니다.

현재 런은 Python 또는 TypeScript SDK에서 각각 get_current_run_tree/getCurrentRunTree 함수를 호출해 접근할 수 있습니다.

RunTree 객체에서 사용 가능한 전체 속성 목록은 이 레퍼런스를 참고하세요.

from langsmith import traceable
from langsmith.run_helpers import get_current_run_tree
from openai import Client

    openai = Client()

    @traceable
    def format_prompt(subject):
        run = get_current_run_tree()
        print(f"format_prompt Run Id: {run.id}")
        print(f"format_prompt Trace Id: {run.trace_id}")
        print(f"format_prompt Parent Run Id: {run.parent_run.id}")
        return [
            {
                "role": "system",
                "content": "You are a helpful assistant.",
            },
            {
                "role": "user",
                "content": f"What's a good name for a store that sells {subject}?"
            }
        ]

    @traceable(run_type="llm")
    def invoke_llm(messages):
        run = get_current_run_tree()
        print(f"invoke_llm Run Id: {run.id}")
        print(f"invoke_llm Trace Id: {run.trace_id}")
        print(f"invoke_llm Parent Run Id: {run.parent_run.id}")
        return openai.chat.completions.create(
            messages=messages, model="gpt-5.4-mini", temperature=0
        )

    @traceable
    def parse_output(response):
        run = get_current_run_tree()
        print(f"parse_output Run Id: {run.id}")
        print(f"parse_output Trace Id: {run.trace_id}")
        print(f"parse_output Parent Run Id: {run.parent_run.id}")
        return response.choices[0].message.content

    @traceable
    def run_pipeline():
        run = get_current_run_tree()
        print(f"run_pipeline Run Id: {run.id}")
        print(f"run_pipeline Trace Id: {run.trace_id}")
        messages = format_prompt("colorful socks")
        response = invoke_llm(messages)
        return parse_output(response)

run_pipeline()
import { traceable, getCurrentRunTree } from "langsmith/traceable";
import OpenAI from "openai";

    const openai = new OpenAI();

    const formatPrompt = traceable((subject: string) => {
        const run = getCurrentRunTree();
        console.log("formatPrompt Run ID", run.id)
        console.log("formatPrompt Trace ID", run.trace_id)
        console.log("formatPrompt Parent Run ID", run.parent_run.id)
        return [
            {
                role: "system" as const,
                content: "You are a helpful assistant.",
            },
            {
                role: "user" as const,
                content: `What's a good name for a store that sells ${subject}?`,
            },
        ];
    }, { name: "formatPrompt" });

    const invokeLLM = traceable(
        async (messages: { role: string; content: string }[]) => {
            const run = getCurrentRunTree();
            console.log("invokeLLM Run ID", run.id)
            console.log("invokeLLM Trace ID", run.trace_id)
            console.log("invokeLLM Parent Run ID", run.parent_run.id)
            return openai.chat.completions.create({
                model: "gpt-5.4-mini",
                messages: messages,
                temperature: 0,
            });
        },
        { run_type: "llm", name: "invokeLLM" }
    );

    const parseOutput = traceable(
        (response: any) => {
            const run = getCurrentRunTree();
            console.log("parseOutput Run ID", run.id)
            console.log("parseOutput Trace ID", run.trace_id)
            console.log("parseOutput Parent Run ID", run.parent_run.id)
            return response.choices[0].message.content;
        },
        { name: "parseOutput" }
    );

    const runPipeline = traceable(
        async () => {
            const run = getCurrentRunTree();
            console.log("runPipline Run ID", run.id)
            console.log("runPipline Trace ID", run.trace_id)
            console.log("runPipline Parent Run ID", run.parent_run?.id)
            const messages = await formatPrompt("colorful socks");
            const response = await invokeLLM(messages);
            return parseOutput(response);
        },
        { name: "runPipeline" }
    );

await runPipeline();

더 알아보기