> ## Documentation Index
> Fetch the complete documentation index at: https://wb-21fd5541-docs-hivemind-launch.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Not Diamond ¬◇

> Not Diamond を使用して、精度を最大化しコストを抑えながら、ニーズに最適なモデルにプロンプトを振り分けます

複雑な LLM ワークフローを構築する際には、精度、
コスト、または call のレイテンシに応じて、異なるモデルにプロンプトを送る必要がある場合があります。[Not Diamond][nd] を使用すると、これらのワークフロー内でプロンプトを
ニーズに最適なモデルへ振り分けることができ、
モデルのコストを抑えながら精度の最大化に役立ちます。

<div id="getting-started">
  ## はじめに
</div>

[アカウントを作成][account] し、[APIキーを生成][keys] してから、API
キーを環境変数 `NOTDIAMOND_API_KEY` として設定してください。

<img src="https://mintcdn.com/wb-21fd5541-docs-hivemind-launch/IJ5wvwHtTZtWc35M/weave/guides/integrations/imgs/notdiamond/api-keys.png?fit=max&auto=format&n=IJ5wvwHtTZtWc35M&q=85&s=1a42b5756504d6e4ae174270545bd475" alt="APIキーを作成" width="3454" height="1912" data-path="weave/guides/integrations/imgs/notdiamond/api-keys.png" />

ここから、次のことができます。

* \[クイックスタートガイド]を試す
* W\&B Weave と Not Diamond を使って[カスタムルーターを構築する][custom router]、または
* [Not Diamond とチャットして][chat]、ルーティングの動作を確認する

<div id="tracing">
  ## トレース
</div>

Weave は [Not Diamond の Python ライブラリ][python] と統合されており、[API コールを自動的にログします][ops]。
ワークフローの開始時に `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>

独自の\[カスタムルーター]を[評価][evals]でトレーニングすることで、特定のユースケースに合わせて、評価パフォーマンスに基づき Not Diamond がプロンプトを振り分けられるようになります。

まず、カスタムルーターをトレーニングします:

```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 = 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
