-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloader.py
More file actions
88 lines (71 loc) · 2.9 KB
/
Copy pathloader.py
File metadata and controls
88 lines (71 loc) · 2.9 KB
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
from pathlib import Path
from typing import Any
import logging
from models import Document
logger = logging.getLogger(__name__)
class DocumentLoader:
"""
Loads text documents from the specified directory.
"""
def __init__(self, data_dir: str | Path = "data") -> None:
self.data_dir = Path(data_dir)
def load(self) -> list[Document]:
documents: list[Document] = []
if not self.data_dir.exists():
logger.warning("Data directory '%s' does not exist.", self.data_dir)
self.data_dir.mkdir(parents=True, exist_ok=True)
return documents
for filepath in self.data_dir.rglob("*.txt"):
try:
with open(filepath, "r", encoding="utf-8") as f:
content = f.read()
documents.append(
Document(
title=filepath.stem,
category="unknown",
topic="general",
content=content,
metadata={"source": str(filepath)},
source_path=filepath,
)
)
except Exception as e:
logger.error("Failed to load %s: %s", filepath, e)
logger.info("Loaded %d documents.", len(documents))
return documents
import json
from pathlib import Path
from models import Document
class MemoryLoader:
"""Loads past session abstracts into the RAG pipeline."""
def __init__(self, history_dir: str = "history"):
self.history_dir = Path(history_dir)
def load_abstracts(self) -> list[Document]:
documents = []
if not self.history_dir.exists():
return documents
# Find all abstract files
for filepath in self.history_dir.rglob("*_abstract.json"):
try:
with open(filepath, "r", encoding="utf-8") as f:
data = json.load(f)
# Format the JSON into a readable text chunk for the LLM
content = (
f"Session Date: {data.get('last_updated')}\n"
f"User's Emotion: {data.get('dominant_emotion')}\n"
f"Summary: {data.get('session_summary')}\n"
f"Topics: {', '.join(data.get('key_topics', []))}"
)
documents.append(
Document(
title=f"Session {data.get('session_id')} Memory",
category="user_memory",
topic="past_conversation",
content=content,
metadata={"source": str(filepath), "type": "memory"},
source_path=filepath,
)
)
except Exception as e:
print(f"[Error] Failed to load memory {filepath}: {e}")
return documents