Skip to content
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

Double remote call on DefaultChatClient with content and chatResponse #2097

Open
Grogdunn opened this issue Jan 22, 2025 · 2 comments
Open

Comments

@Grogdunn
Copy link
Contributor

Bug description
In DefaultChatClient#DefaultCallResponseSpec (also in streaming) call chatResponse or conent trigger the remote call twice.

Environment
Spring AI version M5 confirmed also on main

Steps to reproduce
simply call chatResponse and content and double remote service is invoked

Expected behavior
per "DefaultCallResponseSpec" access to same response (memoized)

Minimal Complete Reproducible example

var response = chatClient.prompt()
		.advisors(new ReReadingAdvisor())
		.call();
response.content();
response.chatResponse();
Grogdunn added a commit to optionfactory/spring-ai that referenced this issue Jan 22, 2025
Grogdunn added a commit to optionfactory/spring-ai that referenced this issue Jan 22, 2025
Signed-off-by: Lorenzo Caenazzo <lorenzo.caenazzo@optionfactory.net>
@ThomasVitale
Copy link
Contributor

ThomasVitale commented Jan 22, 2025

Thanks for raising this issue. The API is designed to support only one terminal call operation via the CallResponseSpec: content(), chatResponse(), entity(), or responseEntity(). I would say that it's by design that if the call action is called multiple times, then the model is called on each call.

That's the same behaviour present in other Client APIs in the Spring portfolio, such as RestClient.

If you need full access to the response (e.g. metadata), I suggest using the chatResponse() terminal operation and then extract the information you need from there (including the content).

@Grogdunn
Copy link
Contributor Author

Ok, but maybe like "java stream API" if a terminal operation is called twice the second call throws.

At the moment the feeling are the "content" call are a shortcut to invoke "chatResponse().getResult().getOutput().getText()" on SAME response.

Last 2 cents seems "DefaultResponseSpec" in DefaultRestClient save the ClientHttpResponse clientResponse and make the operation on that, but not invoke remote service each time that I invoke "getBody" (maybe throw if the body is a consumed)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants