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

# MistralAI

> Weave の自動トレース機能を使用して、MistralAI のモデルの Call をトラッキングおよび監視します。チャット補完、関数呼び出し、モデルインタラクションに対応しています。

<a target="_blank" href="https://colab.research.google.com/github/wandb/examples/blob/master/weave/docs/quickstart_mistral.ipynb" aria-label="Open in Google Colab">
  <img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab" />
</a>

Weave は、[MistralAI Python library](https://github.com/mistralai/client-python) を通じて行われる LLM Call を自動的にトラッキングし、ログします。このガイドでは、MistralAI の Call のトレースを取得する方法、独自の op で Call をラップして再現性を確保する方法、`Model` クラスを使用して実験を整理する方法を説明します。

> Weave は Mistral v1.0 SDK をサポートします。アップグレードの詳細については、[移行ガイド](https://github.com/mistralai/client-python/blob/main/MIGRATION.md) を参照してください。

<h2 id="traces">
  トレース
</h2>

LLM アプリケーションのトレースを開発時と本番の両方で中央データベースに保存することは重要です。これらのトレースはデバッグに使用し、アプリケーションを改善するためのデータセットとしても役立ちます。

Weave は [mistralai](https://github.com/mistralai/client-python) のトレースを自動的に取得します。ライブラリを通常どおり使用するには、まず `weave.init()` を呼び出します：

```python lines theme={"system"}
import weave
weave.init("cheese_recommender")

# その後、通常通り mistralai ライブラリを使用します
import os
from mistralai import Mistral

api_key = os.environ["MISTRAL_API_KEY"]
model = "mistral-large-latest"

client = Mistral(api_key=api_key)

messages = [
    {
        "role": "user",
        "content": "What is the best French cheese?",
    },
]

chat_response = client.chat.complete(
    model=model,
    messages=messages,
)
```

Weave は MistralAI ライブラリを通じて行われたすべての LLM Call をトラッキングしてログします。トレースは Weave の Web インターフェースで確認できます。

[<img src="https://mintcdn.com/coreweave-dbfa0e8d/3Dv_sw2eg8feUJlx/products/wandb/weave/_media/mistral_trace.png?fit=max&auto=format&n=3Dv_sw2eg8feUJlx&q=85&s=6f496fd1aba7c2d4a7d4b61d62eadd88" alt="mistral_trace.png" width="2908" height="1460" data-path="products/wandb/weave/_media/mistral_trace.png" />](https://forge.coreweave.com/wandb/capecape/mistralai_project/weave/calls)

<h2 id="wrap-calls-with-your-own-ops">
  独自の Op で Call をラップする
</h2>

基本的なトレースが動作するようになったら、MistralAI の Call を Weave Op でラップして、実験間で再現できるようにしましょう。Weave Op は、実験中にコードを自動的にバージョン管理することで結果を*再現可能*にし、入力と出力も取得します。[`mistralai.client.MistralClient.chat()`](https://docs.mistral.ai/capabilities/completion) を呼び出す関数を作成して [`@weave.op()`](/ja/products/wandb/weave/guides/tracking/ops) でデコレートすると、Weave が入力と出力を自動的にトラッキングします。次の例は、チーズのレコメンダーでこれを行う方法を示しています。

```python lines {1} theme={"system"}
@weave.op()
def cheese_recommender(region:str, model:str) -> str:
    "Recommend the best cheese in a given region"
    
    messages = [
        {
            "role": "user",
            "content": f"What is the best cheese in {region}?",
        },
    ]

    chat_response = client.chat.complete(
        model=model,
        messages=messages,
    )
    return chat_response.choices[0].message.content

cheese_recommender(region="France", model="mistral-large-latest")
cheese_recommender(region="Spain", model="mistral-large-latest")
cheese_recommender(region="Netherlands", model="mistral-large-latest")
```

[<img src="https://mintcdn.com/coreweave-dbfa0e8d/3Dv_sw2eg8feUJlx/products/wandb/weave/_media/mistral_ops.png?fit=max&auto=format&n=3Dv_sw2eg8feUJlx&q=85&s=8718757963f0f23ce7955c043a19d30f" alt="mistral_ops.png" width="2759" height="1080" data-path="products/wandb/weave/_media/mistral_ops.png" />](https://forge.coreweave.com/wandb/capecape/mistralai_project/weave/calls)

<h2 id="create-a-model-for-easier-experimentation">
  `Model` を使用して実験を簡単に
</h2>

実験の整理は、多くの可動部分があるため困難です。[`Model`](/ja/products/wandb/weave/guides/core-types/models) クラスを使用することで、システムプロンプトや使用するモデルなど、アプリの実験的な詳細を取得して整理できます。これにより、アプリのさまざまな反復処理を整理して比較しやすくなります。

コードのバージョン管理や入出力の取得に加えて、[`Model`](/ja/products/wandb/weave/guides/core-types/models) はアプリケーションの動作を制御する構造化されたパラメーターを取得するため、最適なパラメーターを検索しやすくなります。また、Weave モデル を `serve` や [`Evaluation`](/ja/products/wandb/weave/guides/core-types/evaluations) と一緒に使用することもできます。

次の例では、`model` と `country` を使用して実験できます。これらのいずれかを変更するたびに、`CheeseRecommender` の新しい *バージョン* が作成されます。

```python lines theme={"system"}
import weave
from mistralai import Mistral

weave.init("mistralai_project")

class CheeseRecommender(weave.Model): # `weave.Model` に変更
    model: str
    temperature: float

    @weave.op()
    def predict(self, region:str) -> str: # `predict` に変更
        "Recommend the best cheese in a given region"
        
        client = Mistral(api_key=api_key)

        messages = [
            {
                "role": "user",
                "content": f"What is the best cheese in {region}?",
            },
        ]

        chat_response = client.chat.complete(
            model=model,
            messages=messages,
            temperature=self.temperature
        )
        return chat_response.choices[0].message.content

cheese_model = CheeseRecommender(
    model="mistral-medium-latest",
    temperature=0.0
    )
result = cheese_model.predict(region="France")
print(result)
```

[<img src="https://mintcdn.com/coreweave-dbfa0e8d/3Dv_sw2eg8feUJlx/products/wandb/weave/_media/mistral_model.png?fit=max&auto=format&n=3Dv_sw2eg8feUJlx&q=85&s=576975bf65feac4cb2dd1da1b68244cd" alt="mistral_model.png" width="2894" height="1536" data-path="products/wandb/weave/_media/mistral_model.png" />](https://forge.coreweave.com/wandb/capecape/mistralai_project/weave/models)


## Related topics

- [サンプルコードとノートブック](/ja/products/wandb/examples.md)
