アプリケーションを改善するためには、それが向上しているかどうかを評価する方法が必要です。そのために、一般的な方法は変更があった際に同じ例のセットに対してテストすることです。Weaveには評価を追跡するためのファーストクラスの方法がありますModel & Evaluation クラス。私たちは幅広いユースケースをサポートする柔軟性を可能にするために、最小限の前提条件で構築されたAPIを提供しています。 Evals hero

1. Model

Modelを構築し、プロンプト、温度などのシステムに関する情報をバージョン管理します。 Weaveは自動的にそれらが使用されたタイミングを記録し、変更があった場合にバージョンを更新します。 ModelModelをサブクラス化し、predict関数定義を実装することで宣言されます。この関数は1つの例を受け取り、応答を返します。
import json
import openai
import weave

class ExtractFruitsModel(weave.Model):
    model_name: str
    prompt_template: str

    @weave.op()
    async def predict(self, sentence: str) -> dict:
        client = openai.AsyncClient()

        response = await client.chat.completions.create(
            model=self.model_name,
            messages=[
                {"role": "user", "content": self.prompt_template.format(sentence=sentence)}
            ],
        )
        result = response.choices[0].message.content
        if result is None:
            raise ValueError("No response from model")
        parsed = json.loads(result)
        return parsed
次のようにModelオブジェクトを通常通りインスタンス化できます:
import asyncio
import weave

weave.init('intro-example')

model = ExtractFruitsModel(
    model_name='gpt-3.5-turbo-1106',
    prompt_template='Extract fields ("fruit": <str>, "color": <str>, "flavor": <str>) from the following text, as json: {sentence}'
)
sentence = "There are many fruits that were found on the recently discovered planet Goocrux. There are neoskizzles that grow there, which are purple and taste like candy."
print(asyncio.run(model.predict(sentence)))
# if you're in a Jupyter Notebook, run:
# await model.predict(sentence)
詳細についてはModelsガイドをご覧ください。

2. いくつかの例を収集する

次に、モデルを評価するためのデータセットが必要です。DatasetはWeaveオブジェクトとして保存された例のコレクションです。Weave UIでデータセットをダウンロード、閲覧、評価を実行することができます。 ここではコードで例のリストを構築していますが、実行中のアプリケーションから一度に1つずつログに記録することもできます。
sentences = ["There are many fruits that were found on the recently discovered planet Goocrux. There are neoskizzles that grow there, which are purple and taste like candy.",
"Pounits are a bright green color and are more savory than sweet.",
"Finally, there are fruits called glowls, which have a very sour and bitter taste which is acidic and caustic, and a pale orange tinge to them."]
labels = [
    {'fruit': 'neoskizzles', 'color': 'purple', 'flavor': 'candy'},
    {'fruit': 'pounits', 'color': 'bright green', 'flavor': 'savory'},
    {'fruit': 'glowls', 'color': 'pale orange', 'flavor': 'sour and bitter'}
]
examples = [
    {'id': '0', 'sentence': sentences[0], 'target': labels[0]},
    {'id': '1', 'sentence': sentences[1], 'target': labels[1]},
    {'id': '2', 'sentence': sentences[2], 'target': labels[2]}
]
次に、データセットを公開します:
import weave
# highlight-next-line
weave.init('intro-example')
dataset = weave.Dataset(name='fruits', rows=examples)
# highlight-next-line
weave.publish(dataset)
詳細についてはDatasetsガイドをご覧ください。

3. スコアリング関数を定義する

EvaluationModelのパフォーマンスを、指定されたスコアリング関数またはweave.scorer.Scorerクラスのリストを使用して一連の例で評価します。
# highlight-next-line
import weave
from weave.scorers import MultiTaskBinaryClassificationF1

@weave.op()
def fruit_name_score(target: dict, output: dict) -> dict:
    return {'correct': target['fruit'] == output['fruit']}
独自のスコアリング関数を作成するには、Scorersガイドで詳細をご覧ください。一部のアプリケーションでは、カスタムScorerクラスを作成したい場合があります - 例えば、標準化されたLLMJudgeクラスを特定のパラメータ(例:チャットモデル、プロンプト)、各行の特定のスコアリング、および集計スコアの特定の計算で作成する必要がある場合です。Scorerクラスを定義する方法については、次の章のModel-Based Evaluation of RAG applicationsのチュートリアルで詳細をご覧ください。

4. 評価を実行する

これで、ExtractFruitsModelfruitsデータセットに対する評価をスコアリング関数を使用して実行する準備ができました。
import asyncio
import weave
from weave.scorers import MultiTaskBinaryClassificationF1

weave.init('intro-example')

evaluation = weave.Evaluation(
    name='fruit_eval',
    dataset=dataset, 
    scorers=[
        MultiTaskBinaryClassificationF1(class_names=["fruit", "color", "flavor"]), 
        fruit_name_score
    ],
)
print(asyncio.run(evaluation.evaluate(model)))
# if you're in a Jupyter Notebook, run:
# await evaluation.evaluate(model)
Pythonスクリプトから実行している場合は、asyncio.runを使用する必要があります。ただし、Jupyterノートブックから実行している場合は、awaitを直接使用できます。

5. 評価結果を表示する

Weaveは各予測とスコアのトレースを自動的に記録します。 評価によって出力されたリンクをクリックして、Weave UIで結果を表示します。 Evaluation results

次のステップは?

以下の方法を学びましょう:
  1. モデルのパフォーマンスを比較する:異なるモデルを試して結果を比較する
  2. 組み込みスコアラーを探索する:Weaveの組み込みスコアリング関数をScorers guide
  3. RAGアプリを構築するRAG tutorialに従って、検索拡張生成の評価について学ぶ
  4. 高度な評価パターンModel-Based EvaluationでLLMを審査員として使用する方法を学ぶ