Skip to main content

Explain Large Language Model Behavior Patterns

Project description

StringSight logo

Extract, cluster, and analyze behavioral properties from Generative Models

Python 3.10+ MIT License Docs Blog Website

Annoyed at having to look through your long model conversations or agentic traces? Fear not, StringSight has come to ease your woes. Understand and compare model behavior by automatically extracting behavioral properties from their responses, grouping similar behaviors together, and quantifying how important these behaviors are.

https://github.com/user-attachments/assets/200d3312-0805-43f4-8ce9-401544f03db2

Installation & Quick Start

# Install
pip install stringsight

# Set your API keys
export OPENAI_API_KEY="your-openai-key"
export ANTHROPIC_API_KEY="your-anthropic-key"  # optional
export GOOGLE_API_KEY="your-google-key"        # optional

# Launch the web interface
stringsight launch 

# Or run in background (survives terminal disconnects)
stringsight launch --daemon

# Check status
stringsight status

# View logs
stringsight logs

# Stop the server
stringsight stop

The UI will be available at http://localhost:5180.

For tutorials and examples, see starter_notebook.ipynb or Google Colab.

Install from source

Use this if you want the latest code, plan to modify StringSight, or want an editable install.

# Clone (includes submodules, e.g. the frontend)
git clone --recurse-submodules https://github.com/lisadunlap/stringsight.git
cd stringsight

# If you already cloned without submodules:
# git submodule update --init --recursive

# Create and activate a virtual environment (example: venv)
python -m venv .venv
source .venv/bin/activate

# Install in editable mode
pip install -U pip
pip install -e .

Deployment Options

Local Development:

stringsight launch              # Foreground mode (stops when terminal closes)
stringsight launch --daemon     # Background mode (persistent)

Docker (for production or multi-user setups):

docker compose up -d

The Docker setup includes PostgreSQL, Redis, MinIO storage, and Celery workers for handling long-running jobs.

Usage

Data Format

