8000 Allow single input for generation endpoint (#803) · ngxson/huggingface.js@f7c2973 · GitHub
[go: up one dir, main page]

Skip to content

Commit f7c2973

Browse files
authored
Allow single input for generation endpoint (huggingface#803)
According to spec inputs can be either a single `string` or a `string` array, with the result beeing either a single `TextGenerationOutput` or and array of them.
1 parent c73abf9 commit f7c2973

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

packages/inference/src/tasks/nlp/textGeneration.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { TextGenerationInput, TextGenerationOutput } from "@huggingface/tasks";
22
import { InferenceOutputError } from "../../lib/InferenceOutputError";
33
import type { BaseArgs, Options } from "../../types";
4+
import { toArray } from "../../utils/toArray";
45
import { request } from "../custom/request";
56

67
export type { TextGenerationInput, TextGenerationOutput };
@@ -12,10 +13,12 @@ export async function textGeneration(
1213
args: BaseArgs & TextGenerationInput,
1314
options?: Options
1415
): Promise<TextGenerationOutput> {
15-
const res = await request<TextGenerationOutput[]>(args, {
16-
...options,
17-
taskHint: "text-generation",
18-
});
16+
const res = toArray(
17+
await request<TextGenerationOutput | TextGenerationOutput[]>(args, {
18+
...options,
19+
taskHint: "text-generation",
20+
})
21+
);
1922
const isValidOutput = Array.isArray(res) && res.every((x) => typeof x?.generated_text === "string");
2023
if (!isValidOutput) {
2124
throw new InferenceOutputError("Expected Array<{generated_text: string}>");

0 commit comments

Comments
 (0)
0