-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.py
More file actions
112 lines (90 loc) · 2.79 KB
/
Copy pathschema.py
File metadata and controls
112 lines (90 loc) · 2.79 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
from __future__ import annotations
from dataclasses import dataclass, field
from typing import TYPE_CHECKING, Any
if TYPE_CHECKING:
from memory_engine.memory.domain.retrieval_result import PalaceRecallResult
@dataclass(slots=True)
class EvidenceRef:
source_path: str
section_id: str | None = None
line_start: int | None = None
line_end: int | None = None
metadata: dict[str, Any] = field(default_factory=dict)
@dataclass(slots=True)
class MemoryWeight:
importance: float = 0.0
risk: float = 0.0
novelty: float = 0.0
confidence: float = 1.0
usage_count: int = 0
decay_factor: float = 1.0
def bounded_score(self) -> float:
score = (
self.importance * 0.35
+ self.risk * 0.35
+ self.novelty * 0.15
+ self.confidence * 0.15
)
return max(0.0, min(score, 1.0))
@dataclass(slots=True)
class MemoryNode:
id: str
type: str
content: str
attributes: dict[str, Any] = field(default_factory=dict)
embedding: list[float] | None = None
weights: MemoryWeight = field(default_factory=MemoryWeight)
source_ref: EvidenceRef | None = None
@dataclass(slots=True)
class MemoryEdge:
from_id: str
to_id: str
edge_type: str
weight: float = 1.0
confidence: float = 1.0
bidirectional: bool = False
source_ref: EvidenceRef | None = None
@dataclass(slots=True)
class ActivationContext:
query: str
semantic_weight: float = 0.45
structural_weight: float = 0.15
anomaly_weight: float = 0.15
importance_weight: float = 0.1
exception_weight: float = 0.075
contradiction_weight: float = 0.075
max_hops: int = 2
@dataclass(slots=True)
class PathStep:
node_id: str
reason: str
score: float
via_edge_type: str | None = None
@dataclass(slots=True)
class ActivationTraceStep:
node_id: str
source_node_id: str | None = None
edge_type: str | None = None
hop: int = 0
incoming_activation: float = 0.0
propagated_activation: float = 0.0
activated_score: float | None = None
stopped_reason: str | None = None
is_seed: bool = False
@dataclass(slots=True)
class MemoryPath:
query: str
steps: list[PathStep] = field(default_factory=list)
activation_trace: list[ActivationTraceStep] = field(default_factory=list)
supporting_evidence: list[EvidenceRef] = field(default_factory=list)
final_answer: str = ""
final_score: float = 0.0
@dataclass(slots=True)
class RetrievalResult:
query: str
paths: list[MemoryPath] = field(default_factory=list)
palace_result: PalaceRecallResult | None = None
def best_path(self) -> MemoryPath:
if not self.paths:
raise ValueError("No retrieval paths available.")
return max(self.paths, key=lambda path: path.final_score)