Skip to content

Improving the content methods of the DefaultChatClient #1359

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -279,6 +276,8 @@ public static class DefaultCallResponseSpec implements CallResponseSpec {

private final DefaultChatClientRequestSpec request;

private ChatResponse chatResponse;

public DefaultCallResponseSpec(DefaultChatClientRequestSpec request) {
this.request = request;
}
Expand Down Expand Up @@ -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();
}

}
Expand Down Expand Up @@ -439,6 +442,8 @@ public static class DefaultStreamResponseSpec implements StreamResponseSpec {

private final DefaultChatClientRequestSpec request;

private Flux<ChatResponse> chatResponse;

public DefaultStreamResponseSpec(DefaultChatClientRequestSpec request) {
this.request = request;
}
Expand Down Expand Up @@ -570,11 +575,15 @@ private Flux<ChatResponse> doGetFluxChatResponse(DefaultChatClientRequestSpec in
}

public Flux<ChatResponse> chatResponse() {
return doGetObservableFluxChatResponse(this.request);
this.chatResponse = doGetObservableFluxChatResponse(this.request);
return this.chatResponse;
}

public Flux<String> 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 "";
Expand Down Expand Up @@ -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<String> 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;
}

}
Expand All @@ -967,21 +986,28 @@ public static class DefaultStreamPromptResponseSpec implements StreamPromptRespo

private final StreamingChatModel chatModel;

private Flux<ChatResponse> chatResponse;

public DefaultStreamPromptResponseSpec(StreamingChatModel streamingChatModel, Prompt prompt) {
this.chatModel = streamingChatModel;
this.prompt = prompt;
}

public Flux<ChatResponse> chatResponse() {
return doGetFluxChatResponse(this.prompt);
this.chatResponse = doGetFluxChatResponse(this.prompt);
return this.chatResponse;
}

private Flux<ChatResponse> doGetFluxChatResponse(Prompt prompt) {
return this.chatModel.stream(prompt);
this.chatResponse = this.chatModel.stream(prompt);
return this.chatResponse;
}

public Flux<String> 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 "";
Expand Down