|
|
import pandas as pd |
|
|
from datetime import datetime |
|
|
import hashlib |
|
|
import os |
|
|
|
|
|
class MemoryEngine: |
|
|
def __init__(self, |
|
|
memory_path="SegmentMemory.csv", |
|
|
short_term_path="ShortTermMemory.csv", |
|
|
history_path="SegmentHistory.csv", |
|
|
conflict_path="SegmentConflictMap.csv"): |
|
|
self.memory_path = memory_path |
|
|
self.short_term_path = short_term_path |
|
|
self.history_path = history_path |
|
|
self.conflict_path = conflict_path |
|
|
|
|
|
self.memory_df = self._load_csv(memory_path) |
|
|
self.short_term_df = self._load_csv(short_term_path) |
|
|
self.history_df = self._load_csv(history_path) |
|
|
self.conflict_df = self._load_csv(conflict_path) |
|
|
|
|
|
def _load_csv(self, path): |
|
|
return pd.read_csv(path) if os.path.exists(path) else pd.DataFrame() |
|
|
|
|
|
def _generate_segment_id(self): |
|
|
existing_ids = self.memory_df['SegmentID'].astype(int) if not self.memory_df.empty else [] |
|
|
return str(max(existing_ids) + 1 if len(existing_ids) > 0 else 1).zfill(4) |
|
|
|
|
|
def _hash_text(self, text): |
|
|
return hashlib.sha256(text.encode()).hexdigest()[:12] |
|
|
|
|
|
def insert_segment(self, raw_text, concepts, terms, structure, datapoints, comparisons, applications, agent_id="system"): |
|
|
segment_id = self._generate_segment_id() |
|
|
hash_val = self._hash_text(raw_text) |
|
|
|
|
|
|
|
|
if hash_val in self.memory_df.get("HashChecksum", []): |
|
|
print("Duplicate detected. Skipping.") |
|
|
return |
|
|
|
|
|
|
|
|
trust_score = 0.75 if "error" not in raw_text.lower() else -0.4 |
|
|
entropy = 0.5 |
|
|
relevance = 0.6 |
|
|
|
|
|
|
|
|
conflict_rows = self.memory_df[self.memory_df["RawText"].str.contains(raw_text.split(" ")[0], case=False, na=False)] |
|
|
if not conflict_rows.empty: |
|
|
for _, row in conflict_rows.iterrows(): |
|
|
self._add_conflict(row["SegmentID"], segment_id, agent_id) |
|
|
return |
|
|
|
|
|
|
|
|
new_row = { |
|
|
"SegmentID": segment_id, |
|
|
"RawText": raw_text, |
|
|
"Concepts": concepts, |
|
|
"Terms": terms, |
|
|
"Structure": structure, |
|
|
"DataPoints": datapoints, |
|
|
"Comparisons": comparisons, |
|
|
"Applications": applications, |
|
|
"Links": "", |
|
|
"Entropy": entropy, |
|
|
"TrustScore": trust_score, |
|
|
"RelevanceScore": relevance, |
|
|
"RecallCount": 0, |
|
|
"LastUsed": "", |
|
|
"ConflictsWith": "", |
|
|
"Verified": trust_score > 0.5, |
|
|
"HashChecksum": hash_val |
|
|
} |
|
|
|
|
|
self.memory_df = pd.concat([self.memory_df, pd.DataFrame([new_row])], ignore_index=True) |
|
|
self.memory_df.to_csv(self.memory_path, index=False) |
|
|
|
|
|
self._log_history(agent_id, segment_id, "insert", trust_score, trust_score, relevance, relevance, entropy, entropy, "manual_insert", False) |
|
|
self._add_to_short_term(agent_id, segment_id, raw_text, relevance, trust_score) |
|
|
|
|
|
print(f"Segment {segment_id} inserted.") |
|
|
|
|
|
def _log_history(self, agent_id, seg_id, action, c_before, c_after, r_before, r_after, e_before, e_after, context, conflict): |
|
|
log_entry = { |
|
|
"Timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"), |
|
|
"AgentID": agent_id, |
|
|
"SegmentID": seg_id, |
|
|
"ActionType": action, |
|
|
"ConfidenceBefore": c_before, |
|
|
"ConfidenceAfter": c_after, |
|
|
"RelevanceBefore": r_before, |
|
|
"RelevanceAfter": r_after, |
|
|
"EntropyBefore": e_before, |
|
|
"EntropyAfter": e_after, |
|
|
"UsedInContext": context, |
|
|
"ConflictTriggered": conflict, |
|
|
"Notes": "" |
|
|
} |
|
|
self.history_df = pd.concat([self.history_df, pd.DataFrame([log_entry])], ignore_index=True) |
|
|
self.history_df.to_csv(self.history_path, index=False) |
|
|
|
|
|
def _add_conflict(self, seg_a, seg_b, agent_id): |
|
|
entry = { |
|
|
"SegmentA": seg_a, |
|
|
"SegmentB": seg_b, |
|
|
"ConflictType": "Concept Overlap", |
|
|
"ConflictScore": 0.8, |
|
|
"ResolutionStrategy": "Manual Review", |
|
|
"WinningSegment": "", |
|
|
"LastChecked": datetime.now().strftime("%Y-%m-%d %H:%M:%S"), |
|
|
"ResolutionStatus": "Pending Review", |
|
|
"AgentTrigger": agent_id, |
|
|
"Notes": "Auto conflict detection" |
|
|
} |
|
|
self.conflict_df = pd.concat([self.conflict_df, pd.DataFrame([entry])], ignore_index=True) |
|
|
self.conflict_df.to_csv(self.conflict_path, index=False) |
|
|
print(f"Conflict detected between {seg_a} and {seg_b}. Routed to conflict map.") |
|
|
|
|
|
def _add_to_short_term(self, agent_id, seg_id, raw_text, relevance, confidence): |
|
|
row = { |
|
|
"AgentID": agent_id, |
|
|
"SegmentID": seg_id, |
|
|
"RawText": raw_text, |
|
|
"Priority": relevance, |
|
|
"ActivationTime": datetime.now().strftime("%Y-%m-%d %H:%M:%S"), |
|
|
"ExpiresAt": "", |
|
|
"DecayRate": 0.03, |
|
|
"RecencyScore": 1.0, |
|
|
"FocusLock": False, |
|
|
"UsageContext": "manual_insert", |
|
|
"ConfidenceScore": confidence, |
|
|
"LastInteraction": datetime.now().strftime("%Y-%m-%d %H:%M:%S") |
|
|
} |
|
|
self.short_term_df = pd.concat([self.short_term_df, pd.DataFrame([row])], ignore_index=True) |
|
|
self.short_term_df.to_csv(self.short_term_path, index=False) |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
brain = MemoryEngine() |
|
|
brain.insert_segment( |
|
|
"Hydrogen is the first element on the periodic table.", |
|
|
"hydrogen, atomic number 1", |
|
|
"hydrogen, element, periodic", |
|
|
"Chemistry > Elements", |
|
|
"Atomic number: 1", |
|
|
"Compared with helium", |
|
|
"Used in gas balloons" |
|
|
) |
|
|
|