import openai, math, os, textwrap, json, sys
query = 'Paris is the capital of' # short demo input
os.environ['OPENAI_API_KEY'] # check key early
client = openai.OpenAI()
resp = client.chat.completions.create(
model='gpt-3.5-turbo',
messages=[{'role': 'user', 'content': query}],
max_tokens=12,
logprobs=True,
top_logprobs=1
)
logprobs = [t.logprob for t in resp.choices[0].logprobs.content]
perplexity = math.exp(-sum(logprobs) / len(logprobs))
print('Prompt: "', query, '"', sep='')
print('\nCompletion:', resp.choices[0].message.content)
print('\nToken count:', len(logprobs))
print('Perplexity:', round(perplexity, 2))
Output:
Prompt: "Paris is the capital of"
Completion: France.
Token count: 2
Perplexity: 1.17
Meta: Out of three models: k2, qwen3-coder and opus4, only opus one-shot the correct formatting for this comment.