> ## Documentation Index
> Fetch the complete documentation index at: https://wb-21fd5541-docs-hivemind-launch.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# 속성 정의 및 로깅

> 속성을 사용해 트레이스와 평가에 메타데이터를 추가합니다.

Weave의 속성을 사용하면 트레이스와 평가에 맞춤형 메타데이터를 추가할 수 있습니다. 이 메타데이터에는 환경 이름, 모델 버전, 실험 ID, 사용자 ID, 기타 컨텍스트 정보 등이 포함될 수 있으며, 이를 통해 Weave 데이터를 정리하고 필터링하고 분석할 수 있습니다.

다음 기준으로 트레이스를 그룹화하거나 필터링해야 할 때 속성이 특히 유용합니다:

* 배포
* 테넌트
* Experiments

Weave는 속성을 추가하는 두 가지 방법을 제공합니다:

* **call / calls별 속성**: `weave.attributes()`를 사용해 특정 오퍼레이션이나 코드 블록에 메타데이터를 추가합니다
* **전역 속성**: 초기화 시 `global_attributes` 필드를 사용해 프로젝트의 모든 트레이스와 평가에 적용되는 속성을 설정합니다

트레이스와 평가 중 로깅된 모든 속성은 UI에서 볼 수 있습니다. 그런 다음 이를 사용해 데이터를 필터링하고 그룹화할 수 있습니다.

<Tip>
  call / calls이 시작되면 `call.attributes`는 수정할 수 없습니다. op를 호출하기 전에
  이 컨텍스트 관리자를 사용해 필요한 메타데이터를 설정하세요.
</Tip>

<div id="per-call-attributes">
  ## call / calls별 속성
</div>

`weave.attributes()` 컨텍스트 관리자를 사용하면 특정 트레이스된 오퍼레이션에 메타데이터를 추가할 수 있습니다. 이를 통해 특정 함수 call / calls이나 평가 run에 컨텍스트 정보를 태그할 수 있습니다.

<Tabs>
  <Tab title="Python">
    ```python lines theme={null}
    import weave

    weave.init("<your-team-name/your-project-name>")

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

    # 특정 call / calls에 속성 추가
    with weave.attributes({'env': 'production', 'user_id': '12345'}):
        result = my_function("World")
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript lines theme={null}
    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}!`;
      });

      // 특정 call / calls에 속성 추가
      const result = await withAttributes(
        {env: 'production', user_id: '12345'},
        async () => myFunction('World')
      );

      console.log('Result:', result);
    }

    main().catch(console.error);
    ```
  </Tab>
</Tabs>

이 함수는 컨텍스트 관리자 블록(Python) 또는 콜백 함수(TypeScript) 안의 모든 트레이스된 오퍼레이션에 속성을 적용합니다.

`weave.attributes()` 컨텍스트를 중첩해서 사용할 수도 있습니다. 동일한 키의 경우 내부 컨텍스트가 외부 컨텍스트를 재정의합니다.

<Tabs>
  <Tab title="Python">
    ```python lines theme={null}
    @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'가 적용됨
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript lines theme={null}
    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);
    ```
  </Tab>
</Tabs>

<div id="global-attributes">
  ## 전역 속성
</div>

Weave를 초기화할 때 전역 속성을 설정하면 프로젝트의 **모든** 트레이스와 평가에 자동으로 적용됩니다. 이는 환경, 배포 버전, 팀 정보처럼 프로젝트 전반에서 사용하는 메타데이터를 전달할 때 유용합니다.

<Tabs>
  <Tab title="Python">
    ```python lines theme={null}
    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))  # 모든 전역 속성이 적용됨
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript lines theme={null}
    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);
    ```
  </Tab>
</Tabs>

<div id="combine-global-and-per-call-attributes">
  ### 전역 속성과 call별 속성 함께 사용하기
</div>

전역 속성과 call별 속성을 함께 사용할 수 있습니다. 동일한 키를 가진 call별 속성은 전역 속성을 재정의합니다.

<Tabs>
  <Tab title="Python">
    ```python lines theme={null}
    import weave

    # 전역 속성 설정
    weave.init(
        "my-project",
        global_attributes={
            "env": "production",
            "app_version": "2.1.0"
        }
    )

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

    # 이 call에는 env='production', app_version='2.1.0'이 적용됩니다
    process("test1")

    # 이 call에는 env='staging', app_version='2.1.0', experiment='A'가 적용됩니다
    with weave.attributes({'env': 'staging', 'experiment': 'A'}):
        process("test2")
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript lines theme={null}
    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;
        });

        // 이 call에는 env='production', app_version='2.1.0'이 적용됩니다
        await process('test1');

        // 이 call에는 env='staging', app_version='2.1.0', experiment='A'가 적용됩니다
        await withAttributes(
        {env: 'staging', experiment: 'A'},
        async () => process('test2')
        );
    }
    main().catch(console.error);
    ```
  </Tab>
</Tabs>

<div id="get-attributes-during-execution">
  ## 실행 중 속성 조회
</div>

현재 call에 로깅된 속성 집합을 반환합니다. 이는 call을 디버그하거나 조건부 로직에 사용할 컨텍스트를 확인하는 데 도움이 됩니다.

<Tabs>
  <Tab title="Python">
    다음 예제에서는 Weave 데코레이터로 `process_data` 함수를 로깅하도록 설정하고, 로깅할 속성을 구성한 뒤, 실행 중에 이를 반환합니다.

    ```python lines  theme={null}
    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")
    ```

    출력 결과:

    ```shell theme={null}
    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'}
    ```

    <Note>
      `weave.get_current_call()`은 `@weave.op`로 데코레이트된 함수 **내부에서만** 작동합니다. op 외부에서는 `None`을 반환합니다.
    </Note>
  </Tab>

  <Tab title="TypeScript">
    Weave TypeScript SDK를 사용하면 `client.getCurrentAttributes()`로 현재 속성을 조회할 수 있습니다. Weave Python SDK와 달리 TypeScript에서는 op 내부뿐 아니라 `withAttributes()` 컨텍스트 내에서도 속성에 접근할 수 있습니다.

    ```typescript lines theme={null}
    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);
    ```

    출력 결과:

    ```shell theme={null}
    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' }
    ```
  </Tab>
</Tabs>
