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
![example](example.gif)
## 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",
"displayName": "Ollama Autocoder",
"name": "fabelous-autocoder",
"displayName": "Fabelous Autocoder",
"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",
"publisher": "10nates",
"license": "MIT",
"publisher": "fabel",
"license": "CC BY-ND 4.0",
"bugs": {
"url": "https://github.com/10Nates/ollama-autocoder/issues"
"url": "https://gitea.fabelous.app/fabel/Fabelous-Autocoder/issues"
},
"sponsor": {
"url": "https://ko-fi.com/natehedge"
},
"repository": {
"type": "git",
"url": "https://github.com/10Nates/ollama-autocoder"
"url": "https://gitea.fabelous.app/fabel/Fabelous-Autocoder"
},
"engines": {
"vscode": "^1.73.0"
"vscode": "^1.89.0"
},
"categories": [
"Machine Learning",
@ -25,7 +25,6 @@
"Programming Languages"
],
"keywords": [
"llama",
"ollama",
"gpt",
"coding",
@ -51,6 +50,11 @@
"default": "http://localhost:11434/api/generate",
"description": "The endpoint of the ollama REST API"
},
"ollama-autocoder.authentication": {
"type": "string",
"default": "",
"description": "Authorization Token for Ollama"
},
"ollama-autocoder.model": {
"type": "string",
"default": "openhermes2.5-mistral:7b-q4_K_M",
@ -96,37 +100,83 @@
"type": "boolean",
"default": true,
"description": "Ollama continues autocompletion after what is previewed inline. Disabling disables that feature as some may find it irritating. Multiline completion is still accessible through the shortcut even after disabling."
},
"ollama-autocoder.temperature": {
"type": "number",
"default": 0.5,
"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": [
{
"command": "ollama-autocoder.autocomplete",
"title": "Autocomplete with Ollama"
"command": "fabelous-autocoder.autocomplete",
"title": "Autocompletion with Ollama"
}
]
},
"scripts": {
"vscode:prepublish": "npm run compile",
"compile": "tsc -p ./",
"compile": "tsc --skipLibCheck -p ./",
"package": "npm run compile && vsce package",
"lint": "eslint \"src/**/*.ts\"",
"watch": "tsc -watch -p ./"
"watch": "tsc --skipLibCheck -watch -p ./"
},
"devDependencies": {
"@types/node": "^16.18.34",
"@types/vscode": "^1.73.0",
"@typescript-eslint/eslint-plugin": "^6.7.0",
"@typescript-eslint/parser": "^6.7.0",
"eslint": "^8.26.0",
"typescript": "^5.3.2"
"@types/node": "^20.12.8",
"@types/vscode": "^1.89.0",
"@typescript-eslint/eslint-plugin": "^7.8.0",
"@typescript-eslint/parser": "^7.8.0",
"eslint": "^8.57.0",
"typescript": "^5.4.5"
},
"dependencies": {
"axios": "^1.6.2"
"axios": "^1.6.8"
}
}

View File

