File size: 7,762 Bytes
399944f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
from __future__ import annotations

"""
Minimal entry point to test the Extractor component in isolation.

Pipeline:
    1. Load documents from a text or PDF corpus & chunk them (Chunker).
    2. Decompose each document into atomic sentences/qualities (Decomposer).
    3. Extract S-P-O triplets for each atomic sentence (Triplet Extractor).
    4. Print or optionally write results to a JSON file.

Configuration is driven by environment variables (see ExtractorConfig.from_env).
"""

import json
import os
from dataclasses import dataclass
from typing import Literal, Tuple, List

from rich.console import Console
from rich.table import Table
from rich.panel import Panel
from rich.text import Text

console = Console()

from kbdebugger.utils.warnings_config import install_warning_filters
from kbdebugger.compat.langchain import Document

from kbdebugger.extraction.text_to_sentences import extract_txt_sentences
# from kbdebugger.extraction.pdf_to_sentences import extract_pdf_sentences
from kbdebugger.extraction.pdf_to_chunks import extract_pdf_chunks

from kbdebugger.extraction.decompose import decompose, DecomposeMode
from kbdebugger.extraction.triplet_extraction_batch import extract_triplets_batch
from kbdebugger.types import ExtractionResult, SourceKind
from kbdebugger.extraction.types import Qualities

# ---------------------------------------------------------------------------
# Configuration
# ---------------------------------------------------------------------------

@dataclass(frozen=True)
class ExtractorConfig:
    """
    Runtime configuration for the Extractor main script.

    Environment variables:

    - KB_SOURCE_KIND: one of
        * "TEXT"          → load plain-text file via `text_to_sentences`
        * "PDF_SENTENCES" → load PDF and split directly into sentences
        * "PDF_CHUNKS"    → load PDF and split into larger chunks

      Default: "TEXT"

    - KB_TEXT_PATH: path to txt file (used when KB_SOURCE_KIND == "TEXT")
      Default: "data/DSA/DSA_knowledge.txt"

    - KB_PDF_PATH: path to pdf file (used when KB_SOURCE_KIND starts with "PDF")
      Default: "data/SDS/example.pdf"

    - KB_BATCH_SIZE: number of atomic sentences per LLM batch for triplet extraction.
      Default: 5

    - KB_OUTPUT_PATH: optional path to write JSON results.
      If empty, results are only printed to stdout.
    """

    source_kind: SourceKind
    text_path: str
    pdf_path: str
    batch_size: int
    output_path: str | None

    @classmethod
    def from_env(cls) -> "ExtractorConfig":
        source_raw = os.getenv("KB_SOURCE_KIND", "TEXT").upper().strip()
        if source_raw not in {"TEXT", "PDF_SENTENCES", "PDF_CHUNKS"}:
            raise ValueError(f"Invalid KB_SOURCE_KIND={source_raw!r}")

        text_path = os.getenv("KB_TEXT_PATH", "data/DSA/DSA_knowledge.txt").strip()
        pdf_path = os.getenv("KB_PDF_PATH", "data/SDS/InstructCIR.pdf").strip()

        batch_size_str = os.getenv("KB_BATCH_SIZE", "5").strip()
        try:
            batch_size = max(1, int(batch_size_str))
        except ValueError:
            batch_size = 5

        output_raw = os.getenv("KB_OUTPUT_PATH", "").strip()
        output_path = output_raw or None

        return cls(
            source_kind=source_raw,  # type: ignore[arg-type]
            text_path=text_path,
            pdf_path=pdf_path,
            batch_size=batch_size,
            output_path=output_path,
        )

# ---------------------------------------------------------------------------
# High-level steps
# ---------------------------------------------------------------------------
def load_documents(cfg: ExtractorConfig) -> Tuple[List[Document], DecomposeMode]:
    """
    Use the Chunker submodule to load a corpus into LangChain Documents.
    Returns (documents, decompose_mode).
    """
    match cfg.source_kind:
        case "TEXT":
            docs = extract_txt_sentences(cfg.text_path)
            mode = DecomposeMode.SENTENCES

        # case "PDF_SENTENCES":
        #     docs = extract_pdf_sentences(cfg.pdf_path)
        #     mode = DecomposeMode.SENTENCES

        case "PDF_CHUNKS":
            docs = extract_pdf_chunks(cfg.pdf_path)
            mode = DecomposeMode.CHUNKS

    if not docs:
        raise ValueError("No documents loaded. Check your paths and source kind.")

    return (docs, mode)


def run_extractor(cfg: ExtractorConfig) -> List[ExtractionResult]:
    """
    End-to-end run of the Extractor component:

        Corpus -> Chunked Documents -> Atomic sentences/Qualities -> Triplets
    """
    # 1. Load corpus & chunk into LangChain Documents
    docs, mode = load_documents(cfg)

    # 2. Decompose each document into atomic sentences/qualities
    all_qualities: Qualities = []
    for doc in docs:
        qualities = decompose(text=doc.page_content, mode=mode)
        all_qualities.extend(qualities)

    if not all_qualities:
        raise ValueError("Decomposition produced no qualities.")

    # 3. Triplet extraction in batches
    results = extract_triplets_batch(all_qualities, batch_size=cfg.batch_size)
    return results


def print_results(results: List[ExtractionResult]) -> None:
    """
    Pretty-print extraction results to stdout using rich.
    """
    console.rule("[bold blue]Triplet Extraction Results")

    for i, item in enumerate(results, start=1):
        sentence: str = item.get("sentence", "")
        triplets: List[Tuple[str, str, str]] = item.get("triplets", [])

        # Header panel for each atomic sentence
        console.print(
            Panel.fit(
                Text(sentence, style="bold white"),
                title=f"[cyan]Sentence {i}",
                border_style="cyan",
            )
        )

        if not triplets:
            console.print("[yellow]No triplets extracted.[/yellow]\n")
            continue

        # Table of triplets
        table = Table(
            show_header=True,
            header_style="bold magenta",
            show_lines=True,
            border_style="dim",
        )
        table.add_column("Subject", style="green", ratio=2)
        table.add_column("Relation", style="white", ratio=3)
        table.add_column("Object", style="green", ratio=2)

        for subj, obj, rel in triplets:
            table.add_row(subj, rel, obj)

        console.print(table)
        console.print()  # extra newline


def save_results_json(results: List[ExtractionResult], path: str) -> None:
    """
    Write extraction results to a JSON file.
    """
    data = {
        "results": results,
    }
    os.makedirs(os.path.dirname(path), exist_ok=True)
    with open(path, "w", encoding="utf-8") as f:
        json.dump(data, f, ensure_ascii=False, indent=2)
    print(f"\n[INFO] Wrote JSON results to {path}")


# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------
def main() -> None:
    install_warning_filters()

    cfg = ExtractorConfig.from_env()
    console.rule("[bold green]KBDEBUGGER Extractor")

    console.print(f"[bold]Source kind:[/bold] [cyan]{cfg.source_kind}[/cyan]")
    if cfg.source_kind == "TEXT":
        console.print(f"[bold]Text path:[/bold] [white]{cfg.text_path}[/white]")
    else:
        console.print(f"[bold]PDF path:[/bold] [white]{cfg.pdf_path}[/white]")

    console.print(f"[bold]Batch size:[/bold] [yellow]{cfg.batch_size}[/yellow]")

    if cfg.output_path:
        console.print(f"[bold]Output file:[/bold] [green]{cfg.output_path}[/green]")

    console.print()  # spacing

    results = run_extractor(cfg)

    if cfg.output_path:
        save_results_json(results, cfg.output_path)

    print_results(results)


if __name__ == "__main__":
    main()