Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] 영어공지 자동 번역 기능 #111

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
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);
}
}
37 changes: 35 additions & 2 deletions src/ai/ai.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
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';
Expand All @@ -7,13 +11,24 @@ import {
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);
});
siwonpada marked this conversation as resolved.
Show resolved Hide resolved
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;
}