Skip to content

Commit 24e8a27

Browse files
committed
🏷️ Fix typing of request & streaming request
1 parent 1a43527 commit 24e8a27

File tree

6 files changed

+114
-25
lines changed

6 files changed

+114
-25
lines changed

packages/inference/src/HfInference.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
import * as tasks from "./tasks";
22
import type { Options, RequestArgs } from "./types";
3+
import type { DistributiveOmit } from "./utils/distributive-omit";
34

45
type Task = typeof tasks;
56

67
type TaskWithNoAccessToken = {
78
[key in keyof Task]: (
8-
args: Omit<Parameters<Task[key]>[0], "accessToken">,
9+
args: DistributiveOmit<Parameters<Task[key]>[0], "accessToken">,
910
options?: Parameters<Task[key]>[1]
1011
) => ReturnType<Task[key]>;
1112
};
1213

1314
type TaskWithNoAccessTokenNoModel = {
1415
[key in keyof Task]: (
15-
args: Omit<Parameters<Task[key]>[0], "accessToken" | "model">,
16+
args: DistributiveOmit<Parameters<Task[key]>[0], "accessToken" | "model">,
1617
options?: Parameters<Task[key]>[1]
1718
) => ReturnType<Task[key]>;
1819
};
@@ -51,7 +52,7 @@ export class HfInferenceEndpoint {
5152
for (const [name, fn] of Object.entries(tasks)) {
5253
Object.defineProperty(this, name, {
5354
enumerable: false,
54-
value: (params: Omit<RequestArgs, "model">, options: Options) =>
55+
value: (params: RequestArgs, options: Options) =>
5556
// eslint-disable-next-line @typescript-eslint/no-explicit-any
5657
fn({ ...params, accessToken, model: endpointUrl } as any, { ...defaultOptions, ...options }),
5758
});

packages/inference/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export interface BaseArgs {
3636
}
3737

3838
export type RequestArgs = BaseArgs &
39-
({ data?: Blob | ArrayBuffer } | { inputs: unknown }) & {
39+
({ data: Blob | ArrayBuffer } | { inputs: unknown }) & {
4040
parameters?: Record<string, unknown>;
4141
accessToken?: string;
4242
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// https://dev.to/safareli/pick-omit-and-union-types-in-typescript-4nd9
2+
// https://github.com/microsoft/TypeScript/issues/28339#issuecomment-467393437
3+
/**
4+
* This allows omitting keys from objects inside unions, without merging the individual components of the union.
5+
*/
6+
7+
type Keys<T> = keyof T;
8+
type DistributiveKeys<T> = T extends unknown ? Keys<T> : never;
9+
type Omit_<T, K> = Omit<T, Extract<keyof T, K>>;
10+
11+
export type DistributiveOmit<T, K> = T extends unknown
12+
? keyof Omit_<T, K> extends never
13+
? never
14+
: { [P in keyof Omit_<T, K>]: Omit_<T, K>[P] }
15+
: never;

packages/inference/test/HfInference.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,18 @@ describe.concurrent(
397397
generated_text: "a large brown and white giraffe standing in a field ",
398398
});
399399
});
400+
it("request - google/flan-t5-xxl", async () => {
401+
expect(
402+
await hf.request({
403+
model: "google/flan-t5-xxl",
404+
inputs: "one plus two equals",
405+
})
406+
).toMatchObject([
407+
{
408+
generated_text: expect.any(String),
409+
},
410+
]);
411+
});
400412
it("endpoint - makes request to specified endpoint", async () => {
401413
const ep = hf.endpoint("https://api-inference.huggingface.co/models/google/flan-t5-xxl");
402414
const { generated_text } = await ep.textGeneration({

packages/inference/test/tapes.json

Lines changed: 81 additions & 21 deletions
Large diffs are not rendered by default.

packages/inference/test/vcr.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ async function vcr(
143143
init: {
144144
headers: omit(init.headers as Record<string, string>, "Authorization"),
145145
method: init.method,
146+
body: typeof init.body === "string" && init.body.length < 1_000 ? init.body : undefined,
146147
},
147148
response: {
148149
body: isText ? new TextDecoder().decode(arrayBuffer) : "",

0 commit comments

Comments
 (0)