Skip to content

Commit

Permalink
[FEATURE] 영어공지 자동 번역 기능
Browse files Browse the repository at this point in the history
Fixes #89
  • Loading branch information
siwonpada committed Nov 1, 2024
1 parent 053e930 commit 422545c
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
"cSpell.words": [
"axios",
"datasources",
"datetime",
"dbml",
"deepl",
"gistory",
"gsainfoteam",
"infoteam",
Expand Down
43 changes: 43 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"class-validator": "^0.14.1",
"cookie-parser": "^1.4.6",
"dayjs": "^1.11.10",
"deepl-node": "^1.14.0",
"express-basic-auth": "^1.2.1",
"firebase-admin": "^12.1.0",
"html-to-text": "^9.0.5",
Expand Down
5 changes: 1 addition & 4 deletions src/ai/ai.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ export class AiController {
async translate(
@Body() translateDto: TranslateDto,
): Promise<TranslateResDto> {
return {
text: translateDto.text,
lang: translateDto.targetLang,
};
return this.aiService.translate(translateDto);
}
}
39 changes: 36 additions & 3 deletions src/ai/ai.service.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
import { Injectable } from '@nestjs/common';
import {
Injectable,
InternalServerErrorException,
Logger,
} from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { DeadlineResponse } from './types/deadlineResponse.type';
import OpenAI from 'openai';
import OpenAI, { InternalServerError } from 'openai';
import {
ChatCompletionMessageParam,
ChatCompletionSystemMessageParam,
ChatCompletionUserMessageParam,
} from 'openai/resources';
import * as deepl from 'deepl-node';
import { TranslateResDto } from './dto/res/translateRes.dto';
import { TranslateDto } from './dto/req/translate.dto';

@Injectable()
export class AiService {
private readonly logger = new Logger(AiService.name, {
timestamp: true,
});
private readonly openai: OpenAI;
private readonly translator: deepl.Translator;
constructor(private readonly configService: ConfigService) {
this.openai = new OpenAI({
apiKey: this.configService.get<string>('OPENAI_API_KEY'),
apiKey: this.configService.getOrThrow<string>('OPENAI_API_KEY'),
});
this.translator = new deepl.Translator(
this.configService.getOrThrow<string>('DEEPL_API_KEY'),
);
}

async detectDeadline(body: string, createdAt: Date): Promise<Date | null> {
Expand Down Expand Up @@ -64,4 +79,22 @@ export class AiService {
return null;
}
}

async translate({
text,
targetLang,
}: TranslateDto): Promise<TranslateResDto> {
this.logger.log(`translate called`);
const result = await this.translator
.translateText(text, null, targetLang)
.catch((error) => {
this.logger.error(`deepl translation error: ${error.message}`);
throw new InternalServerErrorException(error.message);
});
this.logger.log(`translate finished`);
return {
text: result.text,
lang: targetLang,
};
}
}
10 changes: 9 additions & 1 deletion src/ai/dto/req/translate.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { ApiProperty } from '@nestjs/swagger';
import { Transform } from 'class-transformer';
import { IsLocale, IsString } from 'class-validator';
import * as deepl from 'deepl-node';

export class TranslateDto {
@ApiProperty({
Expand All @@ -14,5 +16,11 @@ export class TranslateDto {
example: 'ko',
})
@IsLocale()
targetLang: string;
@Transform(({ value }) => {
if (value === 'en') {
return 'en-US';
}
return value;
})
targetLang: deepl.TargetLanguageCode;
}
3 changes: 2 additions & 1 deletion src/ai/dto/res/translateRes.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ApiProperty } from '@nestjs/swagger';
import * as deepl from 'deepl-node';

export class TranslateResDto {
@ApiProperty({
Expand All @@ -11,5 +12,5 @@ export class TranslateResDto {
description: 'Language of the translated text',
example: 'en',
})
lang: string;
lang: deepl.TargetLanguageCode;
}

0 comments on commit 422545c

Please sign in to comment.