Skip to content
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

Merged
merged 10 commits into from May 1, 2023

Conversation

carlos-the-ai
Copy link
Contributor

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:

  • 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 What's the recommended approach for reusing/disposing new HttpClient handlers? dotnet/runtime#16255
  • Updated a sample Program
  • I can add more tests if required

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows SK Contribution Guidelines (https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
  • The code follows the .NET coding conventions (https://learn.microsoft.com/dotnet/csharp/fundamentals/coding-style/coding-conventions) verified with dotnet format
  • 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.

@github-actions github-actions bot added .NET Issue or Pull requests regarding .NET code kernel Issues or pull requests impacting the core kernel samples labels Apr 30, 2023
@carlos-the-ai
Copy link
Contributor Author

@microsoft-github-policy-service agree

@adrianwyatt adrianwyatt self-assigned this May 1, 2023
@adrianwyatt adrianwyatt added the PR: feedback to address Waiting for PR owner to address comments/questions label May 1, 2023
@adrianwyatt
Copy link
Contributor

LGTM - waiting for build break fixes.

Copy link
Member

@lemillermicrosoft lemillermicrosoft left a 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.

@shawncal
Copy link
Member

shawncal commented May 1, 2023

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.

Yup, you are right. I think we can actually take this further and remove the IDelegatingHandlerFactory interface altogether. That will take more work and planning. This change is a huge step in the right direction, and we should close on it before doing more.

It might be useful to add a kernel-syntax-examples to demonstrate supplying a custom httpclient.

@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".

@adrianwyatt adrianwyatt merged commit 8b48224 into microsoft:main May 1, 2023
11 checks passed
awharrison-28 pushed a commit to awharrison-28/semantic-kernel that referenced this pull request May 1, 2023
### 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.
codebrain pushed a commit to searchpioneer/semantic-kernel that referenced this pull request May 16, 2023
### 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.
dehoward pushed a commit to lemillermicrosoft/semantic-kernel that referenced this pull request Jun 1, 2023
### 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.
golden-aries pushed a commit to golden-aries/semantic-kernel that referenced this pull request Oct 10, 2023
### 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.
dluc pushed a commit to microsoft/kernel-memory that referenced this pull request Jan 4, 2024
## 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kernel Issues or pull requests impacting the core kernel .NET Issue or Pull requests regarding .NET code PR: feedback to address Waiting for PR owner to address comments/questions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants