메인 콘텐츠로 건너뛰기
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
사용이 중단된(deprecated) 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를 serveEvaluations와 함께 사용할 수도 있습니다. 아래 예시에서는 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"))