MistralAI

Open In Colab WeaveはMistralAI Python libraryを介して行われるLLM呼び出しを自動的に追跡およびログに記録します。
新しいMistral v1.0 SDKをサポートしています。移行ガイドはhere

Traces

LLMアプリケーションのトレースを開発中も本番環境でも中央データベースに保存することが重要です。これらのトレースはデバッグに使用し、アプリケーションの改善に役立つデータセットとして活用します。 Weaveは自動的にトレースをキャプチャします mistralai。ライブラリは通常通り使用でき、まず次のように呼び出します weave.init():
import weave
weave.init("cheese_recommender")

# then use mistralai library as usual
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呼び出しを追跡し記録します。Weaveウェブインターフェースでトレースを確認できます。 mistral_trace.png

独自のopsでラップする

Weave opsは結果をreproducibleにします。実験中にコードを自動的にバージョン管理し、入力と出力をキャプチャします。単に@weave.op()でデコレートされた関数を作成してmistralai.client.MistralClient.chat()を呼び出すと、Weaveが入力と出力を追跡します。チーズレコメンダーでこれを行う方法を見てみましょう:
# highlight-next-line
@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")
mistral_ops.png

作成するModelより簡単な実験のために

多くの要素がある場合、実験の整理は難しくなります。Modelクラスを使用することで、システムプロンプトや使用するモデルなど、アプリの実験的な詳細をキャプチャして整理できます。これにより、アプリのさまざまなイテレーションを整理して比較するのに役立ちます。 コードのバージョン管理と入出力のキャプチャに加えて、Modelはアプリケーションの動作を制御する構造化されたパラメータをキャプチャし、最適なパラメータを簡単に見つけることができます。Weave Modelsをserve、およびEvaluationと一緒に使用することもできます。 以下の例では、modelcountryを実験できます。これらのいずれかを変更するたびに、新しいversionCheeseRecommenderが得られます。
import weave
from mistralai import Mistral

weave.init("mistralai_project")

class CheeseRecommender(weave.Model): # Change to `weave.Model`
    model: str
    temperature: float

    @weave.op()
    def predict(self, region:str) -> str: # Change to `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)
mistral_model.png