Skip to content

Commit

Permalink
feat: strip incomplete sentences if we're restricting token output
Browse files Browse the repository at this point in the history
  • Loading branch information
Fortyseven committed May 25, 2024
1 parent e53b23b commit aa6007a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/lib/api/api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { get, writable } from 'svelte/store';

import { convertBlobUrlToBase64 } from '$lib/utils';
import { convertBlobUrlToBase64, stripIncompleteSentence } from '$lib/utils';

import {
models,
Expand Down Expand Up @@ -223,7 +223,15 @@ export async function OL_chat(

responseInProgress.set(false);

return get(pendingResponse);
let pending = get(pendingResponse);

if (get(chatState).values.num_predict > -1) {
// strip incomplete sentence from end of last message if we're restricting
// the number of tokens returned
pending.content = stripIncompleteSentence(pending.content);
}

return pending;
} catch (err) {
if (err.name !== 'AbortError') {
console.error('OL_chat error: ', err);
Expand Down
22 changes: 22 additions & 0 deletions src/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,25 @@ export async function convertBlobUrlToBase64(blobUrl) {
xhr.send();
});
}

export function stripIncompleteSentence(text) {
const reSentences = /[\w\n\r\*\?\.\!\"].*/gm;

let sentences = [];
let m;

while ((m = reSentences.exec(text)) !== null) {
if (m.index === reSentences.lastIndex) {
reSentences.lastIndex++;
}

m.forEach((m, gr) => {
console.log('m', m, 'gr', gr);
sentences.push(m);
});
}

sentences = sentences.filter((sent) => '!?.*"\'\n'.includes(sent.at(-1)));

return sentences.join(' ');
}

0 comments on commit aa6007a

Please sign in to comment.