@subhajit-gorai/react-native-mediapipe-llm
Version:
React Native binding for Google AI Edge Gallery's MediaPipe on-device LLM inference engine
64 lines (55 loc) • 2.02 kB
text/typescript
import { useState, useCallback } from 'react';
import { LlmOptions, LlmInferenceHook } from '../types';
export const useLlmInference = (): LlmInferenceHook => {
const [isInitialized, setIsInitialized] = useState(false);
const [isLoading, setIsLoading] = useState(false);
const initialize = useCallback(async (options: LlmOptions): Promise<boolean> => {
setIsLoading(true);
try {
await new Promise(resolve => setTimeout(resolve, 2000));
setIsInitialized(true);
return true;
} catch (error) {
return false;
} finally {
setIsLoading(false);
}
}, []);
const generateResponse = useCallback(async (
prompt: string,
partialCallback?: (partial: string) => void
): Promise<string> => {
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,
};
};