diff --git a/src/services/llm/constants/formatter_constants.ts b/src/services/llm/constants/formatter_constants.ts index 8046ca24b..d0e745c1c 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\n', + CONTEXT_END: '\n' + }, + + /** + * 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.ts index 9fc9f19f4..98a20f223 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 separation if (context) { - systemContent += `\n\n\n${context}\n`; + 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 prompt if (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 = { - 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]; } /**