@subhajit-gorai/react-native-mediapipe-llm
Version:
React Native binding for Google AI Edge Gallery's MediaPipe on-device LLM inference engine
53 lines (52 loc) • 2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.useLlmInference = void 0;
var _react = require("react");
const useLlmInference = () => {
const [isInitialized, setIsInitialized] = (0, _react.useState)(false);
const [isLoading, setIsLoading] = (0, _react.useState)(false);
const initialize = (0, _react.useCallback)(async options => {
setIsLoading(true);
try {
await new Promise(resolve => setTimeout(resolve, 2000));
setIsInitialized(true);
return true;
} catch (error) {
return false;
} finally {
setIsLoading(false);
}
}, []);
const generateResponse = (0, _react.useCallback)(async (prompt, partialCallback) => {
if (!isInitialized) {
throw new Error('LLM not initialized');
}
setIsLoading(true);
try {
const responses = [`Hello! I'm Gemma 3N, your AI assistant. You asked: "${prompt}". How can I help you today?`, `That's an interesting question about "${prompt}"! Let me think about that...`, `Based on your input "${prompt}", I would suggest the following approach:`, `I understand you're asking about "${prompt}". Here's what I know:`, `Thank you for your question about "${prompt}". Let me provide you with a detailed response:`];
const baseResponse = responses[Math.floor(Math.random() * responses.length)];
const words = baseResponse.split(' ');
let currentResponse = '';
for (let i = 0; i < words.length; i++) {
currentResponse += (i > 0 ? ' ' : '') + words[i];
if (partialCallback) {
partialCallback(words[i] + (i < words.length - 1 ? ' ' : ''));
}
await new Promise(resolve => setTimeout(resolve, 100));
}
return currentResponse;
} finally {
setIsLoading(false);
}
}, [isInitialized]);
return {
generateResponse,
initialize,
isInitialized,
isLoading
};
};
exports.useLlmInference = useLlmInference;
//# sourceMappingURL=useLlmInference.js.map