mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-08-11 02:42:27 +08:00
centralize all formatter prompt strings
This commit is contained in:
parent
fde644a432
commit
b7d5d926f7
@ -156,4 +156,48 @@ export const FORMATTER_LOGS = {
|
|||||||
CONTEXT_CLEANING: (provider: string) => `Error cleaning content for ${provider}:`,
|
CONTEXT_CLEANING: (provider: string) => `Error cleaning content for ${provider}:`,
|
||||||
ENCODING: 'Error fixing encoding issues:'
|
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'
|
||||||
|
};
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import type { Message } from '../../ai_interface.js';
|
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
|
* Interface for message formatters that handle provider-specific message formatting
|
||||||
@ -69,7 +70,7 @@ export class OpenAIMessageFormatter extends BaseMessageFormatter {
|
|||||||
if (context) {
|
if (context) {
|
||||||
formattedMessages.push({
|
formattedMessages.push({
|
||||||
role: 'system',
|
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
|
// For Claude, wrap context in XML tags for clear separation
|
||||||
if (context) {
|
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
|
// Add system message if we have content
|
||||||
@ -141,7 +142,7 @@ export class OllamaMessageFormatter extends BaseMessageFormatter {
|
|||||||
|
|
||||||
// Add context to system prompt
|
// Add context to system prompt
|
||||||
if (context) {
|
if (context) {
|
||||||
systemContent += `\n\nReference information:\n${context}`;
|
systemContent += MESSAGE_FORMATTER_TEMPLATES.OLLAMA.REFERENCE_INFORMATION + context;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add system message if we have content
|
// Add system message if we have content
|
||||||
@ -183,7 +184,7 @@ export class DefaultMessageFormatter extends BaseMessageFormatter {
|
|||||||
if (context) {
|
if (context) {
|
||||||
formattedMessages.push({
|
formattedMessages.push({
|
||||||
role: 'user',
|
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 {
|
export class MessageFormatterFactory {
|
||||||
private static formatters: Record<string, MessageFormatter> = {
|
private static formatters: Record<string, MessageFormatter> = {
|
||||||
openai: new OpenAIMessageFormatter(),
|
[PROVIDER_IDENTIFIERS.OPENAI]: new OpenAIMessageFormatter(),
|
||||||
anthropic: new AnthropicMessageFormatter(),
|
[PROVIDER_IDENTIFIERS.ANTHROPIC]: new AnthropicMessageFormatter(),
|
||||||
ollama: new OllamaMessageFormatter(),
|
[PROVIDER_IDENTIFIERS.OLLAMA]: new OllamaMessageFormatter(),
|
||||||
default: new DefaultMessageFormatter()
|
[PROVIDER_IDENTIFIERS.DEFAULT]: new DefaultMessageFormatter()
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -211,7 +212,7 @@ export class MessageFormatterFactory {
|
|||||||
* @returns Message formatter for that provider
|
* @returns Message formatter for that provider
|
||||||
*/
|
*/
|
||||||
static getFormatter(provider: string): MessageFormatter {
|
static getFormatter(provider: string): MessageFormatter {
|
||||||
return this.formatters[provider] || this.formatters.default;
|
return this.formatters[provider] || this.formatters[PROVIDER_IDENTIFIERS.DEFAULT];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user