Skip to content
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

✨ [Widgets] Enable streaming in the conversational widget #486

Merged
merged 22 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 5 additions & 61 deletions packages/inference/src/tasks/nlp/textGeneration.ts
Original file line number Diff line number Diff line change
@@ -1,71 +1,15 @@
import type { TextGenerationInput, TextGenerationOutput } from "@huggingface/tasks/src/tasks/text-generation/inference";
import { InferenceOutputError } from "../../lib/InferenceOutputError";
import type { BaseArgs, Options } from "../../types";
import { request } from "../custom/request";

export type TextGenerationArgs = BaseArgs & {
/**
* A string to be generated from
*/
inputs: string;
parameters?: {
/**
* (Optional: True). Bool. Whether or not to use sampling, use greedy decoding otherwise.
*/
do_sample?: boolean;
/**
* (Default: None). Int (0-250). The amount of new tokens to be generated, this does not include the input length it is a estimate of the size of generated text you want. Each new tokens slows down the request, so look for balance between response times and length of text generated.
*/
max_new_tokens?: number;
/**
* (Default: None). Float (0-120.0). The amount of time in seconds that the query should take maximum. Network can cause some overhead so it will be a soft limit. Use that in combination with max_new_tokens for best results.
*/
max_time?: number;
/**
* (Default: 1). Integer. The number of proposition you want to be returned.
*/
num_return_sequences?: number;
/**
* (Default: None). Float (0.0-100.0). The more a token is used within generation the more it is penalized to not be picked in successive generation passes.
*/
repetition_penalty?: number;
/**
* (Default: True). Bool. If set to False, the return results will not contain the original query making it easier for prompting.
*/
return_full_text?: boolean;
/**
* (Default: 1.0). Float (0.0-100.0). The temperature of the sampling operation. 1 means regular sampling, 0 means always take the highest score, 100.0 is getting closer to uniform probability.
*/
temperature?: number;
/**
* (Default: None). Integer to define the top tokens considered within the sample operation to create new text.
*/
top_k?: number;
/**
* (Default: None). Float to define the tokens that are within the sample operation of text generation. Add tokens in the sample for more probable to least probable until the sum of the probabilities is greater than top_p.
*/
top_p?: number;
/**
* (Default: None). Integer. The maximum number of tokens from the input.
*/
truncate?: number;
/**
* (Default: []) List of strings. The model will stop generating text when one of the strings in the list is generated.
* **/
stop_sequences?: string[];
};
};

export interface TextGenerationOutput {
/**
* The continuated string
*/
generated_text: string;
}

