It has been revealed that as LLMs continue to evolve, they are becoming smaller and smarter, enabling them to run directly on your phone. Take, for instance, the DeepSeek R1 Distil Qwen 2.5 with 1.5 billion parameters, this model really shows how advanced AI can now fit into the palm of your hand!
In this blog, we will guide you through creating a mobile app that allows you to chat with these powerful models locally. The complete code for this tutorial is available in our EdgeLLM repository. If you've ever felt overwhelmed by the complexity of open-source projects, fear not! Inspired by the Pocket Pal app, we will help you build a straightforward React Native application that downloads LLMs from the Hugging Face hub, ensuring everything remains private and runs on your device. We will utilize llama.rn, a binding for llama.cpp, to load GGUF files efficiently! (Related: Tech Report: AI for Game Development: Creating a Farmin...)
This tutorial is designed for anyone who:.
By the end of this guide, you will have a fully functional app that allows you to interact with your favorite models. (Related: Supercharge your OCR Pipelines with Open Models...)
Before we dive into building our app, let's talk about which models work well on mobile devices and what to consider when selecting them.
When running LLMs on mobile devices, size matters significantly:.
When downloading GGUF models, you'll encounter various quantization formats. Understanding these can help you choose the right balance between model size and performance:.
Here are some models that perform well on mobile devices:.
To find additional GGUF models on Hugging Face:.
According to recent reports, when selecting a model, consider both the parameter count and the quantization level. For example, a 7B model with Q2_K quantization might run better than a 2B model with Q8_0 quantization. So if you can fit a small model comfortably on your device try to use a bigger quantized model instead, it might have a better performance.
In a recent development, react Native is a popular framework for building mobile applications using JavaScript and React. It allows developers to create apps that run on both Android and iOS platforms while sharing a significant amount of code, which speeds up the development process and reduces maintenance efforts.
Before you can start coding with React Native, you need to set up your environment properly.
Node.js: Node.js is a JavaScript runtime that allows you to run JavaScript code. It is essential for managing packages and dependencies in your React Native project. You can install it from Node.js downloads.
react-native-community/cli: This command installs the React Native command line interface (CLI), which provides tools to create, build, and manage your React Native projects. Run the following command to install it:.
To run your app during development, you will need an emulator or a simulator:.
If you are on Windows or Linux:.
If you are curious about the difference between simulators and emulators, you can read this article: Difference between Emulator and Simulator, but to put it simply, emulators replicate both hardware and software, while simulators only replicate software.
For setting up Android Studio, follow this excellent tutorial by Expo : Android Studio Emulator Guide.
You can find the full code for this project in the EdgeLLM repo here, there are two folders:.
First, we need to initiate the app using @react-native-community/cli:.
App folders are organized as follows:.
To run the project, and see how it looks like on your own virtual device, follow these steps:.
Navigate to the Project Directory:.
Navigate to the iOS Folder and Install:.
Start the Metro Bundler: Run the following command in the project folder (EdgeLLMPlus or EdgeLLMBasic):.
Launch the App on iOS or Android Simulator: Open another terminal and run:.
This will build and launch the app on your emulator/simulator to test the project before we start coding.
According to recent reports, running a React Native application requires either an emulator/simulator or a physical device. We'll focus on using an emulator since it provides a more streamlined development experience with your code editor and debugging tools side by side.
We start by ensuring our development environment is ready, we need to be in the project folder and run the following commands:.
In a latest terminal, we will launch the app on our chosen platform:.
This should build and launch the app on your emulator/simulator.
Sources indicate that first, let's install the required packages. We aim to load models from the Hugging Face Hub and run them locally. To achieve this, we need to install :.
Let's run the app on our emulator/simulator as we showed before so we can start the development.
We will start by deleting everything from the App.tsx file, and creating an empty code structure like the following :.
Inside the return statement of the App function we define the UI rendered, and outside we define the logic, but all code will be inside the App function.
We will have a screen that looks like this:.
The text "Hello World" is not displayed properly because we are using a simple View component, we need to use a SafeAreaView component to display the text correctly, we will deal with that in the next sections.
Now let's think about what our app needs to track for now:.
Here's how we implement these states using React's useState hook (we will need to import it from react).
This will be added to the App.tsx file inside the App function but outside the return statement as it's part of the logic.
The Message type defines the structure of chat messages, specifying that each message must have a role (either 'user' or 'assistant' or 'system') and content (the actual message text).
Now that we have our basic state management set up, we need to think about how to:.
Let's tackle these one by one in the next sections...
According to recent reports, let's start by defining the model formats our app is going to support and their repositories. Of course llama.rn is a binding for llama.cpp so we need to load GGUF files. To find GGUF repositories for the models we want to support, we can use the search bar on Hugging Face and search for GGUF files for a specific model, or use the script quantize_gguf.py provided here to quantize the model ourselves and upload the files to our hub repository.
In a recent development, the HF_TO_GGUF object maps user-friendly model names to their corresponding Hugging Face repository paths. For example:.
The modelFormats array contains the list of model options that will be displayed to users in the selection screen, we chose Llama 3.2 1B Instruct, DeepSeek R1 Distill Qwen 1.5B, Qwen 2 0.5B Instruct and SmolLM2 1.7B Instruct as they are the most popular small models.
Next, let's create a way to fetch and display available GGUF model files from the hub for our selected model format.
According to recent reports, when a user selects a model format, we make an API call to Hugging Face using the repository path we mapped in our HF_TO_GGUF object. We're specifically looking for files that end with '.gguf' extension, which are our quantized model files.
Once we receive the response, we extract just the filenames of these GGUF files and store them in our availableGGUFs state using setAvailableGGUFs. This allows us to show people a list of available GGUF model variants they can download.
Note: Ensure to import axios and Alert at the top of your file if not already imported.
We need to test that the function is working correclty, let's add a button to the UI to trigger the function, instead of View we will use a SafeAreaView (more on that later) component, and we will display the available GGUF files in a ScrollView component. the onPress function is triggered when the button is pressed:.
This should look something like this :.
Note: For the whole code until now you can check the first_checkpoint branch in the EdgeLLMBasic folder here.
Industry observers note that now let's implement the model download functionality in the handleDownloadModel function which should be called when the user clicks on the download button. This will download the selected GGUF file from Hugging Face and store it in the app's Documents directory:.
Sources indicate that we could have implemented the api requests inside the handleDownloadModel function, but we will keep it in a separate file to keep the code clean and readable. handleDownloadModel calls the downloadModel function, located in src/api, which accepts three parameters: modelName, downloadUrl, and a progress callback function. This callback is triggered during the download process to update the progress. Before downloading we need to have the selectedModelFormat state set to the model format we want to download.
Sources indicate that inside the downloadModel function we use the RNFS module, part of the react-native-fs library, to access the device's file system. It allows developers to read, write, and manage files on the device's storage. In this case, the model is stored in the app's Documents folder using RNFS.DocumentDirectoryPath, ensuring that the downloaded file is accessible to the app. The progress bar is updated accordingly to reflect the current download status and the progress bar component is defined in the components folder.
Let's create src/api/model.ts and copy the code from the src/api/model.ts file in the repo. The logic should be simple to understand. The same goes for the progress bar component in the src/components folder, it's a simple colored View where the width is the progress of the download.
Now we need to test the handleDownloadModel function, let's add a button to the UI to trigger the function, and we will display the progress bar. This will be added under the ScrollView we added before.
In the UI we show a list of the supported model formats and a button to download the model, when the user chooses the model format and clicks on the button the progress bar should be displayed and the download should start. In the test we hardcoded the model to download Llama-3.2-1B-Instruct-Q2_K.gguf, so we need to select Llama-3.2-1B-Instruct as a model format for the function to work, we should have something like:.
Note: For the whole code until now you can check the second_checkpoint branch in the EdgeLLMBasic folder here.
Recent findings suggest that next, we will implement a function to load the downloaded model into a Llama context, as detailed in the llama.rn documentation available here. If a context is already present, we will release it, set the context to null, and reset the conversation to its initial state. Subsequently, we will utilize the initLlama function to load the model into a fresh context and upgrade our state with the newly initialized context.
We need to call the loadModel function when the user clicks on the download button, so we need to add it inside the handleDownloadModel function right after the download is complete if it's successful.
Note: For the whole code until now you can check the third_checkpoint branch in the EdgeLLMBasic folder here.
Industry observers note that with the model now loaded into our context, we can proceed to implement the conversation logic. We'll define a function called handleSendMessage, which will be triggered when the user submits their input. This function will enhancement the conversation state and send the updated conversation to the model via context.completion. The response from the model will then be used to further update the conversation, which means that the conversation will be updated twice in this function.
To test the handleSendMessage function we need to add an input text field and a button to the UI to trigger the function, and we will display the conversation in the ScrollView component.
Industry observers note that if everything is implemented correctly, we should be able to send messages to the model and see the conversation in the ScrollView component, it's not beautiful of course but it's a good start, we will improve the UI later. The result should look like this:.
Note: For the whole code until now you can check the fourth_checkpoint branch in the EdgeLLMBasic folder here.
Now that we have the core functionality implemented, we can focus on the UI. The UI is straightforward, consisting of a model selection screen with a list of models and a chat interface that includes a conversation history and a user input field. During the model download phase, a progress bar is displayed. We intentionally avoid adding many screens to keep the app simple and focused on its core functionality. To keep track of which part of the app is being used, we will use a an other state variable called currentPage, it will be a string that can be either modelSelection or conversation. We add it to the App.tsx file.
For the css we will use the same styles as in the EdgeLLMBasic repo, you can copy the styles from there.
We will start by working on the model selection screen in the App.tsx file, we will add a list of model formats (you need to do the necessary imports and delete the previous code in the SafeAreaView component we used for testing):.
Recent findings suggest that we use SafeAreaView to ensure that the app is displayed correctly on devices with different screen sizes and orientations as we did in the previous section, and we use ScrollView to allow the user to scroll through the model formats. We also use modelFormats.map to map over the modelFormats array and display each model format as a button with a style that changes when the model format is selected. We also use the currentPage state to display the model selection screen only when the currentPage state is set to modelSelection, this is done by using the && operator. The TouchableOpacity component is used to allow the user to select a model format by pressing on it.
Now let's define handleFormatSelection in the App.tsx file:.
According to recent reports, we store the selected model format in the state and clear the previous list of GGUF files from other selections, and then we fetch the latest list of GGUF files for the selected format. The screen should look like this on your device:.
Next, let's add the view to show the list of GGUF files already available for the selected model format, we will add it below the model format selection section.
We need to only show the list of GGUF files if the selectedModelFormat state is not null, which means a model format is selected by the user.
We need to define handleGGUFSelection in the App.tsx file as a function that will trigger an alert to confirm the download of the selected GGUF file. If the user clicks on Yes, the download will start, else the selected GGUF file will be cleared.
handleDownloadAndNavigate is a simple function that will download the selected GGUF file by calling handleDownloadModel (implemented in the previous sections) and navigate to the conversation screen after the download is complete.
Now after clicking on a GGUF file, we should have an alert to confirm or cancel the download :.
We can add a simple ActivityIndicator to the view to display a loading state when the available GGUF files are being fetched. For that we will need to import ActivityIndicator from react-native and define isFetching as a boolean state variable that will be set to true in the start of the fetchAvailableGGUFs function and false when the function is finished as you can see here in the code, and add the ActivityIndicator to the view just before the {availableGGUFs.map((file, index) => (...))} to display a loading state when the available GGUF files are being fetched.
The app should look like this for a brief moment when the GGUF files are being fetched:.
Recent findings suggest that now we should be able to see the different GGUF files available for each model format when we click on it, and we should see the alert when clicking on a GGUF confirming if we want to download the model. Next we need to add the progress bar to the model selection screen, we can do it by importing the ProgressBar component from src/components/ProgressBar.tsx in the App.tsx file as we did before, and we will add it to the view just after the {availableGGUFs.map((file, index) => (...))} to display the progress bar when the model is being downloaded.
It has been revealed that the download progress bar will now be positioned at the bottom of the model selection screen. However, this means that people may need to scroll down to view it. To address this, we will modify the display logic so that the model selection screen is only shown when the currentPage state is set to 'modelSelection' and the added condition that there is no ongoing model download.
After confirming a model download we should have a screen like this :.
Note: For the whole code until now you can check the fifth_checkpoint branch in the EdgeLLMBasic folder here.
Sources indicate that now that we have the model selection screen, we can start working on the conversation screen with the chat interface. This screen will be displayed when currentPage is set to conversation. We will add a conversation history and a user input field to the screen. The conversation history will be displayed in a scrollable view, and the user input field will be displayed at the bottom of the screen out of the scrollable view to stay visible. Each message will be displayed in a different color depending on the role of the message (user or assistant).
We need to add just under the model selection screen the view for the conversation screen:.
We use different styles for the user messages and the model messages, and we use the conversation.slice(1) to remove the first message from the conversation, which is the system message.
It has been revealed that we can now add the user input field at the bottom of the screen and the send button (they should not be inside the ScrollView). As I mentioned before, we will use the handleSendMessage function to send the user message to the model and patch the conversation state with the model response.
Industry observers note that when the user clicks on the send button, the handleSendMessage function will be called and the isGenerating state will be set to true. The send button will then be disabled and the text will change to 'Generating...'. When the model finishes generating the response, the isGenerating state will be set to false and the text will change back to 'Send'.
Note: For the whole code until now you can check the main branch in the EdgeLLMBasic folder here.
The conversation page should now look like this:.
In a recent development, congratulations you've just built the core functionality of your first AI chatbot, the code is available here ! You can now start adding more features to the app to make it more user friendly and efficient.
The app is now fully functional, you can download a model, select a GGUF file, and chat with the model, but the user experience is not the best. In the EdgeLLMPlus repo, I've added some other features, like on the fly generation, automatic scrolling, the inference speed tracking, the thought process of the model like deepseek-qwen-1.5B,... we will not go into details here as it will make the blog too long, we will go through some of the ideas and how to implement them but the whole code is available in the repo.
Industry observers note that the app generates responses incrementally, producing one token at a time rather than delivering the entire response in a single batch. This approach enhances the user experience, allowing consumers to begin reading the response as it is being formed. We achieve this by utilizing a callback function within context.completion, which is triggered after each token is generated, enabling us to revision the conversation state accordingly.
In a recent development, auto Scrolling ensures that the latest messages or tokens are always visible to the user by automatically scrolling the chat view to the bottom as upcoming content is added. To implement that we need we use a reference to the ScrollView to allow programatic control over the scroll position, and we use the scrollToEnd method to scroll to the bottom of the ScrollView when a new message is added to the conversation state. We also define an autoScrollEnabled state variable that will be set to false when the user scrolls up more than 100px from the bottom of the ScrollView.
Inference Speed Tracking is a feature that tracks the time taken to generate each token and displays under each message generated by the model. This feature is easy to implement because the CompletionResult object returned by the context.completion function contains a timings property which is a dictionary containing many metrics about the inference process. We can use the predicted_per_second metric to track the speed of the model.
The thought process is a feature that displays the thought process of the model like deepseek-qwen-1.5B. The app identifies special tokens like and to handle the model's internal reasoning or "thoughts." When a token is encountered, the app enters a "thought block" where it accumulates tokens that represent the model's reasoning. Once the closing token is detected, the accumulated thought is extracted and associated with the message, allowing owners to toggle the visibility of the model's reasoning. To implement this we need to add a thought and showThought property to the Message type. message.thought will store the reasoning of the model and message.showThought will be a boolean that will be set to true when the user clicks on the message to toggle the visibility of the thought.
Industry observers note that the app uses the react-native-markdown-display package to render markdown in the conversation. This package allows us to render code in a better format.
We added a checkDownloadedModels function to the App.tsx file that will check if the model is already downloaded on the device, if it's not we will download it, if it is we will load it into the context directly, and we added some elements in the UI to show if a model is already downloaded or not.
We added two crucial buttons in the UI, the stop button and the back button. The stop button will stop the generation of the response and the back button will navigate to the model selection screen. For that, We added a handleStopGeneration function to the App.tsx file that will stop the generation of the response by calling context.stop and set the isGenerating state to false. We also added a handleBack function to the App.tsx file that will navigate to the model selection screen by setting the currentPage state to modelSelection.
For debugging we use Chrome DevTools as in web development :.
It has been revealed that 3. Find your source files4. Set breakpoints by clicking on line numbers5. Use debugging controls (top right corner):.
This will log the output in the console of Chrome DevTools.
To enhance the user experience, we can add some features like:.
I'm sure you can think of some really cool features to add to the app, feel free to implement them and share them with the community 🤗.
I would like to thank the following people for reviewing this blog post and providing valuable feedback:.
Their expertise and suggestions helped improve the quality and accuracy of this guide.
You now have a working React Native app that can:.
It has been revealed that this implementation serves as a foundation for building more sophisticated AI-powered mobile applications. Remember to consider device capabilities when selecting models and tuning parameters.
In a recent development, would be great for you to give us a better idea of size on disk for this. Uploading to Play/IOS store considerations. Maybe integration from pytorch into tensorflow to take advantage of the M chip in Pixel to really make this stuff scream, using a tool like google-ai-edge/ai-edge-torch. Great start.
Downloading the model after installation is a great idea, as it keeps the app lightweight and gives customers the flexibility to choose the model that best suits their needs.
Hi can I convert VLMs into GGUF?
very helpfull , most of this small models are good in english only , you guys can implement a translation feature model like ML kit to translate to multiple language but i tried it the context is missing since i am translating word to word it miss context, any idea to do that ?