File size: 1,934 Bytes
4a693cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
################################################################################
# FILE: backend/app/services/memory.py
# VERSION: 1.0.0 | SYSTEM: Orbit Memory Protocol
# IDENTITY: The Long-term Memory (Vector DB / ChromaDB)
################################################################################

import chromadb
from chromadb.utils import embedding_functions
import logging
import os

logger = logging.getLogger("Orbit-Memory")

class MemoryService:
    def __init__(self):
        # Store memory in a local directory
        persist_directory = os.path.join(os.getcwd(), "orbit_memory")
        self.client = chromadb.PersistentClient(path=persist_directory)

        # Using default embedding function (requires internet for some, but Chroma's default is local)
        self.collection = self.client.get_or_create_collection(
            name="orbit_user_preferences",
            metadata={"hnsw:space": "cosine"}
        )

    def learn(self, fact: str):
        """Orbit learns something about the user."""
        try:
            # We use the fact itself as the ID or a hash
            fact_id = str(hash(fact))
            self.collection.add(
                documents=[fact],
                ids=[fact_id]
            )
            logger.info(f"Orbit learned: {fact}")
        except Exception as e:
            logger.error(f"Failed to learn: {e}")

    def query(self, user_query: str, n_results: int = 3) -> str:
        """Orbit remembers relevant context."""
        try:
            results = self.collection.query(
                query_texts=[user_query],
                n_results=n_results
            )

            documents = results.get('documents', [[]])[0]
            if documents:
                return "\n".join(documents)
            return ""
        except Exception as e:
            logger.error(f"Memory retrieval failed: {e}")
            return ""

memory_service = MemoryService()