Skip to content

Commit

Permalink
✨ [Widgets] Enable streaming in the conversational widget (#486)
Browse files Browse the repository at this point in the history
Linked to #360 #410 

Should unlock the conversational widget on Mistral if I'm not mistaken?

# TL;DR

- Leverage inference types from `@huggingface/task` to type input and
output of the inference client
- Use the inference client to call the inference serverless API
- Use the streaming API when supported for the model

---------

Co-authored-by: Mishig <[email protected]>
Co-authored-by: Victor Mustar <[email protected]>
  • Loading branch information
3 people authored Feb 22, 2024
1 parent bab7c35 commit bea807a
Show file tree
Hide file tree
Showing 12 changed files with 255 additions and 159 deletions.
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, {
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

0 comments on commit bea807a

Please sign in to comment.