Documentation Index
Fetch the complete documentation index at: https://wb-21fd5541-feature-automate-reference-docs-generation.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
このページでは、W&B WeaveをGoogle Vertex AI APIおよびGoogle Gemini APIで使用する方法について説明します。
Weaveを使用して、Google GenAIアプリケーションを評価、監視、および反復することができます。Weaveは以下のトレースを自動的にキャプチャします:
- Google GenAI SDK(Python SDK、Node.js SDK、Go SDK、およびRESTを介してアクセス可能)
- Google Vertex AI API(GoogleのGeminiモデルおよびvarious partner modelsへのアクセスを提供)
はじめに
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?",
)
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?"
)
独自のオペレーションを追跡する
関数を@weave.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")
より簡単な実験のためにModelを作成する
多くの要素が動いている場合、実験を整理することは難しいです。Modelクラスを使用することで、システムプロンプトや使用しているモデルなど、アプリの実験的な詳細をキャプチャして整理できます。これにより、アプリの異なるイテレーションを整理して比較するのに役立ちます。
コードのバージョン管理と入出力のキャプチャに加えて、Modelはアプリケーションの動作を制御する構造化されたパラメータをキャプチャし、どのパラメータが最も効果的だったかを簡単に見つけることができます。また、Weave Modelsをserve、およびEvaluationと一緒に使用することもできます。
以下の例では、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"))