ragtime 0.2.0

Easy Retrieval Augmented Generation
Documentation
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/// Document handling
use crate::decoder::{Decoded, Decoder};
use anyhow::{anyhow, bail, Result};
use chrono::prelude::*;
use core::fmt;
use fxhash::{FxBuildHasher, FxHashMap};
use indexmap::IndexMap;
use memmap2::Mmap;
use serde::{Deserialize, Serialize};
use std::collections::hash_map::Entry;
use std::error::Error;
use std::fmt::Display;
use std::fs::OpenOptions;
use std::sync::atomic::{AtomicU64, Ordering};
use std::{
    collections::HashMap,
    fs::File,
    path::{Path, PathBuf},
};

#[derive(Debug, Clone)]
pub struct DocumentChanged(pub PathBuf);

impl Display for DocumentChanged {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "The document {:?} has changed since it was indexed",
            &self.0
        )
    }
}

impl Error for DocumentChanged {}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct DocId(u64);

static NEXT_DOCID: AtomicU64 = AtomicU64::new(0);

impl DocId {
    pub fn new() -> Self {
        Self(NEXT_DOCID.fetch_add(1, Ordering::Relaxed))
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct ChunkId(pub u64);

impl fmt::Display for ChunkId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

static NEXT_CHUNKID: AtomicU64 = AtomicU64::new(0);

fn word_boundry(x: u8) -> bool {
    x == 32 || x == 10 || x == 9
}

impl ChunkId {
    pub fn new() -> Self {
        Self(NEXT_CHUNKID.fetch_add(1, Ordering::Relaxed))
    }
}

impl From<u64> for ChunkId {
    fn from(value: u64) -> Self {
        Self(value)
    }
}

impl Into<u64> for ChunkId {
    fn into(self) -> u64 {
        self.0
    }
}

struct ChunkIter<'a> {
    data: &'a [u8],
    id: DocId,
    pos: usize,
    chunk_size: usize,
    overlap: usize,
}

impl<'a> Iterator for ChunkIter<'a> {
    type Item = ChunkDescriptor;

    fn next(&mut self) -> Option<Self::Item> {
        let mut ntok = 0;
        let mut overlap = 0;
        let mut pos = self.pos;
        loop {
            if pos >= self.data.len() {
                if pos == self.pos {
                    break None;
                } else {
                    let start = self.pos;
                    self.pos = self.data.len();
                    let cd = ChunkDescriptor {
                        id: ChunkId::new(),
                        doc: self.id,
                        start,
                        end: self.data.len() - 1,
                    };
                    break Some(cd);
                }
            }
            if ntok == self.chunk_size - self.overlap {
                overlap = pos;
            }
            if ntok >= self.chunk_size {
                let start = self.pos;
                self.pos = overlap;
                let cd = ChunkDescriptor {
                    id: ChunkId::new(),
                    doc: self.id,
                    start,
                    end: pos,
                };
                break Some(cd);
            }
            while pos < self.data.len() && !word_boundry(self.data[pos]) {
                pos += 1
            }
            while pos < self.data.len() && word_boundry(self.data[pos]) {
                pos += 1;
            }
            ntok += 1
        }
    }
}

/// Return an iterator over the chunks in this
/// document. [chunk_size] is the number of words that should be
/// in a chunk, and [overlap] is the number of words of overlap
/// that should exist between chunks. e.g. 512 and 256 would
/// produce 512 word chunks that overlap with each other by 256
/// words.
fn iter_chunks<'a>(
    id: DocId,
    data: &'a [u8],
    chunk_size: usize,
    overlap: usize,
) -> Result<ChunkIter<'a>> {
    if chunk_size == 0 || overlap >= chunk_size {
        bail!("chunk_size must be > 0, overlap must be < tokens")
    }
    Ok(ChunkIter {
        id,
        pos: 0,
        chunk_size,
        overlap,
        data,
    })
}

#[derive(Debug)]
struct Doc {
    decoded: Decoded,
    _file: File,
    map: Mmap,
    last_used: DateTime<Utc>,
    saved: SavedDoc,
}

impl Doc {
    fn new(decoded: Decoded, saved: SavedDoc, file: File, map: Mmap) -> Self {
        Self {
            decoded,
            _file: file,
            map,
            last_used: Utc::now(),
            saved,
        }
    }

