Skip to content

Commit

Permalink
Merge pull request #101 from git1uv/fix/#99
Browse files Browse the repository at this point in the history
Fix : Claude API 호출 xml 파싱 에러 해결
  • Loading branch information
cowboysj authored Nov 26, 2024
2 parents a28a876 + 841523c commit 39db27d
Showing 1 changed file with 6 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ public Mono<ClaudeResponseDto> chatWithClaude(ClaudeRequestDto request, Long cou
+ "</example>"
+ "</systemPrompt>";

// 전체 XML 구조 통합
String xmlPrompt = "<conversationAnalysis>\n"
+ systemPrompt
+ "</conversationAnalysis>\n";
Expand All @@ -184,10 +183,11 @@ public Mono<ClaudeResponseDto> chatWithClaude(ClaudeRequestDto request, Long cou

private Mono<ClaudeResponseDto> parseXMLChatResponse(String xmlResponse, CounselingLog counselingLog, ClaudeRequestDto request) {
try {
String cleanedResponse = cleanXmlString(xmlResponse);
// XML 파서 초기화
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
String assistantResponseText = new JSONObject(xmlResponse)
String assistantResponseText = new JSONObject(cleanedResponse)
.getJSONArray("content")
.getJSONObject(0)
.getString("text");
Expand Down Expand Up @@ -488,6 +488,10 @@ private LocalDateTime generateRandomTime() {
long randomSeconds = ThreadLocalRandom.current().nextLong(minSeconds, maxSeconds);
return LocalDateTime.now().plusSeconds(randomSeconds);
}

private String cleanXmlString(String xml) {
return xml.trim().replaceFirst("^([\\W&&[^<]]+)<", "<");
}
}


Expand Down

0 comments on commit 39db27d

Please sign in to comment.