-
Notifications
You must be signed in to change notification settings - Fork 283
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] Better types for HfInference #1121
base: main
Are you sure you want to change the base?
Conversation
// it("request - openai-community/gpt2", async () => { | ||
// expect( | ||
// await hf.request({ | ||
// model: "openai-community/gpt2", | ||
// inputs: "one plus two equals", | ||
// }) | ||
// ).toMatchObject([ | ||
// { | ||
// generated_text: expect.any(String), | ||
// }, | ||
// ]); | ||
// }); |
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.
Still need to find a way around request
and streamingRequest
... They use a generic type T
which messes thing up I think
export class HfInference { | ||
private readonly accessToken: string; | ||
private readonly defaultOptions: Options; | ||
export class HfInference implements Task { |
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.
export class HfInference implements Task {
That bit makes sure HfInference
defines a method for every task (textToImage
, chatCompletion
, etc)
static mapInferenceFn<TOut, TArgs>(instance: HfInference, func: (...args: [TArgs, Options?]) => TOut) { | ||
return function (...[args, options]: Parameters<(...args: [TArgs, Options?]) => TOut>): TOut { | ||
return func({ ...args, accessToken: instance.accessToken }, { ...instance.defaultOptions, ...(options ?? {}) }); | ||
}; | ||
} |
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.
This helper functions adds some default arguments when calling HfInference.<task>()
while keeping the exact type for inputs and outputs
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.
So jealous about how Typescript's seems so much powerful than Python's typing system... 😒
@@ -40,3 +42,5 @@ export * from "./multimodal/visualQuestionAnswering"; | |||
// Tabular tasks | |||
export * from "./tabular/tabularRegression"; | |||
export * from "./tabular/tabularClassification"; | |||
|
|||
export const speechToText = automaticSpeechRecognition; |
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.
(unrelated) aliasing automaticSpeechRecognition
as speechToText
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.
did it exist before or that's new?
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.
(why not i guess, though we should try to push the "official" task names otherwise it's hard to standardize one way of doing things)
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.
(why not i guess, though we should try to push the "official" task names otherwise it's hard to standardize one way of doing things)
yeah, good point
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.
Do you want to edit HfInferenceEndpoint
too in the same file?
Also note the script packages/inference/scripts/generate-dts.ts
used for generating definition files, you may need to change it now
(and we could switch from tsup to tshy removing the need for the script entirely maybe)
(unfinished, but I would love some inputs)
cc @coyotte508
Before this PR, Typescript was unable to determine actual types for the inputs of methods on the
HfInference
clientHfInference.textToImage
would lack typings forparameters
for exampleThis PR ensures that all tasks that are exported in
src/tasks/index
are re-exported by the clientHfInference
, and introduces a helper function that keeps every task's types along the way