cephalon 0.0.10

A library to extract information from documents, and feed it into vector database to create robust knowledge-base assistant.
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
use crate::models::model::encode_text_with_model_from_path;
#[cfg(not(feature="no-ml"))]
use crate::models::{
    model::encode_text,
    summarize_model::generate_summary,
};

use clap::builder::OsStr;
use rayon::{prelude::*, vec};


use hora::index::hnsw_idx::HNSWIndex;
use hora::core::ann_index::{
    ANNIndex,
    SerializableIndex
};
use hora::core::metrics::Metric;

use toml::{map::Map, Value};
use serde::{Serialize,Deserialize};

use crate::documents::document::{
    Document,
    get_file_text,
    get_file_list, is_supported, 
    UnsupportedDocumentError, DocType, get_text_from_docx, get_text_from_pdf, get_text_from_txt
};



use crate::database::vectordb::{
    create_index,
    load_index, 
    save_index,
};

use crate::database::sql_database::{
    create_sqlite_db,
    load_sqlite_db,
    insert_data_into_sql_db,
    sql_search_by_id
};


use std::fs::create_dir;
use std::path::PathBuf;
use std::io::ErrorKind;
use std::fmt;
use std::process::exit;



type Result<T> = std::result::Result<T, KnowledgeBaseError>;

/// SQL Error
#[cfg(not(feature="no-ml"))]
#[derive(Debug, Clone)]
pub struct KnowledgeBaseError;


impl fmt::Display for KnowledgeBaseError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "invalid sql transaction or connection")
    }
}

#[cfg(not(feature="no-ml"))]
#[derive(Debug)]
pub struct Matches{
    pub document_name:String,
    pub line:String,
}

#[cfg(not(feature="no-ml"))]
#[derive(Debug,Serialize,Deserialize)]
pub struct Cephalon{
    path:PathBuf,
    local_model:bool,
    local_model_path:Option<String>
}

#[cfg(not(feature="no-ml"))]
impl Cephalon{
    
    fn get_text_from_all_docs(&self, doc_list:&mut Vec<Document>){
        doc_list.par_iter_mut().for_each(|doc: &mut Document|{
            println!("Now Processing {:?} ...",doc.get_document_name_as_string());
            let document_data_option: Option<Vec<String>> = get_file_text(doc,256);
            match document_data_option{
                Some(doc_text)=> doc.set_document_data(doc_text),
                None=>println!("Error reading document {:?}",doc.get_document_name_as_string())
            }
            println!("\r Finished Processing file {:?}",doc.get_document_name_as_string());
        });
    }
    
    
    ///Description: This function will the current directory for all files, and store all the supported file_types as
    ///a Vector of Documents doc_list. This is done by calling the get_file_list function. Then using doc_list extract all text from the supported file types and store the data
    ///in the data attribute of Document Structure respectively.That is done by calling the get_text_from_all_docs function. 
    ///Next it will split the text for each document into an array of string. Each string will be the size of tokens that could be accepted by 
    ///an embedding model in sentence_transformer. 
    pub fn search_and_build_index(self, path:&PathBuf){
        let mut project_path: PathBuf = path.clone();
        project_path.push(".cephalon");
        //Get all the supported Documents from the directory and store it in documents
        let mut doc_list:Vec<Document>;
        match get_file_list(path){
            Some(f_list)=>{
                doc_list=f_list;
            },
            None=>{
                panic!("Unable to get a list of file!")
            }
        }

        //Extract text from all the documents in the doc_list
        self.get_text_from_all_docs(&mut doc_list);

        //Generate encodings for all the text of a document in the list doc_list\
        if self.local_model{
            let _ = Document::build_semantic_search(&mut doc_list, (*project_path).to_path_buf(), true, self.local_model_path.unwrap());
        }else{
            let _ = Document::build_semantic_search(&mut doc_list, (*project_path).to_path_buf(), false, "".to_string());
        }
        

    }

