import subprocess
from pathlib import Path
ROOT = Path(__file__).resolve().parent
VENV_PY = ROOT / "Final_venv" / "Scripts" / "python.exe"
TEST_PIPELINE = ROOT / "04_vectoredb" / "test_pipeline.py"
PARSE_OUTPUT = ROOT / "05_generation" / "parse_output.py"
GENERATE_ANSWER = ROOT / "05_generation" / "generate_answer.py"
def run_step(args, cwd, stdin_text=None, capture_output=True):
return subprocess.run(
args,
cwd=cwd,
input=stdin_text,
text=True,
check=True,
capture_output=capture_output,
)
def run_query(query: str) -> str:
"""
Takes a user query and returns the final printed output string from generate_answer.py
"""
query = (query or "").strip()
if not query:
return "No query provided."
# Step 1: run vector db retrieval pipeline (expects query via stdin)
run_step(
[str(VENV_PY), str(TEST_PIPELINE)],
cwd=ROOT,
stdin_text=query + "\n",
)
# Step 2: parse output / prepare prompt context
run_step(
[str(VENV_PY), str(PARSE_OUTPUT), "--query", query],
cwd=ROOT / "05_generation",
)
# Step 3: generate final answer and capture it
result = run_step(
[str(VENV_PY), str(GENERATE_ANSWER)],
cwd=ROOT / "05_generation",
capture_output=True,
)
return result.stdout.strip()