From 3934e8d81facccc42a44932311a4f36906aec8df Mon Sep 17 00:00:00 2001 From: Fred Bricon Date: Thu, 21 Nov 2024 14:48:52 +0100 Subject: [PATCH] Tweak Granite template and postprocessing Signed-off-by: Fred Bricon --- .../filtering/streamTransforms/lineStream.ts | 1 + core/autocomplete/postprocessing/index.ts | 19 ++++++++++++++++++- .../templating/AutocompleteTemplate.ts | 3 ++- core/llm/index.ts | 8 ++++---- 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/core/autocomplete/filtering/streamTransforms/lineStream.ts b/core/autocomplete/filtering/streamTransforms/lineStream.ts index 160fc9d85..c204783ad 100644 --- a/core/autocomplete/filtering/streamTransforms/lineStream.ts +++ b/core/autocomplete/filtering/streamTransforms/lineStream.ts @@ -83,6 +83,7 @@ export const LINES_TO_REMOVE_BEFORE_START = [ "", "[CODE]", "", + "{{FILL_HERE}}" ]; export const ENGLISH_START_PHRASES = [ diff --git a/core/autocomplete/postprocessing/index.ts b/core/autocomplete/postprocessing/index.ts index b80da6b50..3d97df9ca 100644 --- a/core/autocomplete/postprocessing/index.ts +++ b/core/autocomplete/postprocessing/index.ts @@ -83,6 +83,24 @@ export function postprocessCompletion({ } } + if (llm.model.includes("granite")) { + // Granite tends to repeat the start of the line in the completion output + let prefixEnd = prefix.split("\n").pop(); + if (prefixEnd) { + if (completion.startsWith(prefixEnd)) { + completion = completion.slice(prefixEnd.length); + } else { + const trimmedPrefix = prefixEnd.trim(); + const lastWord = trimmedPrefix.split(/\s+/).pop(); + if (lastWord && completion.startsWith(lastWord)) { + completion = completion.slice(lastWord.length); + } else if (completion.startsWith(trimmedPrefix)) { + completion = completion.slice(trimmedPrefix.length); + } + } + } + } + // // If completion starts with multiple whitespaces, but the cursor is at the end of the line // // then it should probably be on a new line // if ( @@ -99,7 +117,6 @@ export function postprocessCompletion({ prefix.endsWith(" ") && completion.startsWith(" ") ) { - const test = prefix.split("\n").pop()?.trim() !== ""; completion = completion.slice(1); } diff --git a/core/autocomplete/templating/AutocompleteTemplate.ts b/core/autocomplete/templating/AutocompleteTemplate.ts index ae8cf1487..c64eca26a 100644 --- a/core/autocomplete/templating/AutocompleteTemplate.ts +++ b/core/autocomplete/templating/AutocompleteTemplate.ts @@ -376,7 +376,8 @@ export function getTemplateForModel(model: string): AutocompleteTemplate { if ( lowerCaseModel.includes("gpt") || lowerCaseModel.includes("davinci-002") || - lowerCaseModel.includes("claude") + lowerCaseModel.includes("claude") || + lowerCaseModel.includes("granite3") ) { return holeFillerTemplate; } diff --git a/core/llm/index.ts b/core/llm/index.ts index 1156120a6..8759af8a5 100644 --- a/core/llm/index.ts +++ b/core/llm/index.ts @@ -469,7 +469,7 @@ export abstract class BaseLLM implements ILLM { ); if (log && this.writeLog) { - await this.writeLog(`Completion:\n\n${completion}\n\n`); + await this.writeLog(`Completion:\n${completion}\n\n`); } return { @@ -521,7 +521,7 @@ export abstract class BaseLLM implements ILLM { this._logTokensGenerated(completionOptions.model, prompt, completion); if (log && this.writeLog) { - await this.writeLog(`Completion:\n\n${completion}\n\n`); + await this.writeLog(`Completion:\n${completion}\n\n`); } } @@ -562,7 +562,7 @@ export abstract class BaseLLM implements ILLM { this._logTokensGenerated(completionOptions.model, prompt, completion); if (log && this.writeLog) { - await this.writeLog(`Completion:\n\n${completion}\n\n`); + await this.writeLog(`Completion:\n${completion}\n\n`); } return completion; @@ -628,7 +628,7 @@ export abstract class BaseLLM implements ILLM { this._logTokensGenerated(completionOptions.model, prompt, completion); if (log && this.writeLog) { - await this.writeLog(`Completion:\n\n${completion}\n\n`); + await this.writeLog(`Completion:\n${completion}\n\n`); } return {