메인 콘텐츠로 건너뛰기
Track LLM inputs & outputs 튜토리얼에서는 LLM의 입력과 출력을 추적하는 기본 개념을 다루었습니다. 이 튜토리얼에서는 다음 내용을 다룹니다:
  • 애플리케이션을 통해 흐르는 데이터를 추적하는 방법
  • 호출 시점의 메타데이터를 추적하는 방법

중첩 함수 호출 추적하기

LLM 기반 애플리케이션에는 여러 차례의 LLM 호출과, 모니터링이 중요한 추가 데이터 처리 및 검증 로직이 포함될 수 있습니다. 많은 앱에서 흔한 깊은 중첩 호출 구조에서도, 추적하려는 모든 함수에 weave.op()을 추가하기만 하면 Weave가 중첩 함수 간의 부모-자식 관계를 계속 추적합니다. 퀵스타트 예제를 기반으로, 다음 코드는 LLM에서 반환된 항목 수를 세는 추가 로직을 더하고, 이를 상위 수준 함수로 모두 감쌉니다. 또한 이 예제는 weave.op()을 사용하여 모든 함수, 그 호출 순서, 그리고 부모-자식 관계를 추적합니다:
import weave
import json
from openai import OpenAI

client = OpenAI()

@weave.op()
def extract_dinos(sentence: str) -> dict:
    response = client.chat.completions.create(
        model="gpt-4o",
        messages=[
            {
                "role": "system",
                "content": """Extract any dinosaur `name`, their `common_name`, \
names and whether its `diet` is a herbivore or carnivore, in JSON format."""
            },
            {
                "role": "user",
                "content": sentence
            }
            ],
            response_format={ "type": "json_object" }
        )
    return response.choices[0].message.content

@weave.op()
def count_dinos(dino_data: dict) -> int:
    # 반환된 리스트의 항목 개수를 셉니다
    k = list(dino_data.keys())[0]
    return len(dino_data[k])

@weave.op()
def dino_tracker(sentence: str) -> dict:
    # LLM을 사용해 공룡을 추출합니다
    dino_data = extract_dinos(sentence)

    # 반환된 공룡의 수를 셉니다
    dino_data = json.loads(dino_data)
    n_dinos = count_dinos(dino_data)
    return {"n_dinosaurs": n_dinos, "dinosaurs": dino_data}

weave.init('jurassic-park')

sentence = """I watched as a Tyrannosaurus rex (T. rex) chased after a Triceratops (Trike), \
both carnivore and herbivore locked in an ancient dance. Meanwhile, a gentle giant \
Brachiosaurus (Brachi) calmly munched on treetops, blissfully unaware of the chaos below."""

result = dino_tracker(sentence)
print(result)
중첩 함수위 코드를 실행하면 두 개의 중첩 함수(extract_dinoscount_dinos)의 입력과 출력뿐 아니라 자동으로 로깅된 OpenAI 트레이스도 확인할 수 있습니다.중첩 Weave 트레이스

메타데이터 추적

weave.attributes 컨텍스트 매니저를 사용해, 호출 시점에 추적할 메타데이터를 담은 딕셔너리를 전달하면 메타데이터를 추적할 수 있습니다. 위의 예시를 이어서 사용해 보겠습니다:
import weave

weave.init('jurassic-park')

sentence = """I watched as a Tyrannosaurus rex (T. rex) chased after a Triceratops (Trike), \
both carnivore and herbivore locked in an ancient dance. Meanwhile, a gentle giant \
Brachiosaurus (Brachi) calmly munched on treetops, blissfully unaware of the chaos below."""

# 이전에 정의한 함수 실행과 함께 메타데이터를 추적합니다
with weave.attributes({'user_id': 'lukas', 'env': 'production'}):
    result = dino_tracker(sentence)
실행 시점에 사용자 ID, 코드의 환경 상태(개발, 스테이징, 프로덕션 등)와 같은 메타데이터를 추적할 것을 권장합니다.시스템 프롬프트와 같은 시스템 설정을 추적하려면 Weave Models 사용을 권장합니다.

다음 단계

  • App Versioning 튜토리얼을(를) 따라 임시 프롬프트, 모델, 애플리케이션 변경 사항을 기록하고 버전 관리하며 체계적으로 정리하세요.