Skip to content

Commit

Permalink
fix(#36): maxToken 수정을 통해 요약 정확도 향상 및 재시도 오류 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
kimhji committed Feb 5, 2025
1 parent 8d2863b commit 5497861
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions server/src/rss/ai/feed-ai.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { delay } from 'rxjs/operators';

@Injectable()
export class FeedAIService {
Expand All @@ -8,6 +9,8 @@ export class FeedAIService {
private URL: URL;
private headers;
private prompt;
static limitLength = 100;
static reReqCount = 5;

constructor(private readonly configService: ConfigService) {
this.API_KEY = this.configService.get<string>('API_KEY');
Expand All @@ -23,9 +26,9 @@ export class FeedAIService {
this.URL = this.configService.get<URL>('CLOVASTUDIO_URL');
this.prompt = {
role: 'system',
content: `- 당신은 텍스트 요약 어시스턴트입니다.
- 주어진 XML 형태의 텍스트를 분석하고 핵심 주제를 추출하여 50자 이하 요약을 제공합니다.
- 이 글에 대한 요약은 해당 글을 홍보하고자 하는 목적으로 사용되며, 내부 내용에 대한 상세한 부분은 요약에 포함되면 안됩니다.
content: `- 당신은 반드시 ${FeedAIService.limitLength} 글자 미만의 요약을 제공하는 텍스트 요약 어시스턴트입니다.
- 주어진 XML 형태의 텍스트를 분석하고 핵심 주제를 추출합니다.
- 이 글에 대한 요약은 해당 글을 홍보하고자 하는 목적으로 사용되며, 내부 내용에 대한 상세 사항은 응답에 포함되면 안됩니다.
- 답변 형태 : ~~~한 주제에 대해 다루고 있는 포스트입니다.`,
};
}
Expand All @@ -42,23 +45,29 @@ export class FeedAIService {
],
topP: 0.6,
topK: 0,
maxTokens: 256,
maxTokens: 30,
temperature: 0.1,
repeatPenalty: 2.0,
stopBefore: [],
includeAiFilters: true,
seed: 0,
};
let count = 0;
let resLength = -1;
let result = '';
while ((resLength < 0 || resLength > 50) && count < 5) {
while (
(resLength <= 0 || resLength > FeedAIService.limitLength) &&
count < FeedAIService.reReqCount
) {
const response = await fetch(this.URL, {
method: 'POST',
headers: this.headers,
body: JSON.stringify(body),
});

if (response.status === 429) {
console.warn('Rate limit 초과. 재시도 중...');
await delay(500);
continue;
}
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
Expand All @@ -70,7 +79,7 @@ export class FeedAIService {
resLength = result.length;
count++;
}
if (resLength > 50) {
if (resLength > FeedAIService.limitLength || resLength <= 0) {
result = '요약 데이터가 유효하지 않습니다.';
}
console.log('응답 데이터:', result);
Expand Down Expand Up @@ -102,15 +111,15 @@ export class FeedAIService {
const jsonString = data.replace('"message":', '').trim();
const parsedData = JSON.parse(jsonString);
if (parsedData.content) {
accumulatedText += parsedData.content;
accumulatedText = parsedData.content.trim();
}
} catch (error) {
console.error('JSON 파싱 실패:', error);
}
});
}
}

await reader.cancel();
return accumulatedText;
}
}

0 comments on commit 5497861

Please sign in to comment.