spring-projects / spring-ai

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

Support Dynamic API Key Configuration Per Request #477

Open Karim-elaoumari opened 3 months ago

Karim-elaoumari commented 3 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 1 month 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);