openvino-genai-node
Version:
OpenVINO™ GenAI pipelines for using from Node.js environment
78 lines (77 loc) • 3.87 kB
TypeScript
import { WhisperPipeline as WhisperPipelineWrapper } from "../addon.js";
import { WhisperDecodedResults } from "../decodedResults.js";
import { WhisperGenerationConfig, WhisperPipelineProperties, StreamingStatus } from "../utils.js";
import { Tokenizer } from "../tokenizer.js";
export type RawSpeechInput = Float32Array | number[];
/**
* Options for Whisper generation methods.
*/
export type WhisperGenerateOptions = {
/** Generation configuration (e.g. language, task, return_timestamps). */
generationConfig?: WhisperGenerationConfig;
/** Callback invoked with each decoded text chunk; return StreamingStatus to control generation. */
streamer?: (chunk: string) => StreamingStatus;
};
/**
* Pipeline for automatic speech recognition using Whisper models.
*
* Expects raw audio normalized to approximately [-1, 1] at 16 kHz sample rate.
* Use a WAV file or decode audio to Float32Array before calling generate().
*/
export declare class WhisperPipeline {
protected readonly modelPath: string;
protected readonly device: string;
protected pipeline: WhisperPipelineWrapper | null;
protected readonly properties: WhisperPipelineProperties;
/**
* Construct a Whisper pipeline from a folder containing model IRs and tokenizer.
* @param modelPath - Path to the folder with model IRs and tokenizer (e.g. openvino_encoder_model.xml, preprocessor_config.json).
* @param device - Inference device (e.g. "CPU", "GPU").
* @param properties - Device and pipeline properties (e.g. word_timestamps: true, CACHE_DIR: "cache").
*/
constructor(modelPath: string, device: string, properties?: WhisperPipelineProperties);
/**
* Load the pipeline. Must be called once before generate().
*/
init(): Promise<void>;
/**
* Stream speech recognition results as an async iterator.
* The iterator yields decoded text chunks during generation.
* When generation finishes, the full decoded text is returned as the final
* iterator value (`done: true`). This value is not available through
* `for await...of`; call `next()` directly to read it.
*
* For custom streaming control, use {@link generate} with a streamer callback instead.
*
* @param rawSpeech - Audio samples as Float32Array or number[], normalized to ~[-1, 1], 16 kHz.
* @param options - Optional generation config (e.g. language, task, return_timestamps).
* @returns Async iterator that yields decoded text chunks as strings.
*/
stream(rawSpeech: RawSpeechInput, options?: WhisperGenerateOptions): AsyncIterableIterator<string>;
/**
* Run speech recognition with optional streaming.
*
* For simple streaming use cases, consider using {@link stream}, which provides
* a convenient async iterator interface.
*
* @param rawSpeech - Audio samples as Float32Array or number[], normalized to ~[-1, 1], 16 kHz.
* @param options - Optional parameters.
* @param options.generationConfig - Generation config (e.g., language, task, return_timestamps).
* @param options.streamer - Optional callback invoked for each decoded chunk.
* - Return a `StreamingStatus` flag to indicate whether generation should be stopped or cancelled
* @returns Decoded texts, scores, optional chunks with timestamps, and perf metrics.
*/
generate(rawSpeech: RawSpeechInput, options?: WhisperGenerateOptions): Promise<WhisperDecodedResults>;
/**
* Get the pipeline tokenizer.
*/
getTokenizer(): Tokenizer;
/**
* Get current generation config (language, task, return_timestamps, etc.).
*/
getGenerationConfig(): Partial<WhisperGenerationConfig>;
/**
* Update generation config (e.g. language, task, return_timestamps).
*/
setGenerationConfig(config: WhisperGenerationConfig): void;
}