Run powerful language models directly on mobile devices with Google's MediaPipe Inference Task API. Fast, private, and works offline.
import { useLLM } from 'expo-llm-mediapipe';
function LlmInference() {
const { generateResponse, isLoaded } = useLLM({
modelUrl: 'https://huggingface.co/google/gemma-1.1-2b...',
modelName: 'gemma-1.1-2b-it-cpu-int4.bin',
});
if (isLoaded) {
const response = await generateResponse(
'Tell me about React Native'
);
console.log(response);
}
}
Run LLM inference directly on the device without relying on external servers.
Works seamlessly on both Android and iOS devices.
Generate token-by-token responses for responsive UIs.
Download, cancel, or delete models on-device with ease.
Simplified APIs for declarative usage within your React Native application.
Advanced APIs for developers who need more control over the inference process.
expo install @expo/mediapipe-llm
import { useState } from 'react';
import { View, Text, Button } from 'react-native';
import { useLLM } from 'expo-llm-mediapipe';
export default function App() {
const [response, setResponse] = useState(null);
const [isGenerating, setIsGenerating] = useState(false);
const {
generateResponse,
isLoaded,
} = useLLM({
storageType: 'asset',
modelName: 'gemma-1.1-2b-it-cpu-int4.bin',
maxTokens: 1024,
temperature: 0.5,
topK: 1,
});
return !isLoaded && (
<Text>Loading...</Text>
);
return (
<View style={{ flex: 1, padding: 20 }}>
<Button
onPress={() => {
setIsGenerating(true);
const res = generateResponse("Tell me about React Native");
setResponse(res);
setIsGenerating(false);
}}
disabled={isGenerating}
title={isGenerating ? "Generating..." : "Generate Text"}
/>
{result && <Text>{result}</Text>}
{error && <Text style={{ color: 'red' }}>{error}</Text>}
</View>
);
}
The MediaPipe LLM Inference API lets you run large language models (LLMs) completely on-device, which you can use to perform a wide range of tasks:
This package provides a React Native wrapper around MediaPipe's LLM inference capabilities, making it easy to integrate powerful on-device language models into your mobile applications with no server dependencies.
Complete documentation of all available APIs and options
Code examples and demos to help you get started
Step-by-step guides for common use cases
List of LLM models compatible with this package