Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
centralize all formatter prompt strings
perf3ctb7d5d926f7bc
diff --git a/src/services/llm/constants/formatter_constants.ts b/src/services/llm/constants/formatter_constants.tsindex 8046ca24bd..d0e745c1c2 100644--- a/src/services/llm/constants/formatter_constants.ts+++ b/src/services/llm/constants/formatter_constants.ts@@ -156,4 +156,48 @@ export const FORMATTER_LOGS = {CONTEXT_CLEANING: (provider: string) => `Error cleaning content for ${provider}:`,ENCODING: 'Error fixing encoding issues:'}-};\ No newline at end of file+};++/**+ * Message formatter text templates+ */+export const MESSAGE_FORMATTER_TEMPLATES = {+ /**+ * OpenAI-specific message templates+ */+ OPENAI: {+ CONTEXT_INSTRUCTION: 'Please use the following context to respond to the user\'s messages:\n\n'+ },++ /**+ * Anthropic-specific message templates+ */+ ANTHROPIC: {+ CONTEXT_START: '\n\n<context>\n',+ CONTEXT_END: '\n</context>'+ },++ /**+ * Ollama-specific message templates+ */+ OLLAMA: {+ REFERENCE_INFORMATION: '\n\nReference information:\n'+ },++ /**+ * Default formatter message templates+ */+ DEFAULT: {+ CONTEXT_INSTRUCTION: 'Here is context to help you answer my questions: '+ }+};++/**+ * Provider identifier constants+ */+export const PROVIDER_IDENTIFIERS = {+ OPENAI: 'openai',+ ANTHROPIC: 'anthropic',+ OLLAMA: 'ollama',+ DEFAULT: 'default'+};diff --git a/src/services/llm/pipeline/interfaces/message_formatter.ts b/src/services/llm/pipeline/interfaces/message_formatter.tsindex 9fc9f19f4a..98a20f2236 100644--- a/src/services/llm/pipeline/interfaces/message_formatter.ts+++ b/src/services/llm/pipeline/interfaces/message_formatter.ts@@ -1,4 +1,5 @@import type { Message } from '../../ai_interface.js';+import { MESSAGE_FORMATTER_TEMPLATES, PROVIDER_IDENTIFIERS } from '../../constants/formatter_constants.js';/*** Interface for message formatters that handle provider-specific message formatting@@ -69,7 +70,7 @@ export class OpenAIMessageFormatter extends BaseMessageFormatter {if (context) {formattedMessages.push({role: 'system',- content: `Please use the following context to respond to the user's messages:\n\n${context}`+ content: MESSAGE_FORMATTER_TEMPLATES.OPENAI.CONTEXT_INSTRUCTION + context});}@@ -102,7 +103,7 @@ export class AnthropicMessageFormatter extends BaseMessageFormatter {// For Claude, wrap context in XML tags for clear separationif (context) {- systemContent += `\n\n<context>\n${context}\n</context>`;+ systemContent += MESSAGE_FORMATTER_TEMPLATES.ANTHROPIC.CONTEXT_START + context + MESSAGE_FORMATTER_TEMPLATES.ANTHROPIC.CONTEXT_END;}// Add system message if we have content@@ -141,7 +142,7 @@ export class OllamaMessageFormatter extends BaseMessageFormatter {// Add context to system promptif (context) {- systemContent += `\n\nReference information:\n${context}`;+ systemContent += MESSAGE_FORMATTER_TEMPLATES.OLLAMA.REFERENCE_INFORMATION + context;}// Add system message if we have content@@ -183,7 +184,7 @@ export class DefaultMessageFormatter extends BaseMessageFormatter {if (context) {formattedMessages.push({role: 'user',- content: `Here is context to help you answer my questions: ${context}`+ content: MESSAGE_FORMATTER_TEMPLATES.DEFAULT.CONTEXT_INSTRUCTION + context});}@@ -199,10 +200,10 @@ export class DefaultMessageFormatter extends BaseMessageFormatter {*/export class MessageFormatterFactory {private static formatters: Record<string, MessageFormatter> = {- openai: new OpenAIMessageFormatter(),- anthropic: new AnthropicMessageFormatter(),- ollama: new OllamaMessageFormatter(),- default: new DefaultMessageFormatter()+ [PROVIDER_IDENTIFIERS.OPENAI]: new OpenAIMessageFormatter(),+ [PROVIDER_IDENTIFIERS.ANTHROPIC]: new AnthropicMessageFormatter(),+ [PROVIDER_IDENTIFIERS.OLLAMA]: new OllamaMessageFormatter(),+ [PROVIDER_IDENTIFIERS.DEFAULT]: new DefaultMessageFormatter()};/**@@ -211,7 +212,7 @@ export class MessageFormatterFactory {* @returns Message formatter for that provider*/static getFormatter(provider: string): MessageFormatter {- return this.formatters[provider] || this.formatters.default;+ return this.formatters[provider] || this.formatters[PROVIDER_IDENTIFIERS.DEFAULT];}/**