Skip to content

Latest commit

 

History

13 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🎲 Log Probability Explorer

An interactive Streamlit app that visualises token-level log probabilities from OpenAI, Azure OpenAI (Azure AI Foundry), or GitHub Models.
Built as an educational tool to help explain how Large Language Models generate text.

Prerequisites

What It Does

  1. Explains context-window usage — shows how much of the selected model's context window your prompt consumes, plus remaining headroom for output.
  2. Tokenises your prompt locally with tiktoken — supports OpenAI model tokenizers and shows either token text or token IDs.
  3. Sends the prompt to an OpenAI, Azure OpenAI, or GitHub Models chat model with logprobs enabled.
  4. Displays the response token-by-token with:
    • Colour-coded confidence badges (green → red)
    • A sortable data table with log probs, percentages, and top alternatives
    • An interactive Plotly bar chart of probabilities across the full response
    • Per-token expanders showing what other tokens the model considered

Quick Start

1. Clone & install

pip install -r requirements.txt

2. Configure .env

Create (or edit) a .env file in the project root. Choose the section that matches your provider:

Option A — OpenAI

PROVIDER=openai
OPENAI_API_KEY=sk-...your-openai-api-key...
LOGPROB_MODEL=gpt-4o          # any model that supports logprobs

Option B — Azure OpenAI

PROVIDER=azure
OPENAI_API_KEY=<your-azure-openai-key>
AZURE_OPENAI_ENDPOINT=https://<your-resource>.cognitiveservices.azure.com
API_VERSION=2025-04-01-preview
LOGPROB_MODEL=gpt-5.2         # must match your Azure deployment name

Option C — GitHub Models

PROVIDER=github
GITHUB_TOKEN=ghp_...your-github-pat...
LOGPROB_MODEL=gpt-4o          # any model available on GitHub Models

To create a PAT, visit github.com/settings/tokens and generate a token. If GITHUB_TOKEN is not set, the app falls back to OPENAI_API_KEY.

Notes:

  • PROVIDER accepts azure, openai, or github. If omitted, the legacy USE_AZURE toggle is used for backward compatibility.
  • For Azure, the endpoint should be the base URL of your Azure Cognitive Services resource — the SDK builds the full Chat Completions path automatically.
  • For OpenAI / GitHub Models, LOGPROB_MODEL should be a model ID (e.g. gpt-4o, gpt-4o-mini). For Azure, it should match the deployment name in your Azure resource.
  • GitHub Models uses the endpoint https://models.inference.ai.azure.com which is handled automatically.

3. Run

python -m streamlit run logprob_demo.py

The app opens in your browser at http://localhost:8501.

Understanding the Output

Tokenizer / Context Window

The Tokenizer tab is focused on prompt size and context-window planning:

  • The large context bar shows prompt tokens / context window for the selected OpenAI model.
  • The vertical marker reserves the model's max-output budget.
  • Free in context is total remaining room for prompt text, retrieved documents, tool calls, and generated output.
  • Headroom for output is the remaining safe prompt budget after reserving max output.
  • The token stream can display either readable token text or numeric token IDs. The full stream is optional for long prompts.

Logprob Confidence

Colour Probability Meaning
🟩 Green > 90 % Model was very confident
🟨 Yellow 50 – 90 % Fairly confident
🟧 Orange 10 – 50 % Multiple plausible options
🟥 Red < 10 % Low confidence / surprising choice

Log Probabilities — Quick Primer

A log probability is the natural logarithm of the model's predicted probability for a token:

probability = e^(logprob)
  • logprob = 0.00 → 100 % confidence
  • logprob = −0.69 → ~50 %
  • logprob = −2.30 → ~10 %

The closer to zero, the more certain the model was.

Project Structure

llm-logprob-demo/
├── .env                 # API credentials (not committed)
├── logprob_demo.py      # Streamlit application
├── requirements.txt     # Python dependencies
└── README.md            # This file

Tech Stack

Screenshots

Screenshot 1

Screenshot 2

Screenshot 3

About

Interactive Streamlit app that visualises token-level log probabilities from Azure OpenAI

Topics

Resources

Security policy

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages