spring-projects / spring-ai

An Application Framework for AI Engineering
https://docs.spring.io/spring-ai/reference/index.html
Apache License 2.0
3.35k stars 866 forks source link

Support Dynamic API Key Configuration Per Request #477

Closed Karim-elaoumari closed 2 weeks ago

Karim-elaoumari commented 8 months ago

I am reaching out to request a new feature that would greatly enhance the flexibility and usability of Spring AI for a wide range of applications, particularly those requiring dynamic API key management. The feature in question is the ability to set or change the API key dynamically for each request made through the OpenAiApi class.

Currently, the OpenAiApi class accepts an API key as a parameter upon instantiation. This design mandates the use of a single API key for the lifetime of the OpenAiApi instance, which poses limitations for applications that require the use of multiple API keys (e.g., in multi-tenant systems where each tenant may use a different API key).

Desired Behavior The ideal solution would allow developers to specify an API key dynamically with each request, rather than at the instance level. This could potentially be achieved through:

Modifying existing methods to accept an API key as a parameter. Providing a mechanism to set the API key via a thread-local context or similar, ensuring thread safety and isolation between requests. Introducing a factory or provider pattern that allows for easy instantiation of OpenAiApi objects with different API keys. Use Case This feature is crucial for multi-tenant applications where different users or clients may need to interact with the OpenAI API using separate credentials. It also enhances security by allowing for finer-grained control over API key usage and rotation policies.

Proposed Solution (Optional) While I understand that changes to the API interaction pattern may have wide-ranging implications, I believe that offering a more flexible approach to API key management could significantly broaden the use cases for Spring AI. A potential solution could involve an overload of the existing methods to include an API key parameter or the introduction of a context manager for API keys.

gauravvishalgv commented 5 months ago

I guess you can create OpenAiApi object dynamically with api key and then use the object to create chatClient. For ex:

OpenAiApi openAiApi = new OpenAiApi(openAiKey);
OpenAiChatClient chatClient = new OpenAiChatClient(openAiApi);
markpollack commented 4 months ago

Thanks for the thoughtful issue. Indeed for now can achieve what you want by maintaining a colleciton of OpenAiChatClient's for each key but that feels heavy handed.

Since at least in the case of OpenAI, this involves setting the http headers, we could add an api-key option to OpenAiChatOptions so that the header can be overwritten at runtime. That would be minimally invasive and not assume any context manager implementation.

Jopo16 commented 1 month ago

Is the new feat still to be discussed/implemented? In my use case its quite similiar, have to use dynamic jwt token, but cannot get my head around it. What would be currently the most elegant approach? Thanks!!

asaikali commented 2 weeks ago

work in this is happening, initial pull request https://github.com/spring-projects/spring-ai/pull/1706

Karim-elaoumari commented 2 weeks ago

finally thanks