Skip to main content

A metric for evaluating LLM hallucination based on Source-based Term Precision.

Project description

Source Term Precision

A Python package to evaluate LLM hallucination and grounding for Japanese text, based on Source-based Term Precision ($P_{src}$).

This metric calculates the ratio of compound terms in the LLM-generated text that are explicitly present in the provided source text. It treats the input source text as the Single Source of Truth (SSoT) to act as a primary filter against plausible but fabricated terminology (hallucinations), especially in strict domains like patent analysis.


本パッケージは、LLM(大規模言語モデル)が生成した日本語テキストのハルシネーションを評価・検知するための評価指標「原典適合率( $P_{src}$ )」を算出するPythonライブラリです。入力原文を唯一の情報源(SSoT)とみなし、原文に存在しない用語の出現をペナルティ化することで、専門家のファクトチェック前の一次フィルタとして機能します。

Installation

pip install source_term_precision

Quick Start

from source_term_precision import GroundingEvaluator

# 評価器の初期化(初回呼び出し時にSudachi辞書をロードします)
evaluator = GroundingEvaluator()

# LLMが参照した原文(Single Source of Truth)
source_text = """
本発明は、半導体記憶装置の製造方法に関する。
特に、メモリセルアレイと周辺回路を同一基板上に形成する工程において、
熱処理温度を制御することで欠陥を低減する技術である。
"""

# LLMによって生成されたテキスト
llm_output = """
### 概要
- この文書は半導体記憶装置の製造プロセスについて説明しています。
- シリコンウェハーの熱処理を行うことで、メモリセルの速度を10%向上させます。

除外語テスト: アイデア
"""

# 評価の実行
result = evaluator.evaluate(
    llm_output=llm_output,
    source_text=source_text,
    exact_excludes={"アイデア"},   # 除外語指定(完全一致)
    partial_excludes={"文書"}      # 除外語指定(部分一致 ※)
)

print(f"Score (P_src): {result.score:.2f}")
print(f"Found Words: {result.found_words}")
print(f"Missing Words (Potential Hallucinations): {result.missing_words}")

# --- Output ---
# Score (P_src): 0.40
# Found Words: ['半導体記憶装置', '熱処理', 'こと', 'メモリセル']
# Missing Words (Potential Hallucinations): ['製造プロセス', '説明', 'シリコンウェハー', '速度', '10%向上', '除外語テスト']

Note: partial_excludes(部分一致での除外)は、意図しない複合語まで除外してしまう可能性があるため、タスクの出力形式に合わせて慎重に指定してください。

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

source_term_precision-0.1.0.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

source_term_precision-0.1.0-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file source_term_precision-0.1.0.tar.gz.

File metadata

  • Download URL: source_term_precision-0.1.0.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for source_term_precision-0.1.0.tar.gz
Algorithm Hash digest
SHA256 85490ba7bab749a4de5b5f2fa1b4a91ac25c2c27e765341eda061c6f6377c00e
MD5 8557892832c221b961ee26e7843ff56b
BLAKE2b-256 d13e50dea249766dd6b22f6246e45bea22e4b9b09b2f4d2d8a857b87fc719fc7

See more details on using hashes here.

File details

Details for the file source_term_precision-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for source_term_precision-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 89a49b936209e8b7b4bcd848a07dae80a7e335f9421214e552cdcb3774efeb45
MD5 c8d3387a9cbf5a25b3c18ca4e0630fcb
BLAKE2b-256 bcc8df400ca732412eef4bd3249a5370016af885eace446e277676eb0936cc66

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page