UNPKG

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

Version:

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

54 lines 1.78 kB
import { useCallback, useRef } from 'react'; import { NativeModules, Platform } from 'react-native'; const LINKING_ERROR = `The package 'react-native-mediapipe-llm' doesn't seem to be linked. Make sure: \n\n` + 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 = NativeModules.MediapipeLlm ? NativeModules.MediapipeLlm : new Proxy({}, { get() { throw new Error(LINKING_ERROR); } }); export function useLlmInference() { const isInitializedRef = useRef(false); const initialize = useCallback(async opts => { try { const success = await MediapipeLlm.initialize(opts); isInitializedRef.current = success; return success; } catch (error) { isInitializedRef.current = false; return false; } }, []); const generateResponse = 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 }; } export default useLlmInference; //# sourceMappingURL=index.js.map