> ## 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.

# Google

> Weave を使用して Google GenAI モデルの呼び出しをトレースし、ログする

<a target="_blank" href="https://github.com/wandb/examples/blob/master/weave/docs/quickstart_google.ipynb">
  <img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Colab で開く" />
</a>

<Note>
  [LLM Playground](../tools/playground) を使用すると、設定不要で Weave 上の Google AI モデルを試せます。
</Note>

このページでは、Google Vertex AI API および Google Gemini API で W\&B Weave を使用する方法を説明します。

Weave を使用すると、Google GenAI アプリケーションを評価、監視し、反復的に改善できます。Weave は次のトレースを自動的に取得します。

1. [Google GenAI SDK](https://github.com/googleapis/python-genai)：Python SDK、Node.js SDK、Go SDK、REST からアクセスできます。
2. [Google Vertex AI API](https://cloud.google.com/vertex-ai/docs)：Google の Gemini モデルと[各種パートナーモデル](https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/use-partner-models)にアクセスできます。

<Note>
  非推奨の [Google AI Python SDK for the Gemini API](https://github.com/google-gemini/deprecated-generative-ai-python) もサポートしています。なお、このサポート自体も非推奨であり、今後のバージョンで削除される予定です。
</Note>

<div id="get-started">
  ## はじめに
</div>

Weave は [Google GenAI SDK](https://github.com/googleapis/python-genai) のトレースを自動的にキャプチャします。トラッキングを開始するには、`weave.init(project_name="<YOUR-WANDB-PROJECT-NAME>")` を呼び出してから、通常どおりライブラリを使用します。

```python lines theme={null}
import os
from google import genai
import weave

weave.init(project_name="google-genai")

google_client = genai.Client(api_key=os.getenv("GOOGLE_GENAI_KEY"))
response = google_client.models.generate_content(
    model="gemini-2.0-flash",
    contents="What's the capital of France?",
)
```

[<img src="https://mintcdn.com/wb-21fd5541-docs-hivemind-launch/HbhUDAG_r2i2K_Pk/weave/guides/integrations/imgs/google-genai-trace.png?fit=max&auto=format&n=HbhUDAG_r2i2K_Pk&q=85&s=c36980462cff419573d780f036c46b00" alt="dspy_trace.png" width="3452" height="1866" data-path="weave/guides/integrations/imgs/google-genai-trace.png" />](https://wandb.ai/geekyrakshit/google-genai/weave/traces)

Weave は、[Vertex APIs](https://github.com/googleapis/python-aiplatform/tree/main/vertexai/generative_models) のトレースも自動的にキャプチャします。トラッキングを開始するには、`weave.init(project_name="<YOUR-WANDB-PROJECT-NAME>")` を呼び出してから、通常どおりライブラリを使用します。

```python lines theme={null}
import vertexai
import weave
from vertexai.generative_models import GenerativeModel

weave.init(project_name="vertex-ai-test")
vertexai.init(project="<YOUR-VERTEXAIPROJECT-NAME>", location="<YOUR-VERTEXAI-PROJECT-LOCATION>")
model = GenerativeModel("gemini-1.5-flash-002")
response = model.generate_content(
    "What's a good name for a flower shop specialising in selling dried flower bouquets?"
)
```

<div id="track-your-own-ops">
  ## 独自のopをトラッキングする
</div>

関数を`@weave.op`でラップすると、入力、出力、アプリのロジックのキャプチャが始まり、データがアプリ内をどのように流れるかをデバッグできるようになります。opは深くネストできるため、トラッキングしたい関数のツリーを構築できます。また、実験を進める中でコードのバージョン管理も自動的に開始され、まだgitにコミットしていないアドホックな変更内容も記録されます。

[`@weave.op`](/ja/weave/guides/tracking/ops)でデコレートした関数を作成するだけです。

以下の例では、`recommend_places_to_visit`という関数を使っています。これは`@weave.op`でラップされた関数で、ある都市で訪れる場所をおすすめします。

```python lines theme={null}
import os
from google import genai
import weave

weave.init(project_name="google-genai")
google_client = genai.Client(api_key=os.getenv("GOOGLE_GENAI_KEY"))


@weave.op()
def recommend_places_to_visit(city: str, model: str = "gemini-1.5-flash"):
    response = google_client.models.generate_content(
        model=model,
        contents="あなたは、都市内の予算に優しい観光スポットをすべて提案するためのアシスタントです",
    )
    return response.text


recommend_places_to_visit("New York")
recommend_places_to_visit("Paris")
recommend_places_to_visit("Kolkata")
```

[<img src="https://mintcdn.com/wb-21fd5541-docs-hivemind-launch/HbhUDAG_r2i2K_Pk/weave/guides/integrations/imgs/google-genai-ops.png?fit=max&auto=format&n=HbhUDAG_r2i2K_Pk&q=85&s=36e9a3f34618b461cc5844d4b97eeb20" alt="dspy_trace.png" width="3452" height="1866" data-path="weave/guides/integrations/imgs/google-genai-ops.png" />](https://wandb.ai/geekyrakshit/google-genai/weave/traces)

<div id="create-a-model-for-easier-experimentation">
  ## 実験しやすくするための `Model` を作成する
</div>

実験には多くの要素が絡むため、整理が難しくなりがちです。[`Model`](../core-types/models) クラスを使用すると、system prompt や使用しているモデルなど、アプリの実験に関する詳細を記録して整理できます。これにより、アプリのさまざまなバージョンを整理し、比較しやすくなります。

[`Model`](../core-types/models) は、コードのバージョン管理や入力/出力の記録に加えて、アプリケーションの挙動を制御する構造化されたパラメーターも記録するため、どのパラメーターが最も効果的だったかを簡単に見つけられます。また、Weave Models は `serve` や [`Evaluation`](../core-types/evaluations) と組み合わせて使用することもできます。

以下の例では、`CityVisitRecommender` を使って実験できます。これらのいずれかを変更するたびに、`CityVisitRecommender` の新しい *バージョン* が作成されます。

```python lines theme={null}
import os
from google import genai
import weave

weave.init(project_name="google-genai")
google_client = genai.Client(api_key=os.getenv("GOOGLE_GENAI_KEY"))


class CityVisitRecommender(weave.Model):
    model: str

    @weave.op()
    def predict(self, city: str) -> str:
        response = google_client.models.generate_content(
            model=self.model,
            contents="あなたは、都市内の予算に優しい観光スポットをすべて提案するためのアシスタントです",
        )
        return response.text


city_recommender = CityVisitRecommender(model="gemini-1.5-flash")
print(city_recommender.predict("New York"))
print(city_recommender.predict("San Francisco"))
print(city_recommender.predict("Los Angeles"))
```
