> ## 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**](../README.mdx) • **Docs**

***

[weave](../README.mdx) / op

# Function: op()

## op(fn, options)

> **op**\<`T`>(`fn`, `options`?): [`Op`](../type-aliases/Op.mdx)\<(...`args`) => `Promise`\<`Awaited`\<`ReturnType`\<`T`>>>>

同期および非同期関数で動作する関数またはメソッドをweave op化するためのラッパー。

ラップされた関数：

1. 元の関数と同じ入力を受け取り、同じ出力を返します。
2. Weave UIで自動的に呼び出しを追跡します。

もし`weave.init`を呼び出さない場合、関数はラップされていないかのように動作します。

### 型パラメータ

• **T** *extends* (...`args`) => `any`

### パラメータ

• **fn**: `T`

ラップする関数

• **options?**: `OpOptions`\<`T`>

呼び出しやパラメータの命名などのオプション設定

### 戻り値

[`Op`](../type-aliases/Op.mdx)\<(...`args`) => `Promise`\<`Awaited`\<`ReturnType`\<`T`>>>>

ラップされた関数

### 例

```ts
// Basic usage
import OpenAI from 'openai';
import * as weave from 'weave';

const client = await weave.init({ project: 'my-project' });
const oaiClient = new OpenAI();

const extract = weave.op(async function extract() {
  return await oaiClient.chat.completions.create({
    model: 'gpt-4-turbo',
    messages: [{ role: 'user', content: 'Create a user as JSON' }],
  });
});

await extract();

// You can also wrap methods by passing the object as the first argument.
// This will bind the method to the object and wrap it with op.
class MyModel {
  private oaiClient: OpenAI;

  constructor() {
    this.oaiClient = new OpenAI();
    this.invoke = weave.op(this, this.invoke);
  }

  async invoke() {
    return await this.oaiClient.chat.completions.create({
      model: 'gpt-4-turbo',
      messages: [{ role: 'user', content: 'Create a user as JSON' }],
    });
  }
}

const model = new MyModel();
const res = await model.invoke();
```

### 定義場所

[op.ts:58](https://github.com/wandb/weave/blob/e2313369cb35bc1b6f97c70539926dd951ead21e/sdks/node/src/op.ts#L58)

## op(thisArg, fn, options)

> **op**\<`T`>(`thisArg`, `fn`, `options`?): [`Op`](../type-aliases/Op.mdx)\<(...`args`) => `Promise`\<`Awaited`\<`ReturnType`\<`T`>>>>

### 型パラメータ

• **T** *extends* (...`args`) => `any`

### パラメータ

• **thisArg**: `any`

• **fn**: `T`

• **options?**: `OpOptions`\<`T`>

### 戻り値

[`Op`](../type-aliases/Op.mdx)\<(...`args`) => `Promise`\<`Awaited`\<`ReturnType`\<`T`>>>>

### 定義場所

[op.ts:62](https://github.com/wandb/weave/blob/e2313369cb35bc1b6f97c70539926dd951ead21e/sdks/node/src/op.ts#L62)