    fn get<'a>(&'a self, chunk: &ChunkDescriptor) -> Result<&'a str> {
        let data = &*self.map;
        if chunk.end >= data.len() {
            bail!(
                "chunk out of bounds, doc: {:?} has changed since it was indexed?",
                chunk.doc
            )
        }
        let res = std::str::from_utf8(&data[chunk.start..chunk.end])?;
        Ok(res)
    }
}

#[derive(Debug, Clone, Copy)]
pub struct ChunkRef<'a> {
    pub original_path: &'a Path,
    pub decoded_path: &'a Path,
    pub summary: Option<&'a str>,
    pub text: &'a str,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
struct ChunkDescriptor {
    id: ChunkId,
    doc: DocId,
    start: usize,
    end: usize,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct SavedDoc {
    id: DocId,
    path: PathBuf,
    summary: Option<String>,
    md5sum: [u8; 16],
    chunks: Vec<ChunkId>,
}

#[derive(Debug, Serialize, Deserialize)]
struct Saved {
    docs: Vec<SavedDoc>,
    chunks: Vec<ChunkDescriptor>,
    next_docid: u64,
    next_chunkid: u64,
    max_mapped: usize,
}

#[derive(Debug)]
pub struct DocStore {
    mapped: IndexMap<DocId, Doc, FxBuildHasher>,
    unmapped: FxHashMap<DocId, SavedDoc>,
    by_path: FxHashMap<PathBuf, DocId>,
    chunks: FxHashMap<ChunkId, ChunkDescriptor>,
    max_mapped: usize,
    decoder: Decoder,
}

impl DocStore {
    pub fn new(max_mapped: usize) -> Result<Self> {
        if max_mapped < 1 {
            bail!("max_mapped must be at least 1")
        }
        Ok(Self {
            mapped: IndexMap::default(),
            unmapped: HashMap::default(),
            by_path: HashMap::default(),
            chunks: HashMap::default(),
            max_mapped,
            decoder: Decoder::new()?,
        })
    }

    pub fn save<P: AsRef<Path>>(&self, path: P) -> Result<()> {
        let mut fd = OpenOptions::new()
            .create(true)
            .write(true)
            .truncate(true)
            .open(path.as_ref())?;
        let next_docid = NEXT_DOCID.load(Ordering::Relaxed);
        let next_chunkid = NEXT_CHUNKID.load(Ordering::Relaxed);
        let docs = self
            .mapped
            .iter()
            .map(|(_, d)| d.saved.clone())
            .chain(self.unmapped.values().cloned())
            .collect();
        let chunks = self.chunks.iter().map(|(_, c)| *c).collect();
        let saved = Saved {
            docs,
            chunks,
            next_chunkid,
            next_docid,
            max_mapped: self.max_mapped,
        };
        Ok(serde_json::to_writer_pretty(&mut fd, &saved)?)
    }

    pub fn load<P: AsRef<Path>>(path: P) -> Result<Self> {
        let saved: Saved = serde_json::from_reader(File::open(path.as_ref())?)?;
        let mut t = Self::new(saved.max_mapped)?;
        for sd in saved.docs {
            t.by_path.insert(sd.path.clone(), sd.id);
            t.unmapped.insert(sd.id, sd);
        }
        for chunk in saved.chunks {
            t.chunks.insert(chunk.id, chunk);
        }
        macro_rules! update {
            ($var:ident, $field:ident) => {
                let _ = $var.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |cur| {
                    if saved.$field > cur {
                        Some(saved.$field)
                    } else {
                        None
                    }
                });
            };
        }
        update!(NEXT_DOCID, next_docid);
        update!(NEXT_CHUNKID, next_chunkid);
        Ok(t)
    }

    pub fn contains<P: AsRef<Path>>(&self, doc: P) -> bool {
        self.by_path.contains_key(doc.as_ref())
    }

