메인 콘텐츠로 건너뛰기
이 가이드에서는 W&B Inference와 함께 W&B Weave를 사용하는 방법을 안내합니다. W&B Inference를 사용하면 자체 인프라를 설정하거나 여러 공급업체의 API 키를 관리할 필요 없이 라이브 오픈소스 모델을 사용해 LLM 애플리케이션을 구축하고 트레이스할 수 있습니다. W&B API 키를 사용하면 W&B Inference에서 호스팅되는 모든 모델과 상호작용할 수 있습니다.

배울 내용

이 가이드에서는 다음을 알아봅니다:
  • Weave 및 W&B Inference 설정
  • 자동 Tracing이 포함된 기본 LLM 애플리케이션 빌드
  • 여러 모델 비교
  • 데이터셋에서 모델 성능 평가
  • Weave UI에서 결과 보기

사전 요구 사항

  • W&B 계정
  • Python 3.8+ 또는 Node.js 18+
  • 필수 패키지가 설치되어 있어야 합니다:
    • Python: pip install weave openai
    • TypeScript: npm install weave openai
  • OpenAI API 키를 환경 변수로 설정해야 합니다

첫 번째 LLM call 트레이스하기

시작하려면 다음 코드 예제를 복사해 붙여넣으세요. 이 코드 예제는 W&B Inference의 Llama 3.1-8B를 사용합니다. 이 코드를 실행하면 Weave가 다음을 수행합니다.
  • LLM call을 자동으로 트레이스합니다
  • 입력, 출력, 지연 시간, 토큰 사용량을 로깅합니다
  • Weave UI에서 트레이스를 확인할 수 있는 링크를 제공합니다
import weave
import openai

# Weave를 초기화합니다 - your-team/your-project로 바꾸세요
weave.init("<team-name>/inference-quickstart")

# W&B Inference를 가리키는 OpenAI 호환 클라이언트를 생성합니다
client = openai.OpenAI(
    base_url='https://api.inference.wandb.ai/v1',
    api_key="YOUR_WANDB_API_KEY",  # 실제 API 키로 바꾸세요
    project="<team-name>/my-first-weave-project",  # 사용량 추적에 필요합니다
)

# 트레이싱을 활성화하려면 함수에 데코레이터를 적용합니다. 표준 OpenAI 클라이언트를 사용하세요
@weave.op()
def ask_llama(question: str) -> str:
    response = client.chat.completions.create(
        model="meta-llama/Llama-3.1-8B-Instruct",
        messages=[
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": question}
        ],
    )
    return response.choices[0].message.content

# 함수를 호출합니다 - Weave가 모든 것을 자동으로 트레이스합니다
result = ask_llama("What are the benefits of using W&B Weave for LLM development?")
print(result)

텍스트 요약 애플리케이션 만들기

다음으로, Weave가 중첩된 오퍼레이션을 어떻게 트레이스하는지 보여 주는 기본 요약 앱 코드를 실행해 보세요:
import weave
import openai

# Weave 초기화 - "<>"로 묶인 값을 본인의 값으로 교체하세요.
weave.init("<team-name>/inference-quickstart")

client = openai.OpenAI(
    base_url='https://api.inference.wandb.ai/v1',
    api_key="YOUR_WANDB_API_KEY",  # 실제 API 키로 교체하세요
    project="<team-name>/my-first-weave-project",  # 사용량 추적에 필요합니다
)

@weave.op()
def extract_key_points(text: str) -> list[str]:
    """Extract key points from a text."""
    response = client.chat.completions.create(
        model="meta-llama/Llama-3.1-8B-Instruct",
        messages=[
            {"role": "system", "content": "Extract 3-5 key points from the text. Return each point on a new line."},
            {"role": "user", "content": text}
        ],
    )
    # 빈 줄을 제외하고 응답을 반환합니다
    return [line for line in response.choices[0].message.content.strip().splitlines() if line.strip()]

@weave.op()
def create_summary(key_points: list[str]) -> str:
    """Create a concise summary based on key points."""
    points_text = "\n".join(f"- {point}" for point in key_points)
    response = client.chat.completions.create(
        model="meta-llama/Llama-3.1-8B-Instruct",
        messages=[
            {"role": "system", "content": "Create a one-sentence summary based on these key points."},
            {"role": "user", "content": f"Key points:\n{points_text}"}
        ],
    )
    return response.choices[0].message.content

@weave.op()
def summarize_text(text: str) -> dict:
    """Main summarization pipeline."""
    key_points = extract_key_points(text)
    summary = create_summary(key_points)
    return {
        "key_points": key_points,
        "summary": summary
    }

# 샘플 텍스트로 사용해 보세요
sample_text = """
The Apollo 11 mission was a historic spaceflight that landed the first humans on the Moon 
on July 20, 1969. Commander Neil Armstrong and lunar module pilot Buzz Aldrin descended 
to the lunar surface while Michael Collins remained in orbit. Armstrong became the first 
person to step onto the Moon, followed by Aldrin 19 minutes later. They spent about 
two and a quarter hours together outside the spacecraft, collecting samples and taking photographs.
"""

result = summarize_text(sample_text)
print("Key Points:", result["key_points"])
print("\nSummary:", result["summary"])

여러 모델 비교

W&B Inference를 사용하면 여러 모델에 액세스할 수 있습니다. 다음 코드를 사용해 Llama와 DeepSeek의 응답 성능을 비교하세요.
import weave
import openai

