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", "name": "fabelous-autocoder",
"displayName": "Fabelous Autocoder", "displayName": "Fabelous Autocoder",
"description": "A simple to use Ollama autocompletion engine with options exposed and streaming functionality", "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", "icon": "icon.png",
"publisher": "fabel", "publisher": "fabel",
"license": "CC BY-ND 4.0", "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 { 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) { async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationToken?: vscode.CancellationToken) {
const document = textEditor.document; const document = textEditor.document;
const position = textEditor.selection.active; const position = textEditor.selection.active;
@ -89,7 +121,7 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
const response = await axios.post(apiEndpoint, { const response = await axios.post(apiEndpoint, {
model: apiModel, model: apiModel,
prompt: fimPrompt, prompt: fimPrompt,
stream: true, stream: false,
raw: true, raw: true,
options: { options: {
num_predict: numPredict, num_predict: numPredict,
@ -98,59 +130,30 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
} }
}, { }, {
cancelToken: axiosCancelToken, cancelToken: axiosCancelToken,
responseType: 'stream',
headers: { headers: {
'Authorization': apiAuthentication 'Authorization': apiAuthentication
} }
}); });
let currentPosition = position; progress.report({ message: "Generating..." });
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;
}
completionText += completion; let completionText = response.data.response;
// Remove any FIM tags and leading/trailing whitespace
const edit = new vscode.WorkspaceEdit(); completionText = completionText.replace(/<fim_middle>|<fim_suffix>|<fim_prefix>/g, '').trim();
edit.insert(document.uri, currentPosition, completion);
await vscode.workspace.applyEdit(edit);
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) }); completionText = removeMatchingPrefix(completionText);
textEditor.selection = newSelection;
});
const finished = new Promise((resolve) => { // Apply the edit
response.data.on('end', () => { const edit = new vscode.WorkspaceEdit();
progress.report({ message: "Fabelous completion finished." }); edit.insert(document.uri, position, completionText);
resolve(true); await vscode.workspace.applyEdit(edit);
});
axiosCancelToken.promise.finally(() => {
resolve(false);
});
});
await finished;
completionText = completionText.replace(/<fim_middle>|<fim_suffix>|<fim_prefix>/g, ''); // Move the cursor to the end of the inserted text
const finalEdit = new vscode.WorkspaceEdit(); const newPosition = position.translate(0, completionText.length);
finalEdit.replace(document.uri, new vscode.Range(position, currentPosition), completionText); textEditor.selection = new vscode.Selection(newPosition, newPosition);
await vscode.workspace.applyEdit(finalEdit);
progress.report({ message: "Fabelous completion finished." });
} catch (err: any) { } catch (err: any) {
vscode.window.showErrorMessage( vscode.window.showErrorMessage(
@ -199,9 +202,16 @@ async function provideCompletionItems(document: vscode.TextDocument, position: v
}); });
if (response_preview.data.response.trim() !== "") { if (response_preview.data.response.trim() !== "") {
const previewText = response_preview.data.response.replace(/<fim_middle>|<fim_suffix>|<fim_prefix>/g, '').trimStart(); let previewText = response_preview.data.response.replace(/<fim_middle>|<fim_suffix>|<fim_prefix>/g, '').trimStart();
item.label = previewText;
item.insertText = previewText; // 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) { } catch (error) {
console.error("Error fetching preview:", error); console.error("Error fetching preview:", error);
@ -240,4 +250,4 @@ function deactivate() { }
module.exports = { module.exports = {
activate, activate,
deactivate, deactivate,
}; };