Bob Belderbos: One Core, Two I... Note

Bob Belderbos: One Core, Two Interfaces, No Rewrites

The author of Ask the Canon built the application by first creating the core functionality and then adding interfaces, following a functional core and thin adapters approach. The core consists of a handful of pure functions in one module, including embed, load_library, search_passages, and reflow, which are used by both the command-line interface and the web application. The load_library function is not cheap to call, so the author used caching to improve performance, with the cache decorator turning the first call into a dictionary lookup. To prevent the first visitor from paying the overhead of loading the library, the author used FastAPI's lifespan to pre-warm the library and load models into RAM before the first request. The author also used lazy loading to defer the loading of the sentence-transformers model until it is actually needed. The application's design uses functions over plain data, with no need for classes, and the core functions can be easily reused in new interfaces. The author emphasizes the importance of engineering judgment in building applications, particularly when going from prototype to production. The application's architecture is designed to be flexible and scalable, with a clear separation between the core functionality and the interfaces. The author plans to discuss post-processing tricks to improve the application's results in a future post. The use of caching, lazy loading, and pre-warming all contribute to the application's performance and efficiency.