@sconedev/ai_toolkit
Version:
Simplify AI integration in web apps with local and offline model support
88 lines (87 loc) • 3.16 kB
TypeScript
export declare class OnnxModelError extends Error {
constructor(message: string);
}
export declare class OnnxRuntimeError extends Error {
constructor(message: string);
}
export interface OnnxModelConfig {
name: string;
modelUrl: string;
inputNames: string[];
outputNames: string[];
inputShapes?: number[][];
description?: string;
version?: string;
isLocal?: boolean;
localPath?: string;
}
export interface LocalOnnxModelConfig {
name: string;
inputNames: string[];
outputNames: string[];
inputShapes?: number[][];
description?: string;
version?: string;
}
export type ModelLoadingState = 'idle' | 'loading' | 'loaded' | 'failed';
/**
* Validates the input data against the expected input shapes and types
* @param inputData The input data to validate
* @param modelConfig The model configuration
* @throws Error if validation fails
*/
declare function validateInputData(inputData: Record<string, any>, modelConfig: OnnxModelConfig | LocalOnnxModelConfig): void;
/**
* Run inference using a local ONNX model file
*
* @param modelPath Path to the local ONNX model file or File object in browser
* @param modelConfig Configuration for the model (input/output names)
* @param inputData Input data for inference
* @param options Additional options for running the model
* @returns Result of the model inference
*/
export declare function runModelFromPath(modelPath: string | File | ArrayBuffer, modelConfig: LocalOnnxModelConfig, inputData: Record<string, any>, options?: {
cacheResults?: boolean;
cacheModel?: boolean;
progressCallback?: (progress: number) => void;
inferenceOptions?: Record<string, any>;
}): Promise<Record<string, any>>;
export declare function runOnnxInference(modelConfig: OnnxModelConfig, inputData: Record<string, any>, options?: {
cacheResults?: boolean;
cacheModel?: boolean;
progressCallback?: (progress: number) => void;
}): Promise<Record<string, any>>;
export declare function unloadOnnxModel(modelName: string): boolean;
export declare function isWasmSupported(): boolean;
declare global {
interface Navigator {
gpu?: any;
}
}
export declare function isWebGPUSupported(): boolean;
/**
* Download an ONNX model from a URL and save it for offline use
*
* @param modelUrl URL to download the model from
* @param options Download options
* @returns Path to the downloaded model or the model buffer (browser)
*/
export declare function downloadModelForOfflineUse(modelUrl: string, options?: {
modelName?: string;
progressCallback?: (progress: number) => void;
saveToIndexedDB?: boolean;
saveToCache?: boolean;
}): Promise<string | ArrayBuffer>;
/**
* Load a saved ONNX model from IndexedDB (browser only)
*/
export declare function loadModelFromIndexedDB(modelName: string): Promise<ArrayBuffer | null>;
/**
* Check if a model exists in the cache
*/
export declare function isModelInCache(modelName: string): boolean;
/**
* Get available execution providers for the current environment
*/
export declare function getAvailableExecutionProviders(): string[];
export { validateInputData };