Skip to content

Commit c3daede

Browse files
committed
Fix for new llama.cpp API response body
1 parent c50fa87 commit c3daede

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

mikupad.html

+34-6
Original file line numberDiff line numberDiff line change
@@ -1738,26 +1738,54 @@
17381738
throw new Error(`HTTP ${res.status}`);
17391739
if (options.stream) {
17401740
for await (const chunk of parseEventStream(res.body)) {
1741-
const probs = chunk.completion_probabilities?.[0]?.probs ?? [];
1741+
const choice = chunk.completion_probabilities?.[0];
1742+
1743+
let probs = [];
1744+
if (choice?.probs) {
1745+
probs = choice.probs ?? [];
1746+
} else if (choice?.top_logprobs) {
1747+
probs = Object.values(choice.top_logprobs).map(({ token, logprob }) => ({
1748+
tok_str: token,
1749+
prob: Math.exp(logprob)
1750+
}));
1751+
}
17421752
const prob = probs.find(p => p.tok_str === chunk.content)?.prob;
1753+
17431754
yield {
17441755
content: chunk.content,
17451756
...(probs.length > 0 ? {
17461757
prob: prob ?? -1,
1747-
completion_probabilities: chunk.completion_probabilities
1758+
completion_probabilities: [{
1759+
content: chunk.content,
1760+
probs
1761+
}]
17481762
} : {})
17491763
};
17501764
}
17511765
} else {
17521766
const { completion_probabilities } = await res.json();
17531767
for (const chunk of completion_probabilities) {
1754-
const probs = chunk.probs ?? [];
1755-
const prob = probs.find(p => p.tok_str === chunk.content)?.prob;
1768+
const token = chunk.content ? chunk.content : chunk.token;
1769+
1770+
let probs = [];
1771+
if (chunk.probs) {
1772+
probs = chunk.probs ?? [];
1773+
} else if (chunk.top_logprobs) {
1774+
probs = Object.values(chunk.top_logprobs).map(({ token, logprob }) => ({
1775+
tok_str: token,
1776+
prob: Math.exp(logprob)
1777+
}));
1778+
}
1779+
const prob = probs.find(p => p.tok_str === token)?.prob;
1780+
17561781
yield {
1757-
content: chunk.content,
1782+
content: token,
17581783
...(probs.length > 0 ? {
17591784
prob: prob ?? -1,
1760-
completion_probabilities: [chunk]
1785+
completion_probabilities: [{
1786+
content: token,
1787+
probs
1788+
}]
17611789
} : {})
17621790
};
17631791
}

0 commit comments

Comments
 (0)