npcpy 1.2.19__py3-none-any.whl → 1.2.20__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- npcpy/ft/memory_trainer.py +158 -151
- {npcpy-1.2.19.dist-info → npcpy-1.2.20.dist-info}/METADATA +1 -1
- {npcpy-1.2.19.dist-info → npcpy-1.2.20.dist-info}/RECORD +6 -6
- {npcpy-1.2.19.dist-info → npcpy-1.2.20.dist-info}/WHEEL +0 -0
- {npcpy-1.2.19.dist-info → npcpy-1.2.20.dist-info}/licenses/LICENSE +0 -0
- {npcpy-1.2.19.dist-info → npcpy-1.2.20.dist-info}/top_level.txt +0 -0
npcpy/ft/memory_trainer.py
CHANGED
|
@@ -3,162 +3,169 @@ try:
|
|
|
3
3
|
import torch
|
|
4
4
|
import torch.nn as nn
|
|
5
5
|
from transformers import AutoTokenizer, AutoModelForSequenceClassification, Trainer, TrainingArguments
|
|
6
|
-
except:
|
|
7
|
-
pass
|
|
8
|
-
import json
|
|
9
|
-
from typing import List, Dict, Tuple
|
|
10
|
-
import random
|
|
11
6
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
self.tokenizer = tokenizer
|
|
16
|
-
self.max_length = max_length
|
|
17
|
-
|
|
18
|
-
def __len__(self):
|
|
19
|
-
return len(self.examples)
|
|
20
|
-
|
|
21
|
-
def __getitem__(self, idx):
|
|
22
|
-
example = self.examples[idx]
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
text = f"Memory: {example['memory']}\nContext: {example.get('context', '')}"
|
|
26
|
-
|
|
27
|
-
encoding = self.tokenizer(
|
|
28
|
-
text,
|
|
29
|
-
truncation=True,
|
|
30
|
-
padding='max_length',
|
|
31
|
-
max_length=self.max_length,
|
|
32
|
-
return_tensors='pt'
|
|
33
|
-
)
|
|
34
|
-
|
|
35
|
-
return {
|
|
36
|
-
'input_ids': encoding['input_ids'].flatten(),
|
|
37
|
-
'attention_mask': encoding['attention_mask'].flatten(),
|
|
38
|
-
'labels': torch.tensor(example['label'], dtype=torch.long)
|
|
39
|
-
}
|
|
7
|
+
import json
|
|
8
|
+
from typing import List, Dict, Tuple
|
|
9
|
+
import random
|
|
40
10
|
|
|
41
|
-
class
|
|
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
|
-
|
|
11
|
+
class MemoryDataset(Dataset):
|
|
12
|
+
def __init__(self, examples: List[Dict], tokenizer, max_length=512):
|
|
13
|
+
self.examples = examples
|
|
14
|
+
self.tokenizer = tokenizer
|
|
15
|
+
self.max_length = max_length
|
|
16
|
+
|
|
17
|
+
def __len__(self):
|
|
18
|
+
return len(self.examples)
|
|
19
|
+
|
|
20
|
+
def __getitem__(self, idx):
|
|
21
|
+
example = self.examples[idx]
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
text = f"Memory: {example['memory']}\nContext: {example.get('context', '')}"
|
|
25
|
+
|
|
26
|
+
encoding = self.tokenizer(
|
|
27
|
+
text,
|
|
28
|
+
truncation=True,
|
|
29
|
+
padding='max_length',
|
|
30
|
+
max_length=self.max_length,
|
|
31
|
+
return_tensors='pt'
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
return {
|
|
35
|
+
'input_ids': encoding['input_ids'].flatten(),
|
|
36
|
+
'attention_mask': encoding['attention_mask'].flatten(),
|
|
37
|
+
'labels': torch.tensor(example['label'], dtype=torch.long)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
class MemoryTrainer:
|
|
41
|
+
def __init__(self, model_name="google/gemma-2b", device="cpu"):
|
|
42
|
+
self.device = device
|
|
43
|
+
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
44
|
+
if self.tokenizer.pad_token is None:
|
|
45
|
+
self.tokenizer.pad_token = self.tokenizer.eos_token
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
self.model = AutoModelForSequenceClassification.from_pretrained(
|
|
49
|
+
model_name,
|
|
50
|
+
num_labels=3
|
|
51
|
+
).to(device)
|
|
52
|
+
|
|
53
|
+
def prepare_training_data(self, approved_memories: List[Dict],
|
|
54
|
+
rejected_memories: List[Dict]) -> List[Dict]:
|
|
55
|
+
"""Prepare training data from memory examples"""
|
|
56
|
+
examples = []
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
for memory in approved_memories:
|
|
60
|
+
examples.append({
|
|
61
|
+
"memory": memory.get("final_memory") or memory.get("initial_memory"),
|
|
62
|
+
"context": memory.get("context", ""),
|
|
63
|
+
"label": 1
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
for memory in rejected_memories:
|
|
68
|
+
examples.append({
|
|
81
69
|
"memory": memory.get("initial_memory"),
|
|
82
70
|
"context": memory.get("context", ""),
|
|
83
|
-
"label":
|
|
71
|
+
"label": 0
|
|
84
72
|
})
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
edited_examples = []
|
|
76
|
+
for memory in approved_memories[:len(rejected_memories)//2]:
|
|
77
|
+
if memory.get("final_memory") and memory.get("initial_memory"):
|
|
78
|
+
|
|
79
|
+
edited_examples.append({
|
|
80
|
+
"memory": memory.get("initial_memory"),
|
|
81
|
+
"context": memory.get("context", ""),
|
|
82
|
+
"label": 2
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
examples.extend(edited_examples)
|
|
86
|
+
random.shuffle(examples)
|
|
87
|
+
return examples
|
|
89
88
|
|
|
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
|
-
|
|
89
|
+
def train(self, approved_memories: List[Dict], rejected_memories: List[Dict],
|
|
90
|
+
output_dir: str = "./memory_model", epochs: int = 3):
|
|
91
|
+
"""Train the memory classification model"""
|
|
92
|
+
|
|
93
|
+
if len(approved_memories) < 10 or len(rejected_memories) < 10:
|
|
94
|
+
print("Not enough training data. Need at least 10 approved and 10 rejected memories.")
|
|
95
|
+
return False
|
|
96
|
+
|
|
97
|
+
training_data = self.prepare_training_data(approved_memories, rejected_memories)
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
split_idx = int(0.8 * len(training_data))
|
|
101
|
+
train_data = training_data[:split_idx]
|
|
102
|
+
val_data = training_data[split_idx:]
|
|
103
|
+
|
|
104
|
+
train_dataset = MemoryDataset(train_data, self.tokenizer)
|
|
105
|
+
val_dataset = MemoryDataset(val_data, self.tokenizer)
|
|
106
|
+
|
|
107
|
+
training_args = TrainingArguments(
|
|
108
|
+
output_dir=output_dir,
|
|
109
|
+
num_train_epochs=epochs,
|
|
110
|
+
per_device_train_batch_size=4,
|
|
111
|
+
per_device_eval_batch_size=4,
|
|
112
|
+
warmup_steps=100,
|
|
113
|
+
weight_decay=0.01,
|
|
114
|
+
logging_dir='./logs',
|
|
115
|
+
evaluation_strategy="epoch",
|
|
116
|
+
save_strategy="epoch",
|
|
117
|
+
load_best_model_at_end=True,
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
trainer = Trainer(
|
|
121
|
+
model=self.model,
|
|
122
|
+
args=training_args,
|
|
123
|
+
train_dataset=train_dataset,
|
|
124
|
+
eval_dataset=val_dataset,
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
trainer.train()
|
|
128
|
+
trainer.save_model()
|
|
129
|
+
self.tokenizer.save_pretrained(output_dir)
|
|
130
|
+
|
|
131
|
+
print(f"Model trained and saved to {output_dir}")
|
|
132
|
+
return True
|
|
134
133
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
134
|
+
def predict_memory_action(self, memory_content: str, context: str = "") -> Tuple[str, float]:
|
|
135
|
+
"""Predict what action to take on a memory"""
|
|
136
|
+
text = f"Memory: {memory_content}\nContext: {context}"
|
|
137
|
+
|
|
138
|
+
encoding = self.tokenizer(
|
|
139
|
+
text,
|
|
140
|
+
truncation=True,
|
|
141
|
+
padding=True,
|
|
142
|
+
max_length=512,
|
|
143
|
+
return_tensors='pt'
|
|
144
|
+
).to(self.device)
|
|
145
|
+
|
|
146
|
+
with torch.no_grad():
|
|
147
|
+
outputs = self.model(**encoding)
|
|
148
|
+
probabilities = torch.softmax(outputs.logits, dim=-1)
|
|
149
|
+
predicted_class = torch.argmax(probabilities, dim=-1).item()
|
|
150
|
+
confidence = probabilities[0][predicted_class].item()
|
|
151
|
+
|
|
152
|
+
actions = {0: "model-rejected", 1: "model-approved", 2: "needs-editing"}
|
|
153
|
+
return actions[predicted_class], confidence
|
|
155
154
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
155
|
+
def auto_approve_memory(self, memory_content: str, context: str = "",
|
|
156
|
+
confidence_threshold: float = 0.8) -> Dict:
|
|
157
|
+
"""Auto-approve memory if confidence is high enough"""
|
|
158
|
+
action, confidence = self.predict_memory_action(memory_content, context)
|
|
159
|
+
|
|
160
|
+
if confidence >= confidence_threshold:
|
|
161
|
+
return {"action": action, "confidence": confidence, "auto_processed": True}
|
|
162
|
+
else:
|
|
163
|
+
return {"action": "pending_approval", "confidence": confidence, "auto_processed": False}
|
|
164
|
+
except:
|
|
165
|
+
Dataset = None
|
|
166
|
+
nn = None
|
|
167
|
+
Trainer = None
|
|
168
|
+
TrainingArguments = None
|
|
169
|
+
|
|
170
|
+
MemoryDataset = None
|
|
171
|
+
MemoryTrainer = None
|
|
@@ -17,7 +17,7 @@ npcpy/data/web.py,sha256=ARGoVKUlQmaiX0zJbSvvFmRCwOv_Z7Pcan9c5GxYObQ,5117
|
|
|
17
17
|
npcpy/ft/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
18
18
|
npcpy/ft/diff.py,sha256=R3Qo6v0-6M1iI0wiXhUzyuYI2ja0q_0i9bE0z3coxzU,28
|
|
19
19
|
npcpy/ft/ge.py,sha256=my5LtGyVTT40V0i1h9FR-tFFA1FHSga-PeCCgUX1UUI,61
|
|
20
|
-
npcpy/ft/memory_trainer.py,sha256=
|
|
20
|
+
npcpy/ft/memory_trainer.py,sha256=QZPznxEEwXbOGroHdMUMa5xpqlNwgV6nqOazI2xgrnQ,6635
|
|
21
21
|
npcpy/ft/rl.py,sha256=l3RUkEJe4b2yB6pildveu2LJymtNq0F17COwf_CCq3U,34
|
|
22
22
|
npcpy/ft/sft.py,sha256=i4ENygRPArbLWN4XZZuBnPWaehs8M-J68JB_mewGJHI,62
|
|
23
23
|
npcpy/gen/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
@@ -41,8 +41,8 @@ npcpy/work/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
|
41
41
|
npcpy/work/desktop.py,sha256=F3I8mUtJp6LAkXodsh8hGZIncoads6c_2Utty-0EdDA,2986
|
|
42
42
|
npcpy/work/plan.py,sha256=QyUwg8vElWiHuoS-xK4jXTxxHvkMD3VkaCEsCmrEPQk,8300
|
|
43
43
|
npcpy/work/trigger.py,sha256=P1Y8u1wQRsS2WACims_2IdkBEar-iBQix-2TDWoW0OM,9948
|
|
44
|
-
npcpy-1.2.
|
|
45
|
-
npcpy-1.2.
|
|
46
|
-
npcpy-1.2.
|
|
47
|
-
npcpy-1.2.
|
|
48
|
-
npcpy-1.2.
|
|
44
|
+
npcpy-1.2.20.dist-info/licenses/LICENSE,sha256=j0YPvce7Ng9e32zYOu0EmXjXeJ0Nwawd0RA3uSGGH4E,1070
|
|
45
|
+
npcpy-1.2.20.dist-info/METADATA,sha256=P5knjysgVTcYCKlMkbJT-iiw_t9cy1SlskD2YuyknHE,26084
|
|
46
|
+
npcpy-1.2.20.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
47
|
+
npcpy-1.2.20.dist-info/top_level.txt,sha256=g1pbSvrOOncB74Bg5-J0Olg4V0A5VzDw-Xz5YObq8BU,6
|
|
48
|
+
npcpy-1.2.20.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|