    ///Search Index for related queries, and covert it back to original text
    pub fn search(self, path:PathBuf, query:String,count:usize)->Option<Vec<Matches>>{
        let mut results:Vec<usize> = vec![];
        let mut project_path = path.clone();
        project_path.push(".cephalon");
        if self.local_model{
            match encode_text_with_model_from_path(&self.local_model_path.unwrap(), &vec![query]){ //Generate Embeddings for the query
                Some(encodings)=>{
                    for encoding in encodings{
                        let index: HNSWIndex<f32, usize> = load_index(project_path.clone());
                        match encoding.1{//Embeddings are stored in location 1 as Option<Vec<f32>>
                            Some(mut embedding)=>{
                                results.append(&mut index.search(&mut embedding, count));
                            },
                            None=>{}
                        }
                        
                    }  
                },
                None=>{
                    return None
                }
            }
        }else{
            match encode_text(&vec![query]){ //Generate Embeddings for the query
                Some(encodings)=>{
                    for encoding in encodings{
                        let index: HNSWIndex<f32, usize> = load_index(project_path.clone());
                        match encoding.1{//Embeddings are stored in location 1 as Option<Vec<f32>>
                            Some(mut embedding)=>{
                                results.append(&mut index.search(&mut embedding, count));
                            },
                            None=>{}
                        }
                        
                    }  
                },
                None=>{
                    return None
                }
            }
        }
        

        let mut search_results:Vec<Matches> = vec![];
        match sql_search_by_id(project_path,results){
            Some(search_output)=>{
                for output in search_output{
                    search_results.push(Matches{document_name:output.0, line:output.1});
                }
            },
            None=>{
                return None
            }
        }

        Some(search_results)

    }
    
}

#[cfg(not(feature="no-ml"))]
pub trait Util{
    fn new(path:PathBuf, local:bool, model_path:String)->Self;
    fn load(path:PathBuf)->Self;
}

#[cfg(not(feature="no-ml"))]
impl Util for Cephalon{

    /// Create a new Cephalon Project. path points to where the new project will be created. 
    /// local is a bool value indicating whether to use a remote model, or a local model
    /// If local is true then model_path will contain the path to the directory where all the model files are located. 
    /// If local is false then the model_path will be an empty string. 
    fn new(path:PathBuf, local:bool, model_path:String)->Cephalon{
        let mut project_path: PathBuf = path.clone();
        project_path.push(".cephalon");
        match create_dir(&project_path){
            Ok(_msg)=>println!("Created project folder"),
            Err(err)=> {
                if err.kind() == ErrorKind::AlreadyExists{
                    println!("Loading Cephalon from previous project")
                }else{
                    panic!("Error creating cephalon project: {:?}",err)
                }
            }
        }

        //Create the index to be saved in .cephalon
        let _index: HNSWIndex<f32, usize> = create_index((*project_path).to_path_buf(),384);
        
        //Create the sqlite database to be saved in .cephalon
        let conn = create_sqlite_db((*project_path).to_path_buf());
        match conn.close(){
            Ok(_c)=>println!("Successfully created database"),
            Err(err)=>panic!("Error close database connection: {:?}",err)
        }

        let cephy: Cephalon;

        if local {
            cephy = Cephalon{path:path.to_path_buf(),local_model:local, local_model_path:Some(model_path)};
        }else{
            cephy = Cephalon{path:path.to_path_buf(),local_model:local, local_model_path:None};
        }
        

        let cephy_toml = toml::to_string(&cephy).expect("Could not encode TOML value");

        project_path.push("cephalon.toml");
        
        std::fs::write(project_path, cephy_toml).expect("Error writing to .toml file");

        cephy

    }

    /// Load an existing Cephalon project by reading the cephalon.toml file and returning the resulting 
    /// Cephalon object. 
    fn load(path:PathBuf)->Cephalon{
        let mut project_path: PathBuf = path.clone();
        project_path.push(".cephalon");
        project_path.push("cephalon.toml");
        let data = match std::fs::read_to_string(project_path){
            Ok(d)=>d,
            Err(err)=>{
                println!("Error reading cephalon.toml file: {:?}",err);
                exit(1);
            }
        };
        let cephy:Cephalon = match toml::from_str(&data){
            Ok(c)=>c,
            Err(err)=>{
                println!("Error Generating Cephalon object from cephalon.toml file: {:?}",err);
                exit(1)
            }
        };
        cephy
    }
}

#[cfg(not(feature="no-ml"))]
pub trait DocumentEncoder{
    fn build_semantic_search(doc_list:&mut Vec<Document>, project_path:PathBuf, local:bool, model_path:String)->Result<()>;
    fn encode_text_via_model(&self, model:&str, local:bool, model_path:&String)->Option<Vec<(String,Option<Vec<f32>>)>>;
    fn load(file_path:String)->Result<Document>;
    fn summarize(&self)->Result<Vec<String>>;
}

#[cfg(not(feature="no-ml"))]
impl DocumentEncoder for Document{

