Weave의 속성을 사용하면 트레이스와 평가에 사용자 정의 메타데이터를 추가로 첨부할 수 있습니다. 이 메타데이터에는 환경 이름, 모델 버전, 실험 ID, 사용자 ID 또는 Weave 데이터를 구성·필터링·분석하는 데 도움이 되는 기타 컨텍스트 정보가 포함될 수 있습니다.
Weave에서는 속성을 추가하는 두 가지 방법을 제공합니다:
- Per-call attributes(호출 단위 속성):
weave.attributes()를 사용하여 특정 연산 또는 코드 블록에 메타데이터를 추가합니다.
- Global attributes(전역 속성): 초기화 시
global_attributes 필드를 사용해 프로젝트의 모든 트레이스와 평가에 적용되는 속성을 설정합니다.
트레이스와 평가 중에 로깅된 모든 속성은 UI에서 확인할 수 있습니다. 그런 다음 이 속성을 사용해 데이터를 필터링하고 그룹화할 수 있습니다.
call.attributes는 호출이 시작된 이후에는 수정할 수 없습니다. op를 호출하기 전에 이 컨텍스트 매니저를 사용해 필요한 메타데이터를 설정하세요.
weave.attributes() 컨텍스트 매니저를 사용하면 특정 트레이스된 연산에 메타데이터를 추가할 수 있습니다. 이를 통해 특정 함수 호출이나 평가 run에 컨텍스트 정보를 태그로 달 수 있습니다.
import weave
weave.init("attribute-check")
@weave.op
def my_function(name: str):
return f"Hello, {name}!"
# 특정 호출에 속성 추가
with weave.attributes({'env': 'production', 'user_id': '12345'}):
result = my_function("World")
import {init, op, withAttributes} from 'weave';
async function main() {
await init('your-team/attribute-example');
const myFunction = op(async function myFunction(name: string) {
return `Hello, ${name}!`;
});
// 특정 호출에 속성 추가
const result = await withAttributes(
{env: 'production', user_id: '12345'},
async () => myFunction('World')
);
console.log('Result:', result);
}
main().catch(console.error);
이 함수는 컨텍스트 매니저 블록(Python) 또는 콜백 함수(TypeScript) 내에서 트레이스되는 모든 연산에 이 속성을 추가합니다.
또한 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") # 세 가지 속성을 모두 가짐
# 내부 컨텍스트가 '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' 속성을 가짐
import {init, op, withAttributes} from 'weave';
async function main() {
await init('your-team/attribute-example');
const processData = op(async function processData(data: string) {
return data.toUpperCase();
});
// 외부 컨텍스트
await withAttributes(
{
env: 'production',
version: '1.0.0',
region: 'us-west-2'
},
async () => {
await processData('hello'); // 세 가지 속성을 모두 가짐
// 내부 컨텍스트가 'version'을 덮어씀
await withAttributes(
{
version: '1.1.0',
experiment: 'exp-456'
},
async () => {
await processData('world'); // env='production', version='1.1.0', region='us-west-2', experiment='exp-456' 속성을 가짐
}
);
}
);
}
main().catch(console.error);
Weave를 초기화할 때 전역 속성을 설정하면, 해당 속성은 프로젝트의 모든 트레이스와 평가에 자동으로 적용됩니다. 이는 환경, 배포 버전, 팀 정보와 같은 프로젝트 전역 메타데이터를 전파하는 데 유용합니다.
import weave
weave.init(
"my-project",
global_attributes={
"env": "production",
"app_version": "2.1.0",
"region": "us-west-2",
"team": "ml-platform"
}
)
# 이제 global_attributes 사전이 이후의 모든 작업에 이 속성들을 적용합니다
@weave.op
def my_function():
return "Hello"
my_function() # 전역 속성이 자동으로 모두 포함됩니다
# 평가에도 전역 속성이 적용됩니다
evaluation = weave.Evaluation(dataset=examples, scorers=[scorer])
asyncio.run(evaluation.evaluate(model)) # 모든 전역 속성을 포함합니다
import {init, op, withAttributes} from 'weave';
async function main() {
await init('your-team/attribute-example', {
globalAttributes: {
env: 'production',
app_version: '2.1.0',
region: 'us-west-2',
team: 'ml-platform'
}
});
// 이제 globalAttributes 객체가 이후의 모든 작업에 이 속성들을 적용합니다
const myFunction = op(async function myFunction() {
return 'Hello';
});
const result = await myFunction(); // 전역 속성이 자동으로 모두 포함됩니다
console.log('Result:', result);
}
main().catch(console.error);
전역 속성과 호출별 속성을 함께 사용할 수 있습니다. 이때 같은 키를 가진 호출별 속성은 전역 속성보다 우선 적용됩니다.
import weave
# 전역 속성 설정
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")
import {init, op, withAttributes} from 'weave';
async function main() {
// 전역 속성 설정
await init('your-team/attribute-example', {
globalAttributes: {
env: 'production',
app_version: '2.1.0'
}
});
const process = op(async function process(data: string) {
return data;
});
// 이 호출에는 env='production', app_version='2.1.0'이 설정됩니다.
await process('test1');
// 이 호출에는 env='staging', app_version='2.1.0', experiment='A'가 설정됩니다.
await withAttributes(
{env: 'staging', experiment: 'A'},
async () => process('test2')
);
}
main().catch(console.error);
호출에 로깅된 현재 속성 집합을 반환합니다. 이는 호출을 디버깅하거나 조건부 로직에 사용할 컨텍스트를 확인하는 데 도움이 됩니다.
다음 예시는 Weave 데코레이터를 사용해 process_data 함수를 로깅하고, 로깅할 속성을 구성한 뒤, 실행 시 해당 속성을 반환합니다.import weave
weave.init("your-team/your-project")
@weave.op
def process_data(data: str):
# op 내부에서 현재 호출 가져오기
call = weave.get_current_call()
if call:
print(f"Attributes: {call.attributes}")
return data.upper()
# 속성을 설정하고 함수를 실행
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을 반환합니다.
Weave TypeScript SDK를 사용하면 client.getCurrentAttributes()를 통해 현재 속성을 가져올 수 있습니다. Weave Python SDK와 달리 TypeScript에서는 op 내부뿐만 아니라 withAttributes() 컨텍스트 내라면 어디에서든 속성에 접근할 수 있습니다.import * as weave from 'weave';
import { withAttributes } from 'weave';
async function main() {
const client = await weave.init('your-team/your-project');
const processData = weave.op(async function processData(data: string) {
// op 내부에서도 속성에 접근할 수 있습니다
const attrs = client.getCurrentAttributes();
console.log('Attributes inside op:', attrs);
return data.toUpperCase();
});
# 속성을 설정하고 함수를 실행
await withAttributes(
{
env: 'production',
version: '1.0.0',
region: 'us-west-2'
},
async () => {
# 컨텍스트 내 어디에서든 속성 가져오기
console.log('Current attributes:', client.getCurrentAttributes());
await processData('hello');
await withAttributes(
{
version: '1.1.0',
experiment: 'exp-456'
},
async () => {
console.log('Nested attributes:', client.getCurrentAttributes());
await processData('world');
}
);
}
);
}
main().catch(console.error);
출력은 다음과 같습니다:Current attributes: { env: 'production', version: '1.0.0', region: 'us-west-2' }
Attributes inside op: { env: 'production', version: '1.0.0', region: 'us-west-2' }
Nested attributes: { env: 'production', version: '1.1.0', region: 'us-west-2', experiment: 'exp-456' }
Attributes inside op: { env: 'production', version: '1.1.0', region: 'us-west-2', experiment: 'exp-456' }