API reference¶
nlp-shap exposes a small, typed public surface. Import the symbols below
from nlp_shap or follow the module pages for implementation detail.
Public exports¶
Symbol |
Description |
|---|---|
Top-level explain pipeline configuration. See Pipeline configuration. |
|
Parse YAML into |
|
Serialize |
|
Frozen conversation input for explainability. See Domain types. |
|
Ordered explainability players. See Domain types. |
|
Boolean coalition presence mask. See Domain types. |
|
Cooperative-game player set and references. See Domain types. |
|
Plugin discovery and resolution. See Plugins. |
|
Whitespace token partition plugin. See Masking. |
|
Shared masked snapshot views. See Masking. |
|
SQLite + blob coalition archive. See Runtime. |
|
Async bounded coalition execution. See Runtime. |
|
Estimand label enum ( |
|
Shapley-weighted coalition aggregation. See Estimand aggregators and exact estimation. |
|
Exact coalition enumeration. See Estimand aggregators and exact estimation. |
|
Uniform Banzhaf coalition aggregation. See Estimand aggregators and exact estimation. |
|
Labelled explain output. See Pipeline configuration. |
|
Run-archive metadata builder. See Pipeline configuration. |
|
Parse manifest payloads at archive boundaries. See Pipeline configuration. |
Modules¶
Package version¶
Multimodal explainability tool for NLP based on Shapley value.
- class nlp_shap.BanzhafAggregator[source]¶
Bases:
objectAggregate coalition samples with uniform Banzhaf coalition weights.
- class nlp_shap.CoalitionMask(present: tuple[bool, ...])[source]¶
Bases:
objectBoolean presence mask aligned with a player set.
- classmethod from_sequence(present: Sequence[bool]) CoalitionMask[source]¶
Build a mask from any boolean sequence.
- class nlp_shap.ConversationSnapshot(turns: tuple[Turn, ...], snapshot_id: str)[source]¶
Bases:
objectFrozen conversation state used as the explainability input.
- classmethod from_turns(turns: tuple[Turn, ...]) ConversationSnapshot[source]¶
Build a snapshot with a stable content-derived identifier.
- class nlp_shap.CooperativeGame(player_set: PlayerSet, empty_value: float = 0.0)[source]¶
Bases:
objectPlayer set and reference values for a characteristic function.
- class nlp_shap.Estimand(*values)[source]¶
Bases:
StrEnumSupported cooperative-game value formulations.
- SHAPLEY = 'shapley'¶
Shapley value with coalition weights k!(n-k-1)!/n!.
- BANZHAF = 'banzhaf'¶
Banzhaf index with uniform coalition weights 1/2^(n-1).
- class nlp_shap.ExactEstimator[source]¶
Bases:
objectEnumerate all coalitions and delegate attribution to an estimand plugin.
- static num_coalitions(num_players: int) int[source]¶
Return the number of coalitions evaluated by exact enumeration.
- static iter_mask_ints(num_players: int) Iterator[int][source]¶
Lazily yield coalition bitmasks except the all-present coalition.
- static mask_int_to_present(mask_int: int, num_players: int) tuple[bool, ...][source]¶
Decode a coalition bitmask into presence flags.
- bind_snapshot(snapshot: ConversationSnapshot) None[source]¶
Attach the conversation snapshot under explanation.
- sample_masks(player_set: PlayerSet, budget_fraction: float, include_minimal_masks: bool, seed: int) Iterator[CoalitionMask][source]¶
Yield every coalition mask except the grand coalition.
- static iter_masks(player_set: PlayerSet) Iterator[CoalitionMask][source]¶
Lazily yield coalition masks except the all-present coalition.
- estimate_attributions(masks: Sequence[CoalitionMask], payoffs: Sequence[float], aggregator: EstimandAggregator) list[float][source]¶
Aggregate coalition payoffs with the selected estimand plugin.
- class nlp_shap.ExplainConfig(*, backend: BackendConfig, generation: GenerationConfig = GenerationConfig(max_new_tokens=128, temperature=0.0, top_k=1, precompute_base=True), explanation: ExplanationConfig = ExplanationConfig(use_v2=True, estimand=<Estimand.SHAPLEY: 'shapley'>, estimator='neyman_cc', value_fn='tfidf_cosine', normalizer='identity', players='tokens', absence_policy='delete', budget=BudgetConfig(fraction=1.0), include_minimal_masks=False, max_inflight=2, archive=ArchiveConfig(path='./runs/{run_id}', flush_every=50), dedup=DedupConfig(enabled='auto'), kv_cache=KvCacheConfig(enabled=True), embedding_mode=<EmbeddingMode.STATIC: 'static'>, seed=42))[source]¶
Bases:
BaseModelTop-level explain pipeline configuration.
- model_config = {'extra': 'forbid', 'frozen': True}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- backend: BackendConfig¶
Backend selection and connection parameters.
- generation: GenerationConfig¶
Generation parameters for base and coalition evaluations.
- explanation: ExplanationConfig¶
Explanation algorithm and runtime controls.
- class nlp_shap.ExplainResult(estimand: Estimand, values: tuple[float, ...])[source]¶
Bases:
objectAttribution output for a single explain run.
- class nlp_shap.Message(role: Role, text: str)[source]¶
Bases:
objectA single explainable text unit with an attached role.
- class nlp_shap.PlayerSet(player_ids: tuple[str, ...])[source]¶
Bases:
objectOrdered explainability players aligned with coalition masks.
- class nlp_shap.PluginGroup(*values)[source]¶
Bases:
StrEnumEntry-point groups registered in packaging metadata.
- ESTIMATORS = 'nlp_shap.estimators'¶
Coalition-sampling estimator plugins.
- ESTIMANDS = 'nlp_shap.estimands'¶
Coalition payoff aggregation plugins.
- VALUE_FNS = 'nlp_shap.value_fns'¶
Utility scoring plugins for coalition outputs.
- BACKENDS = 'nlp_shap.backends'¶
Model inference backend plugins.
- PARTITIONS = 'nlp_shap.partitions'¶
Player-partitioning plugins for conversation snapshots.
- ABSENCE_POLICIES = 'nlp_shap.absence_policies'¶
Absence-policy plugins for rendering masked snapshots.
- class nlp_shap.PluginRegistry[source]¶
Bases:
objectRegister and resolve pipeline plugins by group and name.
- register(group: PluginGroup | str, name: str, factory: Callable[[], object]) None[source]¶
Register a plugin factory under
groupandname.
- resolve(group: PluginGroup | str, name: str) object[source]¶
Instantiate a plugin registered under
groupandname.
- names(group: PluginGroup | str) tuple[str, ...][source]¶
Return sorted plugin names registered for
group.
- load_entry_points(group: PluginGroup | str) None[source]¶
Discover and register plugins from a packaging entry-point group.
- class nlp_shap.Role(*values)[source]¶
Bases:
StrEnumParticipant role attached to a message or token.
- USER = 'user'¶
End-user or human-provided input.
- ASSISTANT = 'assistant'¶
Model-generated output.
- SYSTEM = 'system'¶
System-level instruction or steering text.
- class nlp_shap.RunManifest(estimand: Estimand, run_id: str)[source]¶
Bases:
objectTop-level metadata persisted alongside a run archive.
- to_dict() RunManifestPayload[source]¶
Serialize manifest fields to JSON-compatible primitives.
- class nlp_shap.ShapleyAggregator[source]¶
Bases:
objectAggregate coalition samples with Shapley coalition weights.
- class nlp_shap.Turn(messages: tuple[Message, ...])[source]¶
Bases:
objectOne conversational turn composed of ordered messages.
- nlp_shap.explain_config_from_yaml(text: str) ExplainConfig[source]¶
Parse YAML text into a validated
ExplainConfig.
- nlp_shap.explain_config_to_yaml(config: ExplainConfig) str[source]¶
Serialize a config to YAML with stable key ordering within sections.
- nlp_shap.parse_manifest(payload: RunManifestPayload) RunManifest[source]¶
Parse a typed manifest payload into a run manifest.