Skip to content

feat: add logprobs and top_logprobs parameters to Responses API #1573

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions examples/responses/logprobs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env -S npm run tsn -T

import OpenAI from 'openai';

const openai = new OpenAI();

async function main() {
const result = await openai.responses
.create({
model: 'gpt-4o-2024-08-06',
input: 'solve 8x + 31 = 2',
include: ['message.output_text.logprobs'],
top_logprobs: 20,
});

for(const output of result.output) {
if(output.type === 'message') {
const logprobs = output.content.filter(content => content.type === 'output_text')
for(const logprob of logprobs) {
if(logprob.type === 'output_text') {

// Top Logprobs
console.log(logprob.logprobs?.[0]?.top_logprobs);

// Token
console.log(logprob.logprobs?.[0]?.token);

// Token Logprobs
console.log(logprob.logprobs?.[0]?.logprob);

// Bytes
console.log(logprob.logprobs?.[0]?.bytes);
}
}
}
}
}
main();
16 changes: 16 additions & 0 deletions examples/responses/non-stream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env -S npm run tsn -T

import OpenAI from 'openai';

const openai = new OpenAI();

async function main() {
const result = await openai.responses
.create({
model: 'gpt-4o-2024-08-06',
input: 'solve 8x + 31 = 2',
});

console.log(result.output_text);
}
main();
8 changes: 7 additions & 1 deletion src/resources/responses/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1946,7 +1946,8 @@ export type ResponseIncludable =
| 'message.input_image.image_url'
| 'computer_call_output.output.image_url'
| 'reasoning.encrypted_content'
| 'code_interpreter_call.outputs';
| 'code_interpreter_call.outputs'
| 'message.output_text.logprobs';

/**
* An event that is emitted when a response finishes as incomplete.
Expand Down Expand Up @@ -4688,6 +4689,11 @@ export interface ResponseCreateParamsBase {
*/
include?: Array<ResponseIncludable> | null;

/**
* The number of top logprobs to include in the response.
*/
top_logprobs?: number | null;

/**
* Text, image, or file inputs to the model, used to generate a response.
*
Expand Down