From 9d7e94d09ec5fc8fae1a6d78c38da046b0502bfe Mon Sep 17 00:00:00 2001 From: Josh Long Date: Sat, 18 May 2024 16:29:45 +0200 Subject: [PATCH] support using BeanOutputConverters. --- .../springframework/ai/chat/ChatClient.java | 29 +++++++++++++++---- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/spring-ai-core/src/main/java/org/springframework/ai/chat/ChatClient.java b/spring-ai-core/src/main/java/org/springframework/ai/chat/ChatClient.java index 4b5850e8d..5d90589f7 100644 --- a/spring-ai-core/src/main/java/org/springframework/ai/chat/ChatClient.java +++ b/spring-ai-core/src/main/java/org/springframework/ai/chat/ChatClient.java @@ -8,6 +8,7 @@ import org.springframework.ai.chat.messages.UserMessage; import org.springframework.ai.chat.prompt.ChatOptions; import org.springframework.ai.chat.prompt.Prompt; import org.springframework.ai.chat.prompt.PromptTemplate; +import org.springframework.ai.converter.BeanOutputConverter; import org.springframework.ai.model.function.FunctionCallback; import org.springframework.ai.model.function.FunctionCallbackWrapper; import org.springframework.ai.model.function.FunctionCallingOptionsBuilder; @@ -23,6 +24,8 @@ import java.nio.charset.Charset; import java.util.*; import java.util.function.Consumer; +// todo support plugging in a outputConverter at runtime + /* * @author Mark Pollack * @author Christian Tzolov @@ -304,17 +307,29 @@ public interface ChatClient { } public T single(ParameterizedTypeReference t) { - return null; + // todo once rebased make sure to use the {BeanOutputConverter} that now + // accepts a ParameterizedTypeReference + return doSingleWithBeanOutputConverter( + new BeanOutputConverter(null /* todo */)); + } + + private T doSingleWithBeanOutputConverter(BeanOutputConverter boc) { + var processedUserText = this.request.userText + System.lineSeparator() + System.lineSeparator() + + boc.getFormat(); + var chatResponse = doGetChatResponse(processedUserText); + var stringResponse = chatResponse.getResult().getOutput().getContent(); + return boc.convert(stringResponse); } public T single(Class clzz) { - return null; + Assert.notNull(clzz, "the class must be non-null"); + var boc = new BeanOutputConverter(clzz); + return doSingleWithBeanOutputConverter(boc); } - public ChatResponse chatResponse() { + private ChatResponse doGetChatResponse(String processUserText) { - var userMessage = new UserMessage( - new PromptTemplate(this.request.userText, this.request.userParams).render(), + var userMessage = new UserMessage(new PromptTemplate(processUserText, this.request.userParams).render(), this.request.media); var systemMessage = new SystemMessage( @@ -333,6 +348,10 @@ public interface ChatClient { return this.chatConnector.call(prompt); } + public ChatResponse chatResponse() { + return doGetChatResponse(this.request.userText); + } + public Flux stream(Class t) { return null; }