Skip to main content

Multi-threaded Shapley explainability for LLMs: words, sentences, documents, and tools.

Project description

lighbench logo

llmSHAP

Unit Tests Documentation

A multi-threaded explainability framework using Shapley values for LLM-based outputs.


Getting started

Install (from GitHub) as a package:

pip install "llmSHAP[full] @ git+https://github.com/filipnaudot/llmSHAP.git"

Install in editable mode with all optional dependencies (after cloning the repository):

pip install -e .[full]  # for bash
pip install -e '.[full]'  # for zsh

Documentation is available at llmSHAP Docs and a hands-on tutorial can be found here.


Example usage

from llmSHAP import DataHandler, BasicPromptCodec, ShapleyAttribution
from llmSHAP.llm import OpenAIInterface

data = "In what city is the Eiffel Tower?"
handler = DataHandler(data, permanent_keys={0,3,4})
prompt_codec = BasicPromptCodec(system="Answer the question briefly.")
llm = OpenAIInterface("gpt-4o-mini")

shap = ShapleyAttribution(model=llm,
                          data_handler=handler,
                          prompt_codec=prompt_codec,
                          use_cache=True,
                          num_threads=7)
result = shap.attribution()

print("\n\n### OUTPUT ###")
print(result.output)

print("\n\n### ATTRIBUTION ###")
print(result.attribution)

print("\n\n### HEATMAP ###")
print(result.render())

Example data

You can pass either a string or a dictionary:

from llmSHAP import DataHandler

# String input
data = "The quick brown fox jumps over the lazy dog"
handler = DataHandler(data)

# Dictionary input
data = {"a": "The", "b": "quick", "c": "brown", "d": "fox"}
handler = DataHandler(data)

To exclude certain keys from the computations, use permanent_keys:

from llmSHAP import DataHandler

data = {"a": "The", "b": "quick", "c": "brown", "d": "fox"}
handler = DataHandler(data, permanent_keys={"a", "d"})

# Get data with index 1 WITHOUT the permanent features.
print(handler.get_data({1}, exclude_permanent_keys=True, mask=False))
# Output: {'b': 'quick'}

# Get data with index 1 AND the permanent features.
print(handler.get_data({1}, exclude_permanent_keys=False, mask=False))
# Output: {'a': 'The', 'b': 'quick', 'd': 'fox'}

Comparison with TokenSHAP

Capability llmSHAP TokenSHAP
Threaded ✅ (optional num_threads)
Modular architecture
Exact Shapley option ✅ (Full enumeration) ❌ (Monte Carlo sampling)
Generation caching across coalitions
Heuristics SlidingWindow • Monte Carlo • Counterfactual Monte Carlo
Sentence-/chunk-level attribution
Permanent context pinning (always-included features)
Pluggable similarity metric ✅ TF-IDF, embeddings ✅ TF-IDF, embeddings
Docs & tutorial ✅ Sphinx docs + tutorial ✅ README only
Unit tests & CI ✅ Pytest + GitHub Actions
Vision object attribution ✅ PixelSHAP

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

llmshap-1.0.0.tar.gz (14.5 kB view details)

Uploaded Source

Built Distribution

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

llmshap-1.0.0-py3-none-any.whl (16.1 kB view details)

Uploaded Python 3

File details

Details for the file llmshap-1.0.0.tar.gz.

File metadata

  • Download URL: llmshap-1.0.0.tar.gz
  • Upload date:
  • Size: 14.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.5

File hashes

Hashes for llmshap-1.0.0.tar.gz
Algorithm Hash digest
SHA256 8218307abc1b7dc5b6860b2b47afa7fef59f622eaa97c7822427ee9a391bd7f6
MD5 ca4c500229973453341de931fce13db0
BLAKE2b-256 89bafca26d856589f6c9a2537343f5909a0fa5288c0ed608fc7ec7bb566bccb6

See more details on using hashes here.

File details

Details for the file llmshap-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: llmshap-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 16.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.5

File hashes

Hashes for llmshap-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3d89f0d415d88ce35262146e505c7acc48d05bbe592948ad770744b34ffbb599
MD5 f57b1dc3e59ba1804fd2582e06f00282
BLAKE2b-256 3ba693b8dc32ab95a70e8f77af2590985d2435af52df7d1e54cbafc45cdb18c7

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