메인 콘텐츠로 건너뛰기
이 가이드는 W&B Weave를 W&B Inference와 함께 사용하는 방법을 보여줍니다. W&B Inference를 사용하면 별도의 인프라를 구축하거나 여러 제공업체의 API 키를 관리할 필요 없이 실시간 오픈소스 모델을 활용해 LLM 애플리케이션을 구축하고 트레이싱할 수 있습니다. W&B API 키만 있으면 W&B Inference에서 호스팅하는 모든 모델과 상호작용할 수 있습니다.

학습할 내용

이 가이드에서는 다음 내용을 다룹니다:
  • Weave와 W&B Inference를 설정하는 방법
  • 자동 트레이싱이 적용된 기본 LLM 애플리케이션을 구축하는 방법
  • 여러 모델을 비교하는 방법
  • 데이터셋에서 모델 성능을 평가하는 방법
  • Weave UI에서 결과를 확인하는 방법

사전 준비 사항

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

첫 번째 LLM 호출 트레이스하기

먼저 아래 코드 예제를 복사해 붙여넣으세요. 이 코드 예제는 W&B Inference의 Llama 3.1-8B를 사용합니다. 이 코드를 실행하면 Weave는 다음을 수행합니다.
  • LLM 호출을 자동으로 트레이스합니다.
  • 입력, 출력, 지연 시간, 토큰 사용량을 로깅합니다.
  • 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을 비교하기 위한 모델 클래스 정의
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-0324")

# 두 모델의 응답 비교
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"]},
]

# 스코어러 정의
@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 호출의 타임라인 검토
  • 각 연산의 입력과 출력 검사
  • 토큰 사용량과 예상 비용 확인 (EvaluationLogger에서 자동으로 캡처)
  • 지연 시간과 성능 메트릭 분석
  • Evals 탭으로 이동해 집계된 평가 결과 확인
  • Compare 기능을 사용해 서로 다른 모델 간 성능 비교
  • 특정 예제를 하나씩 살펴보며 서로 다른 모델이 동일한 입력에서 어떻게 동작했는지 확인

사용 가능한 모델

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

다음 단계

문제 해결