メインコンテンツへスキップ
Colab で開く
LLM Playground を使用すると、セットアップ不要で Weave 上で Google AI モデルを試すことができます。
このページでは、W&B Weave を Google Vertex AI API および Google Gemini API と併用する方法を説明します。 Weave を使用して、Google GenAI アプリケーションの評価、監視、および反復的な改善を行うことができます。Weave は次の対象についてトレースを自動的に収集します。
  1. Python SDK、Node.js SDK、Go SDK、および REST から利用可能な Google GenAI SDK
  2. Google の Gemini モデルおよびさまざまなパートナーモデルへのアクセスを提供する Google Vertex AI API
非推奨となっている Google AI Python SDK for the Gemini API についてもサポートしています。ただし、このサポート自体も非推奨であり、今後のバージョンで削除される予定です。

はじめに

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-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?"
)

自分の 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 クラスを使用すると、システムプロンプトや使用しているモデルなど、アプリの実験に関する詳細を記録・整理できます。これにより、アプリのさまざまなバージョンや反復を整理して比較しやすくなります。 コードのバージョニングや入出力の記録に加えて、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"))