import { requestJSON } from './http-client.mjs';
try {
const model = process.env.API_MODEL;
if (!model?.trim()) throw new Error('Set model.');
const result = await requestJSON('/v1/chat/completions', {
model, messages: [{ role: 'user', content: 'Reply with the word hello.' }],
temperature: 0, seed: 0, frequency_penalty: 0, presence_penalty: 0,
logprobs: true, top_logprobs: 2,
});
const choice = result.choices?.[0];
const message = choice?.message;
if (!Array.isArray(result.choices) || result.choices.length !== 1 || choice?.index !== 0
|| choice.finish_reason !== 'stop' || message?.role !== 'assistant'
|| message.refusal != null || message.tool_calls != null || message.function_call != null
|| typeof message.content !== 'string' || !message.content.trim()) throw new Error('Incomplete text.');
const tokens = choice.logprobs?.content;
if (!Array.isArray(tokens) || !tokens.length) throw new Error('Missing logprobs.');
const observations = [];
for (const token of tokens) {
if (!Array.isArray(token?.top_logprobs)) throw new Error('Missing alternatives.');
for (const value of [token, ...token.top_logprobs]) {
if (typeof value?.token !== 'string' || typeof value.logprob !== 'number'
|| !Number.isFinite(value.logprob) || value.logprob > 0
|| (value.bytes != null && (!Array.isArray(value.bytes)
|| value.bytes.some((byte) => !Number.isInteger(byte) || byte < 0 || byte > 255)))) {
throw new Error('Invalid token metadata.');
}
}
observations.push({ token: token.token, logprob: token.logprob,
top_logprobs: token.top_logprobs.map(({ token, logprob }) => ({ token, logprob })) });
}
console.log(JSON.stringify({ text: message.content, tokens: observations }));
} catch {
console.error('Logprobs example failed. Check model support, retained metadata, and completion status. No automatic retry.');
process.exitCode = 1;
}