메인 콘텐츠로 건너뛰기
Weave 의 Attributes를 사용하면 traces와 evaluations에 커스텀 메타데이터를 추가할 수 있습니다. 이 메타데이터에는 환경 이름, 모델 버전, 실험 ID, 사용자 ID 또는 기타 컨텍스트 정보가 포함될 수 있으며, 이를 통해 Weave 데이터를 구성, 필터링 및 분석하는 데 도움을 줍니다. Weave는 attributes를 추가하는 두 가지 방법을 제공합니다:
  • Per-call attributes: weave.attributes()를 사용하여 특정 연산이나 코드 블록에 메타데이터를 추가합니다.
  • Global attributes: 초기화 시 global_attributes 필드를 사용하여 프로젝트의 모든 traces와 evaluations에 적용되는 attributes를 설정합니다.
UI에서 traces 및 evaluation 중에 로그된 모든 attributes를 확인할 수 있습니다. 이후 이를 사용하여 데이터를 필터링하고 그룹화할 수 있습니다.
call.attributes는 호출이 시작된 후에는 수정할 수 없습니다. op를 호출하기 전에 이 컨텍스트 매니저를 사용하여 메타데이터를 설정하세요.

Per-call attributes

weave.attributes() 컨텍스트 매니저를 사용하면 특정 추적 연산에 메타데이터를 추가할 수 있습니다. 이를 통해 특정 함수 호출이나 evaluation run에 컨텍스트 정보를 태깅할 수 있습니다.
import weave

weave.init("attribute-check")

@weave.op
def my_function(name: str):
    return f"Hello, {name}!"

# 특정 호출에 attributes 추가
with weave.attributes({'env': 'production', 'user_id': '12345'}):
    result = my_function("World")
이 함수는 컨텍스트 매니저 블록(Python) 또는 콜백 함수(TypeScript) 내의 모든 추적 연산에 attributes를 첨부합니다. weave.attributes() 컨텍스트를 중첩해서 사용할 수도 있습니다. 내부 컨텍스트는 동일한 키에 대해 외부 컨텍스트를 덮어씁니다:
@weave.op
def process_data(data: str):
    return data.upper()

# 외부 컨텍스트
with weave.attributes({
    "env": "production",
    "version": "1.0.0",
    "region": "us-west-2"
}):
    process_data("hello")  # 세 가지 attributes를 모두 가짐
    
    # 내부 컨텍스트가 'version'을 덮어씀
    with weave.attributes({
        "version": "1.1.0",
        "experiment": "exp-456"
    }):
        process_data("world")  # env='production', version='1.1.0', region='us-west-2', experiment='exp-456'을 가짐

Global attributes

Weave 초기화 중에 global attributes를 설정하면, 프로젝트의 모든 traces와 evaluations에 자동으로 적용됩니다. 이는 환경, 배포 버전 또는 팀 정보와 같이 프로젝트 전반에 걸친 메타데이터를 전파하는 데 유용합니다.
import weave

weave.init(
    "my-project",
    global_attributes={
        "env": "production",
        "app_version": "2.1.0",
        "region": "us-west-2",
        "team": "ml-platform"
    }
)

# global_attributes 사전은 이제 이후의 모든 연산에 이러한 attributes를 적용합니다.
@weave.op
def my_function():
    return "Hello"

my_function()  # 자동으로 모든 global attributes를 가짐

# Evaluations에도 global attributes가 적용됩니다.
evaluation = weave.Evaluation(dataset=examples, scorers=[scorer])
asyncio.run(evaluation.evaluate(model))  # 모든 global attributes를 가짐

Global 및 per-call attributes 조합하기

Global attributes와 per-call attributes를 함께 사용할 수 있습니다. 동일한 키를 가진 per-call attributes는 global attributes를 덮어씁니다:
import weave

# Global attributes 설정
weave.init(
    "my-project",
    global_attributes={
        "env": "production",
        "app_version": "2.1.0"
    }
)

@weave.op
def process(data: str):
    return data

# 이 호출은 다음을 가집니다: env='production', app_version='2.1.0'
process("test1")

# 이 호출은 다음을 가집니다: env='staging', app_version='2.1.0', experiment='A'
with weave.attributes({'env': 'staging', 'experiment': 'A'}):
    process("test2")

실행 중에 attributes 가져오기

현재 call에 기록된 attributes 세트를 반환합니다. 이는 call을 디버깅하거나 조건부 로직을 위한 컨텍스트를 확인하는 데 도움이 될 수 있습니다.
다음 예제는 process_data 함수를 로그하도록 Weave 데코레이터를 설정하고, 로그할 attributes를 구성한 다음, 실행 시 이를 반환합니다.
import weave

weave.init("your-team/your-project")

@weave.op
def process_data(data: str):
    # op 내부에서 현재 call을 가져옵니다.
    call = weave.get_current_call()
    if call:
        print(f"Attributes: {call.attributes}")
    return data.upper()

# attributes를 설정하고 함수를 실행합니다.
with weave.attributes({
    "env": "production",
    "version": "1.0.0",
    "region": "us-west-2"
}):
    process_data("hello")
    
    with weave.attributes({
        "version": "1.1.0",
        "experiment": "exp-456"
    }):
        process_data("world")
출력 결과:
Attributes: {'env': 'production', 'version': '1.0.0', 'region': 'us-west-2'}
Attributes: {'env': 'production', 'version': '1.1.0', 'region': 'us-west-2', 'experiment': 'exp-456'}
weave.get_current_call()@weave.op 데코레이터가 적용된 함수 내부에서만 작동합니다. op 외부에서는 None을 반환합니다.