UNPKG

@subhajit-gorai/react-native-mediapipe-llm

Version:

React Native binding for Google AI Edge Gallery's MediaPipe on-device LLM inference engine

61 lines (60 loc) 1.99 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; exports.useLlmInference = useLlmInference; var _react = require("react"); var _reactNative = require("react-native"); const LINKING_ERROR = `The package 'react-native-mediapipe-llm' doesn't seem to be linked. Make sure: \n\n` + _reactNative.Platform.select({ ios: "- You have run 'cd ios && pod install'\n", default: '' }) + '- You rebuilt the app after installing the package\n' + '- You are not using Expo Go\n'; const MediapipeLlm = _reactNative.NativeModules.MediapipeLlm ? _reactNative.NativeModules.MediapipeLlm : new Proxy({}, { get() { throw new Error(LINKING_ERROR); } }); function useLlmInference() { const isInitializedRef = (0, _react.useRef)(false); const initialize = (0, _react.useCallback)(async opts => { try { const success = await MediapipeLlm.initialize(opts); isInitializedRef.current = success; return success; } catch (error) { isInitializedRef.current = false; return false; } }, []); const generateResponse = (0, _react.useCallback)(async (prompt, partialCallback) => { if (!isInitializedRef.current) { throw new Error('LLM not initialized. Call initialize() first.'); } if (partialCallback) { return new Promise((resolve, reject) => { let fullResponse = ''; MediapipeLlm.generateResponseWithCallback(prompt, (partial, done) => { if (partial) { fullResponse += partial; partialCallback(partial); } if (done) { resolve(fullResponse); } }, error => { reject(new Error(error)); }); }); } else { return MediapipeLlm.generateResponse(prompt); } }, []); return { generateResponse, isInitialized: isInitializedRef.current, initialize }; } var _default = exports.default = useLlmInference; //# sourceMappingURL=index.js.map