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

[Inference] Implement a "1 class = 1 provider<>task pair" logic to isolate provider-specific code #1315

Merged
merged 32 commits into from
Apr 8, 2025

Conversation

hanouticelina
Copy link
Contributor

@hanouticelina hanouticelina commented Mar 26, 2025

The Goal of this PR is to isolate provider-specific logic into its own class/file. Similar to what we have in the Python client, this gives a clear boundary between generic task implementation and provider-specific implementation details.

This makes updating or adding a new provider much easier, adding support for a new provider will be as much as easier as:

  1. register the provider in the mapping.
  2. create a new packages/inference/src/providers/{provider_name}.ts.
  3. implement the methods that require custom handling.

Main changes

  • Added a new TaskProviderHelper abstract class that defines a common interface (makeBody, preparePayload makeRoute, getResponse, prepareHeaders, etc.) that all provider-specific helpers must implement.
  • Added PROVIDERS registry that maps any provider<>task to the corresponding TaskProviderHelper subclass that implements the provider<>task logic.
  • Added a lookup function (getProviderHelper) that takes a provider and task and returns the appropriate TaskProviderHelper instance from the PROVIDERS registry.
  • Implemented the task helpers for providers:
    • All task helpers have been implemented for all providers.

🙏 Any feedback is welcome as the implementation is quite biased towards the Python but we might want to do some things differently here.

@hanouticelina hanouticelina requested a review from Wauplin March 26, 2025 17:57
@hanouticelina hanouticelina marked this pull request as draft March 26, 2025 17:59
@julien-c
Copy link
Member

  • create a new packages/inference/src/providers/{provider_name}.py.

i think you mean .ts, otherwise i'm a bit concerned by this PR 😆

Copy link
Contributor

@Wauplin Wauplin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dunno where the "self-contained logic for each provider <> task"- idea comes from but I really like it! 😄

I tried to review the PR the best I can but hard to be exhaustive 😬 Overall very nice improvement

Copy link
Contributor

@SBrandeis SBrandeis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a big PR - I would like to do some other rounds of review later to ensure I did not miss something :)

Great work overall - it will make contributing a provider much easier 💪

I'm a bit disappointed by the type narrowing (there is too much need for type casts to my taste) - especially when calling providerHelper.getResponse()

Perhaps we need an additional asbtract class per task to narrow down types in getProviderHelper..

I have left some comments on the form too (mostly code style)

@SBrandeis
Copy link
Contributor

@hanouticelina I have opened #1332 with a proposal to improve the typing of getProviderHelper

Let me know what you think

hanouticelina and others added 3 commits April 4, 2025 15:17
This PR introduces task helpers to achieve better typing of the
`getProviderHelper`, this is inspired by @SBrandeis demonstration PR
#1332, without reversing the mapping. There is a lot of diff (sorry for
that) because I isolated every HF inference API-specific code into the
provider file and there are a lot of supported tasks 😬

---------

Co-authored-by: SBrandeis <[email protected]>
Copy link
Contributor

@SBrandeis SBrandeis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's go! 🚀

Copy link
Contributor

@Wauplin Wauplin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let get this merged! 🎉

@hanouticelina
Copy link
Contributor Author

merging!

@hanouticelina hanouticelina merged commit 9b3e2cf into main Apr 8, 2025
5 checks passed
@hanouticelina hanouticelina deleted the refactor-providers branch April 8, 2025 08:37
@julien-c
Copy link
Member

julien-c commented Apr 8, 2025

Woohoo! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants