Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions backend/src/modules/chat/chat.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { JwtModule } from '@nestjs/jwt';
import { Chat, ChatSchema } from './infra/chat.schema';
import { ChatRepositoryMongodb } from './infra/chat.repository.mongodb';
import { ChatService } from './application/chat.service';
Expand All @@ -10,7 +9,7 @@ import { MessageRepositoryMongodb } from '../Messages/infra/message.repository.m
import { ChatController } from './presentation/chat.controller';
import { TicketSchemaClass, TicketSchema } from '../ticket/infra/schemas/ticket.mongo.schema';
import { User, UserSchema } from '../user/user.schema';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { SharedModule } from '../shared/shared.module';

@Module({
imports: [
Expand All @@ -20,13 +19,7 @@ import { ConfigModule, ConfigService } from '@nestjs/config';
{ name: TicketSchemaClass.name, schema: TicketSchema },
{ name: User.name, schema: UserSchema },
]),
JwtModule.registerAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (configService: ConfigService) => ({
secret: configService.get<string>('JWT_SECRET'),
}),
}),
SharedModule,
],
controllers: [ChatController],
providers: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class CreateTicketUseCase {

const ticket = Ticket.create({
...input,
category: triageResult.category,
category: triageResult.categoryId,
clientId: clientId,
});

Expand Down
19 changes: 16 additions & 3 deletions backend/src/modules/triage/application/triage.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ export class TriageService {
const ruleMatch = await this.rulesEngine.match(description);

if (ruleMatch) {
return new Category(ruleMatch.category, ruleMatch.confidence, 'rule');
return new Category(
ruleMatch.categoryId,
ruleMatch.category,
ruleMatch.confidence,
'rule',
);
}

const nlpResult = await this.nlp.classify(description);
Expand All @@ -28,13 +33,21 @@ export class TriageService {
nlpResult.category &&
nlpResult.confidence >= this.CONFIDENCE_THRESHOLD
) {
return new Category(nlpResult.category, nlpResult.confidence, 'nlp');
const fallbackCategory = await this.categoryService.findByName('OTHER');

return new Category(
nlpResult.categoryId ?? fallbackCategory.id,
nlpResult.category,
nlpResult.confidence,
'nlp',
);
}

const fallbackCategory = await this.categoryService.findByName('OTHER');

return new Category(
normalizeIntent(fallbackCategory.name),
fallbackCategory.id,
fallbackCategory.name,
0.5,
'fallback',
);
Expand Down
1 change: 1 addition & 0 deletions backend/src/modules/triage/domain/category.entity.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export class Category {
constructor(
public categoryId: string,
public category: string,
public confidence: number,
public source: 'rule' | 'nlp' | 'fallback',
Expand Down
31 changes: 22 additions & 9 deletions backend/src/modules/triage/infra/nlp.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ import { normalizeIntent } from '../../shared/utils/intent-normalizer';

@Injectable()
export class NlpProvider implements OnModuleInit {
constructor(private readonly categoryService: CategoryService) {}
constructor(private readonly categoryService: CategoryService) { }
private manager: NlpManager;

async onModuleInit() {
this.manager = new NlpManager({ languages: ['pt'] });

await this.addTrainingData();

await this.manager.train();
}

Expand All @@ -29,15 +28,29 @@ export class NlpProvider implements OnModuleInit {
}

async classify(text: string) {
const normalized = this.normalize(text);
const normalized = this.normalize(text);
const result = await this.manager.process('pt', normalized);

if (result.intent === 'None') {
return {
categoryId: null,
category: null,
confidence: result.score,
};
}

const categories = await this.categoryService.findAll();

const result = await this.manager.process('pt', normalized);
const found = categories.find(
(cat) => normalizeIntent(cat.name) === result.intent,
);

return {
category: result.intent === 'None' ? null : result.intent,
confidence: result.score,
};
}
return {
categoryId: found?.id || null,
category: found?.name || null,
confidence: result.score,
};
}

private normalize(text: string): string {
return text.toLowerCase().replace(/\n/g, ' ').slice(0, 500);
Expand Down
6 changes: 3 additions & 3 deletions backend/src/modules/triage/infra/rules.engine.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Injectable } from "@nestjs/common";
import { CategoryService } from "../../category/category.service";
import { normalizeIntent } from '../../shared/utils/intent-normalizer';

@Injectable()
export class RulesEngine {
Expand All @@ -9,13 +8,14 @@ export class RulesEngine {
async match(text: string) {
const categories = await this.categoryService.findAll();

const matches: { category: string; confidence: number }[] = [];
const matches: { categoryId: string; category: string; confidence: number }[] = [];

for (const category of categories) {
for (const keyword of category.keywords || []) {
if (text.toLowerCase().includes(keyword.toLowerCase())) {
matches.push({
category: normalizeIntent(category.name),
categoryId: category.id,
category: category.name,
confidence: 0.95,
});
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ describe('TriageController', () => {

const mockResponse =
new Category(
'1',
'WEB_APP',
0.95,
'rule'
Expand Down Expand Up @@ -78,6 +79,7 @@ describe('TriageController', () => {

const mockResponse =
new Category(
'999',
'OTHER',
0.5,
'fallback'
Expand Down Expand Up @@ -113,6 +115,7 @@ describe('TriageController', () => {
triageService.classify
.mockResolvedValue(
new Category(
'2',
'BI',
0.9,
'rule'
Expand Down
Loading