-
Notifications
You must be signed in to change notification settings - Fork 337
Add comprehensive dependency injection support with IServiceCollection extension methods #696
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
base: main
Are you sure you want to change the base?
Conversation
This commit introduces new features for dependency injection in the OpenAI .NET library, allowing for easier registration and configuration of clients within ASP.NET Core applications. Key changes include: - New extension methods for registering OpenAI clients with support for `appsettings.json` and environment variables. - Introduction of the `OpenAIServiceOptions` class for simplified configuration. - Updates to README.md with detailed usage instructions and examples. - Modifications to project files to include necessary package references. - Comprehensive unit tests to validate the new features and ensure proper functionality.
Should this instead be left to Aspire, e.g. https://www.nuget.org/packages/Aspire.OpenAI ? cc: @eerhardt |
@stephentoub @KrzysztofCwalina, I was wondering about the current status of this PR. Should I wait or make any changes? |
@m-nash @eerhardt, @jsquire, how would you want to handle this PR? It seems like you are working features similar to it. |
@eerhardt Could you review this, please? |
Hi @mokarchi. Thank you for your contribution and your interest in improving the OpenAI developer experience. Apologies for the feedback delay, but there was some coordination needed on other workstreams. As Krzysztof mentioned, we are in the process of designing an experience where clients natively integrate with the .NET configuration, options, and DI systems. Part of that work entails defining the common patterns across the OpenAI clients for DI registration, construction, and configuration and ensuring that they align closely with the .NET ecosystem. Because we don't yet have a fully codified design, we're not able to provide the detailed feedback that would be needed to help your contribution align. As a result, we're unable to move forward at this time. We have a couple of options that you can consider for next steps:
Please let us know how you'd like to proceed. //cc: @m-nash for awareness. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Blocking for now to prevent accidental merge.
Thank you for the update, @jsquire. |
Thank you, @mokarchi. We appreciate your flexibility and understanding. |
Late to the party, but yeah this feels like the best path forward. Once the design is ready to share we can collaborate on the implementation. |
This PR implements enhanced dependency injection support for the OpenAI .NET library, addressing the feature request for built-in extension methods for
IServiceCollection
. The implementation follows .NET ecosystem standards similar to Azure SDKs, making it easier to integrate OpenAI clients into ASP.NET Core, Blazor, and other .NET applications.Key Features Added
Extension Methods for IServiceCollection
The library now provides convenient extension methods for registering OpenAI clients:
Configuration Integration
Full support for
IConfiguration
binding withappsettings.json
:Environment Variable Support
Automatic fallback to the
OPENAI_API_KEY
environment variable for development scenarios:Implementation Details
Available Extension Methods
AddOpenAI()
,AddOpenAIChat()
,AddOpenAIEmbeddings()
,AddOpenAIAudio()
,AddOpenAIImages()
,AddOpenAIModeration()
AddOpenAIFromConfiguration()
,AddChatClientFromConfiguration()
, etc.AddAllOpenAIClientsFromConfiguration()
Enhanced Configuration Options
The new
OpenAIServiceOptions
class extendsOpenAIClientOptions
with DI-specific settings:OpenAIClientOptions
properties (Endpoint, OrganizationId, ProjectId, etc.)Thread-Safe Singleton Registration
All clients are registered as singletons by default, following the existing recommendation in the README. This maximizes resource efficiency and HTTP connection reuse while maintaining thread safety.
Backward Compatibility
This implementation is fully backward compatible. Existing manual registration patterns continue to work unchanged: