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

# Op

この`@weave.op`デコレータはWeaveで関数呼び出しを追跡するために使用されます。装飾された関数の入力、出力、およびメタデータを自動的に記録します。

## 基本的な使用方法

```python
import weave

@weave.op
def my_function(x: int, y: int) -> int:
    return x + y

# When called, this function is automatically tracked
result = my_function(3, 4)
```

## デコレータオプション

### name

操作のカスタム名を設定します。

```python
@weave.op(name="custom_operation_name")
def my_function():
    pass
```

### call\_display\_name

この操作のすべての呼び出しに表示名を設定します。

```python
@weave.op(call_display_name="My Custom Display")
def my_function():
    pass
```

関数を使用して表示名を動的に生成することもできます：

```python
def generate_name(call):
    return f"Processing {call.inputs['item_id']}"

@weave.op(call_display_name=generate_name)
def process_item(item_id: str):
    pass
```

### postprocess\_inputs

ログに記録される前に入力を変換します。

```python
def redact_sensitive(inputs):
    if 'password' in inputs:
        inputs['password'] = '[REDACTED]'
    return inputs

@weave.op(postprocess_inputs=redact_sensitive)
def login(username: str, password: str):
    pass
```

### postprocess\_output

ログに記録される前に出力を変換します。

```python
def format_output(output):
    return weave.Markdown(f"**Result:** {output}")

@weave.op(postprocess_output=format_output)
def generate_report():
    return "Analysis complete"
```

## Opメソッド

### call

結果とCallオブジェクトの両方を取得します。

```python
@weave.op
def my_function(x: int) -> int:
    return x * 2

result, call = my_function.call(5)
print(f"Result: {result}")
print(f"Call ID: {call.id}")
```

### name

操作名を取得または設定します。

```python
@weave.op
def my_function():
    pass

# Get the current name
print(my_function.name)

# Set a new name
my_function.name = "new_operation_name"
```

## 非同期関数の操作

Weaveは非同期操作をサポートしています：

```python
@weave.op
async def async_operation(prompt: str) -> str:
    # Async operations are tracked the same way
    result = await some_async_api(prompt)
    return result
```

## ジェネレータの操作

Weaveはジェネレータ関数を追跡できます：

```python
@weave.op
def generate_items(count: int):
    for i in range(count):
        yield f"Item {i}"

# The output is captured when the generator is consumed
items = list(generate_items(5))
```

## クラスメソッド

クラスのメソッドを装飾します：

```python
class MyClass:
    @weave.op
    def process(self, data: str) -> str:
        return data.upper()

instance = MyClass()
result = instance.process("hello")
```

## 静的メソッドとクラスメソッド

```python
class MyClass:
    @staticmethod
    @weave.op
    def static_method(x: int) -> int:
        return x * 2
    
    @classmethod
    @weave.op
    def class_method(cls, x: int) -> int:
        return x * 3
```

<Note>
  この`@weave.op`デコレータは、デコレータをスタックする場合、最も内側のデコレータである必要があります。
</Note>
