mirror of
https://github.com/hexastack/hexabot
synced 2025-06-26 18:27:28 +00:00
fix: nlp sample populate entities
This commit is contained in:
parent
9f0f5b0c1d
commit
78ad9ea025
@ -16,6 +16,8 @@ import { AttachmentModel } from '@/attachment/schemas/attachment.schema';
|
||||
import { AttachmentService } from '@/attachment/services/attachment.service';
|
||||
import { ChannelModule } from '@/channel/channel.module';
|
||||
import { CmsModule } from '@/cms/cms.module';
|
||||
import { NlpModule } from '@/nlp/nlp.module';
|
||||
import { NlpSampleModel } from '@/nlp/schemas/nlp-sample.schema';
|
||||
import { UserModule } from '@/user/user.module';
|
||||
|
||||
import { BlockController } from './controllers/block.controller';
|
||||
@ -62,12 +64,14 @@ import { SubscriberService } from './services/subscriber.service';
|
||||
ConversationModel,
|
||||
SubscriberModel,
|
||||
AttachmentModel,
|
||||
NlpSampleModel,
|
||||
]),
|
||||
forwardRef(() => ChannelModule),
|
||||
CmsModule,
|
||||
AttachmentModule,
|
||||
EventEmitter2,
|
||||
UserModule,
|
||||
NlpModule,
|
||||
],
|
||||
controllers: [
|
||||
CategoryController,
|
||||
|
@ -29,6 +29,7 @@ import { Conversation } from '../schemas/conversation.schema';
|
||||
import { SubscriberDocument } from '../schemas/subscriber.schema';
|
||||
import { OutgoingMessage } from '../schemas/types/message';
|
||||
|
||||
import { NlpSampleService } from './../../nlp/services/nlp-sample.service';
|
||||
import { BotService } from './bot.service';
|
||||
import { ConversationService } from './conversation.service';
|
||||
import { MessageService } from './message.service';
|
||||
@ -46,6 +47,7 @@ export class ChatService {
|
||||
private readonly websocketGateway: WebsocketGateway,
|
||||
private readonly helperService: HelperService,
|
||||
private readonly attachmentService: AttachmentService,
|
||||
private nlpSampleService: NlpSampleService,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@ -142,6 +144,15 @@ export class ChatService {
|
||||
this.logger.debug('Logging message', received);
|
||||
try {
|
||||
const msg = await this.messageService.create(received);
|
||||
const nlpEntites = event.getNLP();
|
||||
|
||||
if (nlpEntites?.entities) {
|
||||
await this.nlpSampleService.upgradeSampleWithEntities(
|
||||
nlpEntites.entities,
|
||||
msg,
|
||||
);
|
||||
}
|
||||
|
||||
const populatedMsg = await this.messageService.findOneAndPopulate(msg.id);
|
||||
|
||||
if (!populatedMsg) {
|
||||
@ -340,9 +351,6 @@ export class ChatService {
|
||||
await event.preprocess();
|
||||
}
|
||||
|
||||
// Trigger message received event
|
||||
this.eventEmitter.emit('hook:chatbot:received', event);
|
||||
|
||||
if (subscriber?.assignedTo) {
|
||||
this.logger.debug('Conversation taken over', subscriber.assignedTo);
|
||||
return;
|
||||
@ -358,6 +366,9 @@ export class ChatService {
|
||||
}
|
||||
}
|
||||
|
||||
// Trigger message received event
|
||||
this.eventEmitter.emit('hook:chatbot:received', event);
|
||||
|
||||
this.botService.handleMessageEvent(event);
|
||||
} catch (err) {
|
||||
this.logger.error('Error handling new message', err);
|
||||
|
@ -83,7 +83,6 @@ describe('NlpSampleService', () => {
|
||||
NlpEntityService,
|
||||
NlpValueService,
|
||||
LanguageService,
|
||||
|
||||
{
|
||||
provide: CACHE_MANAGER,
|
||||
useValue: {
|
||||
|
@ -16,6 +16,7 @@ import { Document, Query } from 'mongoose';
|
||||
import Papa from 'papaparse';
|
||||
|
||||
import { Message } from '@/chat/schemas/message.schema';
|
||||
import { NLU } from '@/helper/types';
|
||||
import { Language } from '@/i18n/schemas/language.schema';
|
||||
import { LanguageService } from '@/i18n/services/language.service';
|
||||
import { DeleteResult } from '@/utils/generics/base-repository';
|
||||
@ -304,4 +305,46 @@ export class NlpSampleService extends BaseService<
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async upgradeSampleWithEntities(
|
||||
entities: NLU.ParseEntity[],
|
||||
createdMessage: Message,
|
||||
) {
|
||||
if (!('text' in createdMessage.message)) {
|
||||
this.logger.warn('Received message without text attribute');
|
||||
return;
|
||||
}
|
||||
const inferredLanguage = entities.find((e) => e.entity === 'language');
|
||||
const entitiesWithoutLanguage = entities.filter(
|
||||
(e) => e.entity !== 'language',
|
||||
);
|
||||
|
||||
const foundSample = await this.repository.findOne({
|
||||
text: createdMessage.message.text,
|
||||
type: 'inbox',
|
||||
});
|
||||
if (!foundSample) {
|
||||
return;
|
||||
}
|
||||
|
||||
await this.nlpSampleEntityService.storeSampleEntities(
|
||||
foundSample,
|
||||
entitiesWithoutLanguage,
|
||||
);
|
||||
|
||||
const language = await this.languageService.findOne(
|
||||
{ code: inferredLanguage?.value },
|
||||
undefined,
|
||||
{ _id: 1 },
|
||||
);
|
||||
|
||||
if (!language) {
|
||||
this.logger.warn('Unable to find inferred language', inferredLanguage);
|
||||
}
|
||||
|
||||
await this.repository.updateOne(foundSample.id, {
|
||||
type: 'train',
|
||||
...(language && { language: language.id }),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user