# Weave를 초기화합니다. your-team/your-project로 바꾸세요
weave.init("<team-name>/inference-quickstart")

client = openai.OpenAI(
    base_url='https://api.inference.wandb.ai/v1',
    api_key="YOUR_WANDB_API_KEY",  # 실제 API 키로 바꾸세요
    project="<team-name>/my-first-weave-project",  # 사용 추적에 필요합니다
)

# 서로 다른 LLM을 비교하기 위한 Model 클래스를 정의합니다
class InferenceModel(weave.Model):
    model_name: str
    
    @weave.op()
    def predict(self, question: str) -> str:
        response = client.chat.completions.create(
            model=self.model_name,
            messages=[
                {"role": "user", "content": question}
            ],
        )
        return response.choices[0].message.content

# 서로 다른 모델 인스턴스를 생성합니다
llama_model = InferenceModel(model_name="meta-llama/Llama-3.1-8B-Instruct")
deepseek_model = InferenceModel(model_name="deepseek-ai/DeepSeek-V3.1")

# 응답을 비교합니다
test_question = "Explain quantum computing in one paragraph for a high school student."

print("Llama 3.1 8B response:")
print(llama_model.predict(test_question))
print("\n" + "="*50 + "\n")
print("DeepSeek V3 response:")
print(deepseek_model.predict(test_question))

모델 성능 평가

Weave의 내장 EvaluationLogger를 사용해 Q&A 작업에서 모델의 성능을 평가합니다. 그러면 자동 집계, 토큰 사용량 수집, UI에서의 다양한 비교 기능을 포함한 구조화된 평가 추적이 제공됩니다. 이전 섹션에서 사용한 스크립트에 다음 코드를 추가합니다:
from typing import Optional
from weave import EvaluationLogger

# 간단한 데이터셋 생성
dataset = [
    {"question": "What is 2 + 2?", "expected": "4"},
    {"question": "What is the capital of France?", "expected": "Paris"},
    {"question": "Name a primary color", "expected_one_of": ["red", "blue", "yellow"]},
]

# scorer 정의
@weave.op()
def accuracy_scorer(expected: str, output: str, expected_one_of: Optional[list[str]] = None) -> dict:
    """모델 출력의 정확도를 채점합니다."""
    output_clean = output.strip().lower()
    
    if expected_one_of:
        is_correct = any(option.lower() in output_clean for option in expected_one_of)
    else:
        is_correct = expected.lower() in output_clean
    
    return {"correct": is_correct, "score": 1.0 if is_correct else 0.0}

# Weave의 EvaluationLogger를 사용하여 모델 평가
def evaluate_model(model: InferenceModel, dataset: list[dict]):
    """Weave의 내장 평가 프레임워크를 사용하여 데이터셋에 대한 평가를 실행합니다."""
    # 토큰 사용량을 캡처하려면 모델 호출 전에 EvaluationLogger를 초기화하세요
    # W&B Inference에서 비용을 추적할 때 특히 중요합니다
    # 모델 이름을 유효한 형식으로 변환합니다 (영숫자가 아닌 문자를 언더스코어로 대체)
    safe_model_name = model.model_name.replace("/", "_").replace("-", "_").replace(".", "_")
    eval_logger = EvaluationLogger(
        model=safe_model_name,
        dataset="qa_dataset"
    )
    
    for example in dataset:
        # 모델 예측 결과 조회
        output = model.predict(example["question"])
        
        # 예측 로깅
        pred_logger = eval_logger.log_prediction(
            inputs={"question": example["question"]},
            output=output
        )
        
        # 출력 채점
        score = accuracy_scorer(
            expected=example.get("expected", ""),
            output=output,
            expected_one_of=example.get("expected_one_of")
        )
        
        # 점수 로깅
        pred_logger.log_score(
            scorer="accuracy",
            score=score["score"]
        )
        
        # 이 예측에 대한 로깅 완료
        pred_logger.finish()
    
    # 요약 로깅 - Weave가 정확도 점수를 자동으로 집계합니다
    eval_logger.log_summary()
    print(f"{model.model_name} (로깅 이름: {safe_model_name}) 평가가 완료되었습니다. Weave UI에서 결과를 확인하세요.")

# 여러 모델 비교 - Weave 평가 프레임워크의 핵심 기능
models_to_compare = [
    llama_model,
    deepseek_model,
]

for model in models_to_compare:
    evaluate_model(model, dataset)

# Weave UI에서 Evals 탭으로 이동하여 모델 간 결과를 비교하세요
이 예제를 실행하면 터미널에 트레이스 링크가 표시됩니다. 링크를 클릭하면 Weave UI에서 트레이스를 볼 수 있습니다. Weave UI에서는 다음 작업을 할 수 있습니다:
  • 모든 LLM call의 타임라인 검토
  • 각 오퍼레이션의 입력과 출력 확인
  • 토큰 사용량과 예상 비용 확인(EvaluationLogger가 자동으로 수집)
  • 지연 시간과 성능 메트릭 분석
  • Evals 탭으로 이동해 집계된 평가 결과 확인
  • Compare 기능을 사용해 서로 다른 모델의 성능 비교 및 분석
  • 특정 예제를 넘겨 보면서 동일한 입력에 대해 서로 다른 모델이 어떻게 수행되었는지 확인

사용 가능한 모델

사용 가능한 모델의 전체 목록은 W&B Inference 문서의 사용 가능한 모델 섹션을 참조하세요.

다음 단계

문제 해결