openvino-genai-node
Version:
OpenVINO™ GenAI pipelines for using from Node.js environment
165 lines (164 loc) • 9.87 kB
TypeScript
import { Tensor } from "openvino-node";
import type { ChatHistory as IChatHistory } from "./chatHistory.js";
import type { Tokenizer as ITokenizer } from "./tokenizer.js";
import { IReasoningParser, IDeepSeekR1ReasoningParser, IPhi4ReasoningParser, ILlama3PythonicToolParser, ILlama3JsonToolParser } from "./parsers.js";
import { GenerationConfig, GenerationFinishReason, StreamingStatus, VLMPipelineProperties, LLMPipelineProperties, WhisperGenerationConfig, WhisperPipelineProperties, SpeechGenerationConfig, ImageGenerationConfig, ImageGenerationCallback, Text2ImagePipelineProperties, Image2ImagePipelineProperties, InpaintingPipelineProperties, Text2SpeechPipelineProperties } from "./utils.js";
import { VLMPerfMetrics, PerfMetrics, WhisperPerfMetrics, ImageGenerationPerfMetrics, Text2SpeechPerfMetrics } from "./perfMetrics.js";
import type { WhisperDecodedResultChunk, WhisperWordTiming } from "./decodedResults.js";
export type EmbeddingResult = Float32Array | Int8Array | Uint8Array;
export type EmbeddingResults = Float32Array[] | Int8Array[] | Uint8Array[];
export type TextRerankResult = [index: number, score: number];
export type TextRerankResults = TextRerankResult[];
/**
* Pooling strategy
*/
export declare enum PoolingType {
/** First token embeddings */
CLS = 0,
/** The average of all token embeddings */
MEAN = 1
}
export type TextEmbeddingConfig = {
/** Maximum length of tokens passed to the embedding model */
max_length?: number;
/** If 'true', model input tensors are padded to the maximum length */
pad_to_max_length?: boolean;
/** Side to use for padding "left" or "right" */
padding_side?: "left" | "right";
/**
* Batch size of embedding model.
* Useful for database population. If set, the pipeline will fix model shape for inference optimization.
* Number of documents passed to pipeline should be equal to batch_size.
* For query embeddings, batch_size should be set to 1 or not set.
*/
batch_size?: number;
/** Pooling strategy applied to model output tensor */
pooling_type?: PoolingType;
/** If 'true', L2 normalization is applied to embeddings */
normalize?: boolean;
/** Instruction to use for embedding a query */
query_instruction?: string;
/** Instruction to use for embedding a document */
embed_instruction?: string;
};
export interface TextEmbeddingPipelineWrapper {
new (): TextEmbeddingPipelineWrapper;
init(modelPath: string, device: string, config: TextEmbeddingConfig, ovProperties: object, callback: (err: Error | null) => void): void;
embedQuery(text: string, callback: (err: Error | null, value: EmbeddingResult) => void): void;
embedDocuments(documents: string[], callback: (err: Error | null, value: EmbeddingResults) => void): void;
embedQuerySync(text: string): EmbeddingResult;
embedDocumentsSync(documents: string[]): EmbeddingResults;
}
/**
* Configuration parameters for TextRerankPipeline.
*/
export type TextRerankPipelineConfig = {
/**
* Number of documents to return sorted by score.
* @defaultValue 3
*/
top_n?: number;
/** Maximum length of tokens passed to the embedding model. */
max_length?: number;
/** If 'true', model input tensors are padded to the maximum length. */
pad_to_max_length?: boolean;
/** Side to use for padding "left" or "right". */
padding_side?: "left" | "right";
};
export interface TextRerankPipeline {
new (): TextRerankPipeline;
init(modelPath: string, device: string, config: TextRerankPipelineConfig, ovProperties: object, callback: (err: Error | null) => void): void;
rerank(query: string, documents: string[], callback: (err: Error | null, value: TextRerankResults) => void): void;
}
export interface LLMPipeline {
new (): LLMPipeline;
init(modelPath: string, device: string, ovProperties: LLMPipelineProperties, callback: (err: Error | null) => void): void;
generate(inputs: string | string[] | IChatHistory, generationConfig: GenerationConfig, streamer: ((chunk: string) => StreamingStatus) | undefined, callback: (err: Error | null, result: {
texts: string[];
scores: number[];
perfMetrics: PerfMetrics;
parsed: Record<string, unknown>[];
finishReasons: GenerationFinishReason[];
}) => void): void;
startChat(systemMessage: string, callback: (err: Error | null) => void): void;
finishChat(callback: (err: Error | null) => void): void;
getTokenizer(): ITokenizer;
getGenerationConfig(): GenerationConfig;
setGenerationConfig(config: GenerationConfig): void;
}
export interface WhisperPipeline {
new (): WhisperPipeline;
init(modelPath: string, device: string, properties: WhisperPipelineProperties, callback: (err: Error | null) => void): void;
generate(rawSpeech: Float32Array | number[], generationConfig: WhisperGenerationConfig, streamer: ((chunk: string) => StreamingStatus) | undefined, callback: (err: Error | null, result: {
texts: string[];
scores: number[];
perfMetrics: WhisperPerfMetrics;
chunks?: WhisperDecodedResultChunk[];
words?: WhisperWordTiming[];
}) => void): void;
getTokenizer(): ITokenizer;
getGenerationConfig(): Partial<WhisperGenerationConfig>;
setGenerationConfig(config: WhisperGenerationConfig): void;
}
export interface VLMPipeline {
new (): VLMPipeline;
init(modelPath: string, device: string, ovProperties: VLMPipelineProperties, callback: (err: Error | null) => void): void;
generate(inputs: string | IChatHistory, images: Tensor[] | undefined, videos: Tensor[] | undefined, streamer: ((chunk: string) => StreamingStatus) | undefined, generationConfig: GenerationConfig | undefined, callback: (err: Error | null, result: {
texts: string[];
scores: number[];
perfMetrics: VLMPerfMetrics;
parsed: Record<string, unknown>[];
finishReasons: GenerationFinishReason[];
}) => void): void;
startChat(systemMessage: string, callback: (err: Error | null) => void): void;
finishChat(callback: (err: Error | null) => void): void;
getTokenizer(): ITokenizer;
setChatTemplate(template: string): void;
setGenerationConfig(config: GenerationConfig): void;
getGenerationConfig(): GenerationConfig;
}
export interface Text2ImagePipeline {
new (): Text2ImagePipeline;
init(modelPath: string, device: string, properties: Text2ImagePipelineProperties, callback: (err: Error | null) => void): void;
generate(prompt: string, properties: ImageGenerationConfig, streamer: ImageGenerationCallback | undefined, callback: (err: Error | null, result: Tensor) => void): void;
decode(latent: Tensor, callback: (err: Error | null, result: Tensor) => void): void;
getPerformanceMetrics(): ImageGenerationPerfMetrics;
getGenerationConfig(): ImageGenerationConfig;
setGenerationConfig(config: ImageGenerationConfig): void;
}
export interface Image2ImagePipeline {
new (): Image2ImagePipeline;
init(modelPath: string, device: string, properties: Image2ImagePipelineProperties, callback: (err: Error | null) => void): void;
generate(prompt: string, image: Tensor, properties: ImageGenerationConfig, streamer: ImageGenerationCallback | undefined, callback: (err: Error | null, result: Tensor) => void): void;
decode(latent: Tensor, callback: (err: Error | null, result: Tensor) => void): void;
getPerformanceMetrics(): ImageGenerationPerfMetrics;
getGenerationConfig(): ImageGenerationConfig;
setGenerationConfig(config: ImageGenerationConfig): void;
}
export interface InpaintingPipeline {
new (): InpaintingPipeline;
init(modelPath: string, device: string, properties: InpaintingPipelineProperties, callback: (err: Error | null) => void): void;
generate(prompt: string, image: Tensor, mask: Tensor, properties: ImageGenerationConfig, streamer: ImageGenerationCallback | undefined, callback: (err: Error | null, result: Tensor) => void): void;
decode(latent: Tensor, callback: (err: Error | null, result: Tensor) => void): void;
getPerformanceMetrics(): ImageGenerationPerfMetrics;
getGenerationConfig(): ImageGenerationConfig;
setGenerationConfig(config: ImageGenerationConfig): void;
}
export interface Text2SpeechPipeline {
new (): Text2SpeechPipeline;
init(modelPath: string, device: string, properties: Text2SpeechPipelineProperties, callback: (err: Error | null) => void): void;
generate(inputs: string | string[], speakerEmbedding: Tensor | undefined, properties: SpeechGenerationConfig, callback: (err: Error | null, result: {
speeches: Tensor[];
perfMetrics: Text2SpeechPerfMetrics;
}) => void): void;
getGenerationConfig(): SpeechGenerationConfig;
setGenerationConfig(config: SpeechGenerationConfig): void;
}
export declare const TextEmbeddingPipeline: TextEmbeddingPipelineWrapper, TextRerankPipeline: TextRerankPipeline, LLMPipeline: LLMPipeline, VLMPipeline: VLMPipeline, WhisperPipeline: WhisperPipeline, Text2ImagePipeline: Text2ImagePipeline, Image2ImagePipeline: Image2ImagePipeline, InpaintingPipeline: InpaintingPipeline, Text2SpeechPipeline: Text2SpeechPipeline, ChatHistory: IChatHistory, Tokenizer: ITokenizer, ReasoningParser: IReasoningParser, DeepSeekR1ReasoningParser: IDeepSeekR1ReasoningParser, Phi4ReasoningParser: IPhi4ReasoningParser, Llama3PythonicToolParser: ILlama3PythonicToolParser, Llama3JsonToolParser: ILlama3JsonToolParser;
export type ChatHistory = IChatHistory;
export type Tokenizer = ITokenizer;
export type ReasoningParser = IReasoningParser;
export type DeepSeekR1ReasoningParser = IDeepSeekR1ReasoningParser;
export type Phi4ReasoningParser = IPhi4ReasoningParser;
export type Llama3PythonicToolParser = ILlama3PythonicToolParser;
export type Llama3JsonToolParser = ILlama3JsonToolParser;