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.jsonfor 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
- Short-term: Retains the most recent 3-5 messages.
- LRU Cache: Frequently accessed context swapped in from long-term memory.
- Long-term: Chunks and embeds responses into ChromaDB for semantic retrieval.
- Personalized: Analyzes user behavior and explicitly stated preferences to tailor AI responses.
Prompt Construction
Using LangChain and LangGraph, the pipeline:
- Classifies the user input (type, complexity).
- Retrieves relevant context (Short-term, LRU, or Long-term via semantic search).
- Scores and orders the context.
- Injects personalized memory (tone, length, interests).
- Selects an appropriate output schema (e.g., factual, procedural, coding).
- Instructs the model to produce a chain-of-thought ToDo plan (
<TRACE>…</TRACE>) before answering. - Applies adaptive response instructions based on detected question complexity.
- 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
-
Clone the repository:
git clone <repository_url> cd OptiChat
-
Create a virtual environment (optional but recommended):
python -m venv .venv source .venv/bin/activate # On Windows: .venv\Scripts\activate
-
Install dependencies:
pip install -r requirements.txt
-
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. -
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file optichat-0.1.0.tar.gz.
File metadata
- Download URL: optichat-0.1.0.tar.gz
- Upload date:
- Size: 46.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c1fa69004692dc097bcdf0bb45f58ca9dbac6459964d8e743e41a50d63782fc0
|
|
| MD5 |
4f5ed25df1ff4ae5c64a788dfbff840f
|
|
| BLAKE2b-256 |
8a5d57ed4444f530b2cc92c41c349d8bcdfbd715e1e9afd2e323cc67e2ceb19a
|
File details
Details for the file optichat-0.1.0-py3-none-any.whl.
File metadata
- Download URL: optichat-0.1.0-py3-none-any.whl
- Upload date:
- Size: 48.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
980662dbcf3970f56ef4d7f755de12bb45d734849b392e46f2631f94316caab0
|
|
| MD5 |
510df2ca47173c823e23cea300518d00
|
|
| BLAKE2b-256 |
4cc67f2e25be8bcb72a07a4a2b7b8a70fba98b3e3187ac1d07fb2ecc399008dc
|