/**
* Use to continue text from a prompt. This is a very generic task. Recommended model: gpt2 (it’s a simple model, but fun to play with).
*/
export async function textGeneration(args: TextGenerationArgs, options?: Options): Promise<TextGenerationOutput> {
export async function textGeneration(
args: BaseArgs & TextGenerationInput,
options?: Options
): Promise<TextGenerationOutput> {
const res = await request<TextGenerationOutput[]>(args, {
...options,
taskHint: "text-generation",
Expand Down
7 changes: 4 additions & 3 deletions packages/inference/src/tasks/nlp/textGenerationStream.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Options } from "../../types";
import type { BaseArgs, Options } from "../../types";
import { streamingRequest } from "../custom/streamingRequest";
import type { TextGenerationArgs } from "./textGeneration";

import type { TextGenerationInput } from "@huggingface/tasks/src/tasks/text-generation/inference";

export interface TextGenerationStreamToken {
/** Token ID from the model tokenizer */
Expand Down Expand Up @@ -85,7 +86,7 @@ export interface TextGenerationStreamOutput {
* Use to continue text from a prompt. Same as `textGeneration` but returns generator that can be read one token at a time
*/
export async function* textGenerationStream(
args: TextGenerationArgs,
args: BaseArgs & TextGenerationInput,
options?: Options
): AsyncGenerator<TextGenerationStreamOutput> {
yield* streamingRequest<TextGenerationStreamOutput>(args, {
Copy link
Collaborator

@mishig25 mishig25 Feb 21, 2024

Choose a reason for hiding this comment

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

should TextGenerationStreamOutput live in @huggingface/tasks/src/tasks/text-generation/inference just like TextGenerationInput & TextGenerationOutput ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good question - cc @Wauplin with whom we discussed that previously

The current philosophy is to not type the streaming mode because it's transfer-specific, not inference-specific

Copy link
Contributor

Choose a reason for hiding this comment

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

Indeed, we mentioned it here once: #468 (comment).

As a matter of fact, I came to the conclusion today that we should specify the stream parameter and the streamed output in our JS specs. I am currently starting to use the generated types in Python (see ongoing PR) and for now I've kept text_generation apart since I'm missing TextGenerationStreamResponse (defined here but I don't want to mix the auto-generated types with previous definitions). Agree it's more "transfer-specific" rather than "inference-specific" but the thing is that setting stream=True is modifying the output format so we need to document that somewhere.

Expand Down
58 changes: 58 additions & 0 deletions packages/tasks/src/tasks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,64 @@ import zeroShotClassification from "./zero-shot-classification/data";
import zeroShotImageClassification from "./zero-shot-image-classification/data";
import zeroShotObjectDetection from "./zero-shot-object-detection/data";

export type * from "./audio-classification/inference";
export type * from "./automatic-speech-recognition/inference";
export type * from "./document-question-answering/inference";
export type * from "./feature-extraction/inference";
export type * from "./fill-mask/inference";
export type {
ImageClassificationInput,
ImageClassificationOutput,
ImageClassificationOutputElement,
ImageClassificationParameters,
} from "./image-classification/inference";
export type * from "./image-to-image/inference";
export type { ImageToTextInput, ImageToTextOutput, ImageToTextParameters } from "./image-to-text/inference";
export type * from "./image-segmentation/inference";
export type * from "./object-detection/inference";
export type * from "./depth-estimation/inference";
export type * from "./question-answering/inference";
export type * from "./sentence-similarity/inference";
export type * from "./summarization/inference";
export type * from "./table-question-answering/inference";
export type { TextToImageInput, TextToImageOutput, TextToImageParameters } from "./text-to-image/inference";
export type { TextToAudioParameters, TextToSpeechInput, TextToSpeechOutput } from "./text-to-speech/inference";
export type * from "./token-classification/inference";
export type {
Text2TextGenerationParameters,
Text2TextGenerationTruncationStrategy,
TranslationInput,
TranslationOutput,
} from "./translation/inference";
export type {
ClassificationOutputTransform,
TextClassificationInput,
TextClassificationOutput,
TextClassificationOutputElement,
TextClassificationParameters,
} from "./text-classification/inference";
export type {
FinishReason,
PrefillToken,
TextGenerationInput,
TextGenerationOutput,
TextGenerationOutputDetails,
TextGenerationParameters,
TextGenerationSequenceDetails,
Token,
} from "./text-generation/inference";
export type * from "./video-classification/inference";
export type * from "./visual-question-answering/inference";
export type * from "./zero-shot-classification/inference";
export type * from "./zero-shot-image-classification/inference";
export type {
BoundingBox,
ZeroShotObjectDetectionInput,
ZeroShotObjectDetectionInputData,
ZeroShotObjectDetectionOutput,
ZeroShotObjectDetectionOutputElement,
} from "./zero-shot-object-detection/inference";

import type { ModelLibraryKey } from "../model-libraries";

/**
Expand Down
5 changes: 3 additions & 2 deletions packages/widgets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
],
"dependencies": {
"@huggingface/tasks": "workspace:^",
"@huggingface/jinja": "workspace:^"
"@huggingface/jinja": "workspace:^",
"@huggingface/inference": "workspace:^"
},
"peerDependencies": {
"svelte": "^3.59.2"
Expand All @@ -69,7 +70,7 @@
"svelte": "^3.59.2",
"svelte-check": "^3.6.0",
"svelte-preprocess": "^5.1.1",
"tailwindcss": "^3.3.5",
"tailwindcss": "^3.4.1",
"tslib": "^2.4.1",
"vite": "^4.5.0",
"vite-plugin-dts": "^3.6.4"
Expand Down
Loading
Loading