메인 콘텐츠로 건너뛰기
Colab에서 열기
LLM 플레이그라운드를 사용하면 별도 설정 없이 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?"
)

직접 ops 추적하기

함수를 @weave.op으로 감싸면 입력, 출력, 앱 로직 캡처가 시작되어 데이터가 앱 전체에서 어떻게 흐르는지 디버깅할 수 있습니다. ops를 깊게 중첩하고, 추적하려는 함수들을 트리 형태로 구성할 수 있습니다. 또한 실험하는 동안 코드 버전 관리도 자동으로 시작되어 아직 git에 커밋되지 않은 임시 세부 사항까지 캡처합니다. @weave.op으로 데코레이팅된 함수를 만들기만 하면 됩니다. 아래 예제에는 도시에서 방문할 장소를 추천하는, @weave.op으로 감싼 함수 recommend_places_to_visit가 있습니다.
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 클래스를 사용하면 system prompt나 사용 중인 모델처럼 앱의 실험 세부 정보를 캡처하고 정리할 수 있습니다. 이렇게 하면 앱의 여러 반복 버전을 정리하고 비교하는 데 도움이 됩니다. 코드를 버전 관리하고 입력/출력을 캡처하는 것에 더해, 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"))