Documentation Index
Fetch the complete documentation index at: https://wb-21fd5541-john-wbdocs-2044-rename-serverless-products.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
Weave には、AI アプリケーションを評価するための事前定義済みスコアラーがいくつか用意されており、たとえば ハルシネーション検出 や 要約品質 があります。これらを使うと、評価をすばやく定義し、アプリケーションの出力をスコアリングできます。
ローカルスコアラーを利用できるのは、Weave Python SDK のみです。Weave TypeScript SDK では、まだ利用できません。TypeScript で Weave スコアラーを使用する場合は、関数ベースのスコアラー を参照してください。
Weave のあらかじめ用意されている scorer を使用するには、追加の依存関係をいくつかインストールする必要があります。
pip install weave[scorers]
LLM-evaluators
2025年2月更新: LLMを活用する事前定義済みの scorer は、litellm と自動的に統合されるようになりました。
LLMクライアントを渡す必要はなく、model_idを設定するだけで済みます。
サポートされるモデルはこちらを参照してください。
このスコアラーは、入力データに基づいて、AI システムの出力にハルシネーションが含まれていないかを確認します。
from weave.scorers import HallucinationFreeScorer
scorer = HallucinationFreeScorer()
カスタマイズ:
- scorer の
system_prompt フィールドと user_prompt フィールドをカスタマイズして、自分にとって “hallucination” とは何かを定義します。
メモ:
score method は、context という名前の入力列を想定しています。データセットで別の名前を使用している場合は、column_map 属性を使用する ことで、context をデータセット内の列にマッピングできます。
以下は、評価のコンテキストでの例です。
import asyncio
import weave
from weave.scorers import HallucinationFreeScorer
# 必要に応じて列マッピングを使用してscorerを初期化する。
hallucination_scorer = HallucinationFreeScorer(
model_id="openai/gpt-4o", # litellmでサポートされている他のモデルも使用可能
column_map={"context": "input", "output": "other_col"}
)
# データセットを作成する
dataset = [
{"input": "John likes various types of cheese."},
{"input": "Pepe likes various types of cheese."},
]
@weave.op
def model(input: str) -> str:
return "The person's favorite cheese is cheddar."
# 評価を実行する
evaluation = weave.Evaluation(
dataset=dataset,
scorers=[hallucination_scorer],
)
result = asyncio.run(evaluation.evaluate(model))
print(result)
# 出力例:
# {'HallucinationFreeScorer': {'has_hallucination': {'true_count': 2, 'true_fraction': 1.0}}, 'model_latency': {'mean': ...}}
LLM を使用して、要約を元の文章と比較し、その品質を評価します。
from weave.scorers import SummarizationScorer
scorer = SummarizationScorer(
model_id="openai/gpt-4o" # または litellm でサポートされている他のモデル
)
仕組み:
この scorer は、summary を次の 2 つの方法で評価します。
- エンティティ密度: summary 内で言及されている一意のエンティティ (名前、場所、物など) の数を、summary の総語数に対する比率で確認し、summary の「情報密度」を推定します。エンティティの抽出には LLM を使用します。これは、Chain of Density paper における entity density の考え方に類似しています。
- 品質評価: LLM evaluator が summary を
poor、ok、excellent のいずれかに判定します。これらの判定は、その後、集計的なパフォーマンス評価のためのスコア (poor は 0.0、ok は 0.5、excellent は 1.0) に対応付けられます。
カスタマイズ:
summarization_evaluation_system_prompt と summarization_evaluation_prompt を調整して、評価プロセスをカスタマイズします。
メモ:
- この scorer は内部で litellm を使用します。
score method は、元のテキスト (要約対象のテキスト) が input column に含まれていることを前提としています。データセットで別の名を使用している場合は、column_map を使用してください。
以下は、評価の文脈での使用例です。
import asyncio
import weave
from weave.scorers import SummarizationScorer
class SummarizationModel(weave.Model):
@weave.op()
async def predict(self, input: str) -> str:
return "This is a summary of the input text."
# scorerを初期化する
summarization_scorer = SummarizationScorer(
model_id="openai/gpt-4o" # またはlitellmでサポートされている他のモデル
)
# データセットを作成する
dataset = [
{"input": "The quick brown fox jumps over the lazy dog."},
{"input": "Artificial Intelligence is revolutionizing various industries."}
]
# 評価を実行する
evaluation = weave.Evaluation(dataset=dataset, scorers=[summarization_scorer])
results = asyncio.run(evaluation.evaluate(SummarizationModel()))
print(results)
# 出力例:
# {'SummarizationScorer': {'is_entity_dense': {'true_count': 0, 'true_fraction': 0.0}, 'summarization_eval_score': {'mean': 0.0}, 'entity_density': {'mean': 0.0}}, 'model_latency': {'mean': ...}}
OpenAIModerationScorer は OpenAI の Moderation API を使用して、AI システムの出力にヘイトスピーチや露骨な表現などの許可されていないコンテンツが含まれているかどうかを確認します。
from weave.scorers import OpenAIModerationScorer
scorer = OpenAIModerationScorer()
仕組み:
- AI の出力を OpenAI Moderation エンドポイントに送信し、コンテンツがフラグ対象かどうかを示す構造化されたレスポンスを返します。
メモ:
以下は、評価での使用例です。
import asyncio
import weave
from weave.scorers import OpenAIModerationScorer
class MyModel(weave.Model):
@weave.op
async def predict(self, input: str) -> str:
return input
# スコアラーを初期化する
moderation_scorer = OpenAIModerationScorer()
# データセットを作成する
dataset = [
{"input": "I love puppies and kittens!"},
{"input": "I hate everyone and want to hurt them."}
]
# 評価を実行する
evaluation = weave.Evaluation(dataset=dataset, scorers=[moderation_scorer])
results = asyncio.run(evaluation.evaluate(MyModel()))
print(results)
# 出力例:
# {'OpenAIModerationScorer': {'flagged': {'true_count': 1, 'true_fraction': 0.5}, 'categories': {'violence': {'true_count': 1, 'true_fraction': 1.0}}}, 'model_latency': {'mean': ...}}
EmbeddingSimilarityScorer
EmbeddingSimilarityScorer は、AI システムの出力埋め込みと、データセット内の対象テキストの埋め込みとのコサイン類似度を計算します。AI の出力が参照テキストにどの程度近いかを測る際に役立ちます。
from weave.scorers import EmbeddingSimilarityScorer
similarity_scorer = EmbeddingSimilarityScorer(
model_id="openai/text-embedding-3-small", # litellmでサポートされる他のモデルも使用可能
threshold=0.4 # コサイン類似度のしきい値
)
パラメーター:
threshold (float): 2 つのテキストが類似していると見なすために必要な、最小のコサイン類似度スコア (-1 から 1 の範囲。デフォルトは 0.5) 。
使用例:
次の例では、評価のコンテキストで EmbeddingSimilarityScorer を使用します。
import asyncio
import weave
from weave.scorers import EmbeddingSimilarityScorer
# スコアラーを初期化する
similarity_scorer = EmbeddingSimilarityScorer(
model_id="openai/text-embedding-3-small", # litellmでサポートされている他のモデルも使用可能
threshold=0.7
)
# データセットを作成する
dataset = [
{
"input": "He's name is John",
"target": "John likes various types of cheese.",
},
{
"input": "He's name is Pepe.",
"target": "Pepe likes various types of cheese.",
},
]
# モデルを定義する
@weave.op
def model(input: str) -> str:
return "John likes various types of cheese."
# 評価を実行する
evaluation = weave.Evaluation(
dataset=dataset,
scorers=[similarity_scorer],
)
result = asyncio.run(evaluation.evaluate(model))
print(result)
# 出力例:
# {'EmbeddingSimilarityScorer': {'is_similar': {'true_count': 1, 'true_fraction': 0.5}, 'similarity_score': {'mean': 0.844851403}}, 'model_latency': {'mean': ...}}
ValidJSONScorer は、AI システムの出力が有効な JSON かどうかを確認します。出力が JSON 形式であることを想定しており、その妥当性を検証する必要がある場合に便利なスコアラーです。
from weave.scorers import ValidJSONScorer
json_scorer = ValidJSONScorer()
以下は、評価における例です。
import asyncio
import weave
from weave.scorers import ValidJSONScorer
class JSONModel(weave.Model):
@weave.op()
async def predict(self, input: str) -> str:
# これはプレースホルダーです。
# 実際のシナリオでは、JSONを生成します。
return '{"key": "value"}'
model = JSONModel()
json_scorer = ValidJSONScorer()
dataset = [
{"input": "Generate a JSON object with a key and value"},
{"input": "Create an invalid JSON"}
]
evaluation = weave.Evaluation(dataset=dataset, scorers=[json_scorer])
results = asyncio.run(evaluation.evaluate(model))
print(results)
# 出力例:
# {'ValidJSONScorer': {'json_valid': {'true_count': 2, 'true_fraction': 1.0}}, 'model_latency': {'mean': ...}}
ValidXMLScorer は、AI システムの出力が有効な XML かどうかをチェックします。XML 形式の出力を想定している場合に役立ちます。
from weave.scorers import ValidXMLScorer
xml_scorer = ValidXMLScorer()
以下は、評価における例です:
import asyncio
import weave
from weave.scorers import ValidXMLScorer
class XMLModel(weave.Model):
@weave.op()
async def predict(self, input: str) -> str:
# これはプレースホルダーです。実際のシナリオでは、XMLを生成します。
return '<root><element>value</element></root>'
model = XMLModel()
xml_scorer = ValidXMLScorer()
dataset = [
{"input": "Generate a valid XML with a root element"},
{"input": "Create an invalid XML"}
]
evaluation = weave.Evaluation(dataset=dataset, scorers=[xml_scorer])
results = asyncio.run(evaluation.evaluate(model))
print(results)
# 出力例:
# {'ValidXMLScorer': {'xml_valid': {'true_count': 2, 'true_fraction': 1.0}}, 'model_latency': {'mean': ...}}
PydanticScorer は、AI システムの出力を Pydantic モデルに照らして検証し、指定されたスキーマまたはデータ構造に準拠していることを確認します。
from weave.scorers import PydanticScorer
from pydantic import BaseModel
class FinancialReport(BaseModel):
revenue: int
year: str
pydantic_scorer = PydanticScorer(model=FinancialReport)
RAGAS - ContextEntityRecallScorer
ContextEntityRecallScorer は、AI システムの出力と与えられたコンテキストの両方からエンティティを抽出し、リコールスコアを計算することで、コンテキスト再現率を推定します。RAGAS 評価ライブラリに基づいています。
from weave.scorers import ContextEntityRecallScorer
entity_recall_scorer = ContextEntityRecallScorer(
model_id="openai/gpt-4o"
)
仕組み:
- LLM を使用して、出力とコンテキストから一意のエンティティを抽出し、再現率を計算します。
- 再現率 は、コンテキスト内の重要なエンティティのうち、出力で捉えられている割合を示します。
- 再現率スコアを含む辞書を返します。
メモ:
RAGAS - ContextRelevancyScorer
ContextRelevancyScorer は、与えられたコンテキストが AI システムの出力にどの程度関連しているかを評価します。RAGAS 評価ライブラリに基づいています。
from weave.scorers import ContextRelevancyScorer
relevancy_scorer = ContextRelevancyScorer(
model_id="openai/gpt-4o", # またはlitellmでサポートされている他のモデル
relevancy_prompt="""
Given the following question and context, rate the relevancy of the context to the question on a scale from 0 to 1.
Question: {question}
Context: {context}
Relevancy Score (0-1):
"""
)
仕組み:
- LLM を使用して、出力に対するコンテキストの関連性を 0〜1 の尺度で評価します。
relevancy_score を含む辞書を返します。
メモ:
- データセットに
context 列があることを想定しています。列名が異なる場合は、column_map 属性を使用してください。
- 関連性をどのように評価するかは、
relevancy_prompt をカスタマイズして定義します。
以下は、評価における使用例です:
import asyncio
from textwrap import dedent
import weave
from weave.scorers import ContextEntityRecallScorer, ContextRelevancyScorer
class RAGModel(weave.Model):
@weave.op()
async def predict(self, question: str) -> str:
"関連するコンテキストを取得する"
return "Paris is the capital of France."
# プロンプトを定義する
relevancy_prompt: str = dedent("""
以下の質問とコンテキストを踏まえ、質問に対するコンテキストの関連性を0から1のスケールで評価してください。
質問: {question}
コンテキスト: {context}
関連性スコア (0-1):
""")
# scorerを初期化する
entity_recall_scorer = ContextEntityRecallScorer()
relevancy_scorer = ContextRelevancyScorer(relevancy_prompt=relevancy_prompt)
# データセットを作成する
dataset = [
{
"question": "フランスの首都はどこですか?",
"context": "パリはフランスの首都です。"
},
{
"question": "ロミオとジュリエットを書いたのは誰ですか?",
"context": "William Shakespeareは多くの有名な戯曲を書きました。"
}
]
# 評価を実行する
evaluation = weave.Evaluation(
dataset=dataset,
scorers=[entity_recall_scorer, relevancy_scorer]
)
results = asyncio.run(evaluation.evaluate(RAGModel()))
print(results)
# 出力例:
# {'ContextEntityRecallScorer': {'recall': {'mean': ...}},
# 'ContextRelevancyScorer': {'relevancy_score': {'mean': ...}},
# 'model_latency': {'mean': ...}}
注: 組み込みのscorersは、openai/gpt-4o や openai/text-embedding-3-small などのOpenAIモデルを用いて調整されています。別のプロバイダを試したい場合は、model_id フィールドを更新して、別のモデルを使用できます。たとえば、Anthropicのモデルを使用するには:
from weave.scorers import SummarizationScorer
# AnthropicのClaudeモデルに切り替える
summarization_scorer = SummarizationScorer(
model_id="anthropic/claude-3-5-sonnet-20240620"
)