spring-projects / spring-ai

An Application Framework for AI Engineering
https://docs.spring.io/spring-ai/reference/1.0-SNAPSHOT/index.html
Apache License 2.0
2.53k stars 610 forks source link

Ensure that there is a RestClient.Builder in the application context #563

Open habuma opened 3 months ago

habuma commented 3 months ago

When Spring AI auto-configures various client implementations, it depends upon a RestClient.Builder to create a RestClient for those client implementations. That works fine when the project uses the Spring MVC starter because a RestClient.Builder will be auto-configured. But when using the Spring WebFlux starter, no such RestClient.Builder is auto-configured and the application will fail to start.

AFAIK, this happens in all recent versions of Spring AI, including 0.8.1 and 1.0.0-SNAPSHOT.

To reproduce, create a simple Spring AI application with a dependency on the WebFlux starter and almost any model starter...such as the OpenAI starter. You do not need to write any code to reproduce this problem. Just create a new Spring Boot project with the aforementioned starters and then try to run it. The application will fail to start and you'll get the following error:

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 2 of method openAiChatClient in org.springframework.ai.autoconfigure.openai.OpenAiAutoConfiguration required a bean of type 'org.springframework.web.client.RestClient$Builder' that could not be found.

Action:

Consider defining a bean of type 'org.springframework.web.client.RestClient$Builder' in your configuration.

If Spring AI were to ensure the existence of a RestClient.Builder then the application would start and function fine, even when using WebFlux.

Johnnymic commented 2 months ago

can i help

markpollack commented 2 months ago

@habuma implementation suggestions?

ChrisToplikar commented 2 months ago

In case someone is looking for a temporary work around, adding org.springframework.boot:spring-boot-starter-web to the dependencies will resolve the issue.

tzolov commented 2 months ago

It is not acceptable to add starters to utility/library. It is ok to add it to the end application, which is the current behaviour. The goal is to have some defaults (non starter) in the library.

iromu commented 1 week ago

Should not spring-ai use HTTP Interface Client, and detect (and use) either WebClient or RestClient builders?

yueyangbo123 commented 1 week ago

If you have configured "web-application-type", you can try to remove it and restart from

server:
  port: 8091
spring:
  application:
    name: Spring-ai-demo
  main:
    web-application-type: reactive

to

server:
  port: 8091
spring:
  application:
    name: Spring-ai-demo