first commit as Fableous coder main changes commited

This commit is contained in:
Falko Victor Habel 2024-06-02 15:42:29 +02:00
parent 207e4ff829
commit 4ca0d6e105
5 changed files with 115 additions and 55 deletions

View File

@ -1,8 +1,7 @@
# Ollama Autocoder # Fabelous Autocoder
A simple to use Ollama autocompletion engine with options exposed and streaming functionality A simple to use Ollama autocompletion engine with options exposed and streaming functionality
![example](example.gif)
## Requirements ## Requirements

Binary file not shown.

Before

Width:  |  Height:  |  Size: 165 KiB

BIN
icon.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 188 KiB

After

Width:  |  Height:  |  Size: 58 KiB

View File

@ -1,23 +1,23 @@
{ {
"name": "ollama-autocoder", "name": "fabelous-autocoder",
"displayName": "Ollama 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.8", "version": "0.0.1",
"icon": "icon.png", "icon": "icon.png",
"publisher": "10nates", "publisher": "fabel",
"license": "MIT", "license": "CC BY-ND 4.0",
"bugs": { "bugs": {
"url": "https://github.com/10Nates/ollama-autocoder/issues" "url": "https://gitea.fabelous.app/fabel/Fabelous-Autocoder/issues"
}, },
"sponsor": { "sponsor": {
"url": "https://ko-fi.com/natehedge" "url": "https://ko-fi.com/natehedge"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
"url": "https://github.com/10Nates/ollama-autocoder" "url": "https://gitea.fabelous.app/fabel/Fabelous-Autocoder"
}, },
"engines": { "engines": {
"vscode": "^1.73.0" "vscode": "^1.89.0"
}, },
"categories": [ "categories": [
"Machine Learning", "Machine Learning",
@ -25,7 +25,6 @@
"Programming Languages" "Programming Languages"
], ],
"keywords": [ "keywords": [
"llama",
"ollama", "ollama",
"gpt", "gpt",
"coding", "coding",
@ -51,6 +50,11 @@
"default": "http://localhost:11434/api/generate", "default": "http://localhost:11434/api/generate",
"description": "The endpoint of the ollama REST API" "description": "The endpoint of the ollama REST API"
}, },
"ollama-autocoder.authentication": {
"type": "string",
"default": "",
"description": "Authorization Token for Ollama"
},
"ollama-autocoder.model": { "ollama-autocoder.model": {
"type": "string", "type": "string",
"default": "openhermes2.5-mistral:7b-q4_K_M", "default": "openhermes2.5-mistral:7b-q4_K_M",
@ -102,31 +106,77 @@
"type": "number", "type": "number",
"default": 0.5, "default": 0.5,
"description": "Temperature of the model. It is recommended to set it lower than you would for dialogue." "description": "Temperature of the model. It is recommended to set it lower than you would for dialogue."
},
"ollama-autocoder.keep alive": {
"type": "number",
"default": 10,
"description": "Time in minutes before Ollama unloads the model."
},
"ollama-autocoder.top k": {
"type": "integer",
"description": "Top k sampling for the model."
},
"ollama-autocoder.top p": {
"type": "number",
"description": "Top p sampling for the model."
},
"ollama-autocoder.tfs z": {
"type": "number",
"description": "TFS z sampling for the model."
},
"ollama-autocoder.typical p": {
"type": "number",
"description": "Typical p sampling for the model."
},
"ollama-autocoder.repeat last n": {
"type": "number",
"description": "Repeat the last n tokens of the prompt. This can be useful for code completion as it allows the model to have more context."
},
"ollama-autocoder.repeat penalty": {
"type": "number",
"description": "Repetition penalty for the model."
},
"ollama-autocoder.presence penalty": {
"type": "number",
"description": "Presence penalty for the model."
},
"ollama-autocoder.frequency penalty": {
"type": "number",
"description": "Frequency penalty for the model."
},
"ollama-autocoder.num batch": {
"type": "number",
"description": "Batch size."
},
"ollama-autocoder.num keep": {
"type": "number",
"description": "Number of keep tokens for the model."
} }
} }
}, },
"commands": [ "commands": [
{ {
"command": "ollama-autocoder.autocomplete", "command": "fabelous-autocoder.autocomplete",
"title": "Autocomplete with Ollama" "title": "Autocompletion with Ollama"
} }
] ]
}, },
"scripts": { "scripts": {
"vscode:prepublish": "npm run compile", "vscode:prepublish": "npm run compile",
"compile": "tsc -p ./", "compile": "tsc --skipLibCheck -p ./",
"package": "npm run compile && vsce package",
"lint": "eslint \"src/**/*.ts\"", "lint": "eslint \"src/**/*.ts\"",
"watch": "tsc -watch -p ./" "watch": "tsc --skipLibCheck -watch -p ./"
}, },
"devDependencies": { "devDependencies": {
"@types/node": "^16.18.34", "@types/node": "^20.12.8",
"@types/vscode": "^1.73.0", "@types/vscode": "^1.89.0",
"@typescript-eslint/eslint-plugin": "^6.7.0", "@typescript-eslint/eslint-plugin": "^7.8.0",
"@typescript-eslint/parser": "^6.7.0", "@typescript-eslint/parser": "^7.8.0",
"eslint": "^8.26.0", "eslint": "^8.57.0",
"typescript": "^5.3.2" "typescript": "^5.4.5"
}, },
"dependencies": { "dependencies": {
"axios": "^1.6.2" "axios": "^1.6.8"
} }
} }

