minseok
๐ Release BioPhys 6.0 Grand Master: 16GB (14.89GB) Gemma-4 100% Devour, Ecosystem Evolution, Solar MoE, SNN Autoregressive SDK, Dynamic PhaseVM
be99550 | // ๐ [๋ธ๋ํ ์ฌ๊ฑด์ ์งํ์ ํ๋ก๊ทธ๋ํฝ RAG] (src/dynamic_knowledge_rag.rs) | |
| // ์ด๋ก ๊ธฐ๋ฐ: ๋ฒ ์ผ์ํ์ธ-ํธํน ์ ๋ณด ํ๊ณ(Bekenstein Bound) & 't Hooft-Susskind ํ๋ก๊ทธ๋ํฝ ์๋ฆฌ | |
| use std::fs::{self, File, OpenOptions}; | |
| use std::io::{BufRead, BufReader, Write}; | |
| use std::path::Path; | |
| pub struct DynamicDocument { | |
| pub id: usize, | |
| pub title: String, | |
| pub category: String, | |
| pub content: String, | |
| pub horizon_x: u8, // 256x256 ์ฌ๊ฑด์ ์งํ์ 2D X ์ขํ | |
| pub horizon_y: u8, // 256x256 ์ฌ๊ฑด์ ์งํ์ 2D Y ์ขํ | |
| pub hawking_mass: f32, // ์ค๋ ฅ ํฌํ ์ ์ง๋ (์ ๋ณด ๋ฐ๋) | |
| pub keywords: Vec<String>, | |
| } | |
| pub struct DynamicKnowledgeRAG { | |
| pub documents: Vec<DynamicDocument>, | |
| pub corpus_dir: String, | |
| pub horizon_grid: [[f32; 256]; 256], // 2D ์ฌ๊ฑด์ ์งํ์ ์ค๋ ฅ ๊ณก๋ฅ ํ ์ | |
| } | |
| impl DynamicKnowledgeRAG { | |
| pub fn new(corpus_dir: &str) -> Self { | |
| let mut rag = DynamicKnowledgeRAG { | |
| documents: Vec::new(), | |
| corpus_dir: corpus_dir.to_string(), | |
| horizon_grid: [[0.0; 256]; 256], | |
| }; | |
| if !Path::new(corpus_dir).exists() { | |
| let _ = fs::create_dir_all(corpus_dir); | |
| } | |
| rag.initialize_default_corpus(); | |
| rag.reload_all_documents(); | |
| rag | |
| } | |
| fn initialize_default_corpus(&self) { | |
| let sample_file = format!("{}/world_knowledge_2026.jsonl", self.corpus_dir); | |
| if !Path::new(&sample_file).exists() { | |
| if let Ok(mut f) = File::create(&sample_file) { | |
| let initial_data = r#"{"id":1,"category":"์ต์ IT","title":"2026 ์ฐจ์ธ๋ ์์-๊ดํ AI ๊ฐ์๊ธฐ QuantumX-9000","content":"QuantumX-9000์ 2026๋ ์ถ์๋ ์ธ๊ณ ์ต์ด์ ์์จ ์ค๋ฆฌ์ฝ ํฌํ ๋์ค ๊ธฐ๋ฐ ์์ ๊ฐ์ญ ๊ฐ์๊ธฐ๋ก, ํ ๋ผํ๋กญ์ค๋น 0.05W์ ๊ทน์ ์ ๋ ฅ์ ์๋ชจํ๋ฉฐ 1.58๋นํธ ์ฐ์ฐ ์ ์ฉ ๊ดํ ํ ์ ์ฝ์ด๋ฅผ ๋ด์ฅํจ."} | |
| {"id":2,"category":"์ฐ์ฃผํ์ฌ","title":"2026 ์๋ฅดํ ๋ฏธ์ค 3ํธ ์ ์ธ ๋ฌ ์ฐฉ๋ฅ ๋ฐ ์๊ตฌ ๊ธฐ์ง ๊ฑด์ค","content":"2026๋ ์ธ๋ฅ๋ 50์ฌ ๋ ๋ง์ ๋ฌ ๋จ๊ทน ์์ด์ปจ ๋ถ์ง์ ์ ์ธ ์ฐฉ๋ฅํ์ฌ ์ผ์ ์์์์ ์ฑ๊ตดํ๊ณ ์๊ตฌ์ ์ธ ์ ์ธ ์ฐ๊ตฌ ๊ธฐ์ง ์๋ฅดํ ๋ฏธ์ค ๋ฒ ์ด์ค์บ ํ๋ฅผ ๊ณต์ ๊ฐ๋ํจ."} | |
| {"id":3,"category":"์๋ช ๊ณตํ","title":"2026 ๋๋ ธ๋ก๋ด ๊ธฐ๋ฐ ํ์ ์์ธํฌ ์ง์ ์ ๊ฑฐ ์์ ์ฑ๊ณต","content":"2026๋ ํ๊ด์ ํ๊ณ ์ด๋ํ๋ฉฐ ํน์ ์์ธํฌ ํ๋ฉด ํญ์๋ง์ ๊ฐ์งํด ์ ํํ ํ๊ดดํ๋ ์์จ ์ ๋ DNA ์ค๋ฆฌ๊ฐ๋ฏธ ๋๋ ธ๋ก๋ด์ 3์ ์์ ์ํ์ด ์ธ๊ณ ์ต์ด๋ก ์น์ธ๋จ."} | |
| "#; | |
| let _ = f.write_all(initial_data.as_bytes()); | |
| } | |
| } | |
| } | |
| pub fn reload_all_documents(&mut self) { | |
| self.documents.clear(); | |
| self.horizon_grid = [[0.0; 256]; 256]; | |
| if let Ok(entries) = fs::read_dir(&self.corpus_dir) { | |
| for entry in entries.flatten() { | |
| let path = entry.path(); | |
| if path.extension().and_then(|s| s.to_str()) == Some("jsonl") { | |
| if let Ok(file) = File::open(&path) { | |
| let reader = BufReader::new(file); | |
| for line in reader.lines().flatten() { | |
| if line.trim().is_empty() { continue; } | |
| if let Some(mut doc) = self.parse_jsonl_line(&line) { | |
| // ํ๋ก๊ทธ๋ํฝ ์ฌ๊ฑด์ ์งํ์ 2D ์ขํ ํฌ์ | |
| let (hx, hy, mass) = Self::compute_holographic_projection(&doc.title, &doc.content); | |
| doc.horizon_x = hx; | |
| doc.horizon_y = hy; | |
| doc.hawking_mass = mass; | |
| self.horizon_grid[hy as usize][hx as usize] += mass; | |
| self.documents.push(doc); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| /// [ํ๋ก๊ทธ๋ํฝ ํฌ์]: ๊ณ ์ฐจ์ ํ ์คํธ๋ฅผ 2D ์งํ์ 256x256 ์ขํ์ ํธํน ์ค๋ ฅ ๊ณก๋ฅ ๋ก ํฌ์ | |
| fn compute_holographic_projection(title: &str, content: &str) -> (u8, u8, f32) { | |
| let mut hx: u32 = 0; | |
| let mut hy: u32 = 0; | |
| for (i, b) in title.bytes().enumerate() { | |
| hx = (hx * 31 + b as u32 + i as u32) % 256; | |
| } | |
| for (i, b) in content.bytes().enumerate() { | |
| hy = (hy * 37 + b as u32 + (i * 7) as u32) % 256; | |
| } | |
| let mass = (content.len() as f32).ln().max(1.0); | |
| (hx as u8, hy as u8, mass) | |
| } | |
| fn parse_jsonl_line(&self, line: &str) -> Option<DynamicDocument> { | |
| let get_val = |key: &str| -> String { | |
| if let Some(pos) = line.find(&format!("\"{}\":", key)) { | |
| let rest = &line[pos + key.len() + 3..]; | |
| if rest.starts_with('"') { | |
| if let Some(end) = rest[1..].find('"') { | |
| return rest[1..1 + end].to_string(); | |
| } | |
| } else if let Some(end) = rest.find(|c: char| c == ',' || c == '}') { | |
| return rest[..end].trim().to_string(); | |
| } | |
| } | |
| String::new() | |
| }; | |
| let title = get_val("title"); | |
| let category = get_val("category"); | |
| let content = get_val("content"); | |
| let id_str = get_val("id"); | |
| let id = id_str.parse::<usize>().unwrap_or(self.documents.len() + 1); | |
| if !title.is_empty() && !content.is_empty() { | |
| let mut keywords = Vec::new(); | |
| for word in content.split_whitespace().chain(title.split_whitespace()) { | |
| let clean: String = word.chars().filter(|c| c.is_alphanumeric()).collect(); | |
| if clean.len() >= 2 { | |
| keywords.push(clean.to_lowercase()); | |
| } | |
| } | |
| Some(DynamicDocument { id, title, category, content, horizon_x: 0, horizon_y: 0, hawking_mass: 1.0, keywords }) | |
| } else { | |
| None | |
| } | |
| } | |
| /// [์ค์๊ฐ ์ง์ ์ฃผ์ ]: ์ฌ๊ฑด์ ์งํ์ ์ ์ฆ์ ๋ธ๋ํ ์ค๋ ฅ ์ฐ๋ฌผ ํ์ฑ | |
| pub fn ingest_new_knowledge(&mut self, category: &str, title: &str, content: &str) { | |
| let doc_id = self.documents.len() + 1; | |
| let (hx, hy, mass) = Self::compute_holographic_projection(title, content); | |
| let mut keywords = Vec::new(); | |
| for word in content.split_whitespace().chain(title.split_whitespace()) { | |
| let clean: String = word.chars().filter(|c| c.is_alphanumeric()).collect(); | |
| if clean.len() >= 2 { | |
| keywords.push(clean.to_lowercase()); | |
| } | |
| } | |
| let doc = DynamicDocument { | |
| id: doc_id, | |
| title: title.to_string(), | |
| category: category.to_string(), | |
| content: content.to_string(), | |
| horizon_x: hx, | |
| horizon_y: hy, | |
| hawking_mass: mass, | |
| keywords, | |
| }; | |
| self.horizon_grid[hy as usize][hx as usize] += mass; | |
| self.documents.push(doc.clone()); | |
| let sample_file = format!("{}/world_knowledge_2026.jsonl", self.corpus_dir); | |
| if let Ok(mut f) = OpenOptions::new().create(true).append(true).open(&sample_file) { | |
| let line = format!("{{\"id\":{},\"category\":\"{}\",\"title\":\"{}\",\"content\":\"{}\"}}\n", doc_id, category, title, content); | |
| let _ = f.write_all(line.as_bytes()); | |
| } | |
| } | |
| /// [๋ธ๋ํ ์ค๋ ฅ ํฌํ ์ ์ธก์ง์ ๊ฒ์ (Holographic Geodesic RAG)]: | |
| /// ์ง์๊ฐ ์๊ณต๊ฐ ์งํ์ ์ ๋์ ธ์ก์ ๋, ๊ฐ์ฅ ๊ฐํ ์ค๋ ฅ ์ฐ๋ฌผ(Singularity Well)๋ก ๋นจ๋ ค ๋ค์ด๊ฐ๋ ๋ฌธ์๋ฅผ ์ฆ์ ์ธ์ถ! | |
| pub fn search_documents(&self, query: &str, top_k: usize) -> Vec<(&DynamicDocument, f32)> { | |
| let (qx, qy, _qmass) = Self::compute_holographic_projection(query, query); | |
| let query_lower = query.to_lowercase(); | |
| let query_words: Vec<String> = query_lower | |
| .split_whitespace() | |
| .map(|w| w.chars().filter(|c| c.is_alphanumeric()).collect()) | |
| .filter(|w: &String| w.len() >= 2) | |
| .collect(); | |
| let mut scored: Vec<(&DynamicDocument, f32)> = self.documents.iter().map(|doc| { | |
| // 1. ์ฌ๊ฑด์ ์งํ์ 2D ๊ณต๊ฐ ๊ฑฐ๋ฆฌ (์ธก์ง์ ์ธ๋ ฅ) | |
| let dx = (doc.horizon_x as f32 - qx as f32).abs(); | |
| let dy = (doc.horizon_y as f32 - qy as f32).abs(); | |
| let dist_sq = dx * dx + dy * dy + 1.0; | |
| let gravitational_pull = (doc.hawking_mass * 100.0) / dist_sq; | |
| // 2. ์๋ฏธ๋ก ์ ํค์๋ ๊ณต๋ช | |
| let mut semantic_score: f32 = 0.0; | |
| if doc.title.to_lowercase().contains(&query_lower) { | |
| semantic_score += 20.0; | |
| } | |
| for qw in &query_words { | |
| if doc.title.to_lowercase().contains(qw) { | |
| semantic_score += 10.0; | |
| } | |
| if doc.content.to_lowercase().contains(qw) { | |
| semantic_score += 5.0; | |
| } | |
| } | |
| let total_score = gravitational_pull * 0.3 + semantic_score * 0.7; | |
| (doc, total_score) | |
| }).filter(|(_, score)| *score > 0.0).collect(); | |
| scored.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal)); | |
| scored.truncate(top_k); | |
| scored | |
| } | |
| } | |