|
|
import * as tasks from "./tasks/index.js"; |
|
|
import type { Options } from "./types.js"; |
|
|
import { omit } from "./utils/omit.js"; |
|
|
import { typedEntries } from "./utils/typedEntries.js"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type Task = typeof tasks; |
|
|
|
|
|
export class InferenceClient { |
|
|
private readonly accessToken: string; |
|
|
private readonly defaultOptions: Options; |
|
|
|
|
|
constructor( |
|
|
accessToken = "", |
|
|
defaultOptions: Options & { |
|
|
endpointUrl?: string; |
|
|
} = {} |
|
|
) { |
|
|
this.accessToken = accessToken; |
|
|
this.defaultOptions = defaultOptions; |
|
|
|
|
|
for (const [name, fn] of typedEntries(tasks)) { |
|
|
Object.defineProperty(this, name, { |
|
|
enumerable: false, |
|
|
value: (params: Parameters<typeof fn>[0], options: Parameters<typeof fn>[1]) => |
|
|
|
|
|
(fn as any)( |
|
|
|
|
|
{ endpointUrl: defaultOptions.endpointUrl, accessToken, ...params }, |
|
|
{ |
|
|
...omit(defaultOptions, ["endpointUrl"]), |
|
|
...options, |
|
|
} |
|
|
), |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public endpoint(endpointUrl: string): InferenceClient { |
|
|
return new InferenceClient(this.accessToken, { ...this.defaultOptions, endpointUrl }); |
|
|
} |
|
|
} |
|
|
|
|
|
export interface InferenceClient extends Task {} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export class HfInference extends InferenceClient {} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export class InferenceClientEndpoint extends InferenceClient {} |
|
|
|