最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Spring AI Azure OpenAI & Spring App Config Bean Creation Conflicts -- UnsatisfiedDependencyException, No qualifying bean

programmeradmin0浏览0评论

I'm encountering an UnsatisfiedDependencyException when trying to start my Spring Boot application that uses Spring AI with Azure OpenAI. The error message indicates that a bean of type .springframework.ai.chat.model.ChatModel cannot be found.

.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'queryModelController': Unsatisfied dependency expressed through field 'queryService': Error creating bean with name 'queryService': Unsatisfied dependency expressed through field 'chatModel': No qualifying bean of type '.springframework.ai.chat.model.ChatModel' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@.springframework.beans.factory.annotation.Autowired(required=true)}

Project Setup:

  • Spring Boot Version: 3.4.2

  • Java Version: 17

  • Spring AI Azure OpenAI Starter Version: 1.0.0-M5

  • Database: Snowflake (using net.snowflake.client.jdbc.SnowflakeDriver)

  • Configuration: Using bootstrap.yaml for Azure App Configuration and application.yaml for other settings.

Code Snippets:

  1. com.enal.chattosql.nlq.Application:
import .springframework.boot.SpringApplication;
import .springframework.boot.autoconfigure.SpringBootApplication;
import .springframework.boot.context.properties.ConfigurationPropertiesScan;
import .springframework.boot.builder.SpringApplicationBuilder;
import .springframework.boot.context.properties.EnableConfigurationProperties;
import .springframework.ai.autoconfigure.azure.openai.AzureOpenAiAutoConfiguration;
import .springframework.context.annotation.Import;

@SpringBootApplication
@ConfigurationPropertiesScan("com.enal.chattosql.nlq.config")
@EnableConfigurationProperties
@Import(AzureOpenAiAutoConfiguration.class)
public class Application {
    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class)
                .properties("spring.config.name:bootstrap")
                .run(args);
    }
}
  1. com.enal.chattosql.nlq.config.PromptConfigReader
import lombok.Getter;
import lombok.Setter;
import .springframework.ai.autoconfigure.azure.openai.AzureOpenAiAutoConfiguration;
import .springframework.boot.autoconfigure.AutoConfigureAfter;
import .springframework.boot.autoconfigure.AutoConfigureBefore;
import .springframework.boot.context.properties.ConfigurationProperties;
import .springframework.boot.context.properties.EnableConfigurationProperties;
import .springframework.stereotype.Component;

@ConfigurationProperties(prefix = "config")
@Getter
@Setter
@Component
public class PromptConfigReader {
    private String sqlprompt;
}
  1. com.enal.chattosql.nlq.service.QueryService

import com.enal.chattosql.nlq.config.PromptConfigReader;
import .springframework.ai.chat.model.ChatModel;
import .springframework.beans.factory.annotation.Autowired;
import .springframework.stereotype.Service;

@Service
public class QueryService {

    @Autowired
    private ChatModel chatModel;

    @Autowired
    private PromptConfigReader promptConfigReader;

    // ... other methods ...
}
  1. bootstrap.yaml (The Bootstrap Configuration File)
  2. List item
# bootstrap.yaml
spring:
  cloud:
    azure:
      appconfiguration:
        enabled: true
        connection-string: ${AZURE_APPCONFIG_CONNECTION_STRING}
  1. application.yaml
# application.yaml
spring:
  application:
    name: chat-to-sql
  ai:
    azure:
      openai:
        chat:
          model: gpt-35-turbo
          api-key: ${AZURE_OPENAI_API_KEY}
          endpoint: ${AZURE_OPENAI_ENDPOINT}
        deployment-id: ${AZURE_OPENAI_DEPLOYMENT_ID}
  datasource:
    url: jdbc:snowflake://<your-database-host>:<your-database-port>/<your-database-name>
    username: <your-database-username>
    password: <your-database-password>

Troubleshooting Steps Taken:

  • I have tried the following troubleshooting steps:

  • Verified that the Spring AI Azure OpenAI starter dependency is included in pom.xml.

  • Ensured that the Azure OpenAI API key, endpoint, and deployment ID are correctly configured in application.yaml.

  • Ensured that the database connection details are correctly configured in application.yaml.

  • Ensured that the Azure App Configuration connection string is correctly configured in bootstrap.yaml.

  • Tried explicitly importing AzureOpenAiAutoConfiguration in the Application class.

  • Tried explicitly scanning the .springframework.ai.autoconfigure.azure.openai package using @ComponentScan.

  • Tried setting spring.config.name: bootstrap in the Application class.

  • Tried moving the Spring AI configuration to bootstrap.yaml.

  • Tried moving the Spring AI configuration to application.yaml.

  • Tried adding @EnableConfigurationProperties to the Application class.

  • Tried adding @Component to the PromptConfigReader class.

  • Tried removing @EnableConfigurationProperties from the Application class.

  • Tried removing @Component from the PromptConfigReader class.

  • Tried adding @ConfigurationPropertiesScan to the Application class.

  • Tried removing @AutoConfigureBefore and @AutoConfigureAfter from PromptConfigReader class.

Question:

Despite all these attempts, the ChatModel bean is still not being created. I'm not sure what else to try. Can anyone help me understand why the Spring AI auto-configuration is not being triggered and how to resolve this UnsatisfiedDependencyException?

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论