Open In Colab
최신 튜토리얼은 Google Cloud의 Weights & Biases를 방문하세요.
설정 없이 Weave에서 Google AI 모델을 실험해 보고 싶으신가요? LLM Playground를 시도해 보세요.
이 페이지는 W&B Weave를 Google Vertex AI API 및 Google Gemini API와 함께 사용하는 방법을 설명합니다. Weave를 사용하여 Google GenAI 애플리케이션을 평가, 모니터링 및 반복할 수 있습니다. Weave는 다음에 대한 추적을 자동으로 캡처합니다:
  1. Google GenAI SDK, Python SDK, Node.js SDK, Go SDK 및 REST를 통해 액세스할 수 있습니다.
  2. Google Vertex AI API, Google의 Gemini 모델 및 various partner models에 대한 액세스를 제공합니다.
또한 더 이상 사용되지 않는 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?"
)

자체 작업 추적하기

함수를 wrapping하는 @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")
dspy_trace.png

더 쉬운 실험을 위해 Model 생성하기

많은 요소가 있을 때 실험을 체계화하기는 어렵습니다. Model 클래스를 사용하면 시스템 프롬프트나 사용 중인 모델과 같은 앱의 실험 세부 정보를 캡처하고 구성할 수 있습니다. 이를 통해 앱의 다양한 반복을 구성하고 비교할 수 있습니다. 코드 버전 관리 및 입력/출력 캡처 외에도 Model는 애플리케이션의 동작을 제어하는 구조화된 매개변수를 캡처하여 어떤 매개변수가 가장 잘 작동했는지 쉽게 찾을 수 있게 합니다. Weave Models를 serve, 그리고 Evaluation와 함께 사용할 수도 있습니다. 아래 예시에서는 CityVisitRecommender로 실험할 수 있습니다. 이 중 하나를 변경할 때마다 새로운 versionCityVisitRecommender를 얻게 됩니다.
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"))