Skip to content

Commit

Permalink
Merge pull request #13 from KUSITMS-MOAMOA/feat/#10
Browse files Browse the repository at this point in the history
[Feat/#10] : OpenAI 에러 발생 시 클로바로 대체 기능 추가
  • Loading branch information
oosedus authored Feb 8, 2025
2 parents 2ab2fa5 + ea2d081 commit 18b2f0e
Showing 1 changed file with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
import corecord.dev.domain.chat.domain.dto.response.ChatSummaryAiResponse;
import corecord.dev.domain.chat.domain.entity.Chat;
import corecord.dev.domain.chat.exception.ChatException;
import corecord.dev.domain.chat.infra.clova.application.ClovaService;
import corecord.dev.domain.chat.status.ChatErrorStatus;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpServerErrorException;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -22,6 +24,7 @@
@RequiredArgsConstructor
public class OpenAiChatService implements ChatAIService {
private final OpenAiChatModel chatModel;
private final ClovaService clovaService;
private static final String CHAT_SYSTEM_CONTENT = ResourceLoader.getResourceContent("chat-prompt.txt");
private static final String SUMMARY_SYSTEM_CONTENT = ResourceLoader.getResourceContent("chat-summary-prompt.txt");

Expand All @@ -44,7 +47,11 @@ public String generateChatResponse(List<Chat> chatHistory, String userContent) {
// 사용자 입력 추가
messages.add(Map.of("role", "user", "content", userContent));

return chatModel.call(String.valueOf(messages));
try {
return chatModel.call(String.valueOf(messages));
} catch (HttpServerErrorException e) {
return clovaService.generateChatResponse(chatHistory, userContent);
}
}

@Override
Expand All @@ -63,7 +70,12 @@ public ChatSummaryAiResponse generateChatSummaryResponse(List<Chat> chatHistory)
messages.add(Map.of("role", role, "content", chat.getContent()));
}

String response = chatModel.call(String.valueOf(messages));
String response;
try {
response = chatModel.call(String.valueOf(messages));
} catch (HttpServerErrorException e) {
response = clovaService.generateChatSummaryResponse(chatHistory).getContent();
}

return parseChatSummaryResponse(response);
}
Expand Down

0 comments on commit 18b2f0e

Please sign in to comment.