View File

@ -5,6 +5,7 @@ import axios from "axios";
let VSConfig: vscode.WorkspaceConfiguration; let VSConfig: vscode.WorkspaceConfiguration;
let apiEndpoint: string; let apiEndpoint: string;
let apiAuthentication: string;
let apiModel: string; let apiModel: string;
let apiMessageHeader: string; let apiMessageHeader: string;
let apiTemperature: number; let apiTemperature: number;
@ -15,10 +16,22 @@ let responsePreview: boolean | undefined;
let responsePreviewMaxTokens: number; let responsePreviewMaxTokens: number;
let responsePreviewDelay: number; let responsePreviewDelay: number;
let continueInline: boolean | undefined; let continueInline: boolean | undefined;
let keepAlive: number | undefined;
let topK: number | undefined;
let topP: number | undefined;
let tfsZ: number | undefined;
let typicalP: number | undefined;
let repeatLastN: number | undefined;
let repeatPenalty: number | undefined;
let presencePenalty: number | undefined;
let frequencyPenalty: number | undefined;
let numBatch: number | undefined;
let numKeep: number | undefined;
function updateVSConfig() { function updateVSConfig() {
VSConfig = vscode.workspace.getConfiguration("ollama-autocoder"); VSConfig = vscode.workspace.getConfiguration("ollama-autocoder");
apiEndpoint = VSConfig.get("endpoint") || "http://localhost:11434/api/generate"; apiEndpoint = VSConfig.get("endpoint") || "http://localhost:11434/api/generate";
apiAuthentication = VSConfig.get("authentication") || "";
apiModel = VSConfig.get("model") || "openhermes2.5-mistral:7b-q4_K_M"; // The model I tested with apiModel = VSConfig.get("model") || "openhermes2.5-mistral:7b-q4_K_M"; // The model I tested with
apiMessageHeader = VSConfig.get("message header") || ""; apiMessageHeader = VSConfig.get("message header") || "";
numPredict = VSConfig.get("max tokens predicted") || 1000; numPredict = VSConfig.get("max tokens predicted") || 1000;
@ -29,13 +42,22 @@ function updateVSConfig() {
responsePreviewDelay = VSConfig.get("preview delay") || 0; // Must be || 0 instead of || [default] because of truthy responsePreviewDelay = VSConfig.get("preview delay") || 0; // Must be || 0 instead of || [default] because of truthy
continueInline = VSConfig.get("continue inline"); continueInline = VSConfig.get("continue inline");
apiTemperature = VSConfig.get("temperature") || 0.5; apiTemperature = VSConfig.get("temperature") || 0.5;
keepAlive = VSConfig.get("keep alive") || undefined;
topK = VSConfig.get("top k") || undefined;
topP = VSConfig.get("top p") || undefined;
tfsZ = VSConfig.get("tfs z") || undefined;
typicalP = VSConfig.get("typical p") || undefined;
repeatLastN = VSConfig.get("repeat last n") || undefined;
repeatPenalty = VSConfig.get("repeat penalty") || undefined;
presencePenalty = VSConfig.get("presence penalty") || undefined;
frequencyPenalty = VSConfig.get("frequency penalty") || undefined;
numBatch = VSConfig.get("num batch") || undefined;
numKeep = VSConfig.get("num keep") || undefined;
} }
updateVSConfig(); updateVSConfig();
// No need for restart for any of these settings // No need for restart for any of these settings
vscode.workspace.onDidChangeConfiguration(updateVSConfig); vscode.workspace.onDidChangeConfiguration(updateVSConfig);
// Give model additional information // Give model additional information
function messageHeaderSub(document: vscode.TextDocument) { function messageHeaderSub(document: vscode.TextDocument) {
const sub = apiMessageHeader const sub = apiMessageHeader
@ -44,16 +66,13 @@ function messageHeaderSub(document: vscode.TextDocument) {
.replace("{PROJECT_NAME}", vscode.workspace.name || "Untitled"); .replace("{PROJECT_NAME}", vscode.workspace.name || "Untitled");
return sub; return sub;
} }
// 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 // Get the current prompt
let prompt = document.getText(new vscode.Range(document.lineAt(0).range.start, position)); let prompt = document.getText(new vscode.Range(document.lineAt(0).range.start, position));
prompt = prompt.substring(Math.max(0, prompt.length - promptWindowSize), prompt.length); prompt = prompt.substring(Math.max(0, prompt.length - promptWindowSize), prompt.length);
// Show a progress message // Show a progress message
vscode.window.withProgress( vscode.window.withProgress(
{ {
@ -64,7 +83,6 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
async (progress, progressCancellationToken) => { async (progress, progressCancellationToken) => {
try { try {
progress.report({ message: "Starting model..." }); progress.report({ message: "Starting model..." });
let axiosCancelPost: () => void; let axiosCancelPost: () => void;
const axiosCancelToken = new axios.CancelToken((c) => { const axiosCancelToken = new axios.CancelToken((c) => {
const cancelPost = function () { const cancelPost = function () {
@ -75,7 +93,6 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
progressCancellationToken.onCancellationRequested(cancelPost); progressCancellationToken.onCancellationRequested(cancelPost);
vscode.workspace.onDidCloseTextDocument(cancelPost); vscode.workspace.onDidCloseTextDocument(cancelPost);
}); });
// Make a request to the ollama.ai REST API // Make a request to the ollama.ai REST API
const response = await axios.post(apiEndpoint, { const response = await axios.post(apiEndpoint, {
model: apiModel, // Change this to the model you want to use model: apiModel, // Change this to the model you want to use
@ -89,35 +106,33 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
} }
}, { }, {
cancelToken: axiosCancelToken, cancelToken: axiosCancelToken,
responseType: 'stream' responseType: 'stream',
headers: {
'Authorization': apiAuthentication
}
} }
); );
//tracker //tracker
let currentPosition = position; let currentPosition = position;
response.data.on('data', async (d: Uint8Array) => { response.data.on('data', async (d: Uint8Array) => {
progress.report({ message: "Generating..." }); progress.report({ message: "Generating..." });
// Check for user input (cancel) // Check for user input (cancel)
if (currentPosition.line != textEditor.selection.end.line || currentPosition.character != textEditor.selection.end.character) { if (currentPosition.line != textEditor.selection.end.line || currentPosition.character != textEditor.selection.end.character) {
axiosCancelPost(); // cancel axios => cancel finished promise => close notification axiosCancelPost(); // cancel axios => cancel finished promise => close notification
return; return;
} }
// Get a completion from the response // Get a completion from the response
const completion: string = JSON.parse(d.toString()).response; const completion: string = JSON.parse(d.toString()).response;
// lastToken = completion; // lastToken = completion;
if (completion === "") { if (completion === "") {
return; return;
} }
//complete edit for token //complete edit for token
const edit = new vscode.WorkspaceEdit(); const edit = new vscode.WorkspaceEdit();
edit.insert(document.uri, currentPosition, completion); edit.insert(document.uri, currentPosition, completion);
await vscode.workspace.applyEdit(edit); await vscode.workspace.applyEdit(edit);
// Move the cursor to the end of the completion // Move the cursor to the end of the completion
const completionLines = completion.split("\n"); const completionLines = completion.split("\n");
const newPosition = new vscode.Position( const newPosition = new vscode.Position(
@ -129,14 +144,11 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
newPosition newPosition
); );
currentPosition = newPosition; currentPosition = newPosition;
// completion bar // completion bar
progress.report({ message: "Generating...", increment: 1 / (numPredict / 100) }); progress.report({ message: "Generating...", increment: 1 / (numPredict / 100) });
// move cursor // move cursor
textEditor.selection = newSelection; textEditor.selection = newSelection;
}); });
// 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', () => {
@ -147,9 +159,7 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
resolve(false); resolve(false);
}); });
}); });
await finished; await finished;
} catch (err: any) { } catch (err: any) {
// Show an error message // Show an error message
vscode.window.showErrorMessage( vscode.window.showErrorMessage(
@ -160,22 +170,17 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
} }
); );
} }
// Completion item provider callback for activate // Completion item provider callback for activate
async function provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, cancellationToken: vscode.CancellationToken) { async function provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, cancellationToken: vscode.CancellationToken) {
// Create a completion item // Create a completion item
const item = new vscode.CompletionItem("Autocomplete with Ollama"); const item = new vscode.CompletionItem("Autocomplete with Ollama");
// Set the insert text to a placeholder // Set the insert text to a placeholder
item.insertText = new vscode.SnippetString('${1:}'); item.insertText = new vscode.SnippetString('${1:}');
// Wait before initializing Ollama to reduce compute usage // Wait before initializing Ollama to reduce compute usage
if (responsePreview) await new Promise(resolve => setTimeout(resolve, responsePreviewDelay * 1000)); if (responsePreview) await new Promise(resolve => setTimeout(resolve, responsePreviewDelay * 1000));
if (cancellationToken.isCancellationRequested) { if (cancellationToken.isCancellationRequested) {
return [ item ]; return [ item ];
} }
// Set the label & inset text to a shortened, non-stream response // Set the label & inset text to a shortened, non-stream response
if (responsePreview) { if (responsePreview) {
let prompt = document.getText(new vscode.Range(document.lineAt(0).range.start, position)); let prompt = document.getText(new vscode.Range(document.lineAt(0).range.start, position));
@ -188,7 +193,18 @@ async function provideCompletionItems(document: vscode.TextDocument, position: v
options: { options: {
num_predict: responsePreviewMaxTokens, // reduced compute max num_predict: responsePreviewMaxTokens, // reduced compute max
temperature: apiTemperature, temperature: apiTemperature,
stop: ['\n', '```'] stop: ['\n', '```'],
...keepAlive && { keep_alive: keepAlive },
...topK && { top_k: topK },
...topP && { top_p: topP },
...tfsZ && { tfs_z: tfsZ },
...typicalP && { typical_p: typicalP },
...repeatLastN && { repeat_last_n: repeatLastN },
...repeatPenalty && { repeat_penalty: repeatPenalty },
...presencePenalty && { presence_penalty: presencePenalty },
...frequencyPenalty && { frequency_penalty: frequencyPenalty },
...numBatch && { num_batch: numBatch },
...numKeep && { num_keep: numKeep },
} }
}, { }, {
cancelToken: new axios.CancelToken((c) => { cancelToken: new axios.CancelToken((c) => {
@ -198,13 +214,11 @@ async function provideCompletionItems(document: vscode.TextDocument, position: v
cancellationToken.onCancellationRequested(cancelPost); cancellationToken.onCancellationRequested(cancelPost);
}) })
}); });
if (response_preview.data.response.trim() != "") { // default if empty if (response_preview.data.response.trim() != "") { // default if empty
item.label = response_preview.data.response.trimStart(); // tended to add whitespace at the beginning item.label = response_preview.data.response.trimStart(); // tended to add whitespace at the beginning
item.insertText = response_preview.data.response.trimStart(); item.insertText = response_preview.data.response.trimStart();
} }
} }
// Set the documentation to a message // Set the documentation to a message
item.documentation = new vscode.MarkdownString('Press `Enter` to get an autocompletion from Ollama'); item.documentation = new vscode.MarkdownString('Press `Enter` to get an autocompletion from Ollama');
// Set the command to trigger the completion // Set the command to trigger the completion
@ -216,7 +230,6 @@ async function provideCompletionItems(document: vscode.TextDocument, position: v
// Return the completion item // Return the completion item
return [item]; return [item];
} }
// This method is called when extension is activated // This method is called when extension is activated
function activate(context: vscode.ExtensionContext) { function activate(context: vscode.ExtensionContext) {
// Register a completion provider for JavaScript files // Register a completion provider for JavaScript files
@ -225,7 +238,6 @@ function activate(context: vscode.ExtensionContext) {
}, },
...completionKeys.split("") ...completionKeys.split("")
); );
// Register a command for getting a completion from Ollama through command/keybind // Register a command for getting a completion from Ollama through command/keybind
const externalAutocompleteCommand = vscode.commands.registerTextEditorCommand( const externalAutocompleteCommand = vscode.commands.registerTextEditorCommand(
"ollama-autocoder.autocomplete", "ollama-autocoder.autocomplete",
@ -234,14 +246,13 @@ function activate(context: vscode.ExtensionContext) {
autocompleteCommand(textEditor, cancellationToken); autocompleteCommand(textEditor, cancellationToken);
} }
); );
// Add the commands & completion provider to the context // Add the commands & completion provider to the context
context.subscriptions.push(completionProvider); context.subscriptions.push(completionProvider);
context.subscriptions.push(externalAutocompleteCommand); context.subscriptions.push(externalAutocompleteCommand);
} }
// This method is called when extension is deactivated // This method is called when extension is deactivated
// eslint-disable-next-line @typescript-eslint/no-empty-function
function deactivate() { } function deactivate() { }
module.exports = { module.exports = {