Closed
Description
I am trying to use Spring AI in a multi-tenant application. The openAI api key needs to be set on a per request basis according to our user context. I think this functionality was added in #2164 however the documentation uses the OpenAiChatClient
class which I cannot find in milestone 7 (appears to be gone after 0.8.1?). How can I set the api key for chat and embedding requests?
Metadata
Metadata
Assignees
Labels
No labels
Type
Projects
Relationships
Development
No branches or pull requests
Activity
markpollack commentedon Apr 17, 2025
Yikes, many mistakes on my part here, apologies.
The docs are wrong, there was never such a class. The
OpenAiChatAutoConfiguration
is not allowing one to override the use ofSimpleApiKey
.Unfortunately, the
@Bean
definition that createsOpenAiChatModel
contains many arguments, but that would seem to be the best way to add in your own implementation of theApiKey interface
, by basically reproducing what is herespring-ai/auto-configurations/models/spring-ai-autoconfigure-model-openai/src/main/java/org/springframework/ai/model/openai/autoconfigure/OpenAiChatAutoConfiguration.java
Line 72 in 3756e16
I realize that isn't convenient. I think that the
OpenAiChatModel
should have some sortmutate()
method (like in RestClient) so that you can get the autoconfigured one and then change a few details such as the implementation of theApiKey
interface used.tzcsx commentedon Apr 22, 2025
Thanks for the reply. A
mutate()
style pre-initialized builder would be just great.markpollack commentedon May 9, 2025
#3037 Should help, we are merging it into RC1. Closing this issue, please reopen if that solution doens't work for you. Thanks!