UNPKG

@sconedev/ai_toolkit

Version:

Simplify AI integration in web apps with local and offline model support

34 lines (33 loc) 1.38 kB
import { isBrowser } from '../utils/browser-check'; import { runOnnxInference } from '../core/onnxRuntime'; import { runOnnxInferenceBrowser } from '../core/onnxRuntime.browser'; import { predefinedModels, getModelConfig } from '../models/onnxModels'; export class OnnxProvider { constructor(apiKey, model) { this.apiKey = apiKey; // May not be needed for local models this.defaultModel = model || 'text-classification'; } async chat(messages, options) { // This is a simplified example. In a real implementation, you would: // 1. Convert messages to tokens using a tokenizer // 2. Pass those tokens to your ONNX model // 3. Convert the output back to text throw new Error('ONNX provider does not support chat functionality yet'); } async generateImage(description, options) { throw new Error('ONNX provider does not support image generation functionality yet'); } // Add ONNX-specific methods async runModel(modelName, inputs, options) { const modelConfig = getModelConfig(modelName); if (isBrowser()) { return runOnnxInferenceBrowser(modelConfig, inputs, options); } else { return runOnnxInference(modelConfig, inputs, options); } } listAvailableModels() { return Object.keys(predefinedModels); } }