Documentation Index
Fetch the complete documentation index at: https://wb-21fd5541-john-wbdocs-2044-rename-serverless-products.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
このページでは、Google Vertex AI API および Google Gemini API で W&B Weave を使用する方法を説明します。
Weave を使用すると、Google GenAI アプリケーションを評価、監視し、反復的に改善できます。Weave は次のトレースを自動的に取得します。
- Google GenAI SDK:Python SDK、Node.js SDK、Go SDK、REST からアクセスできます。
- Google Vertex AI API:Google の Gemini モデルと各種パートナーモデルにアクセスできます。
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でラップすると、入力、出力、アプリのロジックのキャプチャが始まり、データがアプリ内をどのように流れるかをデバッグできるようになります。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="あなたは、都市内の予算に優しい観光スポットをすべて提案するためのアシスタントです",
)
return response.text
recommend_places_to_visit("New York")
recommend_places_to_visit("Paris")
recommend_places_to_visit("Kolkata")
実験には多くの要素が絡むため、整理が難しくなりがちです。Model クラスを使用すると、system prompt や使用しているモデルなど、アプリの実験に関する詳細を記録して整理できます。これにより、アプリのさまざまなバージョンを整理し、比較しやすくなります。
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="あなたは、都市内の予算に優しい観光スポットをすべて提案するためのアシスタントです",
)
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"))