From 37b476399351de5d7ba1eb9291d271bfa463d2db Mon Sep 17 00:00:00 2001 From: Libing Chen Date: Fri, 9 Aug 2024 11:23:07 +0800 Subject: [PATCH] chore: add exception for structured output --- .../client/ChatGPTServiceProxyFactory.java | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/mvnsearch/chatgpt/spring/client/ChatGPTServiceProxyFactory.java b/src/main/java/org/mvnsearch/chatgpt/spring/client/ChatGPTServiceProxyFactory.java index b477778..f4ea5fe 100644 --- a/src/main/java/org/mvnsearch/chatgpt/spring/client/ChatGPTServiceProxyFactory.java +++ b/src/main/java/org/mvnsearch/chatgpt/spring/client/ChatGPTServiceProxyFactory.java @@ -36,7 +36,6 @@ import java.util.Arrays; import java.util.List; import java.util.Objects; -import java.util.function.Function; import java.util.stream.Stream; public class ChatGPTServiceProxyFactory { @@ -186,15 +185,14 @@ else if (!assistantMessage.isEmpty()) { request.setResponseFormat(ResponseFormat.jsonSchema(jsonSchema)); } if (isStructuredOutput) { - return chatGPTService.chat(request) - .map((Function) chatCompletionResponse -> { - try { - return chatCompletionResponse.getStructuredOutput(outputClass); - } - catch (Exception e) { - throw new RuntimeException(e); - } - }); + return chatGPTService.chat(request).handle((response, sink) -> { + try { + sink.next(response.getStructuredOutput(outputClass)); + } + catch (Exception e) { + sink.error(e); + } + }); } else { return chatGPTService.chat(request).map(ChatCompletionResponse::getReplyText);