refactor(chatbot): enhance EvoaiService with OpenAI integration

- Integrated OpenAI service into the EvoaiService to streamline audio message transcription.
- Updated the constructor to initialize OpenaiService, allowing for direct transcription of audio messages.
- Refactored audio message handling to utilize the OpenAI service for improved reliability and maintainability.
- Adjusted the processing logic to handle transcription results more effectively, ensuring fallback content is provided when transcription fails.

This commit focuses on enhancing the EvoaiService's capabilities by leveraging OpenAI for audio transcription, improving overall service functionality and code structure.
This commit is contained in:
Davidson Gomes 2025-05-21 22:27:59 -03:00
parent 6a0fc19702
commit 5b817028a9
2 changed files with 19 additions and 23 deletions

View File

@ -9,10 +9,13 @@ import { downloadMediaMessage } from 'baileys';
import { v4 as uuidv4 } from 'uuid'; import { v4 as uuidv4 } from 'uuid';
import { BaseChatbotService } from '../../base-chatbot.service'; import { BaseChatbotService } from '../../base-chatbot.service';
import { OpenaiService } from '../../openai/services/openai.service';
export class EvoaiService extends BaseChatbotService<Evoai, EvoaiSetting> { export class EvoaiService extends BaseChatbotService<Evoai, EvoaiSetting> {
private openaiService: OpenaiService;
constructor(waMonitor: WAMonitoringService, prismaRepository: PrismaRepository, configService: ConfigService) { constructor(waMonitor: WAMonitoringService, prismaRepository: PrismaRepository, configService: ConfigService) {
super(waMonitor, prismaRepository, 'EvoaiService', configService); super(waMonitor, prismaRepository, 'EvoaiService', configService);
this.openaiService = new OpenaiService(waMonitor, prismaRepository, configService);
} }
/** /**
@ -42,33 +45,26 @@ export class EvoaiService extends BaseChatbotService<Evoai, EvoaiSetting> {
try { try {
this.logger.debug(`[EvoAI] Processing message with custom process method`); this.logger.debug(`[EvoAI] Processing message with custom process method`);
let contentProcessed = content;
// Check if this is an audio message that we should try to transcribe // Check if this is an audio message that we should try to transcribe
if (msg?.messageType === 'audioMessage' && msg?.message?.audioMessage) { if (this.isAudioMessage(content) && msg) {
this.logger.debug(`[EvoAI] Detected audio message, attempting transcription`);
try { try {
// Download the audio using the whole msg object this.logger.debug(`[Dify] Downloading audio for Whisper transcription`);
const mediaBuffer = await downloadMediaMessage(msg, 'buffer', {}); const transcription = await this.openaiService.speechToText(msg);
this.logger.debug(`[EvoAI] Downloaded audio: ${mediaBuffer?.length || 0} bytes`); if (transcription) {
contentProcessed = transcription;
// Transcribe with OpenAI's Whisper } else {
const transcribedText = await this.speechToText(mediaBuffer); contentProcessed = '[Audio message could not be transcribed]';
this.logger.debug(`[EvoAI] Transcription result: ${transcribedText || 'FAILED'}`);
if (transcribedText) {
// Use the transcribed text instead of the original content
this.logger.debug(`[EvoAI] Using transcribed text: ${transcribedText}`);
// Call the parent process method with the transcribed text
return super.process(instance, remoteJid, bot, session, settings, transcribedText, pushName, msg);
} }
} catch (err) { } catch (err) {
this.logger.error(`[EvoAI] Audio transcription error: ${err}`); this.logger.error(`[Dify] Failed to transcribe audio: ${err}`);
contentProcessed = '[Audio message could not be transcribed]';
} }
} }
// For non-audio messages or if transcription failed, proceed normally // For non-audio messages or if transcription failed, proceed normally
return super.process(instance, remoteJid, bot, session, settings, content, pushName, msg); return super.process(instance, remoteJid, bot, session, settings, contentProcessed, pushName, msg);
} catch (error) { } catch (error) {
this.logger.error(`[EvoAI] Error in process: ${error}`); this.logger.error(`[EvoAI] Error in process: ${error}`);
return; return;

View File

@ -45,11 +45,11 @@ export class ChannelStartupService {
this.chatwootCache, this.chatwootCache,
); );
public typebotService = new TypebotService(waMonitor, this.configService, this.prismaRepository);
public openaiService = new OpenaiService(waMonitor, this.prismaRepository, this.configService); public openaiService = new OpenaiService(waMonitor, this.prismaRepository, this.configService);
public difyService = new DifyService(waMonitor, this.configService, this.prismaRepository); public typebotService = new TypebotService(waMonitor, this.configService, this.prismaRepository, this.openaiService);
public difyService = new DifyService(waMonitor, this.configService, this.prismaRepository, this.openaiService);
public setInstance(instance: InstanceDto) { public setInstance(instance: InstanceDto) {
this.logger.setInstance(instance.instanceName); this.logger.setInstance(instance.instanceName);