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.9k stars 724 forks source link

Failed to instantiate [org.springframework.ai.openai.api.OpenAiApi] #167

Closed StefanoDib closed 9 months ago

StefanoDib commented 9 months ago

Bug description I'm developing an application that interacts with ChatGPT 3.5 and I'm using this library. Since yesterday the project doesn't start anymore because of this error:

Unsatisfied dependency expressed through field 'aiClient': Error creating bean with name 'openAiClient' defined in class path resource [org/springframework/ai/autoconfigure/openai/OpenAiAutoConfiguration.class]: Unsatisfied dependency expressed through method 'openAiClient' parameter 0: Error creating bean with name 'openAiApi' defined in class path resource [org/springframework/ai/autoconfigure/openai/OpenAiAutoConfiguration.class]: Failed to instantiate [org.springframework.ai.openai.api.OpenAiApi]: Factory method 'openAiApi' threw exception with message: org/springframework/web/client/RestClient

My project configuration didn't change since the last time it used to work.

Environment I'm using OpenAi 0.8.0-SNAPSHOT with Java SE 17.0.17. Rolling back to OpenAi 0.7.1-SNAPSHOT is uneffective because it leads to the same error.

The parent I am using is:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.1.0</version>
    <relativePath/>
</parent>

The dependencies are:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.skyscreamer</groupId>
                <artifactId>jsonassert</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.3.2</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-core</artifactId>
        <version>6.2.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
    </dependency>
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20231013</version>
    </dependency>
    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
        <version>2.3.0</version>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>5.9.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>jakarta.annotation</groupId>
        <artifactId>jakarta.annotation-api</artifactId>
        <version>2.1.1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.ai</groupId>
        <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
        <version>0.8.0-SNAPSHOT</version>
    </dependency>
</dependencies>

I noticed that RestClient is included in the Spring Web dependency, so I tried to include Spring Web but it didn't work.

Steps to reproduce Create a Spring Boot project and autowire an AiClient, like so:

import org.springframework.ai.client.AiClient;

@Service 
public class GenericService{

    @Autowired
    AiClient aiClient;

}

Expected behavior The singleton is instantiated properly and the project starts.

markpollack commented 9 months ago

Thanks for reporting this, yes please roll back, we have made some major changes and need some time to work through them.

tzolov commented 9 months ago

@StefanoDib the org/springframework/web/client/RestClient is released with spring-web:6.1.1, currently included in Spring Boot 3.2.0. Until we figure it out if and how this can be backward compatible, can you try to upgrade your project to boot 3.2.0?

StefanoDib commented 9 months ago

@StefanoDib the org/springframework/web/client/RestClient is released with spring-web:6.1.1, currently included in Spring Boot 3.2.0. Until we figure it out if and how this can be backward compatible, can you try to upgrade your project to boot 3.2.0?

Many thanks, upgrading to Spring Parent 3.2.0 and including Spring Web 6.1.1 dependency, worked fine. Have a nice day.