new Version working with first good LM Iteration

This commit is contained in:
Falko Victor Habel 2024-08-19 13:51:41 +02:00
parent 1a98cfe1e1
commit f0c5bb0685
2 changed files with 60 additions and 50 deletions

View File

@ -2,7 +2,7 @@
"name": "fabelous-autocoder",
"displayName": "Fabelous Autocoder",
"description": "A simple to use Ollama autocompletion engine with options exposed and streaming functionality",
"version": "0.1.0",
"version": "0.1.69",
"icon": "icon.png",
"publisher": "fabel",
"license": "CC BY-ND 4.0",

View File

@ -51,9 +51,41 @@ function getContextLines(document: vscode.TextDocument, position: vscode.Positio
}
function createFIMPrompt(prefix: string, suffix: string, language: string): string {
return `<fim_prefix>${prefix}<fim_suffix>${suffix}<fim_middle>\`\`\`${language}\n`;
return `<fim_prefix>${prefix}<fim_middle><fim_suffix>${suffix}\n\`\`\`${language}\n`;
}
function removeMatchingPrefix(generatedContent: string): string {
const functionHeaderPatterns = [
// Java, C#, TypeScript, JavaScript
/^(public|private|protected)?\s*(static\s+)?(async\s+)?\w+(\s*<[^>]+>)?\s+\w+\s*\([^)]*\)\s*{/m,
// Python
/^def\s+\w+\s*\([^)]*\):/m,
// C++, C
/^(\w+\s+)*\w+\s+\w+\s*\([^)]*\)\s*{/m,
// Go
/^func\s+\w+\s*\([^)]*\)(\s+\w+)?\s*{/m,
// Rust
/^(pub\s+)?(fn|async fn)\s+\w+\s*(<[^>]+>)?\s*\([^)]*\)(\s*->\s*\w+)?\s*{/m
];
const lines = generatedContent.split('\n');
const firstNonEmptyLineIndex = lines.findIndex(line => line.trim() !== '');
if (firstNonEmptyLineIndex === -1) {
return '\n' + generatedContent;
}
const firstLine = lines[firstNonEmptyLineIndex];
for (const pattern of functionHeaderPatterns) {
if (pattern.test(firstLine)) {
lines.splice(firstNonEmptyLineIndex, 1);
return '\n' + lines.join('\n');
}
}
return '\n' + generatedContent;
}
async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationToken?: vscode.CancellationToken) {
const document = textEditor.document;
const position = textEditor.selection.active;
@ -89,7 +121,7 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
const response = await axios.post(apiEndpoint, {
model: apiModel,
prompt: fimPrompt,
stream: true,
stream: false,
raw: true,
options: {
num_predict: numPredict,
@ -98,59 +130,30 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
}
}, {
cancelToken: axiosCancelToken,
responseType: 'stream',
headers: {
'Authorization': apiAuthentication
}
});
let currentPosition = position;
let completionText = "";
response.data.on('data', async (d: Uint8Array) => {
progress.report({ message: "Generating..." });
if (currentPosition.line !== textEditor.selection.end.line || currentPosition.character !== textEditor.selection.end.character) {
axiosCancelPost();
return;
}
const completion: string = JSON.parse(d.toString()).response;
if (completion === "") {
return;
}
progress.report({ message: "Generating..." });
completionText += completion;
const edit = new vscode.WorkspaceEdit();
edit.insert(document.uri, currentPosition, completion);
await vscode.workspace.applyEdit(edit);
let completionText = response.data.response;
// Remove any FIM tags and leading/trailing whitespace
completionText = completionText.replace(/<fim_middle>|<fim_suffix>|<fim_prefix>/g, '').trim();
const completionLines = completion.split("\n");
const newPosition = new vscode.Position(
currentPosition.line + completionLines.length - 1,
(completionLines.length > 1 ? 0 : currentPosition.character) + completionLines[completionLines.length - 1].length
);
const newSelection = new vscode.Selection(position, newPosition);
currentPosition = newPosition;
progress.report({ message: "Generating...", increment: 1 / (numPredict / 100) });
textEditor.selection = newSelection;
});
completionText = removeMatchingPrefix(completionText);
const finished = new Promise((resolve) => {
response.data.on('end', () => {
progress.report({ message: "Fabelous completion finished." });
resolve(true);
});
axiosCancelToken.promise.finally(() => {
resolve(false);
});
});
await finished;
// Apply the edit
const edit = new vscode.WorkspaceEdit();
edit.insert(document.uri, position, completionText);
await vscode.workspace.applyEdit(edit);
completionText = completionText.replace(/<fim_middle>|<fim_suffix>|<fim_prefix>/g, '');
const finalEdit = new vscode.WorkspaceEdit();
finalEdit.replace(document.uri, new vscode.Range(position, currentPosition), completionText);
await vscode.workspace.applyEdit(finalEdit);
// Move the cursor to the end of the inserted text
const newPosition = position.translate(0, completionText.length);
textEditor.selection = new vscode.Selection(newPosition, newPosition);
progress.report({ message: "Fabelous completion finished." });
} catch (err: any) {
vscode.window.showErrorMessage(
@ -199,9 +202,16 @@ async function provideCompletionItems(document: vscode.TextDocument, position: v
});
if (response_preview.data.response.trim() !== "") {
const previewText = response_preview.data.response.replace(/<fim_middle>|<fim_suffix>|<fim_prefix>/g, '').trimStart();
item.label = previewText;
item.insertText = previewText;
let previewText = response_preview.data.response.replace(/<fim_middle>|<fim_suffix>|<fim_prefix>/g, '').trimStart();
// Remove matching prefix and repeated function header
const existingContent = document.getText();
previewText = removeMatchingPrefix(existingContent);
if (previewText.trim() !== "") {
item.label = previewText;
item.insertText = previewText;
}
}
} catch (error) {
console.error("Error fetching preview:", error);
@ -240,4 +250,4 @@ function deactivate() { }
module.exports = {
activate,
deactivate,
};
};