How tensorsofthewall/bettersearch works
Based on our analysis of the file structure and README content, BetterSearch is a desktop search tool that combines traditional file indexing with large language model (LLM) capabilities to transform natural language queries into search operations. In essence, it’s a hybrid application that comprises backend processing (indexing files, embedding text, and generating SQL queries using LLMs) and a simple front-end (via the Python app entry point) primarily designed for Windows at its current stage. The system uses separate components for handling file indexing, querying, and model interactions, and it supports various compute modes (CPU-only, multiple GPU modes) through different configuration files.
Main Components Identification
• Application Entry Point: Represented by app.py, which initializes and launches the desktop app.
• Core Modules Directory (bettersearch/):
• Setup and Initialization: bettersearch/__init__.py and setup.py for package and dependency setup.
• Source Code Directory (bettersearch/src):
• Database Component (bettersearch/src/database):
▪ File Indexing: file_indexer.py is responsible for scanning the file system and updating the index.
▪ Parsing and Utility Functions: parse.py and util.py handle file content extraction and processing.
▪ Embedding Model Integration: embedding_model.py uses advanced embedding models (gte-v1.5) to convert text files into vector representations.
▪ Constants: constants.py holds configuration or magic numbers used across the database operations.
• Pipeline Component (bettersearch/src/pipeline):
▪ Query Processing: pipeline.py orchestrates end-to-end query execution (from natural language query interpretation to final SQL generation).
▪ Prompts for LLMs: llama_prompt.md and sqlcoder_prompt.md define the templates for the LLM models.
▪ Pipeline Utilities: util.py supporting additional functions for pipeline operations.
• Configuration and Environment: Multiple configuration files (settings.cfg, various GPU JSON config files, bettersearch_env.yml) define deployment and runtime settings.
• Documentation: The docs/ folder with additional explanations and images.
Relationships and Interactions
• The Application Entry Point (app.py) triggers the initialization routines.
• The File Indexer from the database component continuously monitors the filesystem for changes and updates the content index. This component accesses file parsing utilities.
• The Embedding Model processes content into vector embeddings, which are then stored (possibly in an embedded database like Chroma as hinted in the README).
• The Pipeline component receives natural language queries, uses prompt templates (via llama_prompt.md and sqlcoder_prompt.md) to generate a corresponding SQL query or perform content search.
• The compute mode configurations (CPU-only and GPU variants) affect model loading details, influencing how the embedding and query generation modules interact with the hardware.
• All components communicate through clearly defined function calls within the package; directional data flow goes from user query -> pipeline processing -> file indexing/DB lookup -> LLM query generation -> final response.
Architectural Patterns and Design Principles
• Modular Design: Separation of concerns between file indexing (database), query processing (pipeline), and application startup.
• Extensibility: The configuration files and prompt templates make it easy to adapt to various compute settings and upgrade model integrations.
• Configuration-driven Behavior: Different JSON and YAML files adjust the compute mode and resource management.
Technologies and Frameworks
• Python (>=3.9) is the core language.
• LLM models such as SQLCoder and gte-v1.5 from HuggingFace are integrated.
• Dependency management via Conda (using bettersearch_env.yml).
• Libraries like LangChain, Transformers, OpenVINO, Optimum, and Chroma play key roles in model serving, optimizations, and database handling.