How artificial intelligence, large language models, and retrieval-augmented generation power this app
A Large Language Model (LLM) is a neural network trained on vast amounts of text. It learns statistical patterns β which words follow which other words β until it can generate coherent, contextually appropriate text on almost any topic.
LLMs have a context window limit β they can only read so many words at once. If a user has 500 diary entries, sending them all to Claude would be slow, expensive, and potentially exceed the limit. RAG solves this by retrieving only the most relevant entries first.
ENC:), the pipeline falls back to sorting by pain_level instead. This is why the frontend sends already-decrypted entries in the POST body β the server never sees the plaintext, but the RAG step still works.
TF (Term Frequency) measures how often a word appears in one entry. IDF (Inverse Document Frequency) penalizes words that appear in many entries β common words like "pain" are less useful for distinguishing relevance than specific words like "patella." Multiplied together, TF-IDF surfaces the signal words.
Each entry becomes a vector β one dimension per word, value = TF-IDF weight. The query ("knee pain orthopedist") becomes its own vector. Cosine similarity measures the angle between vectors. Small angle = similar direction = relevant entry.
When you click Generate Summary, here is the full pipeline:
/api/summary/preview. The frontend POSTs the already-decrypted entry list along with your chosen specialty (e.g. "orthopedist") and optional custom doctor text._retrieve_relevant_entries() in summarizer.py builds TF-IDF vectors, computes cosine similarity against the specialty query, and selects the top 25 entries.generate_summary() builds a prompt using specialty-specific instructions from SPECIALTY_PROMPTS (9 specialties) plus the formatted top entries.max_tokens=1200, temperature=0.3). Claude returns a 3-section response: symptom paragraph, patterns paragraph, and 3 suggested questions./api/summary/save, download it, or email it.The Focus page (/focus) lets you type any topic β "knee", "diabetes", "lower back" β and get a targeted AI summary plus a list of all matching entries. It uses the same RAG pipeline as specialty summaries but with top_k=50 (wider net) and a topic-specific prompt.
generate_focus_summary(entries, topic) in summarizer.pyfocus-knee.txt file for sharing with your doctorANTHROPIC_API_KEY lives in Heroku config vars β never in the codebase