Skip to content

Commit 0dbe0ab

Browse files
dgellowstainless-app[bot]
authored andcommitted
fix: chat completion streaming when enabling logprobs
1 parent c39615d commit 0dbe0ab

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

examples/chat-completions.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,18 @@ async function main() {
88
.stream({
99
model: 'mistralai/Mixtral-8x7B-Instruct-v0.1',
1010
messages: [{ role: 'user', content: 'Say this is a test' }],
11+
logprobs: 1,
1112
})
1213
.on('message', (msg) => console.log(msg))
1314
.on('content', (diff) => process.stdout.write(diff));
1415

1516
for await (const chunk of runner) {
1617
// Note: comment out the next line to print chunks as they are streamed from the API
17-
// console.log('chunk', chunk);
18+
console.log('chunk', chunk);
1819
}
1920

2021
const result = await runner.finalMessage();
21-
console.log(result);
22+
console.log({ result });
2223
}
2324

2425
main();

src/lib/ChatCompletionStream.ts

+15-9
Original file line numberDiff line numberDiff line change
@@ -157,20 +157,26 @@ export class ChatCompletionStream
157157
for (const { delta, finish_reason, index, logprobs = null, ...other } of chunk.choices) {
158158
let choice = snapshot.choices[index];
159159
if (!choice) {
160-
choice = snapshot.choices[index] = { finish_reason, index, message: {}, logprobs, ...other };
160+
choice = snapshot.choices[index] = {
161+
finish_reason,
162+
index,
163+
message: {},
164+
logprobs: { token_ids: [], token_logprobs: [], tokens: [] },
165+
...other,
166+
};
161167
}
162168

163169
if (logprobs) {
170+
console.log({ logprobs });
164171
if (!choice.logprobs) {
165-
choice.logprobs = Object.assign({}, logprobs);
166-
} else {
167-
const { content, ...rest } = logprobs;
168-
Object.assign(choice.logprobs, rest);
169-
if (content) {
170-
choice.logprobs.content ??= [];
171-
choice.logprobs.content.push(...content);
172-
}
172+
choice.logprobs = { token_ids: [], token_logprobs: [], tokens: [] };
173173
}
174+
choice.logprobs.token_ids ??= [];
175+
choice.logprobs.token_ids.push(delta.token_id ?? null);
176+
choice.logprobs.token_logprobs ??= [];
177+
choice.logprobs.token_logprobs.push(logprobs ?? null);
178+
choice.logprobs.tokens ??= [];
179+
choice.logprobs.tokens.push(delta.content ?? null);
174180
}
175181

176182
if (finish_reason) choice.finish_reason = finish_reason;

src/resources/completions.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,17 @@ export interface LogProbs {
6666
/**
6767
* List of token IDs corresponding to the logprobs
6868
*/
69-
token_ids?: Array<number>;
69+
token_ids?: Array<number | null>;
7070

7171
/**
7272
* List of token log probabilities
7373
*/
74-
token_logprobs?: Array<number>;
74+
token_logprobs?: Array<number | null>;
7575

7676
/**
7777
* List of token strings
7878
*/
79-
tokens?: Array<string>;
79+
tokens?: Array<string | null>;
8080
}
8181

8282
export interface ToolChoice {

0 commit comments

Comments
 (0)