Skip to content

[Inference Providers] Refactor: better typing? #1332

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

Closed
wants to merge 1 commit into from

Conversation

SBrandeis
Copy link
Contributor

cc @hanouticelina

For demonstration purpose only - let's not merge

I think we can achieve better typing of the getProviderHelper if we reverse the mapping (provider -> task to task -> provider)

See the textToImage file (no more type casts)

Having to implement and extend 2 types when implementing a helper is quite inconvenient on the other hand - so not sure about this either

@hanouticelina
Copy link
Contributor

much better in terms of typing, thank you!

one question: It seems to me (and i might be wrong) the improved typing for providerHelper.getResponse() comes directly from the additional per-task Helpers (TextToImageTaskHelper and TextGenerationTaskHelper) as you suggested in the initial PR as well. if that's the case, what additional typing advantages reversing the main PROVIDERS mapping brings?

@@ -73,7 +74,36 @@ export abstract class TaskProviderHelper {
abstract preparePayload(params: BodyParams): unknown;
}

export class BaseConversationalTask extends TaskProviderHelper {
export interface TextToImageTaskHelper {
getResponse(response: unknown,
Copy link
Contributor

Choose a reason for hiding this comment

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

when making response as unknown here, is it still possible to declare the getResponse method in BlackForestLabsTextToImageTask with response: BlackForestLabsResponse ?
same for other downstream classes that will implement TextToImageTaskHelper.

Copy link
Contributor

Choose a reason for hiding this comment

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

got my answer: it should be good 😄 i was just overthinking it

@SBrandeis SBrandeis closed this Apr 3, 2025
hanouticelina added a commit that referenced this pull request Apr 4, 2025
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]>
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.

2 participants