Multi-threaded Shapley explainability for LLMs: words, sentences, documents, and tools.
Project description
llmSHAP
A multi-threaded explainability framework using Shapley values for LLM-based outputs.
Getting started
Install the llmshap package (with all optional dependencies):
pip install "llmshap[full]"
Install in editable mode with all optional dependencies (after cloning the repository):
pip install -e ".[full]"
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file llmshap-1.0.1.tar.gz.
File metadata
- Download URL: llmshap-1.0.1.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
963ec89fc39a2cd074c424563f1e93b8e3a33d6f3f2a4acb91810c2542301c6d
|
|
| MD5 |
35660cf3b56bda09f0ddecdb57918d14
|
|
| BLAKE2b-256 |
0879b4f62fe024baeb22783bd2f7d27547710a09c100b549b8215a1906e51239
|
File details
Details for the file llmshap-1.0.1-py3-none-any.whl.
File metadata
- Download URL: llmshap-1.0.1-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0f15a33cdfad6b188f0175f219b80d1e6a31085d3e79e4a0ad14069d5b908005
|
|
| MD5 |
fef5d07fe3136a00866f9590f79dbd7b
|
|
| BLAKE2b-256 |
cd73a212d2c51594a2be2d12d3ac5de603a52ab63679f4654522b94a07921b43
|