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

# Google

<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="Open In Colab" />
</a>

<Tip>
  最新のチュートリアルについては、[Google Cloud上のWeights & Biases](https://wandb.ai/site/partners/googlecloud/)をご覧ください。
</Tip>

<Note>
  セットアップなしでWeave上のGoogle AIモデルを試してみたいですか？[LLM Playground](../tools/playground.mdx)をお試しください。
</Note>

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

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モデルおよび[various partner models](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>

## はじめに

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

```python
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?",
)
```

[![dspy\_trace.png](https://mintlify.s3.us-west-1.amazonaws.com/wb-21fd5541-feature-automate-reference-docs-generation/ja/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
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?"
)
```

## 独自のオペレーションを追跡する

関数を`@weave.op`でラップすると、入力、出力、アプリのロジックのキャプチャが開始され、アプリを通じてデータがどのように流れるかをデバッグできます。オペレーションを深くネストして、追跡したい関数のツリーを構築できます。また、実験中にコードを自動的にバージョン管理し、gitにコミットされていないアドホックな詳細をキャプチャします。

単に[`@weave.op`](/ja/guides/tracking/ops)でデコレートされた関数を作成します。

以下の例では、`recommend_places_to_visit`という関数があり、これは`@weave.op`でラップされた、都市で訪れるべき場所を推薦する関数です。

```python
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="You are a helpful assistant meant to suggest all budget-friendly places to visit in a city",
    )
    return response.text


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

[![dspy\_trace.png](https://mintlify.s3.us-west-1.amazonaws.com/wb-21fd5541-feature-automate-reference-docs-generation/ja/guides/integrations/imgs/google-genai-ops.png)](https://wandb.ai/geekyrakshit/google-genai/weave/traces)

## より簡単な実験のために`Model`を作成する

多くの要素が動いている場合、実験を整理することは難しいです。[`Model`](../core-types/models)クラスを使用することで、システムプロンプトや使用しているモデルなど、アプリの実験的な詳細をキャプチャして整理できます。これにより、アプリの異なるイテレーションを整理して比較するのに役立ちます。

コードのバージョン管理と入出力のキャプチャに加えて、[`Model`](../core-types/models)はアプリケーションの動作を制御する構造化されたパラメータをキャプチャし、どのパラメータが最も効果的だったかを簡単に見つけることができます。また、Weave Modelsを`serve`、および[`Evaluation`](../core-types/evaluations.mdx)と一緒に使用することもできます。

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

```python
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="You are a helpful assistant meant to suggest all budget-friendly places to visit in a city",
        )
        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"))
```
