When I dynamically configure the OpenAI apikey in the app instead of configuring it in application.yml, app startup error.
@GetMapping("/call/openai")
public String call() {
OpenAiChatModel chatModel = OpenAiChatModel.builder()
.openAiApi(OpenAiApi.builder()
.apiKey("sk-xxxx")
.baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions")
.build()
).build();
return chatModel.call("hi");
}
logs:
Caused by: java.lang.IllegalArgumentException: OpenAI API key must be set. Use the connection property: spring.ai.openai.api-key or spring.ai.openai.chat.api-key property.
at org.springframework.util.Assert.hasText(Assert.java:253) ~[spring-core-6.2.6.jar:6.2.6]
pom:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-openai</artifactId>
<version>1.1.0</version>
</dependency>
when use spring-ai-starter, apikey must be configured in application.yml, although this may be overridden by MergeUtils.merge in ChatModel.
BTW, use spring-ai-openai, no such problem
Comment From: yuluo-yx
I think one possible implementation is to postpone the apikey check until when the ChatModel is created.