Skip to content

Repository files navigation

Hybrid AI Coding Assistant

A Flask-based coding assistant that switches between online and offline AI responses depending on internet availability. When the app detects connectivity, it uses Gemini for responses and Together AI for chat title generation. When offline, it falls back to locally served Ollama models such as deepseek-coder:6.7b and tinyllama:1.1b.

Features

  • Automatic online/offline response routing
  • Local offline inference through Ollama
  • Web chat interface with syntax-highlighted code blocks
  • Chat history storage in MongoDB when internet is available
  • Auto-generated chat titles
  • Light/dark theme toggle

Tech Stack

  • Python
  • Flask
  • Ollama
  • Gemini API
  • Together AI API
  • MongoDB Atlas
  • Vanilla HTML, CSS, and JavaScript

Project Structure

Offline-AI-main/
|- hybrid_ai_app.py           # Flask app and API routes
|- db.py                      # MongoDB connection setup
|- README_HYBRID_MODEL.md     # Local model server setup notes
|- templates/
|  \- index.html              # Main UI template
|- static/
|  |- script.js               # Frontend chat logic
|  |- style_professional.css  # App styling
|  \- bg.png                  # UI asset
\- utils/
   |- ollama_handler.py       # Online/offline model handlers
   \- db_helpers.py           # Chat document helpers

How It Works

  1. The user submits a prompt from the browser UI.
  2. The Flask app checks internet connectivity.
  3. If online, the app uses Gemini to generate the response.
  4. If offline, the app sends the prompt to a local Ollama server.
  5. If MongoDB is reachable, the conversation is stored in chat history.
  6. On the second turn, the app attempts to generate a short chat title.

Requirements

  • Python 3.10+ recommended
  • Ollama installed locally
  • A downloaded Ollama model for offline responses
  • Optional:
    • Gemini API key for online responses
    • Together AI API key for online title generation
    • MongoDB Atlas connection for chat history

Installation

  1. Clone the repository.
  2. Create and activate a virtual environment.
  3. Install the Python dependencies.

Example:

python -m venv .venv
.venv\Scripts\activate
pip install -r requirements.txt

Environment Variables

Create a .env file in the project root:

GEMINI_API_KEY=your_gemini_api_key
TOGETHER_API_KEY=your_together_api_key

Running Ollama Locally

Start Ollama before launching the Flask app:

ollama serve

Make sure the required models are available locally, for example:

ollama pull deepseek-coder:6.7b
ollama pull tinyllama:1.1b

The default Ollama endpoint expected by the app is:

http://localhost:11434/api/generate

See README_HYBRID_MODEL.md for the existing local model setup notes.

Run the App

Start the Flask server with:

python hybrid_ai_app.py

Then open:

http://127.0.0.1:5000

Main Endpoints

  • GET / renders the chat interface
  • POST /generate generates a response and optionally stores the turn
  • GET /history returns saved chats and supports ?search=keyword

Notes for Developers

Important Security Note

Sensitive credentials should not be hardcoded in source files. If you plan to share or deploy this project, move database credentials and API keys into environment variables before publishing.

Future Improvements

  • Improve chat history browsing and conversation reopening
  • Stream tokens to the UI progressively
  • Add model selection in the interface
  • Add tests and configuration management

About

Offline AI Coding Assistant is a local web-based tool that uses advanced AI models like Deepseek Coder or WizardCoder to solve programming problems without the internet. It runs entirely on your system, ensuring privacy and fast responses for coding help, debugging, and DSA solutions.

Resources

Stars

44 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages