liveBook · Manning

Log probability

As explained in Generative AI in Action

Logprobs are completion metadata containing the natural logarithms of the model probabilities assigned to candidate tokens.

Log probabilities are completion metadata containing the natural logarithms of the probabilities assigned to candidate tokens. For each generated token, the API can expose the selected token’s value and the most likely alternatives. section 3.3.4

The completion API returns log probabilities as token-aligned arrays. Set logprobs to an integer from 0 through 5 to request alternatives; the sampled token is always included, so an array can contain one additional entry. section 3.2.1 section 3.3.4

Use log probabilities to:

  • Inspect which alternative tokens the model considered when a completion is unsatisfactory. section 3.3.4
  • Assess uncertainty: similar probabilities for different tokens indicate that the next token is not clearly determined. section 3.3.4
  • Support selection among multiple completions; best_of chooses the completion with the highest log probability per token. section 3.2.3

Inspecting a dog-name completion

A request for a one-word name for a white miniature poodle can return Cotton. With logprobs=3, the response exposes token-level values and alternatives such as Casper, Coco, and Snow, making the generation easier to inspect. section 3.3.4

Listing 3.18 Output log probabilities
{
  "id": "cmpl-7giPQGlKc6c7BaWmHgOLyZqabIruw",
  "object": "text_completion",
  "created": 1690414840,
  "model": "gpt-35-turbo",
  "choices": [
    {
      "text": "\n\nCotton",
      "index": 0,
      "finish_reason": "stop",
      "logprobs": {
        "tokens": [
          "\n",
          "\n",
          "C",
          "otton"
        ],
        "token_logprobs": [
          -0.0008873215,
          -4.361666e-06,
          -1.026479,
          -0.56846446
        ],
        "top_logprobs": [
          {
            "\n": -0.0008873215,
            "\n\n": -7.660001,
            " Angel": -10.180796
          },
          {
            "\n": -4.361666e-06,
            "\n\n": -12.970553,
            "<|endoftext|>": -15.136529
          },
          {
            "C": -1.026479,
            "P": -2.255978,
            "Snow": -2.1068947
          },
          {
            "asper": -2.001854,
            "oco": -1.957575,
            "otton": -0.56846446
          }
        ],
        "text_offset": [
          54,
          55,
          56,
          57
        ]
      }
    }
  ],
  "usage": {
    "completion_tokens": 4,
    "prompt_tokens": 12,
    "total_tokens": 16
  }
}

Inspect the token sequence, selected-token log probabilities, and top alternative tokens in a completion response. section 3.3.4

Request log probabilities judiciously: they increase generated response data, API cost, and latency. Combine them with temperature when balancing creativity, determinism, and prompt quality. section 3.3.4 section 6.8

How Generative AI in Action covers it

How deep it goes
Defines natural-log token scores and demonstrates response arrays, but does not derive the underlying mathematics.
Examples
Uses a white miniature poodle naming request, Cotton’s token scores, alternatives, and best_of-generated pet-service names.
What it assumes
Assumes familiarity with completion responses and tokens while defining token probabilities and logprobs behavior incrementally.
What you can do after
Diagnose/debug with it by inspecting alternatives, tune randomness, assess uncertainty, and select higher-scoring completions.
How it connects
Connects log probability with completion APIs, prompt engineering, temperature, and multiple-completion selection.

Also explained in

Compare how 8 books explain log probability →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Generative AI in Action. The section links point to the parts of the book it draws on.