Documentation Index
Fetch the complete documentation index at: https://wb-21fd5541-john-wbdocs-2044-rename-serverless-products.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
이 예시에서는 트레이싱, 평가, 비교를 위해 Weave와 함께 W&B Inference를 사용하는 방법을 보여줍니다.
기본 예제: Weave로 Llama 3.1 8B 트레이스하기
이 예제에서는 Llama 3.1 8B 모델에 프롬프트를 보내고 Weave로 call을 트레이스하는 방법을 보여줍니다. Tracing은 LLM call의 전체 입력과 출력을 캡처하고, 성능을 모니터링하며, Weave UI에서 결과를 분석할 수 있게 해줍니다.
이 예제에서는 다음을 수행합니다:
- Chat Completion 요청을 수행하는
@weave.op() 데코레이터가 적용된 함수를 정의합니다
- 트레이스가 기록되어 W&B entity 및 프로젝트에 연결됩니다
- 함수가 자동으로 트레이스되어 입력, 출력, 지연 시간, 메타데이터를 로깅합니다
- 결과는 터미널에 출력되고, 트레이스는 https://wandb.ai의 트레이스 탭에 표시됩니다
이 예제를 실행하기 전에 사전 요구 사항을 완료하세요.
import weave
import openai
# 트레이싱을 위한 Weave 팀 및 프로젝트 설정
weave.init("<your-team>/<your-project>")
client = openai.OpenAI(
base_url='https://api.inference.wandb.ai/v1',
# https://wandb.ai/settings 에서 API 키 생성
api_key="<your-api-key>",
# 선택 사항: 사용량 추적을 위한 팀 및 프로젝트
project="wandb/inference-demo",
)
# Weave에서 모델 call 트레이스
@weave.op()
def run_chat():
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": "Tell me a joke."}
],
)
return response.choices[0].message.content
# 트레이스된 call 실행 및 로그 기록
output = run_chat()
print(output)
코드를 실행한 후 다음 방법으로 Weave에서 트레이스를 확인할 수 있습니다:
- 터미널에 출력된 링크를 클릭합니다(예:
https://wandb.ai/<your-team>/<your-project>/r/call/01977f8f-839d-7dda-b0c2-27292ef0e04g)
- 또는 https://wandb.ai로 이동한 후 트레이스 탭을 선택합니다
고급 예제: Weave Evaluations 및 리더보드 사용
모델 call을 추적하는 것 외에도 성능을 평가하고 리더보드를 게시할 수 있습니다. 이 예제는 질의응답 데이터셋에서 두 모델을 비교합니다.
이 예제를 실행하기 전에 사전 요구 사항을 완료하세요.
import os
import asyncio
import openai
import weave
from weave.flow import leaderboard
from weave.trace.ref_util import get_ref
# 트레이싱을 위한 Weave 팀 및 프로젝트 설정
weave.init("<your-team>/<your-project>")
dataset = [
{"input": "What is 2 + 2?", "target": "4"},
{"input": "Name a primary color.", "target": "red"},
]
@weave.op
def exact_match(target: str, output: str) -> float:
return float(target.strip().lower() == output.strip().lower())
class WBInferenceModel(weave.Model):
model: str
@weave.op
def predict(self, prompt: str) -> str:
client = openai.OpenAI(
base_url="https://api.inference.wandb.ai/v1",
# https://wandb.ai/settings 에서 API 키 생성
api_key="<your-api-key>",
# 선택 사항: 사용량 추적을 위한 팀 및 프로젝트
project="<your-team>/<your-project>",
)
resp = client.chat.completions.create(
model=self.model,
messages=[{"role": "user", "content": prompt}],
)
return resp.choices[0].message.content
llama = WBInferenceModel(model="meta-llama/Llama-3.1-8B-Instruct")
deepseek = WBInferenceModel(model="deepseek-ai/DeepSeek-V3-0324")
def preprocess_model_input(example):
return {"prompt": example["input"]}
evaluation = weave.Evaluation(
name="QA",
dataset=dataset,
scorers=[exact_match],
preprocess_model_input=preprocess_model_input,
)
async def run_eval():
await evaluation.evaluate(llama)
await evaluation.evaluate(deepseek)
asyncio.run(run_eval())
spec = leaderboard.Leaderboard(
name="Inference Leaderboard",
description="Compare models on a QA dataset",
columns=[
leaderboard.LeaderboardColumn(
evaluation_object_ref=get_ref(evaluation).uri(),
scorer_name="exact_match",
summary_metric_path="mean",
)
],
)
weave.publish(spec)
이 코드를 실행한 후 https://wandb.ai/의 W&B 계정으로 이동한 다음, 다음을 수행하세요.