File size: 9,471 Bytes
be99550 | 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 | // ๐ [๋ธ๋ํ ์ฌ๊ฑด์ ์งํ์ ํ๋ก๊ทธ๋ํฝ 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;
#[derive(Clone, Debug)]
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
}
}
|