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

Some clean up in OpenAIClientBase #788

Merged
merged 4 commits into from May 4, 2023

Conversation

stephentoub
Copy link
Member

Motivation and Context

Fix some possible functional issues as well as remove unnecessary code.

Description

  • The type accepts an HttpClient instance that it'll use. It shouldn't then mutate that instance, as it might be shared with other consumers. But it's adding to its DefaultRequestHeaders collection, which will impact all other users of that instance (DefaultRequestHeaders also isn't thread-safe and should only ever be added to while it's not being used for issuing requests). I've removed the interaction with DefaultRequestHeaders and instead added the relevant headers to each message being sent.
  • The base is exposing a lot of protected surface area, but it's not actually extensible outside of the assembly. I've changed all of its internal and protected members to instead be "private protected" or just "private", reflecting their actual usage and ensuring the surface area doesn't show up in public documentation (which protected members would).
  • As it's not actually extensible publicly, and as no derived types are finalizable, it needn't implement the full IDisposable pattern nor use GC.SuppressFinalize.
  • Both the base and the derived image generation client had duplicated APIs for url vs base64 image generation. I've consolidated that via arguments to just have one copy of the code. Note that the base64 image generation was private and unused, so I've deleted that unused member, but the functionality can be exposed easily if desired based on the shared routines now in place.
  • Removed some unnecessary null checking for things that'll never be null.
  • Ensured the HttpClient response message is disposed.

Contribution Checklist

- The type accepts an HttpClient instance that it'll use.  It shouldn't then mutate that instance, as it might be shared with other consumers.  But it's adding to its DefaultRequestHeaders collection, which will impact all other users of that instance (DefaultRequestHeaders also isn't thread-safe and should only ever be added to while it's not being used for issuing requests).  I've removed the interaction with DefaultRequestHeaders and instead added the relevant headers to each message being sent.
- The base is exposing a lot of protected surface area, but it's not actually extensible outside of the assembly.  I've changed all of its internal and protected members to instead be "private protected" or just "private", reflecting their actual usage and ensuring the surface area doesn't show up in public documentation (which protected members would).
- As it's not actually extensible publicly, and as no derived types are finalizable, it needn't implement the full IDisposable pattern nor use GC.SuppressFinalize.
- Both the base and the derived image generation client had duplicated APIs for url vs base64 image generation.  I've consolidated that via arguments to just have one copy of the code. Note that the base64 image generation was private and unused, so I've deleted that unused member, but the functionality can be exposed easily if desired based on the shared routines now in place.
- Removed some unnecessary null checking for things that'll never be null.
- Ensured the HttpClient response message is disposed.
@github-actions github-actions bot added .NET Issue or Pull requests regarding .NET code kernel Issues or pull requests impacting the core kernel labels May 3, 2023
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.

LGTM

@lemillermicrosoft lemillermicrosoft added the PR: ready to merge PR has been approved by all reviewers, and is ready to merge. label May 3, 2023
@shawncal shawncal enabled auto-merge (squash) May 4, 2023 19:15
@shawncal shawncal merged commit 0b9a418 into microsoft:main May 4, 2023
10 checks passed
codebrain pushed a commit to searchpioneer/semantic-kernel that referenced this pull request May 16, 2023
### Motivation and Context
Fix some possible functional issues as well as remove unnecessary code.

### Description
- The type accepts an HttpClient instance that it'll use. It shouldn't
then mutate that instance, as it might be shared with other consumers.
But it's adding to its DefaultRequestHeaders collection, which will
impact all other users of that instance (DefaultRequestHeaders also
isn't thread-safe and should only ever be added to while it's not being
used for issuing requests). I've removed the interaction with
DefaultRequestHeaders and instead added the relevant headers to each
message being sent.
- The base is exposing a lot of protected surface area, but it's not
actually extensible outside of the assembly. I've changed all of its
internal and protected members to instead be "private protected" or just
"private", reflecting their actual usage and ensuring the surface area
doesn't show up in public documentation (which protected members would).
- As it's not actually extensible publicly, and as no derived types are
finalizable, it needn't implement the full IDisposable pattern nor use
GC.SuppressFinalize.
- Both the base and the derived image generation client had duplicated
APIs for url vs base64 image generation. I've consolidated that via
arguments to just have one copy of the code. Note that the base64 image
generation was private and unused, so I've deleted that unused member,
but the functionality can be exposed easily if desired based on the
shared routines now in place.
- Removed some unnecessary null checking for things that'll never be
null.
- Ensured the HttpClient response message is disposed.
dehoward pushed a commit to lemillermicrosoft/semantic-kernel that referenced this pull request Jun 1, 2023
### Motivation and Context
Fix some possible functional issues as well as remove unnecessary code.

### Description
- The type accepts an HttpClient instance that it'll use. It shouldn't
then mutate that instance, as it might be shared with other consumers.
But it's adding to its DefaultRequestHeaders collection, which will
impact all other users of that instance (DefaultRequestHeaders also
isn't thread-safe and should only ever be added to while it's not being
used for issuing requests). I've removed the interaction with
DefaultRequestHeaders and instead added the relevant headers to each
message being sent.
- The base is exposing a lot of protected surface area, but it's not
actually extensible outside of the assembly. I've changed all of its
internal and protected members to instead be "private protected" or just
"private", reflecting their actual usage and ensuring the surface area
doesn't show up in public documentation (which protected members would).
- As it's not actually extensible publicly, and as no derived types are
finalizable, it needn't implement the full IDisposable pattern nor use
GC.SuppressFinalize.
- Both the base and the derived image generation client had duplicated
APIs for url vs base64 image generation. I've consolidated that via
arguments to just have one copy of the code. Note that the base64 image
generation was private and unused, so I've deleted that unused member,
but the functionality can be exposed easily if desired based on the
shared routines now in place.
- Removed some unnecessary null checking for things that'll never be
null.
- Ensured the HttpClient response message is disposed.
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: ready to merge PR has been approved by all reviewers, and is ready to merge.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants