File size: 5,280 Bytes
6b8bb0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
"""Typer command-line entrypoint for DocuLearn-RAG."""

from __future__ import annotations

import json
from pathlib import Path

import typer
from pydantic import BaseModel, ValidationError

from src.export import ExportFormat, export
from src.filters import MetadataFilter, filters_to_dict
from src.indexing import ingest as ingest_data
from src.learning import generate_flashcards, generate_quiz
from src.learning import summarize as summarize_learning
from src.rag import answer, retrieve
from src.schemas import RetrievedChunk

app = typer.Typer(help="Index PDFs and use grounded DocuLearn-RAG learning features.")


def _parse_filters(value: str | None) -> MetadataFilter | None:
    if value is None or not value.strip():
        return None

    parsed: dict[str, object] = {}
    allowed = set(MetadataFilter.model_fields)
    for expression in value.split(","):
        key, separator, raw_value = expression.partition("=")
        key = key.strip()
        raw_value = raw_value.strip()
        if not separator or not key or not raw_value:
            raise typer.BadParameter("Filters must use key=value pairs separated by commas.")
        if key not in allowed:
            raise typer.BadParameter(
                f"Unknown filter '{key}'. Expected one of: {', '.join(sorted(allowed))}."
            )
        if key == "page":
            try:
                parsed[key] = int(raw_value)
            except ValueError as error:
                raise typer.BadParameter("The page filter must be an integer.") from error
        elif key == "filenames":
            parsed[key] = [item.strip() for item in raw_value.split("|") if item.strip()]
        else:
            parsed[key] = raw_value

    try:
        return MetadataFilter.model_validate(parsed)
    except ValidationError as error:
        raise typer.BadParameter(str(error)) from error


def _write_result(
    result: BaseModel,
    output: Path | None,
    fmt: ExportFormat,
) -> None:
    rendered = export(result, fmt=fmt, output=output)
    if output is None:
        typer.echo(rendered, nl=False)
    else:
        typer.echo(f"Exported to {rendered}")


def _print_sources(chunks: list[RetrievedChunk]) -> None:
    if not chunks:
        return
    typer.echo("\nSources:")
    for index, chunk in enumerate(chunks, start=1):
        metadata = chunk.metadata
        typer.echo(f"S{index}: {metadata.filename}, page {metadata.page}")


@app.command()
def ingest(recreate: bool = False) -> None:
    """Index all PDFs in the configured data directory."""
    count = ingest_data(recreate=recreate)
    typer.echo(f"Done. {count} chunks indexed.")


@app.command()
def ask(
    question: str,
    k: int | None = None,
    filters: str | None = None,
) -> None:
    """Ask a grounded question and show its retrieved sources."""
    result = answer(question, k=k, filters=filters_to_dict(_parse_filters(filters)))
    typer.echo(result.answer)
    _print_sources(result.chunks)


@app.command("debug-retrieval")
def debug_retrieval(
    question: str,
    k: int | None = None,
    filters: str | None = None,
    as_json: bool = False,
) -> None:
    """Inspect the chunks retrieved for a question."""
    chunks = retrieve(question, k=k, filters=filters_to_dict(_parse_filters(filters)))
    if as_json:
        typer.echo(
            json.dumps(
                [chunk.model_dump(mode="json") for chunk in chunks],
                ensure_ascii=False,
                indent=2,
            )
        )
        return
    for index, chunk in enumerate(chunks, start=1):
        metadata = chunk.metadata
        typer.echo(
            f"S{index} {metadata.filename} p.{metadata.page} "
            f"score={chunk.score:.4f}\n{chunk.text}\n"
        )


@app.command()
def summarize(
    document: str | None = None,
    query: str | None = None,
    filters: str | None = None,
    k: int | None = None,
    output: Path | None = None,
    fmt: ExportFormat = "text",
) -> None:
    """Create a grounded summary."""
    result = summarize_learning(
        document=document,
        query=query,
        filters=filters_to_dict(_parse_filters(filters)),
        k=k,
    )
    _write_result(result, output, fmt)


@app.command()
def quiz(
    document: str | None = None,
    query: str | None = None,
    filters: str | None = None,
    count: int | None = None,
    k: int | None = None,
    output: Path | None = None,
    fmt: ExportFormat = "text",
) -> None:
    """Generate a grounded multiple-choice quiz."""
    result = generate_quiz(
        document=document,
        query=query,
        filters=filters_to_dict(_parse_filters(filters)),
        count=count,
        k=k,
    )
    _write_result(result, output, fmt)


@app.command()
def flashcards(
    document: str | None = None,
    query: str | None = None,
    filters: str | None = None,
    count: int | None = None,
    k: int | None = None,
    output: Path | None = None,
    fmt: ExportFormat = "text",
) -> None:
    """Generate grounded flashcards."""
    result = generate_flashcards(
        document=document,
        query=query,
        filters=filters_to_dict(_parse_filters(filters)),
        count=count,
        k=k,
    )
    _write_result(result, output, fmt)


if __name__ == "__main__":
    app()