memorisdk 1.0.0__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.
Potentially problematic release.
This version of memorisdk might be problematic. Click here for more details.
- memoriai/__init__.py +140 -0
- memoriai/agents/__init__.py +7 -0
- memoriai/agents/conscious_agent.py +506 -0
- memoriai/agents/memory_agent.py +322 -0
- memoriai/agents/retrieval_agent.py +579 -0
- memoriai/config/__init__.py +14 -0
- memoriai/config/manager.py +281 -0
- memoriai/config/settings.py +287 -0
- memoriai/core/__init__.py +6 -0
- memoriai/core/database.py +966 -0
- memoriai/core/memory.py +1349 -0
- memoriai/database/__init__.py +5 -0
- memoriai/database/connectors/__init__.py +9 -0
- memoriai/database/connectors/mysql_connector.py +159 -0
- memoriai/database/connectors/postgres_connector.py +158 -0
- memoriai/database/connectors/sqlite_connector.py +148 -0
- memoriai/database/queries/__init__.py +15 -0
- memoriai/database/queries/base_queries.py +204 -0
- memoriai/database/queries/chat_queries.py +157 -0
- memoriai/database/queries/entity_queries.py +236 -0
- memoriai/database/queries/memory_queries.py +178 -0
- memoriai/database/templates/__init__.py +0 -0
- memoriai/database/templates/basic_template.py +0 -0
- memoriai/database/templates/schemas/__init__.py +0 -0
- memoriai/integrations/__init__.py +68 -0
- memoriai/integrations/anthropic_integration.py +194 -0
- memoriai/integrations/litellm_integration.py +11 -0
- memoriai/integrations/openai_integration.py +273 -0
- memoriai/scripts/llm_text.py +50 -0
- memoriai/tools/__init__.py +5 -0
- memoriai/tools/memory_tool.py +544 -0
- memoriai/utils/__init__.py +89 -0
- memoriai/utils/exceptions.py +418 -0
- memoriai/utils/helpers.py +433 -0
- memoriai/utils/logging.py +204 -0
- memoriai/utils/pydantic_models.py +258 -0
- memoriai/utils/schemas.py +0 -0
- memoriai/utils/validators.py +339 -0
- memorisdk-1.0.0.dist-info/METADATA +386 -0
- memorisdk-1.0.0.dist-info/RECORD +44 -0
- memorisdk-1.0.0.dist-info/WHEEL +5 -0
- memorisdk-1.0.0.dist-info/entry_points.txt +2 -0
- memorisdk-1.0.0.dist-info/licenses/LICENSE +203 -0
- memorisdk-1.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Base database queries and schema operations
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from abc import ABC, abstractmethod
|
|
6
|
+
from typing import Dict
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class BaseQueries(ABC):
|
|
10
|
+
"""Abstract base class for database queries"""
|
|
11
|
+
|
|
12
|
+
@abstractmethod
|
|
13
|
+
def get_table_creation_queries(self) -> Dict[str, str]:
|
|
14
|
+
"""Return dictionary of table creation SQL statements"""
|
|
15
|
+
pass
|
|
16
|
+
|
|
17
|
+
@abstractmethod
|
|
18
|
+
def get_index_creation_queries(self) -> Dict[str, str]:
|
|
19
|
+
"""Return dictionary of index creation SQL statements"""
|
|
20
|
+
pass
|
|
21
|
+
|
|
22
|
+
@abstractmethod
|
|
23
|
+
def get_trigger_creation_queries(self) -> Dict[str, str]:
|
|
24
|
+
"""Return dictionary of trigger creation SQL statements"""
|
|
25
|
+
pass
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class SchemaQueries:
|
|
29
|
+
"""Schema management queries"""
|
|
30
|
+
|
|
31
|
+
TABLE_CREATION = {
|
|
32
|
+
"chat_history": """
|
|
33
|
+
CREATE TABLE IF NOT EXISTS chat_history (
|
|
34
|
+
chat_id TEXT PRIMARY KEY,
|
|
35
|
+
user_input TEXT NOT NULL,
|
|
36
|
+
ai_output TEXT NOT NULL,
|
|
37
|
+
model TEXT NOT NULL,
|
|
38
|
+
timestamp TIMESTAMP NOT NULL,
|
|
39
|
+
session_id TEXT NOT NULL,
|
|
40
|
+
namespace TEXT NOT NULL DEFAULT 'default',
|
|
41
|
+
tokens_used INTEGER DEFAULT 0,
|
|
42
|
+
metadata TEXT DEFAULT '{}'
|
|
43
|
+
)
|
|
44
|
+
""",
|
|
45
|
+
"short_term_memory": """
|
|
46
|
+
CREATE TABLE IF NOT EXISTS short_term_memory (
|
|
47
|
+
memory_id TEXT PRIMARY KEY,
|
|
48
|
+
chat_id TEXT,
|
|
49
|
+
processed_data TEXT NOT NULL,
|
|
50
|
+
importance_score REAL NOT NULL DEFAULT 0.5,
|
|
51
|
+
category_primary TEXT NOT NULL,
|
|
52
|
+
retention_type TEXT NOT NULL DEFAULT 'short_term',
|
|
53
|
+
namespace TEXT NOT NULL DEFAULT 'default',
|
|
54
|
+
created_at TIMESTAMP NOT NULL,
|
|
55
|
+
expires_at TIMESTAMP,
|
|
56
|
+
access_count INTEGER DEFAULT 0,
|
|
57
|
+
last_accessed TIMESTAMP,
|
|
58
|
+
searchable_content TEXT NOT NULL,
|
|
59
|
+
summary TEXT NOT NULL,
|
|
60
|
+
FOREIGN KEY (chat_id) REFERENCES chat_history (chat_id)
|
|
61
|
+
)
|
|
62
|
+
""",
|
|
63
|
+
"long_term_memory": """
|
|
64
|
+
CREATE TABLE IF NOT EXISTS long_term_memory (
|
|
65
|
+
memory_id TEXT PRIMARY KEY,
|
|
66
|
+
original_chat_id TEXT,
|
|
67
|
+
processed_data TEXT NOT NULL,
|
|
68
|
+
importance_score REAL NOT NULL DEFAULT 0.5,
|
|
69
|
+
category_primary TEXT NOT NULL,
|
|
70
|
+
retention_type TEXT NOT NULL DEFAULT 'long_term',
|
|
71
|
+
namespace TEXT NOT NULL DEFAULT 'default',
|
|
72
|
+
created_at TIMESTAMP NOT NULL,
|
|
73
|
+
access_count INTEGER DEFAULT 0,
|
|
74
|
+
last_accessed TIMESTAMP,
|
|
75
|
+
searchable_content TEXT NOT NULL,
|
|
76
|
+
summary TEXT NOT NULL,
|
|
77
|
+
novelty_score REAL DEFAULT 0.5,
|
|
78
|
+
relevance_score REAL DEFAULT 0.5,
|
|
79
|
+
actionability_score REAL DEFAULT 0.5
|
|
80
|
+
)
|
|
81
|
+
""",
|
|
82
|
+
"rules_memory": """
|
|
83
|
+
CREATE TABLE IF NOT EXISTS rules_memory (
|
|
84
|
+
rule_id TEXT PRIMARY KEY,
|
|
85
|
+
rule_text TEXT NOT NULL,
|
|
86
|
+
rule_type TEXT NOT NULL,
|
|
87
|
+
priority INTEGER DEFAULT 5,
|
|
88
|
+
active BOOLEAN DEFAULT 1,
|
|
89
|
+
context_conditions TEXT,
|
|
90
|
+
namespace TEXT NOT NULL DEFAULT 'default',
|
|
91
|
+
created_at TIMESTAMP NOT NULL,
|
|
92
|
+
updated_at TIMESTAMP NOT NULL,
|
|
93
|
+
processed_data TEXT,
|
|
94
|
+
metadata TEXT DEFAULT '{}'
|
|
95
|
+
)
|
|
96
|
+
""",
|
|
97
|
+
"memory_entities": """
|
|
98
|
+
CREATE TABLE IF NOT EXISTS memory_entities (
|
|
99
|
+
entity_id TEXT PRIMARY KEY,
|
|
100
|
+
memory_id TEXT NOT NULL,
|
|
101
|
+
memory_type TEXT NOT NULL,
|
|
102
|
+
entity_type TEXT NOT NULL,
|
|
103
|
+
entity_value TEXT NOT NULL,
|
|
104
|
+
relevance_score REAL NOT NULL DEFAULT 0.5,
|
|
105
|
+
entity_context TEXT,
|
|
106
|
+
namespace TEXT NOT NULL DEFAULT 'default',
|
|
107
|
+
created_at TIMESTAMP NOT NULL
|
|
108
|
+
)
|
|
109
|
+
""",
|
|
110
|
+
"memory_relationships": """
|
|
111
|
+
CREATE TABLE IF NOT EXISTS memory_relationships (
|
|
112
|
+
relationship_id TEXT PRIMARY KEY,
|
|
113
|
+
source_memory_id TEXT NOT NULL,
|
|
114
|
+
target_memory_id TEXT NOT NULL,
|
|
115
|
+
relationship_type TEXT NOT NULL,
|
|
116
|
+
strength REAL NOT NULL DEFAULT 0.5,
|
|
117
|
+
reasoning TEXT,
|
|
118
|
+
namespace TEXT NOT NULL DEFAULT 'default',
|
|
119
|
+
created_at TIMESTAMP NOT NULL
|
|
120
|
+
)
|
|
121
|
+
""",
|
|
122
|
+
"memory_search_fts": """
|
|
123
|
+
CREATE VIRTUAL TABLE IF NOT EXISTS memory_search_fts USING fts5(
|
|
124
|
+
memory_id,
|
|
125
|
+
memory_type,
|
|
126
|
+
namespace,
|
|
127
|
+
searchable_content,
|
|
128
|
+
summary,
|
|
129
|
+
category_primary,
|
|
130
|
+
content='',
|
|
131
|
+
contentless_delete=1
|
|
132
|
+
)
|
|
133
|
+
""",
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
INDEX_CREATION = {
|
|
137
|
+
# Chat History Indexes
|
|
138
|
+
"idx_chat_namespace_session": "CREATE INDEX IF NOT EXISTS idx_chat_namespace_session ON chat_history(namespace, session_id)",
|
|
139
|
+
"idx_chat_timestamp": "CREATE INDEX IF NOT EXISTS idx_chat_timestamp ON chat_history(timestamp)",
|
|
140
|
+
"idx_chat_model": "CREATE INDEX IF NOT EXISTS idx_chat_model ON chat_history(model)",
|
|
141
|
+
# Short-term Memory Indexes
|
|
142
|
+
"idx_short_term_namespace": "CREATE INDEX IF NOT EXISTS idx_short_term_namespace ON short_term_memory(namespace)",
|
|
143
|
+
"idx_short_term_category": "CREATE INDEX IF NOT EXISTS idx_short_term_category ON short_term_memory(category_primary)",
|
|
144
|
+
"idx_short_term_importance": "CREATE INDEX IF NOT EXISTS idx_short_term_importance ON short_term_memory(importance_score)",
|
|
145
|
+
"idx_short_term_expires": "CREATE INDEX IF NOT EXISTS idx_short_term_expires ON short_term_memory(expires_at)",
|
|
146
|
+
"idx_short_term_created": "CREATE INDEX IF NOT EXISTS idx_short_term_created ON short_term_memory(created_at)",
|
|
147
|
+
"idx_short_term_searchable": "CREATE INDEX IF NOT EXISTS idx_short_term_searchable ON short_term_memory(searchable_content)",
|
|
148
|
+
"idx_short_term_access": "CREATE INDEX IF NOT EXISTS idx_short_term_access ON short_term_memory(access_count, last_accessed)",
|
|
149
|
+
# Long-term Memory Indexes
|
|
150
|
+
"idx_long_term_namespace": "CREATE INDEX IF NOT EXISTS idx_long_term_namespace ON long_term_memory(namespace)",
|
|
151
|
+
"idx_long_term_category": "CREATE INDEX IF NOT EXISTS idx_long_term_category ON long_term_memory(category_primary)",
|
|
152
|
+
"idx_long_term_importance": "CREATE INDEX IF NOT EXISTS idx_long_term_importance ON long_term_memory(importance_score)",
|
|
153
|
+
"idx_long_term_created": "CREATE INDEX IF NOT EXISTS idx_long_term_created ON long_term_memory(created_at)",
|
|
154
|
+
"idx_long_term_searchable": "CREATE INDEX IF NOT EXISTS idx_long_term_searchable ON long_term_memory(searchable_content)",
|
|
155
|
+
"idx_long_term_access": "CREATE INDEX IF NOT EXISTS idx_long_term_access ON long_term_memory(access_count, last_accessed)",
|
|
156
|
+
"idx_long_term_scores": "CREATE INDEX IF NOT EXISTS idx_long_term_scores ON long_term_memory(novelty_score, relevance_score, actionability_score)",
|
|
157
|
+
# Rules Memory Indexes
|
|
158
|
+
"idx_rules_namespace": "CREATE INDEX IF NOT EXISTS idx_rules_namespace ON rules_memory(namespace)",
|
|
159
|
+
"idx_rules_active": "CREATE INDEX IF NOT EXISTS idx_rules_active ON rules_memory(active)",
|
|
160
|
+
"idx_rules_priority": "CREATE INDEX IF NOT EXISTS idx_rules_priority ON rules_memory(priority)",
|
|
161
|
+
"idx_rules_type": "CREATE INDEX IF NOT EXISTS idx_rules_type ON rules_memory(rule_type)",
|
|
162
|
+
"idx_rules_updated": "CREATE INDEX IF NOT EXISTS idx_rules_updated ON rules_memory(updated_at)",
|
|
163
|
+
# Entity Indexes
|
|
164
|
+
"idx_entities_namespace": "CREATE INDEX IF NOT EXISTS idx_entities_namespace ON memory_entities(namespace)",
|
|
165
|
+
"idx_entities_type": "CREATE INDEX IF NOT EXISTS idx_entities_type ON memory_entities(entity_type)",
|
|
166
|
+
"idx_entities_value": "CREATE INDEX IF NOT EXISTS idx_entities_value ON memory_entities(entity_value)",
|
|
167
|
+
"idx_entities_memory": "CREATE INDEX IF NOT EXISTS idx_entities_memory ON memory_entities(memory_id, memory_type)",
|
|
168
|
+
"idx_entities_relevance": "CREATE INDEX IF NOT EXISTS idx_entities_relevance ON memory_entities(relevance_score)",
|
|
169
|
+
"idx_entities_value_type": "CREATE INDEX IF NOT EXISTS idx_entities_value_type ON memory_entities(entity_value, entity_type)",
|
|
170
|
+
# Relationship Indexes
|
|
171
|
+
"idx_relationships_source": "CREATE INDEX IF NOT EXISTS idx_relationships_source ON memory_relationships(source_memory_id)",
|
|
172
|
+
"idx_relationships_target": "CREATE INDEX IF NOT EXISTS idx_relationships_target ON memory_relationships(target_memory_id)",
|
|
173
|
+
"idx_relationships_type": "CREATE INDEX IF NOT EXISTS idx_relationships_type ON memory_relationships(relationship_type)",
|
|
174
|
+
"idx_relationships_strength": "CREATE INDEX IF NOT EXISTS idx_relationships_strength ON memory_relationships(strength)",
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
TRIGGER_CREATION = {
|
|
178
|
+
"short_term_memory_fts_insert": """
|
|
179
|
+
CREATE TRIGGER IF NOT EXISTS short_term_memory_fts_insert AFTER INSERT ON short_term_memory
|
|
180
|
+
BEGIN
|
|
181
|
+
INSERT INTO memory_search_fts(memory_id, memory_type, namespace, searchable_content, summary, category_primary)
|
|
182
|
+
VALUES (NEW.memory_id, 'short_term', NEW.namespace, NEW.searchable_content, NEW.summary, NEW.category_primary);
|
|
183
|
+
END
|
|
184
|
+
""",
|
|
185
|
+
"long_term_memory_fts_insert": """
|
|
186
|
+
CREATE TRIGGER IF NOT EXISTS long_term_memory_fts_insert AFTER INSERT ON long_term_memory
|
|
187
|
+
BEGIN
|
|
188
|
+
INSERT INTO memory_search_fts(memory_id, memory_type, namespace, searchable_content, summary, category_primary)
|
|
189
|
+
VALUES (NEW.memory_id, 'long_term', NEW.namespace, NEW.searchable_content, NEW.summary, NEW.category_primary);
|
|
190
|
+
END
|
|
191
|
+
""",
|
|
192
|
+
"short_term_memory_fts_delete": """
|
|
193
|
+
CREATE TRIGGER IF NOT EXISTS short_term_memory_fts_delete AFTER DELETE ON short_term_memory
|
|
194
|
+
BEGIN
|
|
195
|
+
DELETE FROM memory_search_fts WHERE memory_id = OLD.memory_id AND memory_type = 'short_term';
|
|
196
|
+
END
|
|
197
|
+
""",
|
|
198
|
+
"long_term_memory_fts_delete": """
|
|
199
|
+
CREATE TRIGGER IF NOT EXISTS long_term_memory_fts_delete AFTER DELETE ON long_term_memory
|
|
200
|
+
BEGIN
|
|
201
|
+
DELETE FROM memory_search_fts WHERE memory_id = OLD.memory_id AND memory_type = 'long_term';
|
|
202
|
+
END
|
|
203
|
+
""",
|
|
204
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Chat history database queries
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from typing import Dict
|
|
6
|
+
|
|
7
|
+
from .base_queries import BaseQueries
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class ChatQueries(BaseQueries):
|
|
11
|
+
"""Centralized chat history SQL queries"""
|
|
12
|
+
|
|
13
|
+
def get_table_creation_queries(self) -> Dict[str, str]:
|
|
14
|
+
"""Chat table creation queries"""
|
|
15
|
+
from .base_queries import SchemaQueries
|
|
16
|
+
|
|
17
|
+
return {"chat_history": SchemaQueries.TABLE_CREATION["chat_history"]}
|
|
18
|
+
|
|
19
|
+
def get_index_creation_queries(self) -> Dict[str, str]:
|
|
20
|
+
"""Chat index creation queries"""
|
|
21
|
+
from .base_queries import SchemaQueries
|
|
22
|
+
|
|
23
|
+
return {k: v for k, v in SchemaQueries.INDEX_CREATION.items() if "chat" in k}
|
|
24
|
+
|
|
25
|
+
def get_trigger_creation_queries(self) -> Dict[str, str]:
|
|
26
|
+
"""Chat trigger creation queries"""
|
|
27
|
+
return {} # No triggers for chat history currently
|
|
28
|
+
|
|
29
|
+
# INSERT Queries
|
|
30
|
+
INSERT_CHAT_HISTORY = """
|
|
31
|
+
INSERT INTO chat_history (
|
|
32
|
+
chat_id, user_input, ai_output, model, timestamp,
|
|
33
|
+
session_id, namespace, tokens_used, metadata
|
|
34
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
# SELECT Queries
|
|
38
|
+
SELECT_CHAT_BY_ID = """
|
|
39
|
+
SELECT * FROM chat_history
|
|
40
|
+
WHERE chat_id = ? AND namespace = ?
|
|
41
|
+
"""
|
|
42
|
+
|
|
43
|
+
SELECT_CHAT_BY_SESSION = """
|
|
44
|
+
SELECT chat_id, user_input, ai_output, model, timestamp, tokens_used
|
|
45
|
+
FROM chat_history
|
|
46
|
+
WHERE session_id = ? AND namespace = ?
|
|
47
|
+
ORDER BY timestamp ASC
|
|
48
|
+
"""
|
|
49
|
+
|
|
50
|
+
SELECT_RECENT_CHATS = """
|
|
51
|
+
SELECT chat_id, user_input, ai_output, model, timestamp, tokens_used
|
|
52
|
+
FROM chat_history
|
|
53
|
+
WHERE namespace = ? AND timestamp >= ?
|
|
54
|
+
ORDER BY timestamp DESC
|
|
55
|
+
LIMIT ?
|
|
56
|
+
"""
|
|
57
|
+
|
|
58
|
+
SELECT_CHATS_BY_MODEL = """
|
|
59
|
+
SELECT chat_id, user_input, ai_output, timestamp, tokens_used
|
|
60
|
+
FROM chat_history
|
|
61
|
+
WHERE namespace = ? AND model = ?
|
|
62
|
+
ORDER BY timestamp DESC
|
|
63
|
+
LIMIT ?
|
|
64
|
+
"""
|
|
65
|
+
|
|
66
|
+
SELECT_CHAT_STATISTICS = """
|
|
67
|
+
SELECT
|
|
68
|
+
COUNT(*) as total_chats,
|
|
69
|
+
COUNT(DISTINCT session_id) as unique_sessions,
|
|
70
|
+
COUNT(DISTINCT model) as unique_models,
|
|
71
|
+
SUM(tokens_used) as total_tokens,
|
|
72
|
+
AVG(tokens_used) as avg_tokens,
|
|
73
|
+
MIN(timestamp) as first_chat,
|
|
74
|
+
MAX(timestamp) as last_chat
|
|
75
|
+
FROM chat_history
|
|
76
|
+
WHERE namespace = ?
|
|
77
|
+
"""
|
|
78
|
+
|
|
79
|
+
SELECT_CHATS_BY_DATE_RANGE = """
|
|
80
|
+
SELECT chat_id, user_input, ai_output, model, timestamp, tokens_used
|
|
81
|
+
FROM chat_history
|
|
82
|
+
WHERE namespace = ? AND timestamp BETWEEN ? AND ?
|
|
83
|
+
ORDER BY timestamp DESC
|
|
84
|
+
LIMIT ?
|
|
85
|
+
"""
|
|
86
|
+
|
|
87
|
+
# UPDATE Queries
|
|
88
|
+
UPDATE_CHAT_METADATA = """
|
|
89
|
+
UPDATE chat_history
|
|
90
|
+
SET metadata = ?
|
|
91
|
+
WHERE chat_id = ? AND namespace = ?
|
|
92
|
+
"""
|
|
93
|
+
|
|
94
|
+
# DELETE Queries
|
|
95
|
+
DELETE_CHAT = """
|
|
96
|
+
DELETE FROM chat_history
|
|
97
|
+
WHERE chat_id = ? AND namespace = ?
|
|
98
|
+
"""
|
|
99
|
+
|
|
100
|
+
DELETE_OLD_CHATS = """
|
|
101
|
+
DELETE FROM chat_history
|
|
102
|
+
WHERE namespace = ? AND timestamp < ?
|
|
103
|
+
"""
|
|
104
|
+
|
|
105
|
+
DELETE_CHATS_BY_SESSION = """
|
|
106
|
+
DELETE FROM chat_history
|
|
107
|
+
WHERE session_id = ? AND namespace = ?
|
|
108
|
+
"""
|
|
109
|
+
|
|
110
|
+
# ANALYTICS Queries
|
|
111
|
+
GET_CHAT_VOLUME_BY_DATE = """
|
|
112
|
+
SELECT
|
|
113
|
+
DATE(timestamp) as chat_date,
|
|
114
|
+
COUNT(*) as chat_count,
|
|
115
|
+
SUM(tokens_used) as tokens_used
|
|
116
|
+
FROM chat_history
|
|
117
|
+
WHERE namespace = ? AND timestamp >= ?
|
|
118
|
+
GROUP BY DATE(timestamp)
|
|
119
|
+
ORDER BY chat_date DESC
|
|
120
|
+
"""
|
|
121
|
+
|
|
122
|
+
GET_MODEL_USAGE_STATS = """
|
|
123
|
+
SELECT
|
|
124
|
+
model,
|
|
125
|
+
COUNT(*) as usage_count,
|
|
126
|
+
SUM(tokens_used) as total_tokens,
|
|
127
|
+
AVG(tokens_used) as avg_tokens
|
|
128
|
+
FROM chat_history
|
|
129
|
+
WHERE namespace = ?
|
|
130
|
+
GROUP BY model
|
|
131
|
+
ORDER BY usage_count DESC
|
|
132
|
+
"""
|
|
133
|
+
|
|
134
|
+
GET_SESSION_STATS = """
|
|
135
|
+
SELECT
|
|
136
|
+
session_id,
|
|
137
|
+
COUNT(*) as message_count,
|
|
138
|
+
MIN(timestamp) as session_start,
|
|
139
|
+
MAX(timestamp) as session_end,
|
|
140
|
+
SUM(tokens_used) as total_tokens
|
|
141
|
+
FROM chat_history
|
|
142
|
+
WHERE namespace = ?
|
|
143
|
+
GROUP BY session_id
|
|
144
|
+
ORDER BY session_start DESC
|
|
145
|
+
LIMIT ?
|
|
146
|
+
"""
|
|
147
|
+
|
|
148
|
+
SEARCH_CHAT_CONTENT = """
|
|
149
|
+
SELECT chat_id, user_input, ai_output, model, timestamp
|
|
150
|
+
FROM chat_history
|
|
151
|
+
WHERE namespace = ? AND (
|
|
152
|
+
user_input LIKE ? OR
|
|
153
|
+
ai_output LIKE ?
|
|
154
|
+
)
|
|
155
|
+
ORDER BY timestamp DESC
|
|
156
|
+
LIMIT ?
|
|
157
|
+
"""
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Entity and relationship database queries
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from typing import Dict
|
|
6
|
+
|
|
7
|
+
from .base_queries import BaseQueries
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class EntityQueries(BaseQueries):
|
|
11
|
+
"""Centralized entity and relationship SQL queries"""
|
|
12
|
+
|
|
13
|
+
def get_table_creation_queries(self) -> Dict[str, str]:
|
|
14
|
+
"""Entity table creation queries"""
|
|
15
|
+
from .base_queries import SchemaQueries
|
|
16
|
+
|
|
17
|
+
return {
|
|
18
|
+
"memory_entities": SchemaQueries.TABLE_CREATION["memory_entities"],
|
|
19
|
+
"memory_relationships": SchemaQueries.TABLE_CREATION[
|
|
20
|
+
"memory_relationships"
|
|
21
|
+
],
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
def get_index_creation_queries(self) -> Dict[str, str]:
|
|
25
|
+
"""Entity index creation queries"""
|
|
26
|
+
from .base_queries import SchemaQueries
|
|
27
|
+
|
|
28
|
+
return {
|
|
29
|
+
k: v
|
|
30
|
+
for k, v in SchemaQueries.INDEX_CREATION.items()
|
|
31
|
+
if any(word in k for word in ["entities", "relationships"])
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
def get_trigger_creation_queries(self) -> Dict[str, str]:
|
|
35
|
+
"""Entity trigger creation queries"""
|
|
36
|
+
return {} # No triggers for entities currently
|
|
37
|
+
|
|
38
|
+
# ENTITY INSERT Queries
|
|
39
|
+
INSERT_MEMORY_ENTITY = """
|
|
40
|
+
INSERT INTO memory_entities (
|
|
41
|
+
entity_id, memory_id, memory_type, entity_type, entity_value,
|
|
42
|
+
relevance_score, entity_context, namespace, created_at
|
|
43
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
44
|
+
"""
|
|
45
|
+
|
|
46
|
+
BATCH_INSERT_ENTITIES = """
|
|
47
|
+
INSERT INTO memory_entities (
|
|
48
|
+
entity_id, memory_id, memory_type, entity_type, entity_value,
|
|
49
|
+
relevance_score, entity_context, namespace, created_at
|
|
50
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
51
|
+
"""
|
|
52
|
+
|
|
53
|
+
# RELATIONSHIP INSERT Queries
|
|
54
|
+
INSERT_MEMORY_RELATIONSHIP = """
|
|
55
|
+
INSERT INTO memory_relationships (
|
|
56
|
+
relationship_id, source_memory_id, target_memory_id, relationship_type,
|
|
57
|
+
strength, reasoning, namespace, created_at
|
|
58
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
59
|
+
"""
|
|
60
|
+
|
|
61
|
+
# ENTITY SELECT Queries
|
|
62
|
+
SELECT_ENTITIES_BY_MEMORY = """
|
|
63
|
+
SELECT entity_id, entity_type, entity_value, relevance_score, entity_context
|
|
64
|
+
FROM memory_entities
|
|
65
|
+
WHERE memory_id = ? AND namespace = ?
|
|
66
|
+
ORDER BY relevance_score DESC
|
|
67
|
+
"""
|
|
68
|
+
|
|
69
|
+
SELECT_ENTITIES_BY_TYPE = """
|
|
70
|
+
SELECT entity_id, memory_id, memory_type, entity_value, relevance_score, entity_context
|
|
71
|
+
FROM memory_entities
|
|
72
|
+
WHERE entity_type = ? AND namespace = ?
|
|
73
|
+
ORDER BY relevance_score DESC
|
|
74
|
+
LIMIT ?
|
|
75
|
+
"""
|
|
76
|
+
|
|
77
|
+
SELECT_ENTITIES_BY_VALUE = """
|
|
78
|
+
SELECT entity_id, memory_id, memory_type, entity_type, relevance_score, entity_context
|
|
79
|
+
FROM memory_entities
|
|
80
|
+
WHERE entity_value = ? AND namespace = ?
|
|
81
|
+
ORDER BY relevance_score DESC
|
|
82
|
+
LIMIT ?
|
|
83
|
+
"""
|
|
84
|
+
|
|
85
|
+
SELECT_SIMILAR_ENTITIES = """
|
|
86
|
+
SELECT entity_id, memory_id, memory_type, entity_type, entity_value, relevance_score
|
|
87
|
+
FROM memory_entities
|
|
88
|
+
WHERE entity_value LIKE ? AND entity_type = ? AND namespace = ?
|
|
89
|
+
ORDER BY relevance_score DESC
|
|
90
|
+
LIMIT ?
|
|
91
|
+
"""
|
|
92
|
+
|
|
93
|
+
SELECT_ENTITIES_BY_MEMORY_TYPE = """
|
|
94
|
+
SELECT entity_id, entity_type, entity_value, relevance_score, memory_id
|
|
95
|
+
FROM memory_entities
|
|
96
|
+
WHERE memory_type = ? AND namespace = ?
|
|
97
|
+
ORDER BY relevance_score DESC
|
|
98
|
+
LIMIT ?
|
|
99
|
+
"""
|
|
100
|
+
|
|
101
|
+
# RELATIONSHIP SELECT Queries
|
|
102
|
+
SELECT_RELATIONSHIPS_BY_MEMORY = """
|
|
103
|
+
SELECT relationship_id, target_memory_id, relationship_type, strength, reasoning
|
|
104
|
+
FROM memory_relationships
|
|
105
|
+
WHERE source_memory_id = ? AND namespace = ?
|
|
106
|
+
ORDER BY strength DESC
|
|
107
|
+
"""
|
|
108
|
+
|
|
109
|
+
SELECT_RELATED_MEMORIES = """
|
|
110
|
+
SELECT DISTINCT target_memory_id as memory_id, relationship_type, strength, reasoning
|
|
111
|
+
FROM memory_relationships
|
|
112
|
+
WHERE source_memory_id = ? AND namespace = ?
|
|
113
|
+
UNION
|
|
114
|
+
SELECT DISTINCT source_memory_id as memory_id, relationship_type, strength, reasoning
|
|
115
|
+
FROM memory_relationships
|
|
116
|
+
WHERE target_memory_id = ? AND namespace = ?
|
|
117
|
+
ORDER BY strength DESC
|
|
118
|
+
LIMIT ?
|
|
119
|
+
"""
|
|
120
|
+
|
|
121
|
+
SELECT_RELATIONSHIPS_BY_TYPE = """
|
|
122
|
+
SELECT relationship_id, source_memory_id, target_memory_id, strength, reasoning
|
|
123
|
+
FROM memory_relationships
|
|
124
|
+
WHERE relationship_type = ? AND namespace = ?
|
|
125
|
+
ORDER BY strength DESC
|
|
126
|
+
LIMIT ?
|
|
127
|
+
"""
|
|
128
|
+
|
|
129
|
+
# UPDATE Queries
|
|
130
|
+
UPDATE_ENTITY_RELEVANCE = """
|
|
131
|
+
UPDATE memory_entities
|
|
132
|
+
SET relevance_score = ?
|
|
133
|
+
WHERE entity_id = ? AND namespace = ?
|
|
134
|
+
"""
|
|
135
|
+
|
|
136
|
+
UPDATE_RELATIONSHIP_STRENGTH = """
|
|
137
|
+
UPDATE memory_relationships
|
|
138
|
+
SET strength = ?, reasoning = ?
|
|
139
|
+
WHERE relationship_id = ? AND namespace = ?
|
|
140
|
+
"""
|
|
141
|
+
|
|
142
|
+
# DELETE Queries
|
|
143
|
+
DELETE_ENTITIES_BY_MEMORY = """
|
|
144
|
+
DELETE FROM memory_entities
|
|
145
|
+
WHERE memory_id = ? AND namespace = ?
|
|
146
|
+
"""
|
|
147
|
+
|
|
148
|
+
DELETE_RELATIONSHIPS_BY_MEMORY = """
|
|
149
|
+
DELETE FROM memory_relationships
|
|
150
|
+
WHERE (source_memory_id = ? OR target_memory_id = ?) AND namespace = ?
|
|
151
|
+
"""
|
|
152
|
+
|
|
153
|
+
DELETE_ENTITY = """
|
|
154
|
+
DELETE FROM memory_entities
|
|
155
|
+
WHERE entity_id = ? AND namespace = ?
|
|
156
|
+
"""
|
|
157
|
+
|
|
158
|
+
DELETE_RELATIONSHIP = """
|
|
159
|
+
DELETE FROM memory_relationships
|
|
160
|
+
WHERE relationship_id = ? AND namespace = ?
|
|
161
|
+
"""
|
|
162
|
+
|
|
163
|
+
# ANALYTICS Queries
|
|
164
|
+
GET_ENTITY_STATISTICS = """
|
|
165
|
+
SELECT
|
|
166
|
+
entity_type,
|
|
167
|
+
COUNT(*) as count,
|
|
168
|
+
AVG(relevance_score) as avg_relevance,
|
|
169
|
+
MAX(relevance_score) as max_relevance
|
|
170
|
+
FROM memory_entities
|
|
171
|
+
WHERE namespace = ?
|
|
172
|
+
GROUP BY entity_type
|
|
173
|
+
ORDER BY count DESC
|
|
174
|
+
"""
|
|
175
|
+
|
|
176
|
+
GET_TOP_ENTITIES = """
|
|
177
|
+
SELECT entity_value, entity_type, COUNT(*) as frequency, AVG(relevance_score) as avg_relevance
|
|
178
|
+
FROM memory_entities
|
|
179
|
+
WHERE namespace = ? AND entity_type = ?
|
|
180
|
+
GROUP BY entity_value, entity_type
|
|
181
|
+
ORDER BY frequency DESC, avg_relevance DESC
|
|
182
|
+
LIMIT ?
|
|
183
|
+
"""
|
|
184
|
+
|
|
185
|
+
GET_RELATIONSHIP_STATISTICS = """
|
|
186
|
+
SELECT
|
|
187
|
+
relationship_type,
|
|
188
|
+
COUNT(*) as count,
|
|
189
|
+
AVG(strength) as avg_strength,
|
|
190
|
+
MAX(strength) as max_strength
|
|
191
|
+
FROM memory_relationships
|
|
192
|
+
WHERE namespace = ?
|
|
193
|
+
GROUP BY relationship_type
|
|
194
|
+
ORDER BY count DESC
|
|
195
|
+
"""
|
|
196
|
+
|
|
197
|
+
GET_MEMORY_CONNECTIVITY = """
|
|
198
|
+
SELECT
|
|
199
|
+
memory_id,
|
|
200
|
+
memory_type,
|
|
201
|
+
COUNT(*) as connection_count
|
|
202
|
+
FROM (
|
|
203
|
+
SELECT source_memory_id as memory_id, 'outgoing' as memory_type
|
|
204
|
+
FROM memory_relationships
|
|
205
|
+
WHERE namespace = ?
|
|
206
|
+
UNION ALL
|
|
207
|
+
SELECT target_memory_id as memory_id, 'incoming' as memory_type
|
|
208
|
+
FROM memory_relationships
|
|
209
|
+
WHERE namespace = ?
|
|
210
|
+
) as connections
|
|
211
|
+
GROUP BY memory_id
|
|
212
|
+
ORDER BY connection_count DESC
|
|
213
|
+
LIMIT ?
|
|
214
|
+
"""
|
|
215
|
+
|
|
216
|
+
# SEARCH Queries
|
|
217
|
+
SEARCH_ENTITIES = """
|
|
218
|
+
SELECT entity_id, memory_id, memory_type, entity_type, entity_value, relevance_score
|
|
219
|
+
FROM memory_entities
|
|
220
|
+
WHERE namespace = ? AND (
|
|
221
|
+
entity_value LIKE ? OR
|
|
222
|
+
entity_context LIKE ?
|
|
223
|
+
)
|
|
224
|
+
ORDER BY relevance_score DESC
|
|
225
|
+
LIMIT ?
|
|
226
|
+
"""
|
|
227
|
+
|
|
228
|
+
FIND_ENTITY_CLUSTERS = """
|
|
229
|
+
SELECT entity_value, entity_type, COUNT(DISTINCT memory_id) as memory_count
|
|
230
|
+
FROM memory_entities
|
|
231
|
+
WHERE namespace = ? AND entity_type = ?
|
|
232
|
+
GROUP BY entity_value, entity_type
|
|
233
|
+
HAVING memory_count > ?
|
|
234
|
+
ORDER BY memory_count DESC
|
|
235
|
+
LIMIT ?
|
|
236
|
+
"""
|