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
Allow passing custom HttpClient #743
Conversation
@microsoft-github-policy-service agree |
I merged from main for you, which picked up some duplicate args. Fixing that. Sorry for the churn!
One more...
…semantic-kernel into custom-httpclient
LGTM - waiting for build break fixes. |
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.
It might be useful to add a kernel-syntax-examples
to demonstrate supplying a custom httpclient.
Yup, you are right. I think we can actually take this further and remove the
@lemillermicrosoft I agree with the above. I'll take a note to do that when we finish the change -- if we are able to remove IDelegatingHandlerFactory as noted above, we can likely update "Example08_RetryHandler". There is an example in "KernelBuilder". |
### Motivation and Context My company network needs more custom set up in HttpClient to make calls, but SK OpenAI code only allows me to pass a hander delegate factory. I cannot use SK without more settings in HttpClient and I want to pass in my own instances anyway so I can manage them from my application. I think I am not the only one to need this: - microsoft#527 - microsoft#133 See https://learn.microsoft.com/en-us/dotnet/fundamentals/networking/http/httpclient-guidelines. ### Description Changes included: - The OpenAI and AzureOpenAI connectors take optional HttpClient? in the constructor methods instead of IDelegatingHandlerFactory - Note first approach was adding two new constructor methods to each type so you could choose. But two constructors could not be there together with optional HttpClient? in one and optional IDelegatingHandlerFactory? in the other because it made calls ambiguous. Then I tried making HttpClient not optional, but that made the parameter order different because in some there are also other optional parameters like openai `string? organization = null`. So the not optional HttpClient would have to come BEFORE optional openai organization in one constructor, but the optional IDelegatingHandlerFactory comes AFTER optional openai organization in another constructor. In the end it seemed better to just take HttpClient and do the custom client creation in the extension methods. I hope this is okay I know it is a bigger change than I planned. - IDelegatingHandlerFactory is not being used as a factory in these Open AI connector classes anyway. They use it one to create an HttpClient instance in the constructor method anyway and never call it again so it's just as easy to pass in the HttpClient you want. - Updated the extension methods in `KernelConfigOpenAIExtensions` so that they can optionally pass in a custom HttpClient and ILogger. If null then it uses the same defaults as before. - There was some mix use of `Logger log` and `ILogger logger` in different places so on the parts I changed I set these all to "logger" because it makes it more clear that it is a object and not a "log" method. I can undo if you don't like this. - Changed "credentials" to "credential" to match this is one `TokenCredenial` not a credential collection. I can undo if you don't like this. - Made the default HttpClientHandler static and shared for new HttpClients with `disposeHandler: false` dotnet/runtime#16255 - Updated a sample Program - I can add more tests if required ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [X] The code builds clean without any errors or warnings - [X] The PR follows SK Contribution Guidelines (https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) - [X] The code follows the .NET coding conventions (https://learn.microsoft.com/dotnet/csharp/fundamentals/coding-style/coding-conventions) verified with `dotnet format` - [X] All unit tests pass, and I have added new tests where possible - Most tests pass but it skips some because I don't have keys for all these model types so I hope your github build will run this and double check. - [ ] I didn't break anyone 😄 - I changed the constructor method args in the OpenAI code, and the will break your users that are creating these open ai classes using their constructors directly. If the users are using the extension methods (AddAzureOpenAI) then they should not have problems. - If you want I can put back all of the constructor methods so they can be created with HttpClient or IDelegatingHandlerFactory but with parameters orders different as explained - I don't think those classes need to be aware of IDelegatingHandlerFactory or have it included in the constructor methods, since the extension method or calling code can always convert to HttpClient before calling.
### Motivation and Context My company network needs more custom set up in HttpClient to make calls, but SK OpenAI code only allows me to pass a hander delegate factory. I cannot use SK without more settings in HttpClient and I want to pass in my own instances anyway so I can manage them from my application. I think I am not the only one to need this: - microsoft#527 - microsoft#133 See https://learn.microsoft.com/en-us/dotnet/fundamentals/networking/http/httpclient-guidelines. ### Description Changes included: - The OpenAI and AzureOpenAI connectors take optional HttpClient? in the constructor methods instead of IDelegatingHandlerFactory - Note first approach was adding two new constructor methods to each type so you could choose. But two constructors could not be there together with optional HttpClient? in one and optional IDelegatingHandlerFactory? in the other because it made calls ambiguous. Then I tried making HttpClient not optional, but that made the parameter order different because in some there are also other optional parameters like openai `string? organization = null`. So the not optional HttpClient would have to come BEFORE optional openai organization in one constructor, but the optional IDelegatingHandlerFactory comes AFTER optional openai organization in another constructor. In the end it seemed better to just take HttpClient and do the custom client creation in the extension methods. I hope this is okay I know it is a bigger change than I planned. - IDelegatingHandlerFactory is not being used as a factory in these Open AI connector classes anyway. They use it one to create an HttpClient instance in the constructor method anyway and never call it again so it's just as easy to pass in the HttpClient you want. - Updated the extension methods in `KernelConfigOpenAIExtensions` so that they can optionally pass in a custom HttpClient and ILogger. If null then it uses the same defaults as before. - There was some mix use of `Logger log` and `ILogger logger` in different places so on the parts I changed I set these all to "logger" because it makes it more clear that it is a object and not a "log" method. I can undo if you don't like this. - Changed "credentials" to "credential" to match this is one `TokenCredenial` not a credential collection. I can undo if you don't like this. - Made the default HttpClientHandler static and shared for new HttpClients with `disposeHandler: false` dotnet/runtime#16255 - Updated a sample Program - I can add more tests if required ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [X] The code builds clean without any errors or warnings - [X] The PR follows SK Contribution Guidelines (https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) - [X] The code follows the .NET coding conventions (https://learn.microsoft.com/dotnet/csharp/fundamentals/coding-style/coding-conventions) verified with `dotnet format` - [X] All unit tests pass, and I have added new tests where possible - Most tests pass but it skips some because I don't have keys for all these model types so I hope your github build will run this and double check. - [ ] I didn't break anyone 😄 - I changed the constructor method args in the OpenAI code, and the will break your users that are creating these open ai classes using their constructors directly. If the users are using the extension methods (AddAzureOpenAI) then they should not have problems. - If you want I can put back all of the constructor methods so they can be created with HttpClient or IDelegatingHandlerFactory but with parameters orders different as explained - I don't think those classes need to be aware of IDelegatingHandlerFactory or have it included in the constructor methods, since the extension method or calling code can always convert to HttpClient before calling.
### Motivation and Context My company network needs more custom set up in HttpClient to make calls, but SK OpenAI code only allows me to pass a hander delegate factory. I cannot use SK without more settings in HttpClient and I want to pass in my own instances anyway so I can manage them from my application. I think I am not the only one to need this: - microsoft#527 - microsoft#133 See https://learn.microsoft.com/en-us/dotnet/fundamentals/networking/http/httpclient-guidelines. ### Description Changes included: - The OpenAI and AzureOpenAI connectors take optional HttpClient? in the constructor methods instead of IDelegatingHandlerFactory - Note first approach was adding two new constructor methods to each type so you could choose. But two constructors could not be there together with optional HttpClient? in one and optional IDelegatingHandlerFactory? in the other because it made calls ambiguous. Then I tried making HttpClient not optional, but that made the parameter order different because in some there are also other optional parameters like openai `string? organization = null`. So the not optional HttpClient would have to come BEFORE optional openai organization in one constructor, but the optional IDelegatingHandlerFactory comes AFTER optional openai organization in another constructor. In the end it seemed better to just take HttpClient and do the custom client creation in the extension methods. I hope this is okay I know it is a bigger change than I planned. - IDelegatingHandlerFactory is not being used as a factory in these Open AI connector classes anyway. They use it one to create an HttpClient instance in the constructor method anyway and never call it again so it's just as easy to pass in the HttpClient you want. - Updated the extension methods in `KernelConfigOpenAIExtensions` so that they can optionally pass in a custom HttpClient and ILogger. If null then it uses the same defaults as before. - There was some mix use of `Logger log` and `ILogger logger` in different places so on the parts I changed I set these all to "logger" because it makes it more clear that it is a object and not a "log" method. I can undo if you don't like this. - Changed "credentials" to "credential" to match this is one `TokenCredenial` not a credential collection. I can undo if you don't like this. - Made the default HttpClientHandler static and shared for new HttpClients with `disposeHandler: false` dotnet/runtime#16255 - Updated a sample Program - I can add more tests if required ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [X] The code builds clean without any errors or warnings - [X] The PR follows SK Contribution Guidelines (https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) - [X] The code follows the .NET coding conventions (https://learn.microsoft.com/dotnet/csharp/fundamentals/coding-style/coding-conventions) verified with `dotnet format` - [X] All unit tests pass, and I have added new tests where possible - Most tests pass but it skips some because I don't have keys for all these model types so I hope your github build will run this and double check. - [ ] I didn't break anyone 😄 - I changed the constructor method args in the OpenAI code, and the will break your users that are creating these open ai classes using their constructors directly. If the users are using the extension methods (AddAzureOpenAI) then they should not have problems. - If you want I can put back all of the constructor methods so they can be created with HttpClient or IDelegatingHandlerFactory but with parameters orders different as explained - I don't think those classes need to be aware of IDelegatingHandlerFactory or have it included in the constructor methods, since the extension method or calling code can always convert to HttpClient before calling.
### Motivation and Context My company network needs more custom set up in HttpClient to make calls, but SK OpenAI code only allows me to pass a hander delegate factory. I cannot use SK without more settings in HttpClient and I want to pass in my own instances anyway so I can manage them from my application. I think I am not the only one to need this: - microsoft#527 - microsoft#133 See https://learn.microsoft.com/en-us/dotnet/fundamentals/networking/http/httpclient-guidelines. ### Description Changes included: - The OpenAI and AzureOpenAI connectors take optional HttpClient? in the constructor methods instead of IDelegatingHandlerFactory - Note first approach was adding two new constructor methods to each type so you could choose. But two constructors could not be there together with optional HttpClient? in one and optional IDelegatingHandlerFactory? in the other because it made calls ambiguous. Then I tried making HttpClient not optional, but that made the parameter order different because in some there are also other optional parameters like openai `string? organization = null`. So the not optional HttpClient would have to come BEFORE optional openai organization in one constructor, but the optional IDelegatingHandlerFactory comes AFTER optional openai organization in another constructor. In the end it seemed better to just take HttpClient and do the custom client creation in the extension methods. I hope this is okay I know it is a bigger change than I planned. - IDelegatingHandlerFactory is not being used as a factory in these Open AI connector classes anyway. They use it one to create an HttpClient instance in the constructor method anyway and never call it again so it's just as easy to pass in the HttpClient you want. - Updated the extension methods in `KernelConfigOpenAIExtensions` so that they can optionally pass in a custom HttpClient and ILogger. If null then it uses the same defaults as before. - There was some mix use of `Logger log` and `ILogger logger` in different places so on the parts I changed I set these all to "logger" because it makes it more clear that it is a object and not a "log" method. I can undo if you don't like this. - Changed "credentials" to "credential" to match this is one `TokenCredenial` not a credential collection. I can undo if you don't like this. - Made the default HttpClientHandler static and shared for new HttpClients with `disposeHandler: false` dotnet/runtime#16255 - Updated a sample Program - I can add more tests if required ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [X] The code builds clean without any errors or warnings - [X] The PR follows SK Contribution Guidelines (https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) - [X] The code follows the .NET coding conventions (https://learn.microsoft.com/dotnet/csharp/fundamentals/coding-style/coding-conventions) verified with `dotnet format` - [X] All unit tests pass, and I have added new tests where possible - Most tests pass but it skips some because I don't have keys for all these model types so I hope your github build will run this and double check. - [ ] I didn't break anyone 😄 - I changed the constructor method args in the OpenAI code, and the will break your users that are creating these open ai classes using their constructors directly. If the users are using the extension methods (AddAzureOpenAI) then they should not have problems. - If you want I can put back all of the constructor methods so they can be created with HttpClient or IDelegatingHandlerFactory but with parameters orders different as explained - I don't think those classes need to be aware of IDelegatingHandlerFactory or have it included in the constructor methods, since the extension method or calling code can always convert to HttpClient before calling.
## Motivation and Context For some businesses and their teams, there is often a need for more detailed and personalized configuration when using HttpClient to make network requests. This PR aligns the corresponding features to be on par with the design of Semantic Kernel. Reference: microsoft/semantic-kernel#743 ## Description Introduce a new optional parameter named `httpClient`. By setting its default value to `null`, we ensure that the existing user's code remains unaffected and backward compatible.
Motivation and Context
My company network needs more custom set up in HttpClient to make calls, but SK OpenAI code only allows me to pass a hander delegate factory. I cannot use SK without more settings in HttpClient and I want to pass in my own instances anyway so I can manage them from my application.
I think I am not the only one to need this:
See https://learn.microsoft.com/en-us/dotnet/fundamentals/networking/http/httpclient-guidelines.
Description
Changes included:
string? organization = null
. So the not optional HttpClient would have to come BEFORE optional openai organization in one constructor, but the optional IDelegatingHandlerFactory comes AFTER optional openai organization in another constructor. In the end it seemed better to just take HttpClient and do the custom client creation in the extension methods. I hope this is okay I know it is a bigger change than I planned.KernelConfigOpenAIExtensions
so that they can optionally pass in a custom HttpClient and ILogger. If null then it uses the same defaults as before.Logger log
andILogger logger
in different places so on the parts I changed I set these all to "logger" because it makes it more clear that it is a object and not a "log" method. I can undo if you don't like this.TokenCredenial
not a credential collection. I can undo if you don't like this.disposeHandler: false
What's the recommended approach for reusing/disposing new HttpClient handlers? dotnet/runtime#16255Contribution Checklist
dotnet format