continue-custom/extensions/vscode/src/util/loadAutocompleteModel.ts
Nate Sesti 1386eb692a
Nate/better retrieval (#1677)
* deduplicatearray tests

* break out separate retrieval pipelines

* IConfigHandler

* tests for codebase indexer

* better .continueignore for continue

* indexing fixes

* ignore .gitignore and .continueignore when indexing

* retrieval pipeline improvements
2024-07-05 22:40:19 -07:00

109 lines
3.6 KiB
TypeScript

import type { ILLM } from "core";
import { IConfigHandler } from "core/config/IConfigHandler";
import Ollama from "core/llm/llms/Ollama";
import { GlobalContext } from "core/util/GlobalContext";
import * as vscode from "vscode";
export class TabAutocompleteModel {
private _llm: ILLM | undefined;
private defaultTag = "starcoder2:3b";
private defaultTagName = "Starcoder2 3b";
private globalContext: GlobalContext = new GlobalContext();
private shownOllamaWarning = false;
private shownDeepseekWarning = false;
private configHandler: IConfigHandler;
constructor(configHandler: IConfigHandler) {
this.configHandler = configHandler;
}
clearLlm() {
this._llm = undefined;
}
async getDefaultTabAutocompleteModel() {
const llm = new Ollama({
model: this.defaultTag,
});
try {
const models = await llm.listModels();
if (!models.includes(this.defaultTag)) {
if (!this.shownDeepseekWarning) {
vscode.window
.showWarningMessage(
`Your local Ollama instance doesn't yet have ${this.defaultTagName}. To download this model, run \`ollama run ${this.defaultTag}\` (recommended). If you'd like to use a custom model for tab autocomplete, learn more in the docs`,
"Documentation",
"Copy Command",
)
.then((value) => {
if (value === "Documentation") {
vscode.env.openExternal(
vscode.Uri.parse(
"https://docs.continue.dev/walkthroughs/tab-autocomplete",
),
);
} else if (value === "Copy Command") {
vscode.env.clipboard.writeText(`ollama run ${this.defaultTag}`);
}
});
this.shownDeepseekWarning = true;
}
return undefined;
}
} catch (e) {
if (!this.shownOllamaWarning) {
vscode.window
.showWarningMessage(
"Continue failed to connect to Ollama, which is used by default for tab-autocomplete. If you haven't downloaded it yet, you can do so at https://ollama.ai (recommended). If you'd like to use a custom model for tab autocomplete, learn more in the docs",
"Download Ollama",
"Documentation",
)
.then((value) => {
if (value === "Documentation") {
vscode.env.openExternal(
vscode.Uri.parse(
"https://docs.continue.dev/walkthroughs/tab-autocomplete",
),
);
} else if (value === "Download Ollama") {
vscode.env.openExternal(vscode.Uri.parse("https://ollama.ai"));
}
});
this.shownOllamaWarning = true;
}
return undefined;
}
return llm;
}
async get() {
if (!this._llm) {
const config = await this.configHandler.loadConfig();
if (config.tabAutocompleteModels?.length) {
const selected = this.globalContext.get("selectedTabAutocompleteModel");
if (selected) {
this._llm =
config.tabAutocompleteModels?.find(
(model) => model.title === selected,
) ?? config.tabAutocompleteModels?.[0];
} else {
if (config.tabAutocompleteModels[0].title) {
this.globalContext.update(
"selectedTabAutocompleteModel",
config.tabAutocompleteModels[0].title,
);
}
this._llm = config.tabAutocompleteModels[0];
}
} else {
this._llm = await this.getDefaultTabAutocompleteModel();
}
}
return this._llm;
}
}