メインコンテンツへスキップ
Colab で開く
LLM Playground を使用すると、設定不要で Weave 上の Google AI モデルを試せます。
このページでは、Google Vertex AI API および Google Gemini API で W&B Weave を使用し、Google GenAI アプリケーションを評価、監視し、反復的に改善する方法を説明します。 Weave は次のトレースを自動的に取得します。
非推奨の Google AI Python SDK for the Gemini API もサポートしています。なお、このサポート自体も非推奨であり、今後のバージョンで削除される予定です。

はじめに

以下の例では、サポートされる各 SDK でトレースを有効にする方法を示します。どちらの場合も、必要な Weave 固有の手順は weave.init を呼び出すことだけです。既存の Google GenAI または Vertex AI のコードはそのまま変更せずに使用できます。 Weave は Google GenAI SDK のトレースを自動的に取得します。トラッキングを開始するには、weave.init(project_name="[YOUR-WANDB-PROJECT-NAME]") を呼び出してから、通常どおりライブラリを使用します。
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 Weave は、Vertex APIs のトレースも自動的に取得します。トラッキングを開始するには、weave.init(project_name="[YOUR-WANDB-PROJECT-NAME]") を呼び出してから、通常どおりライブラリを使用します。
import vertexai
import weave
from vertexai.generative_models import GenerativeModel

weave.init(project_name="vertex-ai-test")
vertexai.init(project="[YOUR-VERTEX-AI-PROJECT-NAME]", location="[YOUR-VERTEX-AI-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?"
)

独自のopをトラッキングする

関数を@weave.opでラップすると、入力、出力、アプリのロジックの取得が始まり、データがアプリ内をどのように流れるかをデバッグできるようになります。opは深くネストできるため、トラッキングしたい関数のツリーを構築できます。また、実験を進める中でコードのバージョン管理も自動的に開始され、まだgitにコミットしていないアドホックな変更内容も取得されます。 @weave.opでデコレートした関数を作成します。 以下の例では、recommend_places_to_visitという関数が@weave.opでラップされており、ある都市で訪れる場所をおすすめします。
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

実験しやすくするための Model を作成する

実験には多くの要素が絡むため、整理が難しくなりがちです。Model クラスを使用すると、system prompt や使用しているモデルなど、アプリの実験に関する詳細を取得して整理できます。これにより、アプリのさまざまなバージョンを整理し、比較しやすくなります。 Model は、コードのバージョン管理や入力/出力の取得に加えて、アプリケーションの挙動を制御する構造化されたパラメーターも取得するため、どのパラメーターが最も効果的だったかを簡単に見つけられます。また、Weave Models は serveEvaluation と組み合わせて使用することもできます。 以下の例では、CityVisitRecommender を使って実験できます。これらのいずれかを変更するたびに、CityVisitRecommender の新しい バージョン が作成されます。これにより、試した各設定のバージョン管理された記録が残り、後で比較や評価を行えます。
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"))