Skip to content

Instantly share code, notes, and snippets.

@andrewBatutin
Created December 14, 2025 19:05
Show Gist options
  • Select an option

  • Save andrewBatutin/bf3b233e769fd504905bb69959dcf75a to your computer and use it in GitHub Desktop.

Select an option

Save andrewBatutin/bf3b233e769fd504905bb69959dcf75a to your computer and use it in GitHub Desktop.
logprobs_hello_world
import math
from openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "system", "content": "Answer in 1 word"},{"role": "user", "content": "The capital of Spain, East Prussia is"}],
max_tokens=1,
logprobs=True,
top_logprobs=5 # show top 5 alternatives
)
# The sampled token
print(f"Output: {response.choices[0].message.content}")
# The distribution (roads not taken)
for token_logprob in response.choices[0].logprobs.content:
print("\nTop candidates:")
for alt in token_logprob.top_logprobs:
prob = math.exp(alt.logprob) * 100
print(f" {alt.token:15} → {prob:.1f}%")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment