Created
December 14, 2025 19:05
-
-
Save andrewBatutin/bf3b233e769fd504905bb69959dcf75a to your computer and use it in GitHub Desktop.
logprobs_hello_world
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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