Required columns:

  • prompt: Question/prompt text (this doesn't need to be your actual prompt, just some unique identifier of a run)
  • model: Model name
  • model_response: Model output in one of three formats:
    • OpenAI conversation format: [{"role": "user", "content": "..."}, {"role": "assistant", "content": "..."}] (recommended, we also support multimodal inputs in this format)
    • Plain string: "Model response text..."
    • Custom format: Any other format will be converted to string and thus will not render all pretty in the ui (if you care about that sort of thing)

Optional columns:

  • Scores: You can provide metrics in separate columns (e.g. "accuracy", "helpfulness", etc.—set them using the score_columns parameter) or as a single score column containing a dictionary like {"accuracy": 0.85, "helpfulness": 4.2}.
  • question_id: Unique ID for a question (useful if you have multiple responses for the same prompt, especially for side-by-side pairing)
  • Custom column names via prompt_column, model_column, model_response_column, question_id_column parameters

For side-by-side: Use model_a, model_b, model_a_response, model_b_response (pre-paired data) or pass tidy data with method="side_by_side" to auto-pair by prompt.

Extract and Cluster Properties

import pandas as pd
from stringsight import explain

# Prepare your data
df = pd.DataFrame({
    "prompt": ["What is machine learning?", "Explain quantum computing", ...],
    "model": ["gpt-4", "claude-3", ...],
    "model_response": [
        [{"role": "user", "content": "What is machine learning?"},
         {"role": "assistant", "content": "Machine learning involves..."}, ...],
        [{"role": "user", "content": "Explain quantum computing"},
         {"role": "assistant", "content": "Quantum computing uses..."}, ...]
    ],
    "accuracy": [1, 0, ...], 
    "helpfulness": [4.2, 3.8, ...]
})

# Run analysis
clustered_df, model_stats = explain(
    df,
    model_name="gpt-4.1-mini",
    output_dir="results/test",
    score_columns=["accuracy", "helpfulness"],
    model_response_column="model_response" # it default checks for a model_response column
)

Side-by-Side Comparison

# Compare two models
clustered_df, model_stats = explain(
    df,
    method="side_by_side",
    model_a="gpt-4",
    model_b="claude-3",
    output_dir="results/comparison",
    score_columns=["accuracy", "helpfulness"],
)

Fixed Taxonomy Labeling

from stringsight import label

TAXONOMY = {
    "refusal": "Does the model refuse to follow certain instructions?",
    "hallucination": "Does the model generate false information?",
}

clustered_df, model_stats = label(
    df,
    taxonomy=TAXONOMY,
    output_dir="results/labeled"
)

Extract-Only (No Clustering)

If you only want extracted properties (extraction → JSON parsing → validation) without clustering/metrics:

from stringsight import extract_properties_only

dataset = extract_properties_only(
    df,
    method="single_model",
    model_name="gpt-4.1-mini",
    output_dir="results/extract_only",
    # If True (default), StringSight raises if 0 properties remain after validation.
    # If False, it returns an empty list of properties instead.
    fail_on_empty_properties=False,
)

Output

Output dataframe columns:

  • property_description: Natural language description of behavioral trait
  • category: High-level grouping (e.g., "Reasoning", "Style", "Safety")
  • reason: Why this behavior occurs
  • evidence: Specific quotes demonstrating the behavior
  • unexpected_behavior: Boolean indicating if this is problematic
  • type: Nature of the property (e.g., "content", "format", "style")
  • behavior_type: Classification like "Positive", "Negative (critical)", "Style"
  • cluster_id: Cluster assignment
  • cluster_label: Human-readable cluster name

Output files (when output_dir is specified):

  • clustered_results.parquet: Main dataframe with cluster assignments
  • clustered_results.jsonl / clustered_results_lightweight.jsonl: JSON formats
  • full_dataset.json / full_dataset.parquet: Complete PropertyDataset
  • model_cluster_scores.json: Per model-cluster metrics
  • cluster_scores.json: Aggregated metrics per cluster
  • model_scores.json: Overall metrics per model
  • summary.txt: Human-readable summary

Metrics in model_stats:

The model_stats dictionary contains three DataFrames:

  1. model_cluster_scores: How each model performs on each behavioral cluster
  2. cluster_scores: Aggregated metrics across all models for each cluster
  3. model_scores: Overall metrics for each model across all clusters

Configuration

explain(
    df,
    model_name="gpt-4.1-mini",                      # LLM for extraction
    embedding_model="text-embedding-3-large",       # Embedding model
    min_cluster_size=5,                             # Min cluster size
    sample_size=100,                                # Sample before processing
    output_dir="results/"
)

Advanced Features

See the documentation for:

  • Docker deployment
  • Custom column mapping
  • Multimodal conversations (text + images)
  • Prompt expansion
  • Caching configuration
  • CLI usage

Documentation

Contributing

PRs very welcome, especially if I forgot to include something important in the readme. Questions or issues? Open an issue on GitHub

Tests

Some lightweight development tests live in tests/ and can be run directly, e.g.:

python tests/test_prompts_metadata_unit.py
python tests/test_airline_demo_prompt_generation.py

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

stringsight-0.3.7.tar.gz (3.9 MB view details)

Uploaded Source

Built Distribution

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

stringsight-0.3.7-py3-none-any.whl (3.9 MB view details)

Uploaded Python 3

File details

Details for the file stringsight-0.3.7.tar.gz.

File metadata

  • Download URL: stringsight-0.3.7.tar.gz
  • Upload date:
  • Size: 3.9 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for stringsight-0.3.7.tar.gz
Algorithm Hash digest
SHA256 b5cddf11a79577cf12a2b634ff680f9e954e5467f2abd772dec49e279bbcc4d8
MD5 590cec93ede1a21d21e200cdd9fbbc57
BLAKE2b-256 341d6f73aeae6b4854ca45f3a08d0b48d296aa0660a92d935d5bd4d90ca25e5c

See more details on using hashes here.

File details

Details for the file stringsight-0.3.7-py3-none-any.whl.

File metadata

  • Download URL: stringsight-0.3.7-py3-none-any.whl
  • Upload date:
  • Size: 3.9 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for stringsight-0.3.7-py3-none-any.whl
Algorithm Hash digest
SHA256 22f7be6f9f6c2bbb5c9ea7a620aea84e1864340e0380e2b0293c8c23ab936965
MD5 d81974245412a77a47c7c899b00f5fa9
BLAKE2b-256 c44f2be24a2634a58bfc6f1825113d333ac5a58ddca8754b431410a93de118a0

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