File size: 3,070 Bytes
2651a17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
113
114
115
116
import uuid
import logging
from datetime import datetime

from qdrant_client import QdrantClient
from qdrant_client.models import (
    PointStruct,
    Distance,
    VectorParams,
    Filter,
    FieldCondition,
    MatchValue,
)

from app.core.config import settings
from openai import OpenAI

logger = logging.getLogger(__name__)

openai_client = OpenAI(api_key=settings.OPENAI_API_KEY)


class MemoryClient:

    client = QdrantClient(
        url=settings.QDRANT_HOST,
        api_key=settings.QDRANT_API_KEY,
        timeout=3.0
    )

    collection = settings.QDRANT_COLLECTION

    # 🔥 FIX: SAFE INIT (NO CRASH)
    @classmethod
    def ensure_collection(cls):
        try:
            existing = [c.name for c in cls.client.get_collections().collections]

            if cls.collection not in existing:

                cls.client.create_collection(
                    collection_name=cls.collection,
                    vectors_config=VectorParams(
                        size=1536,
                        distance=Distance.COSINE,
                    ),
                )

        except Exception as e:
            logger.warning(f"Memory init skipped: {e}")

    # EMBEDDING
    @classmethod
    def embed(cls, text: str):
        res = openai_client.embeddings.create(
            model="text-embedding-3-small",
            input=text[:1500],
        )
        return res.data[0].embedding

    # SEARCH (FAST + SAFE)
    @classmethod
    def search_memory(cls, user_id: str, query: str, limit: int = 2):
        try:
            vector = cls.embed(query)

            res = cls.client.query_points(
                collection_name=cls.collection,
                query=vector,
                limit=limit,
                query_filter=Filter(
                    must=[
                        FieldCondition(
                            key="user_id",
                            match=MatchValue(value=user_id),
                        )
                    ]
                ),
            )

            points = getattr(res, "points", [])

            return [
                p.payload.get("text", "")
                for p in points
                if getattr(p, "payload", None)
            ]

        except Exception as e:
            logger.warning(f"Memory search failed (ignored): {e}")
            return []

    # ADD MEMORY
    @classmethod
    def add_memory(cls, user_id: str, text: str):

        try:
            vector = cls.embed(text)

            cls.client.upsert(
                collection_name=cls.collection,
                points=[
                    PointStruct(
                        id=str(uuid.uuid4()),
                        vector=vector,
                        payload={
                            "user_id": user_id,
                            "text": text,
                            "timestamp": datetime.utcnow().isoformat(),
                        },
                    )
                ],
            )

        except Exception as e:
            logger.warning(f"Memory insert failed: {e}")