hezarfen_rag
Clone
https://git.tahinli.com/archive/hezarfen_rag.git
0 watching0 forksPython2.4 MiB184b84dAhmet Kaan Gümüş2 months ago189 commits
| 1 | """`rag.index` — the write path: note + attachments → chunks → vectors → the |
| 2 | note index (`src/index/notes.py`), and the answer object the backend stores. |
| 3 | |
| 4 | WHAT ONE REQUEST DOES |
| 5 | |
| 6 | 1. the note's own `title` + `content` become one document (always); |
| 7 | 2. each `files[]` entry is read as **bytes over its own QUIC blob stream** |
| 8 | (`BlobRequest` → `BlobResponse` header → exactly `size` raw bytes), then |
| 9 | parsed by content type — PDFs through the same canonical loader the |
| 10 | textbook corpus uses (`ingest/canonical.build_canonical`), plain text |
| 11 | decoded directly; |
| 12 | 3. every document is chunked (`chunk.chunk_document`) and embedded in one |
| 13 | pass (`embed_both`: dense + sparse, the provider configured by |
| 14 | `RAG_EMBED_*`); |
| 15 | 4. the note's rows replace whatever this note held before (one unit — a |
| 16 | re-index cannot duplicate); |
| 17 | 5. the answer echoes `files[].id` so the backend can stamp the doc id it |
| 18 | minted onto the attachment row, and names any attachment that failed; |
| 19 | 6. the answer also carries `passages` — the indexed **child** chunks with |
| 20 | their full text — so the caller can show WHAT was extracted instead of |
| 21 | only how many pieces there are (`MAX_PASSAGES`/`MAX_PASSAGES_BYTES` cap |
| 22 | the list; `passages_truncated` says whether anything was left out). |
| 23 | |
| 24 | A FAILED ATTACHMENT DOES NOT ABORT THE INDEX. A blob read that refuses (missing |
| 25 | file, permission, unknown type) is recorded and the rest — including the note |
| 26 | body — is indexed: a partly indexed note beats no note. The backend keeps the |
| 27 | previous `rag_output` rows when the whole round trip fails, which is why a |
| 28 | refusal must be typed rather than answered with an empty "ok". |
| 29 | """ |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | = |
| 41 | = |
| 42 | |
| 43 | = 50 |
| 44 | """Cevaba konan en fazla parça SAYISI.""" |
| 45 | |
| 46 | = 512 * 1024 |
| 47 | """Cevabın tamamı (bütün anahtarlar) için bayt bütçesi. |
| 48 | |
| 49 | Bayt tavanı Çerçeve tavanından (`AI_MAX_FRAME_BYTES` = 8 MiB) çok küçük |
| 50 | seçildi: 8 MiB'lık bir cevap telden geçse bile `rag_output.payload` olarak |
| 51 | saklanır ve panelde bir notu açmak ~8 MiB'lık bir JSON indirmek olurdu. |
| 52 | Parçaların hepsi zaten indekste duruyor — cevaptaki liste bir ÖRNEK.""" |
| 53 | |
| 54 | |
| 55 | |
| 56 | """Tel biçimi (`contract.encode_frame`) ile AYNI serileştirmenin boyutu.""" |
| 57 | return |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | """Çocuk chunk'lar → cevabın `passages` listesi + `passages_truncated`. |
| 63 | |
| 64 | Sözleşme (backend/frontend bu anahtarlara göre kod yazıyor): her satır |
| 65 | `chunk_id`, `doc_id`, `text`, `page_start`, `page_end` taşır ve `text` |
| 66 | indekste duran metnin BİREBİR kendisidir (kısaltma UI'ın işi). |
| 67 | |
| 68 | Bütçe cevabın TAMAMI içindir; `head` cevabın diğer anahtarlarıdır, yani |
| 69 | "512 KB" iddiası telden geçen şeyin kendisi hakkındadır. İki tavan da |
| 70 | (`MAX_PASSAGES`, `MAX_PASSAGES_BYTES`) aynı listeye bakar ve hangisi önce |
| 71 | dolarsa liste orada kesilir — kesildiyse bayrak `True`'dur. |
| 72 | """ |
| 73 | : = |
| 74 | = |
| 75 | |
| 76 | |
| 77 | return , True |
| 78 | = |
| 79 | |
| 80 | |
| 81 | # JSON ilk satırdan sonra her satır için bir virgül harcar; listenin |
| 82 | # kendi köşeli parantezleri `used` içindeki boş listede ödendi. |
| 83 | = + |
| 84 | |
| 85 | return , True |
| 86 | |
| 87 | += |
| 88 | return , False |
| 89 | |
| 90 | |
| 91 | |
| 92 | return in or \ |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | """`text` → canonical units (one per non-empty paragraph). |
| 98 | |
| 99 | The chunker consumes `CanonicalUnit`s, so the note's own prose rides the |
| 100 | exact same pipeline as a parsed PDF page — no second chunker, no second |
| 101 | span scheme. |
| 102 | """ |
| 103 | |
| 104 | = |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | return |
| 113 | |
| 114 | |
| 115 | |
| 116 | """The note's own title + content — doc id is the note's own record key, |
| 117 | so a re-index replaces rows instead of piling up new ones.""" |
| 118 | |
| 119 | = |
| 120 | |
| 121 | |
| 122 | = |
| 123 | return |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | = |
| 129 | = |
| 130 | |
| 131 | |
| 132 | = |
| 133 | return |
| 134 | |
| 135 | |
| 136 | |
| 137 | """PDF bytes → canonical document, through the corpus's own loader. |
| 138 | |
| 139 | `build_canonical` reads a path, so the bytes land in a temp file first; the |
| 140 | doc id is computed from the bytes here (not from the temp path) so it stays |
| 141 | the content hash the citation resolver expects. |
| 142 | """ |
| 143 | |
| 144 | , = |
| 145 | |
| 146 | |
| 147 | |
| 148 | = |
| 149 | = |
| 150 | = # sha256 of the same bytes |
| 151 | |
| 152 | = |
| 153 | = \ |
| 154 | |
| 155 | return |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | pass |
| 161 | |
| 162 | |
| 163 | |
| 164 | """Children only, note-scoped ids (two notes sharing one PDF must not |
| 165 | collide), no parent links (no parent rows are stored).""" |
| 166 | |
| 167 | = |
| 168 | |
| 169 | |
| 170 | continue |
| 171 | = f |
| 172 | = None |
| 173 | |
| 174 | return |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | """Index one course note and return the answer object the backend stores.""" |
| 180 | = |
| 181 | |
| 182 | |
| 183 | = |
| 184 | |
| 185 | = |
| 186 | , = , |
| 187 | |
| 188 | |
| 189 | |
| 190 | , = |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | continue |
| 196 | = |
| 197 | = |
| 198 | = |
| 199 | |
| 200 | |
| 201 | = |
| 202 | in or \ |
| 203 | : |
| 204 | = |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | continue |
| 213 | # noqa: BLE001 |
| 214 | |
| 215 | |
| 216 | continue |
| 217 | |
| 218 | |
| 219 | |
| 220 | = |
| 221 | |
| 222 | |
| 223 | = |
| 224 | |
| 225 | = |
| 226 | , = |
| 227 | = |
| 228 | = |
| 229 | |
| 230 | = |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | # Parça listesi indeksin KENDİSİ değil, ondan bir örnektir: buradaki |
| 238 | # herhangi bir arıza cevabı düşürmez, yalnız listeyi boşaltır (`chunks` |
| 239 | # yine tam sayıyı söyler). Backend eski `rag_output` satırını ancak TÜM |
| 240 | # tur düşerse korur — parça üretimi yüzünden reddetmek, indekslenmiş bir |
| 241 | # notu kaybettirirdi. |
| 242 | |
| 243 | , = |
| 244 | # noqa: BLE001 |
| 245 | , = , True |
| 246 | |
| 247 | return |
| 248 | |
| 249 | |
| 250 | |
| 251 | """One attachment's bytes over its own blob stream. |
| 252 | |
| 253 | The request shape is the bridge's (`protocol.rs::BlobRequest`): the file's |
| 254 | record id, the school uuid, and the note's author as `on_behalf_of` — the |
| 255 | `ai` principal can view no course, so a read as itself always earns |
| 256 | `forbidden`. |
| 257 | """ |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | return |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | """One or two Turkish sentences — the human is the reader of this payload.""" |
| 271 | = or |
| 272 | = |
| 273 | = |
| 274 | |
| 275 | |
| 276 | = |
| 277 | += |
| 278 | |
| 279 | return |