Skip to content

Commit

Permalink
feat(frontend): apply nlp language updates
Browse files Browse the repository at this point in the history
  • Loading branch information
marrouchi committed Sep 23, 2024
1 parent c304992 commit fa055fa
Show file tree
Hide file tree
Showing 12 changed files with 221 additions and 98 deletions.
1 change: 1 addition & 0 deletions api/src/chat/repositories/message.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export class MessageRepository extends BaseRepository<
text: _doc.message.text,
type: NlpSampleState.inbox,
trained: false,
// @TODO : We need to define the language in the message entity
language: defaultLang.id,
};
try {
Expand Down
10 changes: 10 additions & 0 deletions api/src/i18n/services/language.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,14 @@ export class LanguageService extends BaseService<Language> {
async getDefaultLanguage() {
return await this.findOne({ default: true });
}

/**
* Retrieves the language by code.
*
* @returns A promise that resolves to the `Language` object.
*/
@Cacheable(DEFAULT_LANGUAGE_CACHE_KEY)
async getLanguageByCode(code: string) {
return await this.findOne({ code });
}
}
9 changes: 3 additions & 6 deletions api/src/nlp/controllers/nlp-sample.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ describe('NlpSampleController', () => {
trained: true,
type: NlpSampleState.test,
entities: [],
language: enLang.id,
language: 'en',
};
const result = await nlpSampleController.create(nlSample);
expect(result).toEqualPayload({
Expand Down Expand Up @@ -279,7 +279,7 @@ describe('NlpSampleController', () => {
value: 'update',
},
],
language: frLang.id,
language: 'fr',
});
const updatedSample = {
text: 'updated',
Expand All @@ -302,15 +302,12 @@ describe('NlpSampleController', () => {
});

it('should throw exception when nlp sample id not found', async () => {
const frLang = await languageService.findOne({
code: 'fr',
});
await expect(
nlpSampleController.updateOne(byeJhonSampleId, {
text: 'updated',
trained: true,
type: NlpSampleState.test,
language: frLang.id,
language: 'fr',
}),
).rejects.toThrow(NotFoundException);
});
Expand Down
32 changes: 19 additions & 13 deletions api/src/nlp/controllers/nlp-sample.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,21 +122,24 @@ export class NlpSampleController extends BaseController<
@CsrfCheck(true)
@Post()
async create(
@Body() { entities: nlpEntities, ...createNlpSampleDto }: NlpSampleDto,
@Body()
{
entities: nlpEntities,
language: languageCode,
...createNlpSampleDto
}: NlpSampleDto,
): Promise<NlpSampleFull> {
const nlpSample = await this.nlpSampleService.create(
createNlpSampleDto as NlpSampleCreateDto,
);
const language = await this.languageService.getLanguageByCode(languageCode);
const nlpSample = await this.nlpSampleService.create({
...createNlpSampleDto,
language: language.id,
});

const entities = await this.nlpSampleEntityService.storeSampleEntities(
nlpSample,
nlpEntities,
);

const language = await this.languageService.findOne(
createNlpSampleDto.language,
);

return {
...nlpSample,
entities,
Expand Down Expand Up @@ -250,7 +253,11 @@ export class NlpSampleController extends BaseController<
async findPage(
@Query(PageQueryPipe) pageQuery: PageQueryDto<NlpSample>,
@Query(PopulatePipe) populate: string[],
@Query(new SearchFilterPipe<NlpSample>({ allowedFields: ['text', 'type'] }))
@Query(
new SearchFilterPipe<NlpSample>({
allowedFields: ['text', 'type', 'language'],
}),
)
filters: TFilterQuery<NlpSample>,
) {
return this.canPopulate(populate)
Expand All @@ -270,11 +277,12 @@ export class NlpSampleController extends BaseController<
@Patch(':id')
async updateOne(
@Param('id') id: string,
@Body() updateNlpSampleDto: NlpSampleDto,
@Body() { entities, language: languageCode, ...sampleAttrs }: NlpSampleDto,
): Promise<NlpSampleFull> {
const { entities, ...sampleAttrs } = updateNlpSampleDto;
const language = await this.languageService.getLanguageByCode(languageCode);
const sample = await this.nlpSampleService.updateOne(id, {
...sampleAttrs,
language: language.id,
trained: false,
});

Expand All @@ -288,8 +296,6 @@ export class NlpSampleController extends BaseController<
const updatedSampleEntities =
await this.nlpSampleEntityService.storeSampleEntities(sample, entities);

const language = await this.languageService.findOne(sampleAttrs.language);

return {
...sample,
language,
Expand Down
7 changes: 6 additions & 1 deletion api/src/nlp/dto/nlp-sample.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class NlpSampleCreateDto {
@IsOptional()
type?: NlpSampleState;

@ApiProperty({ description: 'NLP sample language', type: String })
@ApiProperty({ description: 'NLP sample language id', type: String })
@IsString()
@IsNotEmpty()
@IsObjectId({ message: 'Language must be a valid ObjectId' })
Expand All @@ -56,6 +56,11 @@ export class NlpSampleDto extends NlpSampleCreateDto {
})
@IsOptional()
entities?: NlpSampleEntityValue[];

@ApiProperty({ description: 'NLP sample language code', type: String })
@IsString()
@IsNotEmpty()
language: string;
}

export class NlpSampleUpdateDto extends PartialType(NlpSampleCreateDto) {}
13 changes: 7 additions & 6 deletions frontend/src/components/nlp/NlpSampleDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ import { DialogControlProps } from "@/hooks/useDialog";
import { useToast } from "@/hooks/useToast";
import { EntityType } from "@/services/types";
import {
INlpDatasetSample,
INlpDatasetSampleAttributes,
INlpSampleFormAttributes,
INlpSampleFull,
} from "@/types/nlp-sample.types";

import NlpDatasetSample from "./components/NlpTrainForm";

export type NlpSampleDialogProps = DialogControlProps<INlpSampleFull>;
export type NlpSampleDialogProps = DialogControlProps<INlpDatasetSample>;
export const NlpSampleDialog: FC<NlpSampleDialogProps> = ({
open,
data: sample,
Expand All @@ -44,15 +44,16 @@ export const NlpSampleDialog: FC<NlpSampleDialogProps> = ({
toast.success(t("message.success_save"));
},
});
const onSubmitForm = (params: INlpSampleFormAttributes) => {
const onSubmitForm = (form: INlpSampleFormAttributes) => {
if (sample?.id) {
updateSample(
{
id: sample.id,
params: {
text: params.text,
type: params.type,
entities: [...params.keywordEntities, ...params.traitEntities],
text: form.text,
type: form.type,
entities: [...form.keywordEntities, ...form.traitEntities],
language: form.language,
},
},
{
Expand Down
Loading

0 comments on commit fa055fa

Please sign in to comment.