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

# ネストされた関数をトレースする

> W&B トレースを使用して、深くネストした Call 構造をトラッキングする方法を学びます

LLM を活用したアプリケーションには、複数の LLM 呼び出しに加えて、監視が重要な追加のデータ処理や検証ロジックが含まれることがあります。これらのネストされた関数とその親子関係は、Python では `@weave.op()` デコレーターを使用し、TypeScript では `weave.op()` でラップすることで、Weave でトラッキングできます。

アプリケーションの完全な実行フローを把握するために、関数やサブ関数はできるだけ細かい粒度でデコレートすることをお勧めします。これにより、アプリケーションの挙動をより深く理解し、改善しやすくなります。

次のコードは、[クイックスタートの例](/ja/weave/quickstart) を基に、LLM から返された項目数を数え、それらをより上位の関数でまとめるロジックを追加したものです。さらに、この例では `weave.op()` を使用して、すべての関数、その呼び出し順序、および親子関係をトレースします:

<Tabs>
  <Tab title="Python">
    ```python {1,7,26,32,42} theme={null}
    import weave
    import json
    from openai import OpenAI

    client = OpenAI()

    @weave.op()
    def extract_dinos(sentence: str) -> dict:
        response = client.chat.completions.create(
            model="gpt-4o",
            messages=[
                {
                    "role": "system",
                    "content": """Extract any dinosaur `name`, their `common_name`, \
    names and whether its `diet` is a herbivore or carnivore, in JSON format."""
                },
                {
                    "role": "user",
                    "content": sentence
                }
                ],
                response_format={ "type": "json_object" }
            )
        return response.choices[0].message.content

    @weave.op()
    def count_dinos(dino_data: dict) -> int:
        # 返されたリスト内の項目数を数える
        k = list(dino_data.keys())[0]
        return len(dino_data[k])

    @weave.op()
    def dino_tracker(sentence: str) -> dict:
        # LLM を使用して恐竜を抽出する
        dino_data = extract_dinos(sentence)

        # 返された恐竜の数を数える
        dino_data = json.loads(dino_data)
        n_dinos = count_dinos(dino_data)
        return {"n_dinosaurs": n_dinos, "dinosaurs": dino_data}

    weave.init('jurassic-park')

    sentence = """I watched as a Tyrannosaurus rex (T. rex) chased after a Triceratops (Trike), \
    both carnivore and herbivore locked in an ancient dance. Meanwhile, a gentle giant \
    Brachiosaurus (Brachi) calmly munched on treetops, blissfully unaware of the chaos below."""

    result = dino_tracker(sentence)
    print(result)
    ```

    **ネストされた関数**

    上記のコードを実行すると、**Traces** ページに、ネストされた 2 つの関数 (`extract_dinos` と `count_dinos`) の入力と出力に加え、自動的に記録された OpenAI のトレースが表示されます。

    <img src="https://mintcdn.com/wb-21fd5541-run-filter-ui-updates/11L77DKwPIpkOJ5w/images/tutorial_tracing_2_nested_dinos.png?fit=max&auto=format&n=11L77DKwPIpkOJ5w&q=85&s=0bd806957b4da21858abe5b8572f4731" alt="中央のトレース ツリー パネルと、選択した Call の詳細パネルを示す、ネストされた Weave Traces ページ" width="1354" height="1334" data-path="images/tutorial_tracing_2_nested_dinos.png" />
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={null}
    import OpenAI from 'openai';
    import * as weave from 'weave';

    const openai = new OpenAI();

    const extractDinos = weave.op(async (sentence: string) => {
      const response = await openai.chat.completions.create({
        model: 'gpt-4o',
        messages: [
          {
            role: 'system',
            content:
              'Extract any dinosaur `name`, their `common_name`, names and whether its `diet` is a herbivore or carnivore, in JSON format.',
          },
          {role: 'user', content: sentence},
        ],
        response_format: {type: 'json_object'},
      });
      return response.choices[0].message.content;
    });

    const countDinos = weave.op(async (dinoData: string) => {
      const parsed = JSON.parse(dinoData);
      return Object.keys(parsed).length;
    });

    const dinoTracker = weave.op(async (sentence: string) => {
      const dinoData = await extractDinos(sentence);
      const nDinos = await countDinos(dinoData);
      return {nDinos, dinoData};
    });

    async function main() {
      await weave.init('jurassic-park');

      const sentence = `I watched as a Tyrannosaurus rex (T. rex) chased after a Triceratops (Trike),
            both carnivore and herbivore locked in an ancient dance. Meanwhile, a gentle giant
            Brachiosaurus (Brachi) calmly munched on treetops, blissfully unaware of the chaos below.`;

      const result = await dinoTracker(sentence);
      console.log(result);
    }

    main();

    ```

    **ネストされた関数**

    上記のコードを実行すると、**Traces** ページに、ネストされた 2 つの関数 (`extract_dinos` と `count_dinos`) の入力と出力に加え、自動的に記録された OpenAI のトレースが表示されます。

    <img src="https://mintcdn.com/wb-21fd5541-run-filter-ui-updates/11L77DKwPIpkOJ5w/images/tutorial_tracing_2_nested_dinos.png?fit=max&auto=format&n=11L77DKwPIpkOJ5w&q=85&s=0bd806957b4da21858abe5b8572f4731" alt="中央のトレース ツリー パネルと、選択した Call の詳細パネルを示す、ネストされた Weave Traces ページ" width="1354" height="1334" data-path="images/tutorial_tracing_2_nested_dinos.png" />
  </Tab>
</Tabs>

<div id="tracking-metadata">
  ## メタデータをトラッキングする
</div>

`weave.attributes` コンテキストマネージャーを使用し、call 時にトラッキングするメタデータを含む辞書を渡すことで、メタデータをトラッキングできます。

上記の例の続きです。

<Tabs>
  <Tab title="Python">
    ```python lines {1,10-11} theme={null}
    import weave

    weave.init('jurassic-park')

    sentence = """I watched as a Tyrannosaurus rex (T. rex) chased after a Triceratops (Trike), \
    both carnivore and herbivore locked in an ancient dance. Meanwhile, a gentle giant \
    Brachiosaurus (Brachi) calmly munched on treetops, blissfully unaware of the chaos below."""

    # 以前に定義した関数とあわせてメタデータをトラッキングする
    with weave.attributes({'user_id': 'lukas', 'env': 'production'}):
        result = dino_tracker(sentence)
    ```
  </Tab>

  <Tab title="TypeScript">
    ```plaintext theme={null}
    この機能はまだ TypeScript では利用できません。
    ```
  </Tab>
</Tabs>

<Note>
  ユーザー ID やコードの実行環境 (development、staging、または production) などのメタデータは、実行時にトラッキングすることを推奨します。

  system prompt などのシステム設定をトラッキングするには、[Weave Models](/ja/weave/guides/core-types/models) を使用することを推奨します。
</Note>

attributes の使用方法の詳細は、[属性を定義してログする](/ja/weave/guides/tools/attributes) を参照してください。

<div id="whats-next">
  ## 次のステップ
</div>

* [App Versioning チュートリアル](/ja/weave/tutorial-weave_models) に沿って、アドホックなプロンプト、モデル、アプリケーションへの変更を記録し、バージョン管理し、整理しましょう。
