> ## Documentation Index
> Fetch the complete documentation index at: https://docs.wandb.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Not Diamond ¬◇

> Not Diamond를 사용해 필요에 맞는 모델로 프롬프트를 라우팅하고, 정확도는 높이면서 비용은 절감하세요

복잡한 LLM 워크플로를 구축할 때는 정확도, 비용 또는 호출 지연 시간에 따라 서로 다른 모델에 프롬프트를 보내야 할 수 있습니다.
이러한 워크플로에서 [Not Diamond][nd]를 사용하면 프롬프트를 필요에 맞는 모델로 라우팅하여,
정확도는 높이고 모델 비용은 절감할 수 있습니다.

이 가이드에서는 Not Diamond를 W\&B Weave와 통합해 Weave가 라우팅된 모델 Call을 자동으로 트레이스하는 방법과,
Weave Evaluations를 사용해 자체 성능 데이터를 기반으로 프롬프트를 라우팅하는 맞춤형 라우터를 트레이닝하는 방법을 설명합니다.

<div id="get-started">
  ## 시작하기
</div>

Weave로 Not Diamond를 사용하려면 Not Diamond 계정과 API 키가 필요합니다. [계정을 생성][account]하고 [API 키를 발급][keys]한 다음, API 키를 `NOTDIAMOND_API_KEY`라는 이름의 환경 변수에 추가하세요.

<img src="https://mintcdn.com/wb-21fd5541/S0cRiDzxeODX77LU/weave/guides/integrations/imgs/notdiamond/api-keys.png?fit=max&auto=format&n=S0cRiDzxeODX77LU&q=85&s=74a45e52c932eaff2094855c07831f0d" alt="API 키 발급" width="3454" height="1912" data-path="weave/guides/integrations/imgs/notdiamond/api-keys.png" />

이제 다음 중 원하는 작업을 수행할 수 있습니다.

* \[퀵스타트 가이드]를 사용해 보세요.
* Weave와 Not Diamond를 사용해 [맞춤형 라우터를 구축][custom router]하세요.
* [Not Diamond와 채팅][chat]하여 라우팅이 실제로 어떻게 동작하는지 확인하세요.

<div id="tracing">
  ## 트레이싱
</div>

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

```python lines theme={null}
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 등
```

<div id="custom-routing">
  ## 맞춤형 라우팅
</div>

특수한 사용 사례에서는 [Evaluations][evals]를 기반으로 자체 \[맞춤형 라우터]를 트레이닝하여
Not Diamond가 기본 라우팅 정책 대신 평가 성능에 따라 프롬프트를 라우팅하도록 할 수 있습니다.

먼저 맞춤형 라우터를 트레이닝하세요. `train_router` 호출은 나중에 사용할 수 있도록
트레이닝된 라우터를 식별하는 `preference_id`를 반환합니다:

```python lines theme={null}
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` 요청에 전달하면, 맞춤형 라우터를 재사용하여
평가 데이터를 기준으로 성능은 최대화하고 비용은 최소화하도록 프롬프트를 라우팅할 수 있습니다:

```python lines theme={null}
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 등
```

<div id="additional-support">
  ## 추가 지원팀
</div>

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

[account]: https://app.notdiamond.ai

[chat]: https://chat.notdiamond.ai

[custom router]: https://docs.notdiamond.ai/docs/router-training-quickstart

[docs]: https://docs.notdiamond.ai

[evals]: /weave/guides/core-types/evaluations

[keys]: https://app.notdiamond.ai/keys

[nd]: https://www.notdiamond.ai/

[ops]: /weave/guides/tracking/ops

[python]: https://github.com/Not-Diamond/notdiamond-python

[quickstart guide]: https://docs.notdiamond.ai/docs/quickstart-routing

[support]: mailto:support@notdiamond.ai
