Skip to main content

An advanced terminal-based chat application built with Python and Textual.

Project description

OptiChat

OptiChat is an advanced terminal-based chat application built with Python and Textual. It features a robust multi-tier memory system, personalized memory tracking, dynamic model connectivity (including cloud and local Ollama models), and a sophisticated prompt construction pipeline for high-quality, contextual AI responses.

🌟 Key Features

  • Terminal-based UI: A beautiful, responsive interface built with Textual, featuring tabs, chat session sidebars, and customizable themes.
  • Multi-Tier Memory System:
    • Short-Term Memory: Token-budgeted rolling window for recent context.
    • LRU Memory: Background-processed cache of frequently used messages.
    • Long-Term Memory: Persistent vector store (ChromaDB) for semantic search across conversations.
    • Personalized Memory: Automatically learns and updates user preferences, interests, and interaction styles with conflict resolution.
  • Dynamic Model Connectivity: Support for OpenAI, Anthropic, Gemini, and local models via Ollama.
  • Prompt Construction Pipeline: Utilizes LangGraph to dynamically classify queries, retrieve memory, apply personalization, and enforce structured output schemas.
  • Chat Trace Logs: Every assistant response includes a collapsible section showing the model's chain-of-thought ToDo plan – what the model thought before responding.
  • Adaptive Response: Response length and depth dynamically adapt to question complexity (simple → concise, complex → thorough and comprehensive).
  • Auto Chat Naming: New chats are automatically renamed based on your first question (2-3 word title) via a background thread.
  • Secure Local Storage: All data, including settings, API keys (via .env), SQLite databases for chats, and ChromaDB vectors, are stored securely in your local ~/.optichat/ directory.

🏗️ Architecture

Storage

OptiChat stores its data locally in ~/.optichat/. This includes:

  • config.json for global settings.
  • optichat.db (SQLite) for storing chats, messages, and session metadata.
  • chroma/ for ChromaDB vector embeddings.
  • Flat files for chat-specific short-term and LRU caches.

Memory Pipeline

  1. Short-term: Retains the most recent 3-5 messages.
  2. LRU Cache: Frequently accessed context swapped in from long-term memory.
  3. Long-term: Chunks and embeds responses into ChromaDB for semantic retrieval.
  4. Personalized: Analyzes user behavior and explicitly stated preferences to tailor AI responses.

Prompt Construction

Using LangChain and LangGraph, the pipeline:

  1. Classifies the user input (type, complexity).
  2. Retrieves relevant context (Short-term, LRU, or Long-term via semantic search).
  3. Scores and orders the context.
  4. Injects personalized memory (tone, length, interests).
  5. Selects an appropriate output schema (e.g., factual, procedural, coding).
  6. Instructs the model to produce a chain-of-thought ToDo plan (<TRACE>…</TRACE>) before answering.
  7. Applies adaptive response instructions based on detected question complexity.
  8. Streams the final response and parses the trace log for display.

Chat Trace Logs

Every assistant response includes a collapsible Chat Trace Logs widget at the bottom of the message bubble. This displays the numbered ToDo plan (chain-of-thought) that the model produced before generating its answer. Click to expand and inspect the model's reasoning process — useful for debugging, understanding responses, and evaluating quality.

Adaptive Response

Response length automatically adapts to question complexity:

Complexity Behaviour
Simple Concise, focused answer — a few sentences.
Moderate Well-structured with paragraphs, lists, and examples.
Complex Comprehensive and thorough — covers all aspects, edge cases, and examples.

Complexity is auto-detected from signal words (e.g., "briefly" → simple, "in detail" → complex).

Auto Chat Naming

New chats start with a generic "Chat N" name. After the first AI response, a background thread automatically renames the chat based on your first question, producing a short 2-3 word title.

🛠️ Setup & Installation

  1. Clone the repository:

    git clone <repository_url>
    cd OptiChat
    
  2. Create a virtual environment (optional but recommended):

    python -m venv .venv
    source .venv/bin/activate  # On Windows: .venv\Scripts\activate
    
  3. Install dependencies:

    pip install -r requirements.txt
    
  4. Run OptiChat:

    python main.py # runs in terminal 
    textual run --dev main.py # runs in textual UI (Slower startup)
    

    Note: OptiChat will automatically create the ~/.optichat/ directory and necessary files upon first launch.

  5. Configure AI Models:

    • Launch the application and navigate to the Settings tab.
    • Enter your API keys for Cloud Providers (OpenAI, Anthropic, Gemini).
    • Alternatively, ensure Ollama is running locally to auto-detect and use local models.
    • DISCLAIMER: API models consume a lot of tokens for chats as multiple calls are used for a single response, use local models for longer conversations

⌨️ Keyboard Shortcuts

Shortcut Action
Ctrl+Q Quit OptiChat and close the layout
Ctrl+R Toggle streaming on/off
Ctrl+C Cancel current streaming response mid-output
↑ / ↓ Scroll through input history (previous commands/messages)
Page Up / Page Down Scroll the main panel content

🚀 Development Roadmap

OptiChat is developed in structured phases:

  • Phase 1: UI Design via Textual - Building the responsive terminal interface, navigation, settings panels for API keys and themes, and chat windows.
  • Phase 2: Core Backend & Model Connectivity - Initializing the ~/.optichat/ environment, implementing SQLite for chat history, and connecting to Cloud/Local AI models using LangChain.
  • Phase 3: Memory Storing Mechanism - Implementing the background threads for Short-Term, LRU, and Long-Term (ChromaDB) memory handling, along with personalized memory updates.
  • Phase 4: Prompt Construction Pipeline - Orchestrating the advanced LangGraph pipeline for query classification, semantic retrieval, schema enforcement, chain-of-thought trace logs, adaptive response, auto chat naming, and intelligent prompt assembly.

Developed using Textual, LangChain, and LangGraph.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

optichat-0.1.1.tar.gz (46.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

optichat-0.1.1-py3-none-any.whl (48.7 kB view details)

Uploaded Python 3

File details

Details for the file optichat-0.1.1.tar.gz.

File metadata

  • Download URL: optichat-0.1.1.tar.gz
  • Upload date:
  • Size: 46.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for optichat-0.1.1.tar.gz
Algorithm Hash digest
SHA256 85790e8bac1487ffb542a2369cfe4820b139393e86c571af9a0d28c951ff0e92
MD5 fc09d639a6b29f1641e0efafa649c1cb
BLAKE2b-256 911b6365d72f723617eabf2982fcb76c7d564e5a7d83d913a60e64ab9aae4f39

See more details on using hashes here.

File details

Details for the file optichat-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: optichat-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 48.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for optichat-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 49ce9af72374ad084b8916381c7942a52ff1f7d9b23407fb64110004594b0062
MD5 13ee371049103cee74c9647b6ae89a7e
BLAKE2b-256 61cefc329dab9ddf07e3e54f3754db2d4c59a620353bf017e44884087a5b651c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page