    /// Building Semantic Search for a vector of documents. 
    fn build_semantic_search(doc_list:&mut Vec<Document>, project_path:PathBuf, local:bool, model_path:String)->Result<()>{
        // We iterate through documents, generate the embeddings, and add the embeddings to index. 
        //Get the index
        let mut index:HNSWIndex<f32,usize> = create_index(project_path.clone(), 384);
        let mut id:usize = 0;


        for doc in doc_list{
            match doc.encode_text_via_model("all-MiniLM-L6-v2", local, &model_path){
                Some(vector_embeddings)=>{
                    let sentences:&Vec<String>; 
                    match doc.get_document_data(){
                        Some(data)=>{
                            sentences = data;
                            for embedding_data in vector_embeddings{
                                id+=1;
                                match embedding_data.1{
                                    Some(embedding)=>{
                                        //Insert it into the index
                                        match index.add(&embedding, id){
                                            Ok(_msg)=>{},
                                            Err(err)=>{
                                                println!("Error: {}, on id:{}",err,id);
                                            }
                                        }

                                        //Insert it into sql db for text retreival 
                                        match insert_data_into_sql_db(project_path.clone() ,&doc.get_document_name_as_string().unwrap(),&embedding_data.0,id){
                                            Ok(_msg)=>{},
                                            Err(err)=>{
                                                println!("Error inserting line:{} due to error:{:?}",embedding_data.0, err);
                                            }
                                }

                                    },
                                    None=>{

                                    }
                                }
                            }
                        },
                        None=>{
                            println!("No Text found for file:{}",doc.get_document_name_as_string().unwrap());
                            continue
                        }
                    }

                },
                None=>{
                    println!("Error generating embeddings for: {:?}",doc.get_document_name_as_string().unwrap());
                    continue
                }
            }
        }

        save_index(&mut index, project_path);


        Ok(())
    }

    /// Encode text of the current document via a sentence embedding model.
    /// If the model was unable to encode the text into vector embeddings then 
    /// none is returned. 
    fn encode_text_via_model(&self, _model:&str, local:bool, model_path:&String)->Option<Vec<(String,Option<Vec<f32>>)>>{
        let mut encodings:Vec<Vec<f32>> = vec![];
        let sentences:Vec<String>;
        match self.get_document_data(){
            Some(vec_string)=>{
                sentences = vec_string.to_vec();
            },
            None=>{
                println!("Document has no parsed data");
                return None
            }
        }
        if local{
            match encode_text_with_model_from_path(model_path,&sentences){
                Some(embedded_sentences)=>{
                    return Some(embedded_sentences)
                },
                None=>{
                    println!("Unable to generate Embeddings for document:{:?}",self.get_document_name_as_string());
                    return None
                }
        }
        }else{
            match encode_text(&sentences){
                Some(embedded_sentences)=>{
                    return Some(embedded_sentences)
                },
                None=>{
                    println!("Unable to generate Embeddings for document:{:?}",self.get_document_name_as_string());
                    return None
                }
        }
        }
        
    }

    fn load(file_path:String)->Result<Document>{

        let file_metadata:std::fs::Metadata;

        match std::fs::metadata(&file_path){
            Ok(mdata)=>{
                file_metadata= mdata;
            },
            Err(err)=>{
                return Err(KnowledgeBaseError)
            }
        }

        if !file_metadata.is_file(){//If the Path is not a Document return an error
            return Err(KnowledgeBaseError)
        }
        let file_name:String;
        match std::path::Path::new(&file_path).file_name(){
            Some(name)=>{
                file_name = name.to_string_lossy().to_string();
            },
            None=>{
                return Err(KnowledgeBaseError)
            }
        }
        let document:Document = Document{// 
            name:file_name,
            path:PathBuf::from(file_path),
            metadata:file_metadata,
            data:None,
            encodings:None
        };
        Ok(document)
    }

    fn summarize(&self)->Result<Vec<String>> {
        let doc_text:String;
        
        let summary:Vec<String>;
        match self.get_document_data_as_string(){
            Ok(doc_text)=>{
                match generate_summary(doc_text){
                    Ok(doc_summary)=>{
                        summary = doc_summary;
                    },
                    Err(err)=>{
                        return Err(KnowledgeBaseError)
                    }
                }
            },
            Err(err)=>{
                return Err(KnowledgeBaseError)
            }
            
        }
        
        Ok(summary)
    }

}