Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replaceSystemMessage model config option #3787

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 24 additions & 22 deletions core/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ export interface IndexingProgressUpdate {
desc: string;
shouldClearIndexes?: boolean;
status:
| "loading"
| "indexing"
| "done"
| "failed"
| "paused"
| "disabled"
| "cancelled";
| "loading"
| "indexing"
| "done"
| "failed"
| "paused"
| "disabled"
| "cancelled";
debugInfo?: string;
}

Expand Down Expand Up @@ -450,6 +450,7 @@ export interface LLMOptions {
title?: string;
uniqueId?: string;
systemMessage?: string;
replaceSystemMessage?: boolean;
contextLength?: number;
maxStopWords?: number;
completionOptions?: CompletionOptions;
Expand Down Expand Up @@ -650,10 +651,10 @@ export interface IDE {
getCurrentFile(): Promise<
| undefined
| {
isUntitled: boolean;
path: string;
contents: string;
}
isUntitled: boolean;
path: string;
contents: string;
}
>;

getPinnedFiles(): Promise<string[]>;
Expand Down Expand Up @@ -832,11 +833,11 @@ export interface CustomCommand {
export interface Prediction {
type: "content";
content:
| string
| {
type: "text";
text: string;
}[];
| string
| {
type: "text";
text: string;
}[];
}

export interface ToolExtras {
Expand Down Expand Up @@ -903,6 +904,7 @@ export interface ModelDescription {
template?: TemplateType;
completionOptions?: BaseCompletionOptions;
systemMessage?: string;
replaceSystemMessage?: boolean;
requestOptions?: RequestOptions;
promptTemplates?: { [key: string]: string };
capabilities?: ModelCapability;
Expand Down Expand Up @@ -1157,9 +1159,9 @@ export interface Config {
embeddingsProvider?: EmbeddingsProviderDescription | ILLM;
/** The model that Continue will use for tab autocompletions. */
tabAutocompleteModel?:
| CustomLLM
| ModelDescription
| (CustomLLM | ModelDescription)[];
| CustomLLM
| ModelDescription
| (CustomLLM | ModelDescription)[];
/** Options for tab autocomplete */
tabAutocompleteOptions?: Partial<TabAutocompleteOptions>;
/** UI styles customization */
Expand Down Expand Up @@ -1249,9 +1251,9 @@ export type PackageDetailsSuccess = PackageDetails & {
export type PackageDocsResult = {
packageInfo: ParsedPackageInfo;
} & (
| { error: string; details?: never }
| { details: PackageDetailsSuccess; error?: never }
);
| { error: string; details?: never }
| { details: PackageDetailsSuccess; error?: never }
);

export interface TerminalOptions {
reuseTerminal?: boolean;
Expand Down
25 changes: 16 additions & 9 deletions core/llm/countTokens.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Tiktoken, encodingForModel as _encodingForModel } from "js-tiktoken";

import { ChatMessage, MessageContent, MessagePart } from "../index.js";
import { ChatMessage, LLMOptions, MessageContent, MessagePart } from "../index.js";

import { renderChatMessage } from "../util/messageContent.js";
import {
Expand All @@ -27,9 +27,9 @@ class LlamaEncoding implements Encoding {
}

class NonWorkerAsyncEncoder implements AsyncEncoder {
constructor(private readonly encoding: Encoding) {}
constructor(private readonly encoding: Encoding) { }

async close(): Promise<void> {}
async close(): Promise<void> { }

async encode(text: string): Promise<number[]> {
return this.encoding.encode(text);
Expand Down Expand Up @@ -380,15 +380,18 @@ function compileChatMessages(
prompt: string | undefined = undefined,
functions: any[] | undefined = undefined,
systemMessage: string | undefined = undefined,
llmOptions?: LLMOptions,
): ChatMessage[] {
let msgsCopy = msgs
? msgs
.map((msg) => ({ ...msg }))
.filter((msg) => !chatMessageIsEmpty(msg) && msg.role !== "system")
.map((msg) => ({ ...msg }))
.filter((msg) => !chatMessageIsEmpty(msg) && msg.role !== "system")
: [];

msgsCopy = addSpaceToAnyEmptyMessages(msgsCopy);

const replaceSystemMessage = llmOptions?.replaceSystemMessage ?? false;

if (prompt) {
const promptMsg: ChatMessage = {
role: "user",
Expand All @@ -406,11 +409,15 @@ function compileChatMessages(
content = renderChatMessage(msgs?.[0]);
}
if (systemMessage && systemMessage.trim() !== "") {
const shouldAddNewLines = content !== "";
if (shouldAddNewLines) {
content += "\n\n";
if (replaceSystemMessage) {
content = systemMessage;
} else {
const shouldAddNewLines = content !== "";
if (shouldAddNewLines) {
content += "\n\n";
}
content += systemMessage;
}
content += systemMessage;
}
const systemChatMsg: ChatMessage = {
role: "system",
Expand Down
11 changes: 6 additions & 5 deletions core/llm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,11 @@ export abstract class BaseLLM implements ILLM {
options.completionOptions?.maxTokens ??
(llmInfo?.maxCompletionTokens
? Math.min(
llmInfo.maxCompletionTokens,
// Even if the model has a large maxTokens, we don't want to use that every time,
// because it takes away from the context length
this.contextLength / 4,
)
llmInfo.maxCompletionTokens,
// Even if the model has a large maxTokens, we don't want to use that every time,
// because it takes away from the context length
this.contextLength / 4,
)
: DEFAULT_MAX_TOKENS),
};
this.requestOptions = options.requestOptions;
Expand Down Expand Up @@ -261,6 +261,7 @@ export abstract class BaseLLM implements ILLM {
undefined,
functions,
this.systemMessage,
this._llmOptions,
);
}

Expand Down
1 change: 1 addition & 0 deletions core/llm/llms/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export async function llmFromDescription(
cls.defaultOptions?.completionOptions?.maxTokens,
},
systemMessage,
replaceSystemMessage: desc.replaceSystemMessage ?? false,
writeLog,
uniqueId,
};
Expand Down
5 changes: 3 additions & 2 deletions docs/docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ Each model has specific configuration options tailored to its provider and funct
- `template`: Chat template to format messages. Auto-detected for most models but can be overridden. See intelliJ suggestions.
- `promptTemplates`: A mapping of prompt template names (e.g., `edit`) to template strings. [Customization Guide](https://docs.continue.dev/model-setup/configuration#customizing-the-edit-prompt).
- `completionOptions`: Model-specific completion options, same format as top-level [`completionOptions`](#completionoptions), which they override.
- `systemMessage`: A system message that will precede responses from the LLM.
- `systemMessage`: A system message that will precede responses from the LLM. (Note: this property has higher precendence than the root `systemMessage` property)
- `replaceSystemMessage`: If `true`, replaces the system message with the one specified in the model config's `systemMessage` property or with the root `systemMessage` property. If `false`, appends the `systemMessage` to the default system message, if any exists for the model. (default: `false`)
- `requestOptions`: Model-specific HTTP request options, same format as top-level [`requestOptions`](#requestoptions), which they override.
- `apiType`: Specifies the type of API (`openai` or `azure`).
- `apiVersion`: Azure API version (e.g., `2023-07-01-preview`).
Expand Down Expand Up @@ -381,7 +382,7 @@ An optional token that identifies the user, primarily for authenticated services

### `systemMessage`

Defines a system message that appears before every response from the language model, providing guidance or context.
Defines a system message that appears before every response from the language model, providing guidance or context. Note: `systemMessage` in the model config takes precedence over this setting.

### `disableIndexing`

Expand Down
5 changes: 5 additions & 0 deletions extensions/vscode/config_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,11 @@
"description": "A system message that will always be followed by the LLM",
"type": "string"
},
"replaceSystemMessage": {
"title": "Replace System Message",
"description": "If true, the system message will replace the default system message instead of being appended to it",
"type": "boolean"
},
"requestOptions": {
"title": "Request Options",
"description": "Options for the HTTP request to the LLM.",
Expand Down