mirror of
https://github.com/ItzCrazyKns/Perplexica.git
synced 2025-12-17 00:58:15 +00:00
implemented a refactoring plan with the
configurable delay feature. 1. Created AlternatingMessageValidator (renamed from MessageProcessor): -Focused on handling alternating message patterns -Made it model-agnostic with configuration-driven approach -Kept the core validation logic intact 2. Created ReasoningChatModel (renamed from DeepSeekChat): -Made it generic for any model with reasoning/thinking capabilities -Added configurable streaming delay parameter (streamDelay) -Implemented delay logic in the streaming process 3. Updated the DeepSeek provider: -Now uses ReasoningChatModel for deepseek-reasoner with a 50ms delay -Uses standard ChatOpenAI for deepseek-chat -Added a clear distinction between models that need reasoning capabilities Updated references in metaSearchAgent.ts: 4. Changed import from messageProcessor to alternatingMessageValidator -Updated function calls to use the new validator -The configurable delay implementation allows to control the speed of token generation, which can help with the issue you were seeing. The delay is set to 20ms by default for the deepseek-reasoner model, but you can adjust his value in the deepseek.ts provider file to find the optimal speed. This refactoring maintains all the existing functionality while making the code more maintainable and future-proof. The separation of concerns between message validation and model implementation will make it easier to add support for other models with similar requirements in the future.
This commit is contained in:
95
src/utils/alternatingMessageValidator.ts
Normal file
95
src/utils/alternatingMessageValidator.ts
Normal file
@@ -0,0 +1,95 @@
|
||||
// Using the import paths that have been working for you
|
||||
import { BaseMessage, HumanMessage, AIMessage, SystemMessage } from "@langchain/core/messages";
|
||||
import logger from "./logger";
|
||||
|
||||
export interface MessageValidationRules {
|
||||
requireAlternating?: boolean;
|
||||
firstMessageType?: typeof HumanMessage | typeof AIMessage;
|
||||
allowSystem?: boolean;
|
||||
}
|
||||
|
||||
export class AlternatingMessageValidator {
|
||||
private rules: MessageValidationRules;
|
||||
private modelName: string;
|
||||
|
||||
constructor(modelName: string, rules: MessageValidationRules) {
|
||||
this.rules = rules;
|
||||
this.modelName = modelName;
|
||||
}
|
||||
|
||||
processMessages(messages: BaseMessage[]): BaseMessage[] {
|
||||
// Always respect requireAlternating for models that need it
|
||||
if (!this.rules.requireAlternating) {
|
||||
return messages;
|
||||
}
|
||||
|
||||
const processedMessages: BaseMessage[] = [];
|
||||
|
||||
for (let i = 0; i < messages.length; i++) {
|
||||
const currentMsg = messages[i];
|
||||
|
||||
// Handle system messages
|
||||
if (currentMsg instanceof SystemMessage) {
|
||||
if (this.rules.allowSystem) {
|
||||
processedMessages.push(currentMsg);
|
||||
} else {
|
||||
logger.warn(`${this.modelName}: Skipping system message - not allowed`);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// Handle first non-system message
|
||||
if (processedMessages.length === 0 ||
|
||||
processedMessages[processedMessages.length - 1] instanceof SystemMessage) {
|
||||
if (this.rules.firstMessageType &&
|
||||
!(currentMsg instanceof this.rules.firstMessageType)) {
|
||||
logger.warn(`${this.modelName}: Converting first message to required type`);
|
||||
processedMessages.push(new this.rules.firstMessageType({
|
||||
content: currentMsg.content,
|
||||
additional_kwargs: currentMsg.additional_kwargs
|
||||
}));
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Handle alternating pattern
|
||||
const lastMsg = processedMessages[processedMessages.length - 1];
|
||||
if (lastMsg instanceof HumanMessage && currentMsg instanceof HumanMessage) {
|
||||
logger.warn(`${this.modelName}: Skipping consecutive human message`);
|
||||
continue;
|
||||
}
|
||||
if (lastMsg instanceof AIMessage && currentMsg instanceof AIMessage) {
|
||||
logger.warn(`${this.modelName}: Skipping consecutive AI message`);
|
||||
continue;
|
||||
}
|
||||
|
||||
// For deepseek-reasoner, strip out reasoning_content from message history
|
||||
if (this.modelName === 'deepseek-reasoner' && currentMsg instanceof AIMessage) {
|
||||
const { reasoning_content, ...cleanedKwargs } = currentMsg.additional_kwargs;
|
||||
processedMessages.push(new AIMessage({
|
||||
content: currentMsg.content,
|
||||
additional_kwargs: cleanedKwargs
|
||||
}));
|
||||
} else {
|
||||
processedMessages.push(currentMsg);
|
||||
}
|
||||
}
|
||||
|
||||
return processedMessages;
|
||||
}
|
||||
}
|
||||
|
||||
// Pre-configured validators for specific models
|
||||
export const getMessageValidator = (modelName: string): AlternatingMessageValidator | null => {
|
||||
const validators: Record<string, MessageValidationRules> = {
|
||||
'deepseek-reasoner': {
|
||||
requireAlternating: true,
|
||||
firstMessageType: HumanMessage,
|
||||
allowSystem: true
|
||||
},
|
||||
// Add more model configurations as needed
|
||||
};
|
||||
|
||||
const rules = validators[modelName];
|
||||
return rules ? new AlternatingMessageValidator(modelName, rules) : null;
|
||||
};
|
||||
Reference in New Issue
Block a user