hezarfen_rag
Clone
https://git.tahinli.com/Hezarfen/hezarfen_rag.git
0 watching0 forksPython2.4 MiB184b84dAhmet Kaan Gümüş2 months ago189 commits
| 1 | """The course-note index — the retrieval channel `rag.index` fills. |
| 2 | |
| 3 | WHY IT IS ITS OWN INDEX. A textbook corpus is a file on disk |
| 4 | (`data/<school>/<kasa>/<sinif>/<ders>/kitap.pdf`) keyed by `(school, sinif, |
| 5 | ders)`. A course note names none of those on the wire: the `rag.index` payload |
| 6 | carries the note's `course` **uuid**, while a `rag.chat` scope pair carries a |
| 7 | class grade and a course **title** (derived by the backend from the asker's own |
| 8 | memberships) — and the backend's AI-read allowlist has no route that would let |
| 9 | this service resolve one to the other. There is no join, so a note cannot be |
| 10 | filed under a subject corpus. |
| 11 | |
| 12 | WHAT IT IS INSTEAD. One bucket per **school** — the tenant dimension every store |
| 13 | already filters on, by exact equality (`guard/tenant.py`). `replace_note()` swaps |
| 14 | a note's rows as one unit (a re-index never duplicates); `retrieve()` ranks that |
| 15 | bucket's rows and `rag.chat` merges the hits into the subject corpus's. |
| 16 | |
| 17 | HONEST LIMIT — the (class, subject) gate does not reach here. `can_access` |
| 18 | isolates class+subject for textbook chunks; a note carries neither on the wire, |
| 19 | so its rows cannot be checked against the asker's scope pairs. The backend still |
| 20 | refuses to *link* a citation the asker may not open (it stores `file: null`), |
| 21 | but a note's *text* from another course of the same school can be quoted. |
| 22 | Closing that needs the note's class+subject on the `rag.index` payload — a wire |
| 23 | change owned by the backend, not by this repo. |
| 24 | |
| 25 | IN-MEMORY, DERIVED, DISPOSABLE. Nothing here is a source of truth: a restart |
| 26 | loses it, and any note edit re-dispatches `rag.index` — the backend's own model |
| 27 | of a `rag_output` row ("derived data"). |
| 28 | """ |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | #: One attachment's byte ceiling — the backend's own `MAX_MAX_FILE_BYTES` |
| 43 | #: (`constant.rs:80`). A blob header promising more is refused before a byte of |
| 44 | #: the body is read. |
| 45 | = 25 * 1024 * 1024 |
| 46 | |
| 47 | |
| 48 | |
| 49 | """Content-derived document id — the same rule the textbook path uses |
| 50 | (`ingest/canonical.py`: the first 12 hex of the sha256). Identical bytes |
| 51 | hash to one document, which is exactly what the backend's citation |
| 52 | resolver expects (several files may claim one doc id).""" |
| 53 | return |
| 54 | |
| 55 | |
| 56 | |
| 57 | """One school's note rows, plus indexes rebuilt lazily after a write.""" |
| 58 | |
| 59 | |
| 60 | : = |
| 61 | : = |
| 62 | : = |
| 63 | : = |
| 64 | : = # chunk_id -> Chunk |
| 65 | : = # span_id -> {"page", "bbox"} |
| 66 | : = # chunk_id -> note key |
| 67 | = None |
| 68 | = None |
| 69 | = None |
| 70 | = True |
| 71 | |
| 72 | |
| 73 | |
| 74 | return |
| 75 | |
| 76 | = |
| 77 | |
| 78 | |
| 79 | = |
| 80 | |
| 81 | = |
| 82 | |
| 83 | |
| 84 | = = = None |
| 85 | = False |
| 86 | |
| 87 | |
| 88 | |
| 89 | """School-scoped course-note rows + their three retrieval indexes. |
| 90 | |
| 91 | Thread-safe: `rag.index` runs in the transport's executor thread while |
| 92 | `rag.chat` retrieves on request threads. |
| 93 | """ |
| 94 | |
| 95 | |
| 96 | : = |
| 97 | = |
| 98 | |
| 99 | # -- yazma ------------------------------------------------------------ |
| 100 | |
| 101 | """Swap one note's rows for `rows` — the note-level unit of update. |
| 102 | |
| 103 | `rows` is `[(Chunk, dense_vector, sparse_dict), ...]`. The previous rows |
| 104 | of this note are dropped first, so a re-index is idempotent by |
| 105 | construction (chunk ids are note-scoped; nothing survives to duplicate). |
| 106 | """ |
| 107 | |
| 108 | = |
| 109 | |
| 110 | = |
| 111 | |
| 112 | |
| 113 | = |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | = |
| 119 | = |
| 120 | |
| 121 | = |
| 122 | |
| 123 | = True |
| 124 | return |
| 125 | |
| 126 | |
| 127 | |
| 128 | = |
| 129 | |
| 130 | = |
| 131 | |
| 132 | return 0 |
| 133 | = |
| 134 | = True |
| 135 | return |
| 136 | |
| 137 | |
| 138 | |
| 139 | = |
| 140 | |
| 141 | return 0 |
| 142 | = |
| 143 | = |
| 144 | = |
| 145 | = |
| 146 | = |
| 147 | = |
| 148 | |
| 149 | = |
| 150 | |
| 151 | |
| 152 | |
| 153 | return |
| 154 | |
| 155 | # -- okuma ------------------------------------------------------------ |
| 156 | |
| 157 | |
| 158 | """Rank this school's note rows — `[(chunk_id, rrf_score), ...]`. |
| 159 | |
| 160 | The tenant filter is the store's own (`school=` on every index search): |
| 161 | another school's notes, and rows with no school stamp, are never |
| 162 | returned (`guard/tenant.py`). |
| 163 | """ |
| 164 | |
| 165 | return |
| 166 | |
| 167 | = |
| 168 | |
| 169 | return |
| 170 | |
| 171 | , , = , , |
| 172 | = |
| 173 | = |
| 174 | = |
| 175 | |
| 176 | |
| 177 | = |
| 178 | |
| 179 | return |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | return |
| 186 | return None |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | return |
| 193 | return None |
| 194 | |
| 195 | |
| 196 | |
| 197 | return |
| 198 | |
| 199 | |
| 200 | |
| 201 | : | None = None |
| 202 | = |
| 203 | |
| 204 | |
| 205 | |
| 206 | """The process-wide note index. |
| 207 | |
| 208 | `rag.index` (transport thread) and `rag.chat` (request threads) must see the |
| 209 | same rows, and the service is built per corpus — so the store is a process |
| 210 | singleton, like `guard/tenant.py`'s rules and the provider configuration. |
| 211 | """ |
| 212 | global _SHARED |
| 213 | |
| 214 | |
| 215 | = |
| 216 | return |
| 217 | |
| 218 | |
| 219 | |
| 220 | """`{chunk_id: object}` that falls back to the note index. |
| 221 | |
| 222 | `Generator` holds `chunks_by_id` and `span_meta` — both built once, from a |
| 223 | corpus that was on disk at build time. Note rows arrive later (and are |
| 224 | replaced), so lookups consult the live note index first; the base mapping |
| 225 | answers everything else. Read-only `Mapping` is enough: the pipeline only |
| 226 | reads (`cid in chunks`, `chunks[cid].text`, `span_meta.get(sid)`). |
| 227 | """ |
| 228 | |
| 229 | |
| 230 | = |
| 231 | = # (key) -> value | None |
| 232 | |
| 233 | |
| 234 | = |
| 235 | |
| 236 | return |
| 237 | return |
| 238 | |
| 239 | |
| 240 | yield from |
| 241 | # Live rows are not enumerated: the base keys are what a caller |
| 242 | # iterates for counts/aggregates, and the note rows are already found |
| 243 | # by id. Enumerating them here would silently change `len()` semantics. |
| 244 | |
| 245 | |
| 246 | return |
| 247 | |
| 248 | |
| 249 | = |
| 250 | return |
| 251 | |
| 252 | |
| 253 | return is not None or in |
| 254 | |
| 255 | |
| 256 | |
| 257 | """RRF-merge two best-first rank lists — the subject corpus's hits and the |
| 258 | school's note hits. Ranks (not scores) are what RRF uses, which is exactly |
| 259 | what two differently-calibrated indexes need.""" |
| 260 | |
| 261 | return |
| 262 | |
| 263 | return |
| 264 | return |