new update to be able to replace the prompt with text from the document

This commit is contained in:
Falko Victor Habel 2024-07-04 08:46:02 +02:00
parent 71319b43ad
commit 68b7eda25c
2 changed files with 47 additions and 39 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.0.35", "version": "0.0.4",
"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,42 +51,50 @@ function messageHeaderSub(document: vscode.TextDocument) {
} }
// internal function for autocomplete, not directly exposed // internal function for autocomplete, not directly exposed
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;
// Get the current prompt
let prompt = document.getText(new vscode.Range(document.lineAt(0).range.start, position)); // Get the current prompt
prompt = prompt.substring(Math.max(0, prompt.length - promptWindowSize), prompt.length); let prompt = document.getText(new vscode.Range(document.lineAt(0).range.start, position));
// Show a progress message prompt = prompt.substring(Math.max(0, prompt.length - promptWindowSize), prompt.length);
vscode.window.withProgress(
{ // Replace {Prompt} with the extracted text from the document
location: vscode.ProgressLocation.Notification, const sub = messageHeaderSub(document).replace("{Prompt}", prompt);
title: "Ollama Autocoder",
cancellable: true, // Show a progress message
}, vscode.window.withProgress(
async (progress, progressCancellationToken) => { {
try { location: vscode.ProgressLocation.Notification,
progress.report({ message: "Starting model..." }); title: "Ollama Autocoder",
let axiosCancelPost: () => void; cancellable: true,
const axiosCancelToken = new axios.CancelToken((c) => { },
const cancelPost = function () { async (progress, progressCancellationToken) => {
c("Autocompletion request terminated by user cancel"); try {
}; progress.report({ message: "Starting model..." });
axiosCancelPost = cancelPost;
if (cancellationToken) cancellationToken.onCancellationRequested(cancelPost); let axiosCancelPost: () => void;
progressCancellationToken.onCancellationRequested(cancelPost); const axiosCancelToken = new axios.CancelToken((c) => {
vscode.workspace.onDidCloseTextDocument(cancelPost); const cancelPost = function () {
}); c("Autocompletion request terminated by user cancel");
// Make a request to the ollama.ai REST API };
const response = await axios.post(apiEndpoint, {
model: apiModel, // Change this to the model you want to use axiosCancelPost = cancelPost;
prompt: messageHeaderSub(textEditor.document) + prompt, if (cancellationToken) cancellationToken.onCancellationRequested(cancelPost);
stream: true, progressCancellationToken.onCancellationRequested(cancelPost);
raw: true, vscode.workspace.onDidCloseTextDocument(cancelPost);
options: { });
num_predict: numPredict,
temperature: apiTemperature, // Make a request to the ollama.ai REST API
stop: ["```"] const response = await axios.post(apiEndpoint, {
} model: apiModel, // Change this to the model you want to use
prompt: sub, // Use the modified sub string with the replaced prompt
stream: true,
raw: true,
options: {
num_predict: numPredict,
temperature: apiTemperature,
stop: ["```"]
}
}, { }, {
cancelToken: axiosCancelToken, cancelToken: axiosCancelToken,
responseType: 'stream', responseType: 'stream',
@ -135,7 +143,7 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
// Keep cancel window available // Keep cancel window available
const finished = new Promise((resolve) => { const finished = new Promise((resolve) => {
response.data.on('end', () => { response.data.on('end', () => {
progress.report({ message: "Ollama completion finished." }); progress.report({ message: "Fabelous completion finished." });
resolve(true); resolve(true);
}); });
axiosCancelToken.promise.finally(() => { // prevent notification from freezing on user input cancel axiosCancelToken.promise.finally(() => { // prevent notification from freezing on user input cancel
@ -146,7 +154,7 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
} catch (err: any) { } catch (err: any) {
// Show an error message // Show an error message
vscode.window.showErrorMessage( vscode.window.showErrorMessage(
"Ollama encountered an error: " + err.message "Fabelous Autocoder encountered an error: " + err.message
); );
console.log(err); console.log(err);
} }