Skip to main content

Tool for interpreting large language models using Shapley values.

Project description

TokenSHAP

TokenSHAP offers a novel method for interpreting large language models (LLMs) using Monte Carlo Shapley value estimation. This Python library attributes importance to individual tokens within input prompts, enhancing our understanding of model decisions. By leveraging concepts from cooperative game theory adapted to the dynamic nature of natural language, TokenSHAP facilitates a deeper insight into how different parts of an input contribute to the model's response.

Tokens Importance

About TokenSHAP

The method introduces an efficient way to estimate the importance of tokens based on Shapley values, providing interpretable, quantitative measures of token importance. It addresses the combinatorial complexity of language inputs and demonstrates efficacy across various prompts and LLM architectures. TokenSHAP represents a significant advancement in making AI more transparent and trustworthy, particularly in critical applications such as healthcare diagnostics, legal analysis, and automated decision-making systems.

Installation

To install TokenSHAP, clone the repository and install the required dependencies:

git clone https://github.com/ronigold/TokenSHAP.git
cd TokenSHAP
pip install -r requirements.txt

Usage

TokenSHAP is easy to use with any model that supports SHAP value computation for NLP. Here’s a quick guide:

# Import TokenSHAP
from token_shap import TokenSHAP

# Initialize with your model & tokenizer
model_name = "llama3"
tokenizer_path = "NousResearch/Hermes-2-Theta-Llama-3-8B"
tshap = TokenSHAP(model_name, tokenizer_path)

# Analyze token importance
prompt = "Why is the sky blue?"
results = tshap.analyze(prompt)

Results will include SHAP values for each token, indicating their contribution to the model's output.

Key Features

  • Interpretability for LLMs: Delivers a methodical approach to understanding how individual components of input affect LLM outputs.
  • Monte Carlo Shapley Estimation: Utilizes a Monte Carlo approach to efficiently compute Shapley values for tokens, suitable for extensive texts and large models.
  • Versatile Application: Applicable across various LLM architectures and prompt types, from factual questions to complex multi-sentence inputs.

Contributing

We welcome contributions from the community, whether it's adding new features, improving documentation, or reporting bugs. Here’s how you can contribute:

  1. Fork the project
  2. Create your feature branch (git checkout -b feature/YourAmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/YourAmazingFeature)
  5. Open a pull request

Support

For support, please email roni.goldshmidt@getnexar.com or miriam.horovicz@ni.com, or open an issue on our GitHub project page.

License

TokenSHAP is distributed under the MIT License. See LICENSE file for more information.

Authors

  • Miriam Horovicz
  • Roni Goldshmidt

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

tokenshap-0.1.0.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

TokenSHAP-0.1.0-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tokenshap-0.1.0.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for tokenshap-0.1.0.tar.gz
Algorithm Hash digest
SHA256 79637ce1ba35299211c4ebafeb75df8b6da96c02b8bcb136c575c3fb5dfbb13a
MD5 67ee464e8f9a03ec796a6f20dfca3d89
BLAKE2b-256 63a8d787d01ffa530b54a0475f3bd427a3a30f5329c5197dbc60c309f3fb3f64

See more details on using hashes here.

File details

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

File metadata

  • Download URL: TokenSHAP-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for TokenSHAP-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6fa2d57a876c37c9a74de3ab0f631e19297b35b02c4dc7162d80a8c0fdbb02ef
MD5 b15710f8c7aac1657702055b104a94ba
BLAKE2b-256 63e39e53ae0f57c54fd49b20396e68f1046ec7e51d4c2be1fb7946a3549ebdd4

See more details on using hashes here.

Supported by

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