Skip to main content
복잡한 LLM 워크플로를 구축할 때는 정확도, 비용 또는 호출 지연 시간에 따라 서로 다른 모델에 프롬프트를 보내야 할 수 있습니다. 이러한 워크플로에서 Not Diamond를 사용하면 프롬프트를 필요에 맞는 모델로 라우팅하여, 정확도는 높이고 모델 비용은 절감할 수 있습니다. 이 가이드에서는 Not Diamond를 W&B Weave와 통합해 Weave가 라우팅된 모델 Call을 자동으로 트레이스하는 방법과, Weave Evaluations를 사용해 자체 성능 데이터를 기반으로 프롬프트를 라우팅하는 맞춤형 라우터를 트레이닝하는 방법을 설명합니다.

시작하기

Weave로 Not Diamond를 사용하려면 Not Diamond 계정과 API 키가 필요합니다. 계정을 생성하고 API 키를 발급한 다음, API 키를 NOTDIAMOND_API_KEY라는 이름의 환경 변수에 추가하세요. API 키 발급 이제 다음 중 원하는 작업을 수행할 수 있습니다.

트레이싱

Weave는 Not Diamond의 Python 라이브러리와 통합되어 API Call을 자동으로 로깅하므로, 나머지 Weave 트레이스와 함께 라우팅 결정과 공급자의 응답을 확인할 수 있습니다. 워크플로 시작 시 weave.init()를 실행한 다음, 평소처럼 라우팅된 공급자를 계속 사용하세요:
from notdiamond import NotDiamond

import weave
weave.init('notdiamond-quickstart')

client = NotDiamond()
session_id, provider = client.chat.completions.model_select(
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Concisely explain merge sort."}
    ],
    model=['openai/gpt-4o', 'anthropic/claude-3-5-sonnet-20240620']
)

print("LLM called: ", provider.provider)  # openai, anthropic 등
print("Provider model: ", provider.model) # gpt-4o, claude-3-5-sonnet-20240620 등

맞춤형 라우팅

특수한 사용 사례에서는 Evaluations를 기반으로 자체 [맞춤형 라우터]를 트레이닝하여 Not Diamond가 기본 라우팅 정책 대신 평가 성능에 따라 프롬프트를 라우팅하도록 할 수 있습니다. 먼저 맞춤형 라우터를 트레이닝하세요. train_router 호출은 나중에 사용할 수 있도록 트레이닝된 라우터를 식별하는 preference_id를 반환합니다:
from weave.flow.eval import EvaluationResults
from weave.integrations.notdiamond.custom_router import train_router

# gpt-4o와 Claude 3.5 Sonnet에 대한 Evaluation 구축
evaluation = weave.Evaluation(...)
gpt_4o = weave.Model(...)
sonnet = weave.Model(...)

model_evals = {
    'openai/gpt-4o': evaluation.get_eval_results(gpt_4o),
    'anthropic/claude-3-5-sonnet-20240620': evaluation.get_eval_results(sonnet),
}
preference_id = train_router(
    model_evals=model_evals,
    prompt_column="prompt",
    response_column="actual",
    language="en",
    maximize=True,
)
이 preference ID를 모든 model_select 요청에 전달하면, 맞춤형 라우터를 재사용하여 평가 데이터를 기준으로 성능은 최대화하고 비용은 최소화하도록 프롬프트를 라우팅할 수 있습니다:
from notdiamond import NotDiamond
client = NotDiamond()

import weave
weave.init('notdiamond-quickstart')

session_id, provider = client.chat.completions.model_select(
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Concisely explain merge sort."}
    ],
    model=['openai/gpt-4o', 'anthropic/claude-3-5-sonnet-20240620'],

    # 이 preference ID를 전달하면 맞춤형 라우터를 재사용합니다
    preference_id=preference_id
)

print("LLM called: ", provider.provider)  # openai, anthropic 등
print("Provider model: ", provider.model) # gpt-4o, claude-3-5-sonnet-20240620 등

추가 지원팀

추가 도움이 필요하면 문서를 확인하거나 메시지 보내기를 이용하세요.