@ -5,6 +5,7 @@ import axios from "axios";
let VSConfig: vscode.WorkspaceConfiguration;
let apiEndpoint: string;
let apiAuthentication: string;
let apiModel: string;
let apiMessageHeader: string;
let apiTemperature: number;
@ -15,10 +16,22 @@ let responsePreview: boolean | undefined;
let responsePreviewMaxTokens: number;
let responsePreviewDelay: number;
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() {
VSConfig = vscode.workspace.getConfiguration("ollama-autocoder");
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
apiMessageHeader = VSConfig.get("message header") || "";
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
continueInline = VSConfig.get("continue inline");
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();
// No need for restart for any of these settings
vscode.workspace.onDidChangeConfiguration(updateVSConfig);
// Give model additional information
function messageHeaderSub(document: vscode.TextDocument) {
const sub = apiMessageHeader
@ -44,16 +66,13 @@ function messageHeaderSub(document: vscode.TextDocument) {
.replace("{PROJECT_NAME}", vscode.workspace.name || "Untitled");
return sub;
}
// internal function for autocomplete, not directly exposed
async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationToken?: vscode.CancellationToken) {
const document = textEditor.document;
const position = textEditor.selection.active;
// Get the current prompt
let prompt = document.getText(new vscode.Range(document.lineAt(0).range.start, position));
prompt = prompt.substring(Math.max(0, prompt.length - promptWindowSize), prompt.length);
// Show a progress message
vscode.window.withProgress(
{
@ -64,7 +83,6 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
async (progress, progressCancellationToken) => {
try {
progress.report({ message: "Starting model..." });
let axiosCancelPost: () => void;
const axiosCancelToken = new axios.CancelToken((c) => {
const cancelPost = function () {
@ -75,7 +93,6 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
progressCancellationToken.onCancellationRequested(cancelPost);
vscode.workspace.onDidCloseTextDocument(cancelPost);
});
// 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
@ -89,35 +106,33 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
}
}, {
cancelToken: axiosCancelToken,
responseType: 'stream'
responseType: 'stream',
headers: {
'Authorization': apiAuthentication
}
}
);
//tracker
let currentPosition = position;
response.data.on('data', async (d: Uint8Array) => {
progress.report({ message: "Generating..." });
// Check for user input (cancel)
if (currentPosition.line != textEditor.selection.end.line || currentPosition.character != textEditor.selection.end.character) {
axiosCancelPost(); // cancel axios => cancel finished promise => close notification
return;
}
// Get a completion from the response
const completion: string = JSON.parse(d.toString()).response;
// lastToken = completion;
if (completion === "") {
return;
}
//complete edit for token
const edit = new vscode.WorkspaceEdit();
edit.insert(document.uri, currentPosition, completion);
await vscode.workspace.applyEdit(edit);
// Move the cursor to the end of the completion
const completionLines = completion.split("\n");
const newPosition = new vscode.Position(
@ -129,14 +144,11 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
newPosition
);
currentPosition = newPosition;
// completion bar
progress.report({ message: "Generating...", increment: 1 / (numPredict / 100) });
// move cursor
textEditor.selection = newSelection;
});
// Keep cancel window available
const finished = new Promise((resolve) => {
response.data.on('end', () => {
@ -147,9 +159,7 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
resolve(false);
});
});
await finished;
} catch (err: any) {
// Show an error message
vscode.window.showErrorMessage(
@ -160,22 +170,17 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
}
);
}
// Completion item provider callback for activate
async function provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, cancellationToken: vscode.CancellationToken) {
// Create a completion item
const item = new vscode.CompletionItem("Autocomplete with Ollama");
// Set the insert text to a placeholder
item.insertText = new vscode.SnippetString('${1:}');
// Wait before initializing Ollama to reduce compute usage
if (responsePreview) await new Promise(resolve => setTimeout(resolve, responsePreviewDelay * 1000));
if (cancellationToken.isCancellationRequested) {
return [ item ];
}
// Set the label & inset text to a shortened, non-stream response
if (responsePreview) {
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: {
num_predict: responsePreviewMaxTokens, // reduced compute max
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) => {
@ -198,13 +214,11 @@ async function provideCompletionItems(document: vscode.TextDocument, position: v
cancellationToken.onCancellationRequested(cancelPost);
})
});
if (response_preview.data.response.trim() != "") { // default if empty
item.label = response_preview.data.response.trimStart(); // tended to add whitespace at the beginning
item.insertText = response_preview.data.response.trimStart();
}
}
// Set the documentation to a message
item.documentation = new vscode.MarkdownString('Press `Enter` to get an autocompletion from Ollama');
// Set the command to trigger the completion
@ -216,7 +230,6 @@ async function provideCompletionItems(document: vscode.TextDocument, position: v
// Return the completion item
return [item];
}
// This method is called when extension is activated
function activate(context: vscode.ExtensionContext) {
// Register a completion provider for JavaScript files
@ -225,7 +238,6 @@ function activate(context: vscode.ExtensionContext) {
},
...completionKeys.split("")
);
// Register a command for getting a completion from Ollama through command/keybind
const externalAutocompleteCommand = vscode.commands.registerTextEditorCommand(
"ollama-autocoder.autocomplete",
@ -234,17 +246,16 @@ function activate(context: vscode.ExtensionContext) {
autocompleteCommand(textEditor, cancellationToken);
}
);
// Add the commands & completion provider to the context
context.subscriptions.push(completionProvider);
context.subscriptions.push(externalAutocompleteCommand);
}
// This method is called when extension is deactivated
// eslint-disable-next-line @typescript-eslint/no-empty-function
function deactivate() { }
module.exports = {
activate,
deactivate,
};
};