Bug description I'm trying to do manual configuration of the Chat clients so I have set the following in application.properties. spring.ai.chat.client.enabled=false

But I still get the following error.

java.lang.IllegalArgumentException: Either API key or OpenAI API key must not be empty
        at org.springframework.ai.autoconfigure.azure.openai.AzureOpenAiAutoConfiguration.openAIClientBuilder(AzureOpenAiAutoConfiguration.java:104) ~[spring-ai-spring-boot-autoconfigure-1.0.0-SNAPSHOT.jar:1.0.0-SNAPSHOT]


I also tried the following setting spring.ai.azure.openai.chat.enabled=false

This didn't help either.

Environment Java 21 Spring AI 1.0.0-SNAPSHOT Spring Boot 3.4.2

Steps to reproduce Add the following dependency to your pom.xml

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-azure-openai</artifactId>
</dependency>

Set the following props in application.properties

spring.ai.chat.client.enabled=false
spring.ai.azure.openai.chat.enabled=false

Run the application.

Expected behavior Expect that auto configuration of the chat clients is disabled and allows manual configuration via code. Instead you get the above exception.

Comment From: colommar

Did you set the key?

Comment From: rwankar

After some experimentation I realised it still needs the api key in the file. But I'm planning to read it from the database. Is there a way to avoid setting it in application.properties? I'll be creating all the classes manually via API and don't need Spring to do any injection. So I don't even want to have a configuration class that injects these variables into Spring. Just want to completely bypass Spring.

Comment From: colommar

After some experimentation I realised it still needs the api key in the file. But I'm planning to read it from the database. Is there a way to avoid setting it in application.properties? I'll be creating all the classes manually via API and don't need Spring to do any injection. So I don't even want to have a configuration class that injects these variables into Spring. Just want to completely bypass Spring.经过一番实验,我意识到它仍然需要文件中的 api 密钥。但我打算从数据库中读取它。有没有办法避免在 application.properties 中设置它?我将通过 API 手动创建所有类,不需要 Spring 执行任何注入。所以我什至不想有一个将这些变量注入 Spring 的配置类。只想完全绕过 Spring。

Of course, you can set it in the environment path. Which can be done like this in application properties.

${OPEN_AI_KEY}

Comment From: rwankar

I don't want to read it from the environment. I'll be connecting to a database and reading it from there.

Comment From: saeb-panahifar

You can fetch config data from a db or another source and manually set up the Azure Chat Client.

Here is a sample code snippet:

var openAIClient = new OpenAIClientBuilder()
  .credential(new AzureKeyCredential("DB_AZURE_OPENAI_API_KEY")))
  .endpoint("DB_AZURE_OPENAI_ENDPOINT");

var openAIChatOptions = AzureOpenAiChatOptions.builder()
  .deploymentName("DB_MODEL_NAME")
  .temperature(0.4)
  .maxTokens(200)
  .build();

var chatModel = new AzureOpenAiChatModel(openAIClient, openAIChatOptions);

ChatResponse response = chatModel.call(
  new Prompt("Generate the names of 5 famous pirates."));

// Or with streaming responses
Flux<ChatResponse> response = this.chatModel.stream(
  new Prompt("Generate the names of 5 famous pirates."));

Resource: https://docs.spring.io/spring-ai/reference/api/chat/azure-openai-chat.html#_manual_configuration

Comment From: rwankar

Somehow my point is being lost. I'm doing exactly what you've said above. However, I'm still forced to set the API Key in the application.properties file which I don't want to. I was able to proceed by setting dummy value in the properties file but ideally I shouldn't have to.

Comment From: saeb-panahifar

I tested it locally, and it worked fine.

Comment From: rwankar

I confirm this works. The mistake was I had a dependency on the starter package. I thought I had changed it.

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-azure-openai-spring-boot-starter</artifactId>
</dependency>

instead you have to use

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-azure-openai</artifactId>
</dependency>