Skip to content

A React Native application that enables on-device Large Language Model (LLM) chat functionality with intelligent context management. Built with TypeScript and modern React Native practices.

Notifications You must be signed in to change notification settings

Arunkumarvallal/ReactNativeLLM

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

ReactNativeLLM

A React Native application that enables on-device Large Language Model (LLM) chat functionality with intelligent context management. Built with TypeScript and modern React Native practices.

πŸš€ Features

Core Functionality

  • On-Device LLM Execution: Run AI models locally without internet dependency
  • Model Management: Download, select, and manage multiple AI models
  • Interactive Chat Interface: Seamless messaging experience with Gifted Chat
  • Context-Aware Conversations: Intelligent context injection from markdown files
  • Theme Support: Dynamic light/dark theme with system preference detection
  • Network Status: Real-time network connectivity monitoring

Context Management System (MCP)

  • Smart Context Processing: Automatically processes context.md files into searchable chunks
  • Relevance Scoring: Intelligent keyword matching for context retrieval
  • Manual Context Control: Toggle context on/off per conversation
  • Context File Management: Create, refresh, and manage context files
  • Memory Persistence: Chat history and context state persistence across sessions

πŸ—οΈ Architecture

Key Components

  • Model Selection Screen: Download and select AI models
  • Chat Screen: Conversation interface with context controls
  • Context Manager: Processes and manages context from markdown files
  • Theme System: Centralized theme management with override capabilities
  • Chat State Management: Persistent chat history per model

Data Flow

graph TD
    A[App Launch] --> B[Model Selection Screen]
    B --> C{Model Downloaded?}
    C -->|No| D[Download Model]
    C -->|Yes| E[Select Model]
    D --> F[Model Ready]
    E --> F
    F --> G[Navigate to Chat]
    G --> H[Initialize Context Manager]
    H --> I{Context File Exists?}
    I -->|Yes| J[Load & Process Context]
    I -->|No| K[No Context Available]
    J --> L[Chat Ready]
    K --> L
    L --> M[User Sends Message]
    M --> N{Context Enabled?}
    N -->|Yes| O[Find Relevant Context]
    N -->|No| P[Send Message Directly]
    O --> Q[Enhance Message with Context]
    Q --> R[Send to LLM]
    P --> R
    R --> S[AI Response]
    S --> T[Display Response]
    T --> U[Save Chat State]
Loading

πŸ“± Installation & Setup

Prerequisites

  • Node.js (v16 or higher)
  • React Native development environment
  • iOS Simulator (for iOS development)
  • Android Studio & emulator (for Android development)

Installation

  1. Clone the repository

    git clone <repository-url>
    cd ReactNativeLLM
  2. Install dependencies

    npm install
    # or
    yarn install
  3. iOS Setup (macOS only)

    cd ios && pod install && cd ..

πŸƒβ€β™‚οΈ Running the Application

Start Metro Bundler

npm start
# or
yarn start

Run on iOS

npm run ios
# or
yarn ios

# For specific simulator
npx react-native run-ios --simulator="iPhone 15 Pro"

Run on Android

npm run android
# or
yarn android

# For specific device
npx react-native run-android --deviceId=<device-id>

Additional Commands

# Clean build (if experiencing issues)
npm run clean
# or
yarn clean

# Run tests
npm test
# or
yarn test

# TypeScript type checking
npx tsc --noEmit

# Lint code
npx eslint . --ext .js,.jsx,.ts,.tsx

πŸ“‹ Usage Guide

1. Model Management

  • Launch the app to see the Model Selection screen
  • Download required AI models using the download button
  • Select a downloaded model to proceed to chat
  • Network connectivity required for initial model download

2. Chat Interface

  • Context Toggle: Enable/disable context injection for conversations
  • Theme Toggle: Switch between light and dark modes
  • Context Refresh: Manually refresh context from file system
  • Back Navigation: Return to model selection

3. Context System

  • Create a context.md file in the app's document directory
  • Use the context toggle button (long press) to create a test context file
  • Context is automatically processed into searchable chunks
  • Relevant context is injected into conversations when enabled

4. Context File Format

Create a context.md file with the sections.

πŸ“ Project Structure

src/
β”œβ”€β”€ components/           # Reusable UI components
β”‚   β”œβ”€β”€ ChatHeader.tsx           # Chat screen header with controls
β”‚   β”œβ”€β”€ ContextToggleButton.tsx  # Context enable/disable button
β”‚   β”œβ”€β”€ CustomGiftedChat.tsx     # Customized chat interface
β”‚   β”œβ”€β”€ ModelSelection.tsx       # Model list and download UI
β”‚   └── ThemeToggleButton.tsx    # Theme switching component
β”œβ”€β”€ hooks/               # Custom React hooks
β”‚   β”œβ”€β”€ useChatMessages.ts       # Chat message management
β”‚   β”œβ”€β”€ useContextManager.ts     # Context system integration
β”‚   β”œβ”€β”€ useModelDownload.ts      # Model download management
β”‚   └── useModelPreparation.ts   # Model loading and preparation
β”œβ”€β”€ screens/             # Main application screens
β”‚   β”œβ”€β”€ ChatScreen.tsx           # Chat interface screen
β”‚   └── ModelSelectionScreen.tsx # Model management screen
β”œβ”€β”€ services/            # Business logic and external services
β”‚   β”œβ”€β”€ ContextFileManager.ts    # File system operations
β”‚   β”œβ”€β”€ ContextProcessor.ts      # Context processing logic
β”‚   β”œβ”€β”€ MCPContextManager.ts     # Main context orchestration
β”‚   └── TestContextHelper.ts     # Development utilities
β”œβ”€β”€ theme/               # Theme management
β”‚   β”œβ”€β”€ ThemeContext.tsx         # Theme context provider
β”‚   └── theme.ts                 # Theme definitions
β”œβ”€β”€ types/               # TypeScript type definitions
└── utils/               # Utility functions and constants

πŸ”§ Key Technologies

  • React Native: Cross-platform mobile development
  • TypeScript: Type-safe development
  • react-native-ai: Local LLM execution
  • React Navigation: Navigation management
  • Gifted Chat: Chat UI components
  • Phosphor React Native: Icon library
  • React Native FS: File system operations

πŸ› Troubleshooting

Common Issues

  1. Metro bundler issues

    npx react-native start --reset-cache
  2. iOS build issues

    cd ios && pod install && cd ..
    npx react-native run-ios --clean
  3. Android build issues

    cd android && ./gradlew clean && cd ..
    npx react-native run-android
  4. Context not loading

    • Check if context.md exists in document directory
    • Use the refresh button to manually reload context
    • Verify file permissions and content format

πŸ“ Development Notes

  • Models are stored locally using react-native-ai package
  • Chat state is persisted in memory per model session
  • Context files are monitored for changes (manual refresh required)
  • Theme preference follows system settings by default
  • Network status affects model download capability

🚧 Future Enhancements

  • Model deletion functionality
  • Context file editing within app
  • Conversation export/import
  • Advanced context search and filtering
  • Multi-language support
  • Voice input/output integration

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

About

A React Native application that enables on-device Large Language Model (LLM) chat functionality with intelligent context management. Built with TypeScript and modern React Native practices.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published