Skip to main content

Spatial Faithfulness Score (SFS) SDK — a domain-agnostic Python library for measuring numerical faithfulness in RAG systems

Project description

SFS SDK — Spatial Faithfulness Score

A lightweight Python SDK for measuring numerical faithfulness in Retrieval-Augmented Generation (RAG) systems. SFS extracts numerical claims from LLM-generated text and verifies them against ground-truth evidence.

SFS = verified_claims / total_claims

Installation

pip install sfs-sdk

Quick Start

from sfs_sdk import SFSClient

client = SFSClient(preset="property_valuation")

report = client.compute(
    text="The median price is $850,000 located 12.5 km from CBD.",
    evidence=[
        {"key": "median_price", "value": 842000, "unit": "AUD"},
        {"key": "cbd_distance", "value": 12.3, "unit": "km"},
    ],
)

print(f"SFS Score: {report.sfs_score}")        # 1.0
print(f"Verified: {report.verified_claims}/{report.total_claims}")

Features

  • Zero LLM dependency — deterministic regex-based claim extraction
  • Domain presets — property valuation, medical, legal, financial
  • Custom domains — define your own extraction patterns and tolerances
  • Pydantic models — fully typed, serializable data structures
  • Lightweight — only depends on pydantic

Usage

Extract Claims

from sfs_sdk import SFSClient

client = SFSClient(preset="medical")
claims = client.extract("Patient received 500 mg of amoxicillin for 7 days.")

for claim in claims:
    print(f"  {claim.claim_type}: {claim.extracted_value} {claim.unit}")

Verify Claims

report = client.verify(
    claims=claims,
    evidence=[
        {"key": "dosage", "value": 500, "unit": "dosage"},
        {"key": "duration", "value": 7, "unit": "duration"},
    ],
)
print(f"SFS: {report.sfs_score}")

Verify a Single Claim

result = client.verify_single(
    claim_text="The property is 12.5 km from CBD.",
    claim_type="distance",
    extracted_value=12.5,
    unit="km",
    evidence=[{"key": "cbd_distance", "value": 12.3, "unit": "km"}],
)
print(f"Verified: {result.verified}, Error: {result.error:.2%}")

Custom Domain Configuration

from sfs_sdk import SFSClient, DomainConfig, ExtractionPattern

config = DomainConfig(
    name="My Domain",
    tolerance=0.10,
    extraction_patterns=[
        ExtractionPattern(
            claim_type="temperature",
            pattern=r'([\d.]+)\s*°[CF]',
            unit="degrees",
            min_value=-50,
            max_value=200,
        ),
    ],
)

client = SFSClient(config=config)

Low-Level API

For direct access without the client wrapper:

from sfs_sdk import extract_claims, compute_sfs, EvidencePool, EvidenceItem

claims = extract_claims("The price is $500,000.")
evidence = EvidencePool(items=[
    EvidenceItem(key="price", value=495000, unit="USD"),
])
report = compute_sfs(claims, evidence)

Domain Presets

Preset Tolerance Claim Types
property_valuation 15% currency, percentage, distance, count
medical 10% dosage (5%), lab_value, percentage, count, duration
legal 10% currency (5%), percentage, duration, count
financial 10% currency, percentage, ratio (5%)
from sfs_sdk import SFSClient
presets = SFSClient.available_presets()

How It Works

  1. Extract — Regex patterns scan the LLM output for numerical claims (prices, distances, percentages, etc.)
  2. Match — Each claim is matched to the closest evidence item by unit
  3. Verify — Relative error is computed: |claimed - actual| / actual
  4. Score — A claim passes if its error is within the domain tolerance

Companion: SFS MCP Toolkit

For LLM-native integration via the Model Context Protocol, see sfs-mcp-toolkit — same engine, exposed as MCP tools for Claude Desktop, Claude Code, and other MCP clients.

Citation

If you use SFS in your research, please cite:

@software{rathnasinghe2025sfs,
  author  = {Rathnasinghe, Ganusha},
  title   = {SFS SDK: Spatial Faithfulness Score for RAG Verification},
  year    = {2025},
  url     = {https://github.com/rganushachadika/sfs-sdk},
}

License

MIT License. See LICENSE for details.

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

sfs_sdk-0.1.0.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

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

sfs_sdk-0.1.0-py3-none-any.whl (13.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for sfs_sdk-0.1.0.tar.gz
Algorithm Hash digest
SHA256 46ac964d7d6b63cb260849b388455add1dc459f5284c343cbbd044e6909f44e2
MD5 05d33e672f9069e7d01c4e6422e9e18f
BLAKE2b-256 e4f89d4af5457efeebe03f34591bcbf1cf6b73a30dd08f6f597e2631a97cd4a7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sfs_sdk-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 13.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for sfs_sdk-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e66f1470097dfddfde1eac0b63cbc1f5cb47ee0d17f61b4a1a6960f155e5e463
MD5 479c3b6a416fa17d1b36c6f1bbdce360
BLAKE2b-256 898af1a8410beef7fc8203d5ca9a52722d5e52f150f4b1c1a6135ce6c2167a5f

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