-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Easy way to log requests and responses to LLM's #883
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
I handle this using the Logbook logging library. @Bean
public RestClientCustomizer restClientCustomizer(Logbook logbook) {
return restClientBuilder -> restClientBuilder.requestInterceptor(new LogbookClientHttpRequestInterceptor(logbook));
} |
@piotrooo Thanks for the tip. IMHO it is's maybe a bit to much to introduce such a new dependency just for request/response logging. |
I opened a discussion in #512 about this topic. In the meantime, this is how I solved it. You can see the full examples here: https://github.com/ThomasVitale/concerto-for-java-and-ai/blob/main/mousike/src/main/java/com/thomasvitale/mousike/ai/clients/HttpClientAutoConfiguration.java#L16 @Bean
RestClientCustomizer restClientCustomizer(HttpClientProperties httpClientProperties) {
HttpClientConfig clientConfig = HttpClientConfig.builder()
.connectTimeout(httpClientProperties.getConnectTimeout())
.readTimeout(httpClientProperties.getReadTimeout())
.sslBundle(httpClientProperties.getSslBundle())
.logRequests(httpClientProperties.isLogRequests())
.logResponses(httpClientProperties.isLogResponses())
.build();
return restClientBuilder -> {
restClientBuilder
.requestFactory(new BufferingClientHttpRequestFactory(
ClientHttpRequestFactories.get(ClientHttpRequestFactorySettings.DEFAULTS
.withConnectTimeout(clientConfig.connectTimeout())
.withReadTimeout(clientConfig.readTimeout()))))
.requestInterceptors(interceptors -> {
if (clientConfig.logRequests() || clientConfig.logResponses()) {
interceptors.add(new HttpLoggingInterceptor(clientConfig.logRequests(), clientConfig.logResponses()));
}
});
};
} |
As always, it depends. If you want something for development purposes, a @ThomasVitale example is enough. On the other hand, if you want a fully-featured sample with configuration and battle-proven features, use library (e.g. logbook). |
Both solutions are viable solutions for something that you would need in the initial development and debug fase. |
I've added some thoughts for discussion here. Certainly improvements in this area need to be made. |
I shared some additional thoughts in #512 (comment) |
I'm using SpringBoot 3.4.3 and Spring AI 1.0.0-SNAPSHOT and the solution @ThomasVitale provided didn't work as the API seems to have changed. I then tried the solution by @piotrooo and provided my own log implementation. The
After much frustration I landed on this StackOverflow link https://stackoverflow.com/questions/78444230/how-to-change-the-restclient-implementation-for-springai. The actual logging code is a copy from Dan Vega's article here https://www.danvega.dev/blog/spring-boot-rest-client-logging Here is my implementation that works. May be it can help someone. If I was making a mistake I'd like to know what it is.
|
There are currently two ways to get the LLM requests and responses:
|
Was looking for ways to log request/response from call to LLM's, and found this discussion.
I think it would be great if this would be supported as a configuration property as it is in Langchain4j.
Discussed in #450
Originally posted by iAMSagar44 March 15, 2024
Has anyone managed to find a way to log the requests and responses to/from Open AI using Spring AI.
I have tried various logging settings in the application.properties but no luck.
With Langchain4J, there is a specific property for logging the requests and responses to/from the AI models, but couldn't find anything similar with Spring AI.
The text was updated successfully, but these errors were encountered: