Spaces:
Running
Running
Merge branch 'main' of github.com:kunpai/HASHIRU
Browse files
src/manager/manager.py
CHANGED
|
@@ -23,7 +23,8 @@ class GeminiManager:
|
|
| 23 |
system_prompt_file="./src/models/system4.prompt",
|
| 24 |
gemini_model="gemini-2.5-pro-exp-03-25",
|
| 25 |
local_only=False, allow_tool_creation=True,
|
| 26 |
-
cloud_only=False, use_economy=True
|
|
|
|
| 27 |
load_dotenv()
|
| 28 |
self.toolsLoader: ToolManager = toolsLoader
|
| 29 |
if not toolsLoader:
|
|
@@ -33,12 +34,13 @@ class GeminiManager:
|
|
| 33 |
self.allow_tool_creation = allow_tool_creation
|
| 34 |
self.cloud_only = cloud_only
|
| 35 |
self.use_economy = use_economy
|
|
|
|
| 36 |
|
| 37 |
self.API_KEY = os.getenv("GEMINI_KEY")
|
| 38 |
self.client = genai.Client(api_key=self.API_KEY)
|
| 39 |
self.toolsLoader.load_tools()
|
| 40 |
self.model_name = gemini_model
|
| 41 |
-
self.memory_manager = MemoryManager()
|
| 42 |
with open(system_prompt_file, 'r', encoding="utf8") as f:
|
| 43 |
self.system_prompt = f.read()
|
| 44 |
self.messages = []
|
|
@@ -158,6 +160,8 @@ class GeminiManager:
|
|
| 158 |
return formatted_history
|
| 159 |
|
| 160 |
def get_k_memories(self, query, k=5, threshold=0.0):
|
|
|
|
|
|
|
| 161 |
memories = MemoryManager().get_memories()
|
| 162 |
for i in range(len(memories)):
|
| 163 |
memories[i] = memories[i]['memory']
|
|
@@ -165,7 +169,12 @@ class GeminiManager:
|
|
| 165 |
return []
|
| 166 |
top_k = min(k, len(memories))
|
| 167 |
# Semantic Retrieval with GPU
|
| 168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
print(f"Using device: {device}")
|
| 170 |
model = SentenceTransformer('all-MiniLM-L6-v2', device=device)
|
| 171 |
doc_embeddings = model.encode(memories, convert_to_tensor=True, device=device)
|
|
@@ -180,18 +189,19 @@ class GeminiManager:
|
|
| 180 |
return results
|
| 181 |
|
| 182 |
def run(self, messages):
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
|
|
|
| 195 |
yield from self.invoke_manager(messages)
|
| 196 |
|
| 197 |
def invoke_manager(self, messages):
|
|
|
|
| 23 |
system_prompt_file="./src/models/system4.prompt",
|
| 24 |
gemini_model="gemini-2.5-pro-exp-03-25",
|
| 25 |
local_only=False, allow_tool_creation=True,
|
| 26 |
+
cloud_only=False, use_economy=True,
|
| 27 |
+
use_memory=True):
|
| 28 |
load_dotenv()
|
| 29 |
self.toolsLoader: ToolManager = toolsLoader
|
| 30 |
if not toolsLoader:
|
|
|
|
| 34 |
self.allow_tool_creation = allow_tool_creation
|
| 35 |
self.cloud_only = cloud_only
|
| 36 |
self.use_economy = use_economy
|
| 37 |
+
self.use_memory = use_memory
|
| 38 |
|
| 39 |
self.API_KEY = os.getenv("GEMINI_KEY")
|
| 40 |
self.client = genai.Client(api_key=self.API_KEY)
|
| 41 |
self.toolsLoader.load_tools()
|
| 42 |
self.model_name = gemini_model
|
| 43 |
+
self.memory_manager = MemoryManager() if use_memory else None
|
| 44 |
with open(system_prompt_file, 'r', encoding="utf8") as f:
|
| 45 |
self.system_prompt = f.read()
|
| 46 |
self.messages = []
|
|
|
|
| 160 |
return formatted_history
|
| 161 |
|
| 162 |
def get_k_memories(self, query, k=5, threshold=0.0):
|
| 163 |
+
if not self.use_memory:
|
| 164 |
+
return []
|
| 165 |
memories = MemoryManager().get_memories()
|
| 166 |
for i in range(len(memories)):
|
| 167 |
memories[i] = memories[i]['memory']
|
|
|
|
| 169 |
return []
|
| 170 |
top_k = min(k, len(memories))
|
| 171 |
# Semantic Retrieval with GPU
|
| 172 |
+
if torch.cuda.is_available():
|
| 173 |
+
device = 'cuda'
|
| 174 |
+
elif torch.backends.mps.is_available() and torch.backends.mps.is_built():
|
| 175 |
+
device = 'mps'
|
| 176 |
+
else:
|
| 177 |
+
device = 'cpu'
|
| 178 |
print(f"Using device: {device}")
|
| 179 |
model = SentenceTransformer('all-MiniLM-L6-v2', device=device)
|
| 180 |
doc_embeddings = model.encode(memories, convert_to_tensor=True, device=device)
|
|
|
|
| 189 |
return results
|
| 190 |
|
| 191 |
def run(self, messages):
|
| 192 |
+
if self.use_memory:
|
| 193 |
+
memories = self.get_k_memories(messages[-1]['content'], k=5, threshold=0.1)
|
| 194 |
+
if len(memories) > 0:
|
| 195 |
+
messages.append({
|
| 196 |
+
"role": "memories",
|
| 197 |
+
"content": f"{memories}",
|
| 198 |
+
})
|
| 199 |
+
messages.append({
|
| 200 |
+
"role": "assistant",
|
| 201 |
+
"content": f"Memories: {memories}",
|
| 202 |
+
"metadata": {"title": "Memories"}
|
| 203 |
+
})
|
| 204 |
+
yield messages
|
| 205 |
yield from self.invoke_manager(messages)
|
| 206 |
|
| 207 |
def invoke_manager(self, messages):
|
src/tools/default_tools/memory_manager.py
CHANGED
|
@@ -2,6 +2,7 @@
|
|
| 2 |
__all__ = ['MemoryManager']
|
| 3 |
|
| 4 |
import json
|
|
|
|
| 5 |
|
| 6 |
|
| 7 |
class MemoryManager():
|
|
@@ -43,11 +44,13 @@ class MemoryManager():
|
|
| 43 |
return memory_list
|
| 44 |
|
| 45 |
def update_memories(self, memories):
|
| 46 |
-
|
|
|
|
| 47 |
with open("src/data/memory.json", "w") as f:
|
| 48 |
json.dump(memories, f, indent=4)
|
| 49 |
|
| 50 |
|
|
|
|
| 51 |
def run(self, **kwargs):
|
| 52 |
# save it to src/data/memory.json
|
| 53 |
action = kwargs.get("action")
|
|
|
|
| 2 |
__all__ = ['MemoryManager']
|
| 3 |
|
| 4 |
import json
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
|
| 8 |
class MemoryManager():
|
|
|
|
| 44 |
return memory_list
|
| 45 |
|
| 46 |
def update_memories(self, memories):
|
| 47 |
+
os.makedirs("src/data", exist_ok=True)
|
| 48 |
+
# Save the memory to src/data/memory.json
|
| 49 |
with open("src/data/memory.json", "w") as f:
|
| 50 |
json.dump(memories, f, indent=4)
|
| 51 |
|
| 52 |
|
| 53 |
+
|
| 54 |
def run(self, **kwargs):
|
| 55 |
# save it to src/data/memory.json
|
| 56 |
action = kwargs.get("action")
|