From e5ed4d3dc2821fc5491823028f2e42ac89e62f4e Mon Sep 17 00:00:00 2001 From: Patrick Erichsen Date: Wed, 9 Apr 2025 15:16:27 -0700 Subject: [PATCH] remove `baseChatSystemMessage` from ILLM --- core/index.d.ts | 4 +- core/llm/constructMessages.ts | 7 ++- core/llm/index.ts | 45 +++++++++++++++---- gui/src/redux/thunks/streamResponse.ts | 5 ++- .../thunks/streamResponseAfterToolCall.ts | 5 ++- 5 files changed, 51 insertions(+), 15 deletions(-) diff --git a/core/index.d.ts b/core/index.d.ts index c9e8092be..a1ac7177a 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -592,9 +592,6 @@ export interface LLMOptions { maxEmbeddingChunkSize?: number; maxEmbeddingBatchSize?: number; - // Chat options - baseChatSystemMessage?: string; - // Cloudflare options accountId?: string; @@ -1043,6 +1040,7 @@ export interface ModelDescription { template?: TemplateType; completionOptions?: BaseCompletionOptions; systemMessage?: string; + baseChatSystemMessage?: string; requestOptions?: RequestOptions; promptTemplates?: { [key: string]: string }; cacheBehavior?: CacheBehavior; diff --git a/core/llm/constructMessages.ts b/core/llm/constructMessages.ts index 69425d33e..ff6f34b9d 100644 --- a/core/llm/constructMessages.ts +++ b/core/llm/constructMessages.ts @@ -8,7 +8,10 @@ const DEFAULT_SYSTEM_MESSAGE = ` const CANCELED_TOOL_CALL_MESSAGE = "This tool call was cancelled by the user. You should clarify next steps, as they don't wish for you to use this tool."; -export function constructMessages(history: ChatHistoryItem[]): ChatMessage[] { +export function constructMessages( + history: ChatHistoryItem[], + baseChatSystemMessage: string | undefined, +): ChatMessage[] { const filteredHistory = history.filter( (item) => item.message.role !== "system", ); @@ -16,7 +19,7 @@ export function constructMessages(history: ChatHistoryItem[]): ChatMessage[] { msgs.push({ role: "system", - content: DEFAULT_SYSTEM_MESSAGE, + content: baseChatSystemMessage ?? DEFAULT_SYSTEM_MESSAGE, }); for (let i = 0; i < filteredHistory.length; i++) { diff --git a/core/llm/index.ts b/core/llm/index.ts index 41b174213..723ab2f45 100644 --- a/core/llm/index.ts +++ b/core/llm/index.ts @@ -194,7 +194,7 @@ export abstract class BaseLLM implements ILLM { this.title = options.title; this.uniqueId = options.uniqueId ?? "None"; - this.baseSystemMessage = options.baseSystemMessage; + this.systemMessage = options.systemMessage; this.contextLength = options.contextLength ?? llmInfo?.contextLength ?? DEFAULT_CONTEXT_LENGTH; this.maxStopWords = options.maxStopWords ?? this.maxStopWords; @@ -305,7 +305,7 @@ export abstract class BaseLLM implements ILLM { supportsImages: this.supportsImages(), prompt: undefined, functions, - baseSystemMessage: this.baseSystemMessage, + systemMessage: this.systemMessage, rules: this.rules ?? [], }); } @@ -317,14 +317,43 @@ export abstract class BaseLLM implements ILLM { const msgs: ChatMessage[] = [{ role: "user", content: prompt }]; - const baseSystemMessage = this.baseSystemMessage; - if (baseSystemMessage) { - msgs.unshift({ role: "system", content: baseSystemMessage }); + const systemMessage = this.systemMessage; + if (systemMessage) { + msgs.unshift({ role: "system", content: systemMessage }); } return this.templateMessages(msgs); } + private _compilePromptForLog( + prompt: string, + completionOptions: CompletionOptions, + ): string { + const completionOptionsLog = JSON.stringify( + { + contextLength: this.contextLength, + ...completionOptions, + }, + null, + 2, + ); + + let requestOptionsLog = ""; + if (this.requestOptions) { + requestOptionsLog = JSON.stringify(this.requestOptions, null, 2); + } + + return ( + "##### Completion options #####\n" + + completionOptionsLog + + (requestOptionsLog + ? "\n\n##### Request options #####\n" + requestOptionsLog + : "") + + "\n\n##### Prompt #####\n" + + prompt + ); + } + private _logEnd( model: string, prompt: string, @@ -1143,13 +1172,13 @@ export abstract class BaseLLM implements ILLM { public renderPromptTemplate( template: PromptTemplate, history: ChatMessage[], - templateVariables: Record, + otherData: Record, canPutWordsInModelsMouth = false, ): string | ChatMessage[] { if (typeof template === "string") { const data: any = { history: history, - ...templateVariables, + ...otherData, }; if (history.length > 0 && history[0].role === "system") { data.system_message = history.shift()!.content; @@ -1159,7 +1188,7 @@ export abstract class BaseLLM implements ILLM { return compiledTemplate(data); } const rendered = template(history, { - ...templateVariables, + ...otherData, supportsCompletions: this.supportsCompletions() ? "true" : "false", supportsPrefill: this.supportsPrefill() ? "true" : "false", }); diff --git a/gui/src/redux/thunks/streamResponse.ts b/gui/src/redux/thunks/streamResponse.ts index e79bcdf85..c5442da55 100644 --- a/gui/src/redux/thunks/streamResponse.ts +++ b/gui/src/redux/thunks/streamResponse.ts @@ -85,7 +85,10 @@ export const streamResponseThunk = createAsyncThunk< // Construct messages from updated history const updatedHistory = getState().session.history; - const messages = constructMessages([...updatedHistory]); + const messages = constructMessages( + [...updatedHistory], + selectedChatModel?.baseChatSystemMessage, + ); posthog.capture("step run", { step_name: "User Input", diff --git a/gui/src/redux/thunks/streamResponseAfterToolCall.ts b/gui/src/redux/thunks/streamResponseAfterToolCall.ts index 3fa335d8b..9cb6a657f 100644 --- a/gui/src/redux/thunks/streamResponseAfterToolCall.ts +++ b/gui/src/redux/thunks/streamResponseAfterToolCall.ts @@ -59,7 +59,10 @@ export const streamResponseAfterToolCall = createAsyncThunk< dispatch(setActive()); const updatedHistory = getState().session.history; - const messages = constructMessages([...updatedHistory]); + const messages = constructMessages( + [...updatedHistory], + selectedChatModel?.baseChatSystemMessage, + ); unwrapResult(await dispatch(streamNormalInput({ messages }))); }), );