Skip to content

Commit 41966cd

Browse files
authored
Menu items for Install/Upgrade llama.cpp, download a model and start llama.cpp server (mac)
- Install/Upgrade llama.cpp, download a model and start llama.cpp server with one menu item (only for mac) - new property launch_cmd for executing a shell command (for example for starting llama.cpp) from the menu
2 parents 3126abf + b8a7f39 commit 41966cd

File tree

7 files changed

+132
-7
lines changed

7 files changed

+132
-7
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ dist
33
node_modules
44
.vscode-test/
55
*.vsix
6+
.idea/

package.json

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@
5656
{
5757
"command": "extension.copyChunks",
5858
"title": "Copy Chunks"
59+
},
60+
{
61+
"command": "extension.showMenu",
62+
"title": "Show Menu"
5963
}
6064
],
6165
"keybindings": [
@@ -77,7 +81,7 @@
7781
{
7882
"command": "extension.copyChunks",
7983
"key": "ctrl+shift+,",
80-
"when": "editorTextFocus"
84+
"when": "true"
8185
},
8286
{
8387
"command": "extension.copyIntercept",
@@ -99,11 +103,22 @@
99103
"key": "ctrl+right",
100104
"when": "editorTextFocus && inlineSuggestionVisible"
101105
}
106+
,
107+
{
108+
"command": "extension.showMenu",
109+
"key": "ctrl+shift+m",
110+
"when": "true"
111+
}
102112
],
103113
"configuration": {
104114
"type": "object",
105115
"title": "llama.vscode Configuration",
106116
"properties": {
117+
"llama-vscode.launch_cmd": {
118+
"type": "string",
119+
"default": "",
120+
"description": "Shell command executed from menu"
121+
},
107122
"llama-vscode.endpoint": {
108123
"type": "string",
109124
"default": "http://127.0.0.1:8012",

src/architect.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,15 @@ export class Architect {
7474
context.subscriptions.push(acceptFirstWordCommand);
7575
}
7676

77-
77+
registerCommandShowMenu = (context: vscode.ExtensionContext) => {
78+
const showMenuCommand = vscode.commands.registerCommand(
79+
'extension.showMenu',
80+
async () => {
81+
await this.app.menu.showMenu();
82+
}
83+
);
84+
context.subscriptions.push(showMenuCommand);
85+
}
7886

7987
setPeriodicRingBufferUpdate = (context: vscode.ExtensionContext) => {
8088
const ringBufferIntervalId = setInterval(this.app.extraContext.periodicRingBufferUpdate, this.app.extConfig.ring_update_ms);

src/configuration.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import OpenAI from "openai";
44
export class Configuration {
55
// extension configs
66
enabled = true;
7+
launch_cmd = ""
78
endpoint = "http=//127.0.0.1:8012";
89
auto = true;
910
api_key = "";
@@ -92,6 +93,7 @@ export class Configuration {
9293
private updateConfigs = (config: vscode.WorkspaceConfiguration) => {
9394
// TODO Handle the case of wrong types for the configuration values
9495
this.endpoint = this.trimTrailingSlash(String(config.get<string>("endpoint")));
96+
this.launch_cmd = String(config.get<string>("launch_cmd"));
9597
this.use_openai_endpoint = Boolean(config.get<boolean>("use_openai_endpoint"));
9698
this.openai_client_model = String(config.get<string>("openai_client_model"));
9799
this.openai_prompt_template = String(config.get<string>("openai_prompt_template"));

src/extension.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import * as vscode from 'vscode';
22
import {Application} from "./application";
33

4+
let app: Application
45
export function activate(context: vscode.ExtensionContext) {
5-
let app = Application.getInstance();
6+
app = Application.getInstance();
67
app.architect.setStatusBar(context)
78
app.architect.setOnChangeConfiguration(context);
89
app.architect.setCompletionProvider(context);
@@ -15,8 +16,10 @@ export function activate(context: vscode.ExtensionContext) {
1516
app.architect.setOnChangeActiveFile(context);
1617
app.architect.registerCommandAcceptFirstLine(context);
1718
app.architect.registerCommandAcceptFirstWord(context);
19+
app.architect.registerCommandShowMenu(context);
1820
}
1921

2022
export function deactivate() {
21-
// Nothing to do. VS Code will dispose all registerd disposables
23+
// VS Code will dispose all registerd disposables
24+
app.llamaServer.killCmd();
2225
}

src/llama-server.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import axios from "axios";
22
import {Application} from "./application";
3+
import { EventEmitter } from 'events';
4+
import vscode, { Terminal } from "vscode";
35

46
const STATUS_OK = 200;
57

@@ -21,6 +23,8 @@ export interface LlamaResponse {
2123
export class LlamaServer {
2224
// private extConfig: Configuration;
2325
private app: Application
26+
private vsCodeTerminal: Terminal | undefined;
27+
private eventEmitter: EventEmitter;
2428
private readonly defaultRequestParams = {
2529
top_k: 40,
2630
top_p: 0.99,
@@ -31,6 +35,8 @@ export class LlamaServer {
3135

3236
constructor(application: Application) {
3337
this.app = application;
38+
this.eventEmitter = new EventEmitter();
39+
this.vsCodeTerminal = undefined;
3440
}
3541

3642
private replacePlaceholders(template: string, replacements: { [key: string]: string }): string {
@@ -145,4 +151,23 @@ export class LlamaServer {
145151
this.app.extConfig.axiosRequestConfig
146152
);
147153
};
154+
155+
onlaunchCmdClose = (callback: (data: { code: number, stderr: string }) => void): void => {
156+
this.eventEmitter.on('processClosed', callback);
157+
}
158+
159+
shellCmd = (launchCmd: string): void => {
160+
if (!launchCmd) {
161+
return;
162+
}
163+
this.vsCodeTerminal = vscode.window.createTerminal({
164+
name: 'llama.cpp Command Terminal'
165+
});
166+
this.vsCodeTerminal.show(true);
167+
this.vsCodeTerminal.sendText(launchCmd);
168+
}
169+
170+
killCmd = (): void => {
171+
if (this.vsCodeTerminal) this.vsCodeTerminal.dispose();
172+
}
148173
}

src/menu.ts

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export class Menu {
88
}
99

1010
createMenuItems = (currentLanguage: string | undefined, isLanguageEnabled: boolean): vscode.QuickPickItem[] => {
11-
return [
11+
let menuItems = [
1212
{
1313
label: `${this.app.extConfig.enabled ? 'Disable' : 'Enable'} All Completions`,
1414
description: `Turn ${this.app.extConfig.enabled ? 'off' : 'on'} completions globally`
@@ -22,15 +22,86 @@ export class Menu {
2222
},
2323
{
2424
label: "$(book) View Documentation...",
25-
}
26-
].filter(Boolean) as vscode.QuickPickItem[];
25+
}]
26+
27+
if (process.platform === 'darwin') { // if mac os
28+
menuItems.push(
29+
{
30+
label: "Start model Qwen2.5-Coder-1.5B-Q8_0-GGUF (<= 8GB VRAM)",
31+
description: `Requires brew, installs/upgrades llama.cpp server, downloads the model if not available, and runs llama.cpp server`
32+
},
33+
{
34+
label: "Start model Qwen2.5-Coder-3B-Q8_0-GGUF (<= 16GB VRAM)",
35+
description: `Requires brew, installs/upgrades llama.cpp server, downloads the model if not available, and runs llama.cpp server`
36+
},
37+
{
38+
label: "Start model Qwen2.5-Coder-7B-Q8_0-GGUF (> 16GB VRAM)",
39+
description: `Requires brew, installs/upgrades llama.cpp server, downloads the model if not available, and runs llama.cpp server`
40+
},
41+
{
42+
label: "Start model Qwen2.5-Coder-1.5B-Q8_0-GGUF (CPU Only)",
43+
description: `Requires brew, installs/upgrades llama.cpp server, downloads the model if not available, and runs llama.cpp server`
44+
},
45+
{
46+
label: "Start model Qwen2.5-Coder-0.5B-Q8_0-GGUF (CPU Only)",
47+
description: `Requires brew, installs/upgrades llama.cpp server, downloads the model if not available, and runs llama.cpp server`
48+
})
49+
}
50+
51+
menuItems.push(
52+
{
53+
label: "Start llama.cpp server with custom command from launch_cmd property",
54+
description: `Runs the command from property launch_cmd`
55+
},
56+
{
57+
label: "Stop llama.cpp server",
58+
description: `Stops llama.cpp server if it was started from llama.vscode menu."`
59+
})
60+
61+
return menuItems.filter(Boolean) as vscode.QuickPickItem[];
2762
}
2863

2964
handleMenuSelection = async (selected: vscode.QuickPickItem, currentLanguage: string | undefined, languageSettings: Record<string, boolean>) => {
65+
const DEFAULT_PORT_FIM_MODEL = "8012"
66+
const PRESET_PLACEHOLDER = "[preset]";
67+
const MODEL_PLACEHOLDER = "[model]"
68+
let endpointParts = this.app.extConfig.endpoint.split(":");
69+
let port = endpointParts[endpointParts.length -1]
70+
if (!Number.isInteger(Number(port))) port = DEFAULT_PORT_FIM_MODEL
71+
let llmMacVramTemplate = " brew install llama.cpp && llama-server --" + PRESET_PLACEHOLDER + " --port " + port
72+
let llmMacCpuTemplate = " brew install llama.cpp && llama-server -hf " + MODEL_PLACEHOLDER + " --port " + port + " -ub 1024 -b 1024 -dt 0.1 --ctx-size 0 --cache-reuse 256"
73+
3074
switch (selected.label) {
3175
case "$(gear) Edit Settings...":
3276
await vscode.commands.executeCommand('workbench.action.openSettings', 'llama-vscode');
3377
break;
78+
case "$(gear) Start model Qwen2.5-Coder-1.5B-Q8_0-GGUF (<= 8GB VRAM)":
79+
await this.app.llamaServer.killCmd();
80+
await this.app.llamaServer.shellCmd(llmMacVramTemplate.replace(PRESET_PLACEHOLDER, "fim-qwen-1.5b-default"));
81+
break;
82+
case "Start model Qwen2.5-Coder-3B-Q8_0-GGUF (<= 16GB VRAM)":
83+
await this.app.llamaServer.killCmd();
84+
await this.app.llamaServer.shellCmd(llmMacVramTemplate.replace(PRESET_PLACEHOLDER, "fim-qwen-3b-default"));
85+
break;
86+
case "Start model Qwen2.5-Coder-7B-Q8_0-GGUF (> 16GB VRAM)":
87+
await this.app.llamaServer.killCmd();
88+
await this.app.llamaServer.shellCmd(llmMacVramTemplate.replace(PRESET_PLACEHOLDER, "fim-qwen-7b-default"));
89+
break;
90+
case "Start model Qwen2.5-Coder-1.5B-Q8_0-GGUF (CPU Only)":
91+
await this.app.llamaServer.killCmd();
92+
await this.app.llamaServer.shellCmd(llmMacCpuTemplate.replace(MODEL_PLACEHOLDER, "ggml-org/Qwen2.5-Coder-1.5B-Q8_0-GGUF"));
93+
break;
94+
case "Start model Qwen2.5-Coder-0.5B-Q8_0-GGUF (CPU Only)":
95+
await this.app.llamaServer.killCmd();
96+
await this.app.llamaServer.shellCmd(llmMacCpuTemplate.replace(MODEL_PLACEHOLDER, "ggml-org/Qwen2.5-Coder-0.5B-Q8_0-GGUF"));
97+
break;
98+
case "Start llama.cpp server with custom command from launch_cmd property":
99+
await this.app.llamaServer.killCmd();
100+
await this.app.llamaServer.shellCmd(this.app.extConfig.launch_cmd);
101+
break;
102+
case "Stop llama.cpp server":
103+
await this.app.llamaServer.killCmd();
104+
break;
34105
case "$(book) View Documentation...":
35106
await vscode.env.openExternal(vscode.Uri.parse('https://github.com/ggml-org/llama.vscode'));
36107
break;

0 commit comments

Comments
 (0)