> ## Documentation Index
> Fetch the complete documentation index at: https://docs.coreweave.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Google Gen AI

> CoreWeave Forge SDK を使用して、Google Gen AI SDK から Gemini への Call を Agent Lens でトレースします。

CoreWeave Forge SDK は、Python 向け (`google-genai`) および Node.js 向け (`@google/genai`) の Google Gen AI SDK で実行する Call を自動的にトレースします。トレースを初期化すると、会話内で実行する各 Call が LLM スパンとして Agent Lens に記録されます。記録される情報は、メッセージ、システム指示、ツール、メディア、リクエスト設定、応答メタデータ、およびトークン使用量 (キャッシュされた入力トークンと思考トークンを含む) です。Google Gen AI SDK の呼び出し方法を変更する必要はありません。

このインテグレーションでは、コンテンツ生成 (ストリーミングを含む) 、チャットヘルパー、および Imagen による画像生成がトレースされます。Vertex AI クライアント経由の Call は `vertex_ai` に、Gemini Developer API の Call は `gemini` に分類されます。

> [!NOTE]
>
> Google の Agent Development Kit (ADK) で構築したエージェントをトレースする方法については、[Google ADK](https://docs.coreweave.com/ja/products/agent-lens/integrations/google-adk.md) を参照してください。

## 前提条件

* CoreWeave Forge アカウントと、環境変数 `WANDB_API_KEY` に設定済みの [APIキー](https://forge.coreweave.com/settings#apikeys)。
* 環境変数 `GEMINI_API_KEY` に設定済みの [Gemini APIキー](https://aistudio.google.com/apikey)。
* Python の場合: Python 3.10 以降、および `google-genai` 1.0.0～1.23.0。
* TypeScript の場合: Node.js 18.19 以降 (または 20.6 以降) 、および `@google/genai` 0.2.0 以降。

## パッケージのインストール

```bash Python theme={"system"}
pip install "coreweave[google-genai]"
```

```bash TypeScript theme={"system"}
npm install @coreweave/forge-sdk @google/genai
```

## Google Gen AI の会話をトレースする

Forge のチーム名と project 名を指定して `tracing.init()` を呼び出します。Python では、`tracing.init()` によって Google Gen AI SDK にパッチが適用されます。これは、SDK をこの呼び出しの前後どちらでインポートしても同じです。TypeScript では、TypeScript タブに示すインストルメンテーションフラグを付けて Node を起動します。

Agent Lens に表示されるのは、会話内で実行された Call のみです。会話の外で行われた Call には会話 ID がないため、Agent Lens には表示されません。エージェントセッションごとに会話を 1 つ作成し、ユーザーメッセージごとにターンを開始してください。すべてのターンは会話の ID を共有するため、Agent Lens はそれらのターンを 1 つの会話にまとめます。また、各 Call は、それが実行されたターンの LLM スパンになります。

> [tab] Python
>
> ```python lines theme={"system"}
> from google import genai
> from coreweave.forge.agentlens import tracing
>
> tracing.init("[YOUR-TEAM]/[YOUR-PROJECT]")
> client = genai.Client()
>
> questions = [
>     "What is inference latency?",
>     "How does batching affect it?",
>     "Summarize what we discussed in one sentence.",
> ]
>
> chat = client.chats.create(model="gemini-flash-latest")
> with tracing.Conversation(agent_name="support-agent") as conversation:
>     for question in questions:
>         with conversation.start_turn(user_message=question):
>             response = chat.send_message(question)
>             print(response.text)
>
> tracing.shutdown()
> ```

> [tab] TypeScript
>
> エージェントの各 run を `tracing.runIsolated()` でラップし、その会話とターンが同時に実行される他の run と混ざらないようにします。
>
> ```typescript lines title="app.mjs" theme={"system"}
> import {GoogleGenAI} from '@google/genai';
> import {tracing} from '@coreweave/forge-sdk/agentlens';
>
> await tracing.init('[YOUR-TEAM]/[YOUR-PROJECT]');
> const google = new GoogleGenAI({apiKey: process.env.GEMINI_API_KEY});
>
> const questions = [
>   'What is inference latency?',
>   'How does batching affect it?',
>   'Summarize what we discussed in one sentence.',
> ];
>
> await tracing.runIsolated(async () => {
>   const conversation = tracing.startConversation({agentName: 'support-agent'});
>   const chat = google.chats.create({model: 'gemini-flash-latest'});
>   try {
>     for (const question of questions) {
>       const turn = conversation.startTurn({userMessage: question});
>       try {
>         const response = await chat.sendMessage({message: question});
>         console.log(response.text);
>       } finally {
>         turn.end();
>       }
>     }
>   } finally {
>     conversation.end();
>   }
> });
>
> await tracing.shutdown();
> ```
>
> インストルメンテーションフラグを付けてスクリプトを実行します。このフラグを指定すると、コードより先に Forge のインストルメンテーションが読み込まれるため、コードが Google Gen AI SDK をインポートした時点で SDK にパッチが適用されます。
>
> ```bash theme={"system"}
> node --import=@coreweave/forge-sdk/instrument app.mjs
> ```
>
> バンドラーで依存関係を 1 つのファイルにまとめている場合など、フラグを使用できない場合は、代わりにクライアントをラップします。
>
> ```typescript theme={"system"}
> import {integrations} from '@coreweave/forge-sdk/agentlens';
>
> const google = integrations.wrapGoogleGenAI(
>   new GoogleGenAI({apiKey: process.env.GEMINI_API_KEY})
> );
> ```
>
> フラグなしで実行すると、`tracing.init()` はその旨の警告を 1 回だけ出力します。クライアントをラップしている場合は、この警告を無視してかまいません。

リクエストごとに 1 つのターンを処理する Web アプリなどで、既存の会話に後からターンを追加するには、会話を作成するたびに同じ `conversation_id` (Python) または `conversationId` (TypeScript) を渡します。

会話、ターン、その他のスパンタイプの詳細については、[エージェントをトレースする](https://docs.coreweave.com/ja/products/agent-lens/tracing/instrument.md)を参照してください。

## 自動パッチ適用を無効にする

Python では、`tracing.init()` に `autopatch_integrations=False` を渡します。こうすることで、`tracing.integrations.patch_google_genai()` と `tracing.integrations.unpatch_google_genai()` を使用して、Google Gen AI SDK へのパッチの適用と解除を手動で行えるようになります。

TypeScript では、`--import=@coreweave/forge-sdk/instrument` フラグを付けずに実行し、トレースしたいクライアントだけを `integrations.wrapGoogleGenAI()` でラップします。

## トレースを表示する

[CoreWeave Forge](https://forge.coreweave.com) で、プロダクトメニューから Agent Lens を選択し、サイドメニュー上部の project セレクターで対象の project を選択してから、**Conversations** を選択します。詳細については、[エージェントのアクティビティを表示する](https://docs.coreweave.com/ja/products/agent-lens/conversations/view-activity.md)を参照してください。
