diff --git a/spring-ai-core/src/main/java/org/springframework/ai/chat/client/DefaultChatClient.java b/spring-ai-core/src/main/java/org/springframework/ai/chat/client/DefaultChatClient.java index 767f6d26ae2..2f7736bffd5 100644 --- a/spring-ai-core/src/main/java/org/springframework/ai/chat/client/DefaultChatClient.java +++ b/spring-ai-core/src/main/java/org/springframework/ai/chat/client/DefaultChatClient.java @@ -59,10 +59,7 @@ import org.springframework.ai.model.function.FunctionCallingOptions; import org.springframework.core.ParameterizedTypeReference; import org.springframework.core.io.Resource; -import org.springframework.util.Assert; -import org.springframework.util.CollectionUtils; -import org.springframework.util.MimeType; -import org.springframework.util.StringUtils; +import org.springframework.util.*; import io.micrometer.observation.Observation; import io.micrometer.observation.ObservationRegistry; @@ -279,6 +276,8 @@ public static class DefaultCallResponseSpec implements CallResponseSpec { private final DefaultChatClientRequestSpec request; + private ChatResponse chatResponse; + public DefaultCallResponseSpec(DefaultChatClientRequestSpec request) { this.request = request; } @@ -386,11 +385,15 @@ private ChatResponse doGetChatResponse(DefaultChatClientRequestSpec inputRequest } public ChatResponse chatResponse() { - return doGetChatResponse(); + this.chatResponse = doGetChatResponse(); + return this.chatResponse; } public String content() { - return doGetChatResponse().getResult().getOutput().getContent(); + if (this.chatResponse == null) { + this.chatResponse = doGetChatResponse(); + } + return this.chatResponse.getResult().getOutput().getContent(); } } @@ -439,6 +442,8 @@ public static class DefaultStreamResponseSpec implements StreamResponseSpec { private final DefaultChatClientRequestSpec request; + private Flux chatResponse; + public DefaultStreamResponseSpec(DefaultChatClientRequestSpec request) { this.request = request; } @@ -570,11 +575,15 @@ private Flux doGetFluxChatResponse(DefaultChatClientRequestSpec in } public Flux chatResponse() { - return doGetObservableFluxChatResponse(this.request); + this.chatResponse = doGetObservableFluxChatResponse(this.request); + return this.chatResponse; } public Flux content() { - return doGetObservableFluxChatResponse(this.request).map(r -> { + if (this.chatResponse == null) { + this.chatResponse = doGetObservableFluxChatResponse(this.request); + } + return this.chatResponse.map(r -> { if (r.getResult() == null || r.getResult().getOutput() == null || r.getResult().getOutput().getContent() == null) { return ""; @@ -938,25 +947,35 @@ public static class DefaultCallPromptResponseSpec implements CallPromptResponseS private final Prompt prompt; + private ChatResponse chatResponse; + public DefaultCallPromptResponseSpec(ChatModel chatModel, Prompt prompt) { this.chatModel = chatModel; this.prompt = prompt; } public String content() { - return doGetChatResponse(this.prompt).getResult().getOutput().getContent(); + if (this.chatResponse == null) { + this.chatResponse = doGetChatResponse(this.prompt); + } + return this.chatResponse.getResult().getOutput().getContent(); } public List contents() { - return doGetChatResponse(this.prompt).getResults().stream().map(r -> r.getOutput().getContent()).toList(); + if (this.chatResponse == null) { + this.chatResponse = doGetChatResponse(this.prompt); + } + return this.chatResponse.getResults().stream().map(r -> r.getOutput().getContent()).toList(); } public ChatResponse chatResponse() { - return doGetChatResponse(this.prompt); + this.chatResponse = doGetChatResponse(this.prompt); + return this.chatResponse; } private ChatResponse doGetChatResponse(Prompt prompt) { - return chatModel.call(prompt); + this.chatResponse = chatModel.call(prompt); + return this.chatResponse; } } @@ -967,21 +986,28 @@ public static class DefaultStreamPromptResponseSpec implements StreamPromptRespo private final StreamingChatModel chatModel; + private Flux chatResponse; + public DefaultStreamPromptResponseSpec(StreamingChatModel streamingChatModel, Prompt prompt) { this.chatModel = streamingChatModel; this.prompt = prompt; } public Flux chatResponse() { - return doGetFluxChatResponse(this.prompt); + this.chatResponse = doGetFluxChatResponse(this.prompt); + return this.chatResponse; } private Flux doGetFluxChatResponse(Prompt prompt) { - return this.chatModel.stream(prompt); + this.chatResponse = this.chatModel.stream(prompt); + return this.chatResponse; } public Flux content() { - return doGetFluxChatResponse(this.prompt).map(r -> { + if (this.chatResponse == null) { + this.chatResponse = doGetFluxChatResponse(this.prompt); + } + return this.chatResponse.map(r -> { if (r.getResult() == null || r.getResult().getOutput() == null || r.getResult().getOutput().getContent() == null) { return "";