diff --git a/backend/app/core/sqlite_db.py b/backend/app/core/sqlite_db.py index cfcccc1..52af255 100644 --- a/backend/app/core/sqlite_db.py +++ b/backend/app/core/sqlite_db.py @@ -22,13 +22,14 @@ _SEED_FILTER = ( ) _SEED_GENERATE = ( - "Question: {question}\n\n" - "Answer the question using ONLY these document chunks. " - "Do not use any external knowledge. " - "Format your answer as bullet points. " - "Cite your sources inline using the exact bracket labels provided, " - "e.g. [filename, page N]. Place the citation at the end of each relevant point.\n\n" - "Document chunks:\n{context}\n\n" + "You must answer each sub-question using ONLY the document chunks provided for it.\n" + "Do not use any external knowledge.\n" + "Format your answer as markdown sections — one section per sub-question.\n" + "Each section should start with \"## Sub-question N: \"\n" + "Each section should contain 1-5 bullet points.\n" + "Cite your sources inline using bracket labels, e.g. [filename, page N].\n" + "Place the citation at the end of each relevant bullet point.\n\n" + "{context_sections}\n\n" "Answer:" ) @@ -113,6 +114,8 @@ def init_history_db(conn: sqlite3.Connection) -> None: final_answer TEXT DEFAULT NULL, sources TEXT DEFAULT NULL, profile_used TEXT DEFAULT NULL, + chunks_retrieved_per_subq_count TEXT DEFAULT NULL, + chunks_filtered_per_subq_count TEXT DEFAULT NULL, created_at TEXT NOT NULL DEFAULT (datetime('now')) ) """) @@ -120,6 +123,13 @@ def init_history_db(conn: sqlite3.Connection) -> None: CREATE INDEX IF NOT EXISTS idx_query_history_created_at ON query_history(created_at DESC) """) + for col_name in ["chunks_retrieved_per_subq_count", "chunks_filtered_per_subq_count"]: + try: + conn.execute( + f"ALTER TABLE query_history ADD COLUMN {col_name} TEXT DEFAULT NULL" + ) + except Exception: + pass conn.commit() logger.info("History DB tables initialized.") diff --git a/backend/app/services/history_service.py b/backend/app/services/history_service.py index e00f4ba..21ae786 100644 --- a/backend/app/services/history_service.py +++ b/backend/app/services/history_service.py @@ -26,6 +26,7 @@ _INSERT_COLUMNS = ( "generate_prompt", "generator_time_ms", "total_time_ms", "final_answer", "sources", "profile_used", + "chunks_retrieved_per_subq_count", "chunks_filtered_per_subq_count", ) diff --git a/backend/app/test/test_phase3_history_service.py b/backend/app/test/test_phase3_history_service.py index 88b90d4..cfa5f07 100644 --- a/backend/app/test/test_phase3_history_service.py +++ b/backend/app/test/test_phase3_history_service.py @@ -47,6 +47,8 @@ CREATE TABLE IF NOT EXISTS query_history ( final_answer TEXT DEFAULT NULL, sources TEXT DEFAULT NULL, profile_used TEXT DEFAULT NULL, + chunks_retrieved_per_subq_count TEXT DEFAULT NULL, + chunks_filtered_per_subq_count TEXT DEFAULT NULL, created_at TEXT NOT NULL DEFAULT (datetime('now')) ) """ diff --git a/backend/app/test/test_phase3_prompt_service.py b/backend/app/test/test_phase3_prompt_service.py index 16411bc..baace5b 100644 --- a/backend/app/test/test_phase3_prompt_service.py +++ b/backend/app/test/test_phase3_prompt_service.py @@ -98,7 +98,7 @@ def test_get_profile_prompts_returns_all_three_steps(tmp_path): assert set(prompts.keys()) == {"decompose", "filter", "generate"} assert "{question}" in prompts["decompose"] assert "{question}" in prompts["filter"] - assert "{question}" in prompts["generate"] + assert "{context_sections}" in prompts["generate"] def test_get_profile_prompts_invalid_name_raises(tmp_path): diff --git a/backend/app/test/test_phase3_sqlite_db.py b/backend/app/test/test_phase3_sqlite_db.py index bd56ec3..7372618 100644 --- a/backend/app/test/test_phase3_sqlite_db.py +++ b/backend/app/test/test_phase3_sqlite_db.py @@ -311,7 +311,7 @@ def test_seed_default_profiles_contains_expected_templates(tmp_path, monkeypatch assert "{question}" in filter_row["prompt_template"] assert "{chunks}" in filter_row["prompt_template"] - # Generate must have {question} and {context} + # Generate must have {context_sections} generate_row = conn.execute( """SELECT sp.prompt_template FROM system_prompts sp @@ -319,8 +319,7 @@ def test_seed_default_profiles_contains_expected_templates(tmp_path, monkeypatch WHERE spp.name='A' AND sp.step_name='generate'""" ).fetchone() assert generate_row is not None - assert "{question}" in generate_row["prompt_template"] - assert "{context}" in generate_row["prompt_template"] + assert "{context_sections}" in generate_row["prompt_template"] conn.close()