centralize all formatter prompt strings

This commit is contained in:
perf3ct 2025-04-02 17:29:53 +00:00
parent fde644a432
commit b7d5d926f7
No known key found for this signature in database
GPG Key ID: 569C4EEC436F5232
2 changed files with 55 additions and 10 deletions

View File

@ -156,4 +156,48 @@ export const FORMATTER_LOGS = {
CONTEXT_CLEANING: (provider: string) => `Error cleaning content for ${provider}:`,
ENCODING: 'Error fixing encoding issues:'
}
};
};
/**
* 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'
};

View File

@ -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<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 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<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];
}
/**