Weave の属性機能を使用すると、トレースや評価にカスタムメタデータを付けられます。このメタデータには、環境名、モデルバージョン、実験 ID、ユーザー ID、その他のコンテキスト情報などを含めることができ、Weave データを整理、フィルタリング、および分析するのに役立ちます。
Weave では、属性を追加する方法が 2 つあります:
- 呼び出し単位の属性 (Per-call attributes):
weave.attributes() を使用して、特定のオペレーションやコードブロックにメタデータを追加します
- グローバル属性 (Global attributes):
global_attributes フィールドを使用して、プロジェクト内のすべてのトレースと評価に適用される属性を、初期化時に設定します
トレースや評価中にログに記録されたすべての属性は、UI で確認できます。その後、それらを使ってデータをフィルタリングおよびグループ化できます。
call.attributes は、呼び出しが開始された後は変更できません。
このコンテキストマネージャを使用して、オペレーションを呼び出す前にメタデータを設定してください。
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") # 3 つすべての属性が付与される
# 内側のコンテキストが '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'); // 3 つすべての属性が付与される
// 内側のコンテキストが '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);
現在の呼び出しに対してログされている属性のセットを返します。これにより、呼び出しのデバッグや条件付きロジック用のコンテキスト取得に役立ちます。
次の例では、process_data 関数をログするように Weave デコレータを設定し、ログする属性を設定したうえで、実行時にそれらを取得しています。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()
# 属性を設定して関数を実行
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' }