    /// Add the specified document to the collection. The returned
    /// iterator contains the chunks of the document, the size and
    /// overlap of the chunks are controlled by the chunk_size and
    /// overlap parameters.
    ///
    /// The model should consume the iterator and embed each of the
    /// chunks, indexing them by ChunkId, which will uniquely identify
    /// both the document and the chunk.
    pub fn add_document<P: AsRef<Path>, S: AsRef<str>, F: FnMut(ChunkId, &str) -> Result<()>>(
        &mut self,
        path: P,
        summary: Option<S>,
        chunk_size: usize,
        overlap: usize,
        mut embed: F,
    ) -> Result<()> {
        self.gc();
        let decoded = self.decoder.decode(path.as_ref())?;
        let id = match self.by_path.entry(PathBuf::from(path.as_ref())) {
            Entry::Vacant(e) => *e.insert(DocId::new()),
            Entry::Occupied(_) => bail!("document {:?} already loaded", path.as_ref()),
        };
        let file = File::open(decoded.decoded_path())?;
        let map = unsafe { Mmap::map(&file)? };
        let saved = SavedDoc {
            id,
            path: decoded.original_path().to_path_buf(),
            summary: summary.map(|s| s.as_ref().into()),
            md5sum: md5::compute(&*map).0,
            chunks: vec![],
        };
        let chunks = &mut self.chunks;
        let mapped = &mut self.mapped;
        let mut doc = Doc::new(decoded, saved, file, map);
        macro_rules! fail {
            ($e:expr) => {{
                self.by_path.remove(path.as_ref());
                for cid in doc.saved.chunks {
                    chunks.remove(&cid);
                }
                return Err($e);
            }};
        }
        for chunk in iter_chunks(id, &*doc.map, chunk_size, overlap)? {
            let s = match doc.get(&chunk) {
                Ok(s) => s,
                Err(e) => fail!(e),
            };
            if let Err(e) = embed(chunk.id, s) {
                fail!(e)
            }
            doc.saved.chunks.push(chunk.id);
            chunks.insert(chunk.id, chunk);
        }
        mapped.insert(id, doc);
        Ok(())
    }

    /// Remove the specified document from the document store
    pub fn remove_document<P: AsRef<Path>>(&mut self, path: P) -> Vec<ChunkId> {
        let id = match self.by_path.remove(path.as_ref()) {
            Some(id) => id,
            None => return vec![],
        };
        let saved = match self.mapped.swap_remove(&id) {
            Some(doc) => {
                self.unmapped.remove(&id);
                doc.saved
            }
            None => match self.unmapped.remove(&id) {
                Some(saved) => saved,
                None => return vec![]
            }
        };
        for id in &saved.chunks {
            self.chunks.remove(id);
        }
        saved.chunks
    }

    fn gc(&mut self) {
        if self.mapped.len() > self.max_mapped {
            self.mapped
                .sort_by(|_, d0, _, d1| d1.last_used.cmp(&d0.last_used));
            while self.mapped.len() > 0 && self.mapped.len() > self.max_mapped {
                let (id, doc) = self.mapped.pop().unwrap();
                self.unmapped.insert(id, doc.saved);
            }
        }
    }

    /// Get a reference to the chunk identified by [id]
    pub fn get_chunk_ref<'a, I: Into<ChunkId>>(&'a mut self, id: I) -> Result<ChunkRef<'a>> {
        let id = id.into();
        let chunk = *self
            .chunks
            .get(&id)
            .ok_or_else(|| anyhow!("no such chunk {id:?}"))?;
        match self.mapped.get_mut(&chunk.doc) {
            Some(doc) => {
                doc.last_used = Utc::now();
            }
            None => match self.unmapped.get(&chunk.doc) {
                None => bail!("no document for chunk {id}"),
                Some(saved) => {
                    let decoded = self.decoder.decode(&saved.path)?;
                    let file = File::open(decoded.decoded_path())?;
                    let map = unsafe { Mmap::map(&file)? };
                    let md5sum = md5::compute(&*map).0;
                    if md5sum != saved.md5sum {
                        bail!(DocumentChanged(decoded.original_path().to_path_buf()))
                    }
                    let doc = Doc::new(
                        decoded,
                        self.unmapped.remove(&chunk.doc).unwrap(),
                        file,
                        map,
                    );
                    self.mapped.insert(chunk.doc, doc);
                    self.gc()
                }
            },
        };
        let doc = self
            .mapped
            .get(&chunk.doc)
            .ok_or_else(|| anyhow!("document isn't loaded"))?;
        Ok(ChunkRef {
            summary: doc.saved.summary.as_ref().map(|s| s.as_str()),
            text: doc.get(&chunk)?,
            original_path: doc.decoded.original_path(),
            decoded_path: doc.decoded.decoded_path(),
        })
    }

    pub(super) fn decoder_mut(&mut self) -> &mut Decoder {
        &mut self.decoder
    }
}