From 098fb35dfcc8de09bcbfa8ee1d5d9865800c5301 Mon Sep 17 00:00:00 2001 From: Nate Sesti Date: Fri, 19 Jan 2024 12:21:41 -0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20add=20'mirostat'=20to=20completionO?= =?UTF-8?q?ptions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/config/types.ts | 52 ++++++++++------------- core/index.d.ts | 25 ++--------- core/llm/constants.ts | 10 +++-- core/llm/index.ts | 22 ++++++---- core/llm/llms/FreeTrial.ts | 16 ++++++- core/llm/llms/LlamaCpp.ts | 3 +- core/llm/llms/Ollama.ts | 1 + core/llm/llms/index.ts | 3 +- extensions/vscode/config_schema.json | 5 +++ gui/src/redux/selectors/modelSelectors.ts | 3 +- 10 files changed, 74 insertions(+), 66 deletions(-) diff --git a/core/config/types.ts b/core/config/types.ts index 06b16c5af..8d91ff986 100644 --- a/core/config/types.ts +++ b/core/config/types.ts @@ -134,17 +134,8 @@ declare global { message: string; } - export interface CompletionOptions { + export interface CompletionOptions extends BaseCompletionOptions { model: string; - - maxTokens: number; - temperature?: number; - topP?: number; - topK?: number; - minP?: number; - presencePenalty?: number; - frequencyPenalty?: number; - stop?: string[]; } export type ChatMessageRole = "user" | "assistant" | "system"; @@ -187,20 +178,11 @@ declare global { // LLM - export interface LLMFullCompletionOptions { + export interface LLMFullCompletionOptions extends BaseCompletionOptions { raw?: boolean; log?: boolean; model?: string; - - temperature?: number; - topP?: number; - topK?: number; - minP?: number; - presencePenalty?: number; - frequencyPenalty?: number; - stop?: string[]; - maxTokens?: number; } export interface LLMOptions { model: string; @@ -265,12 +247,19 @@ declare global { line: string; } + export class Problem { + filepath: string; + range: Range; + message: string; + } + export interface IDE { getSerializedConfig(): Promise; getConfigJsUrl(): Promise; getDiff(): Promise; getTerminalContents(): Promise; listWorkspaceContents(directory?: string): Promise; + listFolders(): Promise; getWorkspaceDirs(): Promise; writeFile(path: string, contents: string): Promise; showVirtualFile(title: string, contents: string): Promise; @@ -293,6 +282,7 @@ declare global { getOpenFiles(): Promise; getSearchResults(query: string): Promise; subprocess(command: string): Promise<[string, string]>; + getProblems(filepath?: string | undefined): Promise; // Embeddings /** @@ -305,10 +295,9 @@ declare global { tags: string[] ): void; retrieveChunks( - v: number[], + text: string, n: number, - tags: string[], - providerId: string + directory: string | undefined ): Promise; } @@ -320,7 +309,7 @@ declare global { addContextItem: (item: ContextItemWithId) => void; history: ChatMessage[]; input: string; - params?: any; + params?: { [key: string]: any } | undefined; contextItems: ContextItemWithId[]; } @@ -359,12 +348,15 @@ declare global { | "url" | "tree" | "http" - | "codebase"; + | "codebase" + | "problems" + | "folder"; type TemplateType = | "llama2" | "alpaca" | "zephyr" + | "phi2" | "phind" | "anthropic" | "chatml" @@ -373,7 +365,7 @@ declare global { | "deepseek" | "xwin-coder" | "neural-chat"; - + type ModelProvider = | "openai" | "free-trial" @@ -410,6 +402,7 @@ declare global { | "codellama-7b" | "codellama-13b" | "codellama-34b" + | "phi2" | "phind-codellama-34b" | "wizardcoder-7b" | "wizardcoder-13b" @@ -468,8 +461,9 @@ declare global { minP?: number; presencePenalty?: number; frequencyPenalty?: number; + mirostat?: number; stop?: string[]; - maxTokens: number; + maxTokens?: number; } export interface ModelDescription { @@ -556,9 +550,9 @@ declare global { disableSessionTitles?: boolean; disableIndexing?: boolean; userToken?: string; - embeddingsProvider?: EmbeddingsProvider; + embeddingsProvider: EmbeddingsProvider; } - + } export {}; diff --git a/core/index.d.ts b/core/index.d.ts index 5056cae99..0e2522fbe 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -132,17 +132,8 @@ export interface ContinueError { message: string; } -export interface CompletionOptions { +export interface CompletionOptions extends BaseCompletionOptions { model: string; - - maxTokens: number; - temperature?: number; - topP?: number; - topK?: number; - minP?: number; - presencePenalty?: number; - frequencyPenalty?: number; - stop?: string[]; } export type ChatMessageRole = "user" | "assistant" | "system"; @@ -185,20 +176,11 @@ export type ChatHistory = ChatHistoryItem[]; // LLM -export interface LLMFullCompletionOptions { +export interface LLMFullCompletionOptions extends BaseCompletionOptions { raw?: boolean; log?: boolean; model?: string; - - temperature?: number; - topP?: number; - topK?: number; - minP?: number; - presencePenalty?: number; - frequencyPenalty?: number; - stop?: string[]; - maxTokens?: number; } export interface LLMOptions { model: string; @@ -477,8 +459,9 @@ interface BaseCompletionOptions { minP?: number; presencePenalty?: number; frequencyPenalty?: number; + mirostat?: number; stop?: string[]; - maxTokens: number; + maxTokens?: number; } export interface ModelDescription { diff --git a/core/llm/constants.ts b/core/llm/constants.ts index 2b0bf820d..fceec1dc4 100644 --- a/core/llm/constants.ts +++ b/core/llm/constants.ts @@ -1,7 +1,10 @@ -const DEFAULT_MAX_TOKENS = 1000; +const DEFAULT_MAX_TOKENS = 1024; +const DEFAULT_CONTEXT_LENGTH = 4096; +const DEFAULT_TEMPERATURE = 0.5; + const DEFAULT_ARGS = { - max_tokens: DEFAULT_MAX_TOKENS, - temperature: 0.5, + maxTokens: DEFAULT_MAX_TOKENS, + temperature: DEFAULT_TEMPERATURE, }; const CONTEXT_LENGTH_FOR_MODEL: { [name: string]: number } = { @@ -22,6 +25,7 @@ const PROXY_URL = "http://localhost:65433"; export { CONTEXT_LENGTH_FOR_MODEL, DEFAULT_ARGS, + DEFAULT_CONTEXT_LENGTH, DEFAULT_MAX_TOKENS, PROXY_URL, TOKEN_BUFFER_FOR_SAFETY, diff --git a/core/llm/index.ts b/core/llm/index.ts index ea98ceb40..eefaec334 100644 --- a/core/llm/index.ts +++ b/core/llm/index.ts @@ -11,7 +11,12 @@ import { TemplateType, } from ".."; import { ideRequest, ideStreamRequest } from "../ide/messaging"; -import { CONTEXT_LENGTH_FOR_MODEL, DEFAULT_ARGS } from "./constants"; +import { + CONTEXT_LENGTH_FOR_MODEL, + DEFAULT_ARGS, + DEFAULT_CONTEXT_LENGTH, + DEFAULT_MAX_TOKENS, +} from "./constants"; import { compileChatMessages, countTokens, @@ -35,13 +40,13 @@ import { codellamaEditPrompt, deepseekEditPrompt, mistralEditPrompt, + neuralChatEditPrompt, openchatEditPrompt, phindEditPrompt, simplestEditPrompt, simplifiedEditPrompt, xWinCoderEditPrompt, zephyrEditPrompt, - neuralChatEditPrompt, } from "./templates/edit"; const PROVIDER_HANDLES_TEMPLATING: ModelProvider[] = [ @@ -240,11 +245,11 @@ export abstract class BaseLLM implements ILLM { this.uniqueId = options.uniqueId || "None"; this.model = options.model; this.systemMessage = options.systemMessage; - this.contextLength = options.contextLength || 4096; + this.contextLength = options.contextLength || DEFAULT_CONTEXT_LENGTH; this.completionOptions = { ...options.completionOptions, model: options.model || "gpt-4", - maxTokens: options.completionOptions?.maxTokens || 1024, + maxTokens: options.completionOptions?.maxTokens || DEFAULT_MAX_TOKENS, }; this.requestOptions = options.requestOptions; this.promptTemplates = { @@ -283,14 +288,15 @@ export abstract class BaseLLM implements ILLM { options.model !== this.model && options.model in CONTEXT_LENGTH_FOR_MODEL ) { - contextLength = CONTEXT_LENGTH_FOR_MODEL[options.model] || 4096; + contextLength = + CONTEXT_LENGTH_FOR_MODEL[options.model] || DEFAULT_CONTEXT_LENGTH; } return compileChatMessages( options.model, messages, contextLength, - options.maxTokens, + options.maxTokens || DEFAULT_MAX_TOKENS, undefined, functions, this.systemMessage @@ -421,7 +427,7 @@ export abstract class BaseLLM implements ILLM { completionOptions.model, this.contextLength, prompt, - completionOptions.maxTokens + completionOptions.maxTokens || DEFAULT_MAX_TOKENS ); if (!raw) { @@ -466,7 +472,7 @@ export abstract class BaseLLM implements ILLM { completionOptions.model, this.contextLength, prompt, - completionOptions.maxTokens + completionOptions.maxTokens || DEFAULT_MAX_TOKENS ); if (!raw) { diff --git a/core/llm/llms/FreeTrial.ts b/core/llm/llms/FreeTrial.ts index f1fe0e238..27ce0b040 100644 --- a/core/llm/llms/FreeTrial.ts +++ b/core/llm/llms/FreeTrial.ts @@ -15,11 +15,23 @@ class FreeTrial extends BaseLLM { }; } + private _convertArgs(options: CompletionOptions): any { + return { + model: options.model, + frequency_penalty: options.frequencyPenalty, + presence_penalty: options.presencePenalty, + max_tokens: options.maxTokens, + stop: options.stop, + temperature: options.temperature, + top_p: options.topP, + }; + } + protected async *_streamComplete( prompt: string, options: CompletionOptions ): AsyncGenerator { - const args = this.collectArgs(options); + const args = this._convertArgs(this.collectArgs(options)); const response = await this.fetch(`${SERVER_URL}/stream_complete`, { method: "POST", @@ -39,7 +51,7 @@ class FreeTrial extends BaseLLM { messages: ChatMessage[], options: CompletionOptions ): AsyncGenerator { - const args = this.collectArgs(options); + const args = this._convertArgs(this.collectArgs(options)); const response = await this.fetch(`${SERVER_URL}/stream_chat`, { method: "POST", diff --git a/core/llm/llms/LlamaCpp.ts b/core/llm/llms/LlamaCpp.ts index 3a9fd1a5c..ddb0c00a6 100644 --- a/core/llm/llms/LlamaCpp.ts +++ b/core/llm/llms/LlamaCpp.ts @@ -14,6 +14,7 @@ class LlamaCpp extends BaseLLM { frequency_penalty: options.frequencyPenalty, presence_penalty: options.presencePenalty, min_p: options.minP, + mirostat: options.mirostat, }; return finalOptions; @@ -25,7 +26,7 @@ class LlamaCpp extends BaseLLM { ): AsyncGenerator { const headers = { "Content-Type": "application/json", - "Authorization": `Bearer ${this.apiKey}`, + Authorization: `Bearer ${this.apiKey}`, ...this.requestOptions?.headers, }; diff --git a/core/llm/llms/Ollama.ts b/core/llm/llms/Ollama.ts index 4fea77e49..b5c434332 100644 --- a/core/llm/llms/Ollama.ts +++ b/core/llm/llms/Ollama.ts @@ -95,6 +95,7 @@ class Ollama extends BaseLLM { num_predict: options.maxTokens, stop: options.stop, num_ctx: this.contextLength, + mirostat: options.mirostat, }, }; diff --git a/core/llm/llms/index.ts b/core/llm/llms/index.ts index 5c5ccca06..9da9ec2c0 100644 --- a/core/llm/llms/index.ts +++ b/core/llm/llms/index.ts @@ -6,6 +6,7 @@ import { LLMOptions, ModelDescription, } from "../.."; +import { DEFAULT_MAX_TOKENS } from "../constants"; import Anthropic from "./Anthropic"; import Bedrock from "./Bedrock"; import DeepInfra from "./DeepInfra"; @@ -125,7 +126,7 @@ export async function llmFromDescription( maxTokens: finalCompletionOptions.maxTokens || cls.defaultOptions?.completionOptions?.maxTokens || - 1024, + DEFAULT_MAX_TOKENS, }, systemMessage, }; diff --git a/extensions/vscode/config_schema.json b/extensions/vscode/config_schema.json index 390e5ad8f..c2b18ecdf 100644 --- a/extensions/vscode/config_schema.json +++ b/extensions/vscode/config_schema.json @@ -31,6 +31,11 @@ "description": "The frequency penalty of the completion.", "type": "number" }, + "mirostat": { + "title": "Mirostat", + "description": "Enable Mirostat sampling, controlling perplexity during text generation (default: 0, 0 = disabled, 1 = Mirostat, 2 = Mirostat 2.0). Only available for Ollama, LM Studio, and llama.cpp providers", + "type": "number" + }, "stop": { "title": "Stop", "description": "The stop tokens of the completion.", diff --git a/gui/src/redux/selectors/modelSelectors.ts b/gui/src/redux/selectors/modelSelectors.ts index 17aa924ff..a2fb54d18 100644 --- a/gui/src/redux/selectors/modelSelectors.ts +++ b/gui/src/redux/selectors/modelSelectors.ts @@ -1,3 +1,4 @@ +import { DEFAULT_MAX_TOKENS } from "core/llm/constants"; import { RootStore } from "../store"; export const defaultModelSelector = (state: RootStore) => { @@ -6,5 +7,5 @@ export const defaultModelSelector = (state: RootStore) => { }; export const contextLengthSelector = (state: RootStore) => { - return defaultModelSelector(state)?.contextLength || 4096; + return defaultModelSelector(state)?.contextLength || DEFAULT_MAX_TOKENS; };