Skip to content

Commit 279a55b

Browse files
shl0501shl0501
andauthored
feat(be): implement question shortening feature (#67)
feat: implement question shortening feature Co-authored-by: shl0501 <[email protected]>
1 parent a244340 commit 279a55b

File tree

6 files changed

+78
-2
lines changed

6 files changed

+78
-2
lines changed

apps/server/src/ai/ai.controller.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import { ApiBody } from '@nestjs/swagger';
33

44
import { AiService } from './ai.service';
55
import { ImproveQuestionDto } from './dto/improve-question.dto';
6+
import { ShortenQuestionDto } from './dto/shorten-question.dto';
67
import { ImproveQuestionSwagger } from './swagger/improve-question.swagger';
8+
import { ShortenQuestionSwagger } from './swagger/shorten-question.swagger';
79

810
import { SessionTokenValidationGuard } from '@common/guards/session-token-validation.guard';
911
@Controller('ai')
@@ -19,4 +21,14 @@ export class AiController {
1921
const result = { question: await this.aiService.requestImproveQuestion(userContent) };
2022
return { result };
2123
}
24+
25+
@Post('question-shorten')
26+
@ShortenQuestionSwagger()
27+
@ApiBody({ type: ShortenQuestionDto })
28+
@UseGuards(SessionTokenValidationGuard)
29+
public async shortenQuestion(@Body() shortenQuestionDto: ShortenQuestionDto) {
30+
const { body: userContent } = shortenQuestionDto;
31+
const result = { question: await this.aiService.requestShortenQuestion(userContent) };
32+
return { result };
33+
}
2234
}

apps/server/src/ai/ai.service.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ export class AiService {
3737
return await this.requestAIResponse(userContent, prompt.improveQuestion);
3838
}
3939

40+
public async requestShortenQuestion(userContent: string) {
41+
return await this.requestAIResponse(userContent, prompt.shortenQuestion);
42+
}
43+
4044
private async requestAIResponse(userContent: string, prompt: string) {
4145
const headers = {
4246
Authorization: this.API_KEY,
@@ -56,7 +60,7 @@ export class AiService {
5660
],
5761
topP: 0.8,
5862
topK: 0,
59-
maxTokens: 512,
63+
maxTokens: 1024,
6064
temperature: 0.5,
6165
repeatPenalty: 5.0,
6266
stopBefore: [],
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { ApiProperty } from '@nestjs/swagger';
2+
import { IsNotEmpty, IsString } from 'class-validator';
3+
4+
import { BaseDto } from '@common/base.dto';
5+
6+
export class ShortenQuestionDto extends BaseDto {
7+
@ApiProperty({
8+
example: '리마큐가 뭐임?',
9+
description: '질문 본문 내용',
10+
required: true,
11+
})
12+
@IsString()
13+
@IsNotEmpty({ message: '질문 본문은 필수입니다.' })
14+
body: string;
15+
}

apps/server/src/ai/promt.constant.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,31 @@ export const prompt = {
3333
[응답 예시2]
3434
# 결혼 적령 시기
3535
- 결혼 적령 시기에 대해 설명해주세요.`,
36+
37+
shortenQuestion: `당신은 질문 요약 전문가입니다. 아래의 기준에 따라 사용자가 입력한 질문을 분석하고, 짧게 요약해 주세요
38+
0. **마크다운 유지**: 원본 질문에 포함된 모든 링크, 이미지, 코드 블록 등 모든 마크다운 요소를 그대로 보존하세요. 수정 과정 중 형식, URL, 이미지 주소 등이 변경되거나 누락되지 않도록 주의하세요.
39+
1. **글자 수 제한**: 원본 질문이 링크를 제외하고 500자 이하가 될 수 있도록 하세요. '![](링크 주소)'와 같은 형태는 500자에 포함되지 않으니 링크는 반드시 포함하세요.
40+
2. 요약의 핵심: 질문의 주요 요소와 의도를 파악하여, 불필요한 부분은 제거하되 핵심 내용은 그대로 반영해 주세요.
41+
3. 정보 누락 방지: 중요한 정보나 세부 사항이 빠지지 않도록 주의해 주세요.
42+
4. 명확성과 이해 용이성: 요약된 질문이 누구에게나 명확하고 쉽게 이해될 수 있도록 작성해 주세요.
43+
5. **질문 속 URL, 이미지 주소 유지**: 원본 질문에 포함된 모든 링크, 이미지, 코드 블록 등 모든 마크다운 요소를 그대로 보존하세요. 수정 과정 중 형식, URL, 이미지 주소 등이 변경되거나 누락되지 않도록 주의하세요. 링크 형식은 '![]()'으로 되어 있습니다. 반드시 포함하세요.
44+
최종적으로 개선된, 짧고 명료한 질문을 출력해 주세요.
45+
---
46+
아래는 입출력 예시입니다.
47+
48+
[질문 예시1]
49+
# 누가 더 위대한 축구선수인가: 메시 vs 호날두
50+
메시와 호날두 중에서 누가 더 위대한 선수일까요?
51+
![두 선수의 매서운 눈빛](https://private-user-images.githubusercontent.com/11)
52+
- [메시](https://private-user-images.githubusercontent.com/112055561)
53+
- [호날두](https://private-user-images.githubusercontent.com/112055562)
54+
55+
[응답 예시1]
56+
# 메시 vs 호날두
57+
![두 선수의 매서운 눈빛](https://private-user-images.githubusercontent.com/11)
58+
- [메시](https://private-user-images.githubusercontent.com/112055561)
59+
- [호날두](https://private-user-images.githubusercontent.com/112055562)
60+
61+
62+
`,
3663
} as const;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { applyDecorators } from '@nestjs/common';
2+
import { ApiOperation, ApiResponse } from '@nestjs/swagger';
3+
4+
export const ShortenQuestionSwagger = () =>
5+
applyDecorators(
6+
ApiOperation({ summary: '질문 요약' }),
7+
ApiResponse({
8+
status: 201,
9+
description: '질문 요약 성공',
10+
schema: {
11+
example: {
12+
result: {
13+
question: '리마큐(Remacu)란 무엇인가요?',
14+
},
15+
},
16+
},
17+
}),
18+
);

apps/server/src/auth/auth.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Inject, Injectable, OnModuleInit } from '@nestjs/common';
1+
import { Inject, Injectable } from '@nestjs/common';
22
import { JwtService } from '@nestjs/jwt';
33
import * as bcrypt from 'bcryptjs';
44
import Redis from 'ioredis';

0 commit comments

Comments
 (0)