Skip to content

How to use dynamic api keys with openAI #2731

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tzcsx opened this issue Apr 14, 2025 · 3 comments
Closed

How to use dynamic api keys with openAI #2731

tzcsx opened this issue Apr 14, 2025 · 3 comments
Assignees
Milestone

Comments

@tzcsx
Copy link

tzcsx commented Apr 14, 2025

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?

@markpollack
Copy link
Member

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 of SimpleApiKey.

Unfortunately, the @Bean definition that creates OpenAiChatModel contains many arguments, but that would seem to be the best way to add in your own implementation of the ApiKey interface, by basically reproducing what is here

I realize that isn't convenient. I think that the OpenAiChatModel should have some sort mutate() method (like in RestClient) so that you can get the autoconfigured one and then change a few details such as the implementation of the ApiKey interface used.

@markpollack markpollack added this to the 1.0.0-RC1 milestone Apr 17, 2025
@markpollack markpollack self-assigned this Apr 17, 2025
@tzcsx
Copy link
Author

tzcsx commented Apr 22, 2025

Thanks for the reply. A mutate() style pre-initialized builder would be just great.

@markpollack
Copy link
Member

#3037 Should help, we are merging it into RC1. Closing this issue, please reopen if that solution doens't work for you. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants