File size: 12,142 Bytes
4e3c158
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#!/usr/bin/env python3
"""
LanceDB Conversation Retrieval Script

This script demonstrates how to retrieve conversations from LanceDB
using the existing memory system integration in Atom.

Features:
- Retrieve conversation history for specific users
- Search conversations using semantic similarity
- Export conversation data in various formats
- Test LanceDB connectivity and health
"""

import argparse
import asyncio
from datetime import datetime
import json
import os
import sys
from typing import Dict, List, Optional

# Add backend to path to import the necessary modules
sys.path.append(os.path.join(os.path.dirname(__file__), "backend"))

try:
    from backend.python_api_service.lancedb_handler import (
        get_conversation_history,
        get_lancedb_connection,
        search_conversation_context,
        store_conversation_context,
    )

    LANCEDB_AVAILABLE = True
except ImportError as e:
    print(f"Warning: LanceDB modules not available: {e}")
    LANCEDB_AVAILABLE = False


class LanceDBConversationRetriever:
    """Class to handle conversation retrieval from LanceDB"""

    def __init__(self, db_path: str = "data/lancedb"):
        self.db_path = db_path
        self.db_connection = None

    async def initialize(self):
        """Initialize LanceDB connection"""
        if not LANCEDB_AVAILABLE:
            print("LanceDB is not available. Please check the installation.")
            return False

        try:
            self.db_connection = await get_lancedb_connection(self.db_path)
            print(f"✅ Successfully connected to LanceDB at {self.db_path}")
            return True
        except Exception as e:
            print(f"❌ Failed to connect to LanceDB: {e}")
            return False

    async def get_user_conversations(
        self,
        user_id: str,
        session_id: Optional[str] = None,
        limit: int = 50,
        offset: int = 0,
    ) -> Dict:
        """Get conversation history for a specific user"""
        if not self.db_connection:
            return {"status": "error", "message": "LanceDB not connected"}

        try:
            result = await get_conversation_history(
                self.db_connection, user_id, session_id, limit, offset
            )
            return result
        except Exception as e:
            return {"status": "error", "message": f"Failed to get conversations: {e}"}

    async def search_conversations(
        self,
        query_text: str,
        user_id: str,
        session_id: Optional[str] = None,
        limit: int = 10,
    ) -> Dict:
        """Search conversations using semantic similarity"""
        if not self.db_connection:
            return {"status": "error", "message": "LanceDB not connected"}

        try:
            # Generate a simple embedding for the query (placeholder)
            # In production, this would use a proper embedding model
            query_embedding = [0.1] * 384  # Standard embedding dimension

            result = await search_conversation_context(
                self.db_connection, query_embedding, user_id, session_id, limit
            )
            return result
        except Exception as e:
            return {
                "status": "error",
                "message": f"Failed to search conversations: {e}",
            }

    async def get_conversation_stats(self, user_id: str) -> Dict:
        """Get conversation statistics for a user"""
        if not self.db_connection:
            return {"status": "error", "message": "LanceDB not connected"}

        try:
            # Get all conversations for the user
            result = await get_conversation_history(
                self.db_connection, user_id, limit=1000
            )

            if result.get("status") != "success":
                return result

            conversations = result.get("conversations", [])

            # Calculate statistics
            stats = {
                "total_conversations": len(conversations),
                "user_id": user_id,
                "first_conversation": None,
                "last_conversation": None,
                "message_counts": {"user": 0, "assistant": 0, "system": 0},
                "timeline": [],
            }

            if conversations:
                # Sort by timestamp
                sorted_conv = sorted(
                    conversations, key=lambda x: x.get("timestamp", "")
                )
                stats["first_conversation"] = sorted_conv[0].get("timestamp")
                stats["last_conversation"] = sorted_conv[-1].get("timestamp")

                # Count messages by role
                for conv in conversations:
                    role = conv.get("role", "user")
                    stats["message_counts"][role] = (
                        stats["message_counts"].get(role, 0) + 1
                    )

                    # Add to timeline
                    stats["timeline"].append(
                        {
                            "timestamp": conv.get("timestamp"),
                            "role": role,
                            "content_preview": conv.get("content", "")[:100] + "..."
                            if len(conv.get("content", "")) > 100
                            else conv.get("content", ""),
                        }
                    )

            return {"status": "success", "stats": stats}

        except Exception as e:
            return {"status": "error", "message": f"Failed to get stats: {e}"}


async def test_lancedb_connection():
    """Test LanceDB connection and basic functionality"""
    print("🧪 Testing LanceDB Connection...")

    retriever = LanceDBConversationRetriever()
    connected = await retriever.initialize()

    if not connected:
        print("❌ LanceDB connection test failed")
        return False

    print("✅ LanceDB connection test passed")
    return True


async def retrieve_user_conversations(user_id: str, limit: int = 20):
    """Retrieve and display conversations for a specific user"""
    print(f"📝 Retrieving conversations for user: {user_id}")

    retriever = LanceDBConversationRetriever()
    await retriever.initialize()

    # Get conversation history
    result = await retriever.get_user_conversations(user_id, limit=limit)

    if result.get("status") == "success":
        conversations = result.get("conversations", [])
        total_count = result.get("total_count", 0)

        print(f"📊 Found {len(conversations)} conversations (total: {total_count})")
        print("-" * 80)

        for i, conv in enumerate(conversations, 1):
            timestamp = conv.get("timestamp", "Unknown")
            role = conv.get("role", "unknown").upper()
            content = conv.get("content", "")
            session_id = conv.get("session_id", "N/A")

            print(f"{i}. [{timestamp}] {role} (Session: {session_id})")
            print(f"   {content[:200]}{'...' if len(content) > 200 else ''}")
            print()

    else:
        print(f"❌ Failed to retrieve conversations: {result.get('message')}")


async def search_user_conversations(user_id: str, query: str, limit: int = 10):
    """Search conversations for a specific user"""
    print(f"🔍 Searching conversations for user '{user_id}': '{query}'")

    retriever = LanceDBConversationRetriever()
    await retriever.initialize()

    # Search conversations
    result = await retriever.search_conversations(query, user_id, limit=limit)

    if result.get("status") == "success":
        results = result.get("results", [])

        print(f"📊 Found {len(results)} relevant conversations")
        print("-" * 80)

        for i, res in enumerate(results, 1):
            timestamp = res.get("timestamp", "Unknown")
            role = res.get("role", "unknown").upper()
            content = res.get("content", "")
            similarity = res.get("similarity_score", 0)
            session_id = res.get("session_id", "N/A")

            print(f"{i}. [{timestamp}] {role} (Session: {session_id})")
            print(f"   Similarity: {similarity:.3f}")
            print(f"   {content[:200]}{'...' if len(content) > 200 else ''}")
            print()

    else:
        print(f"❌ Failed to search conversations: {result.get('message')}")


async def export_conversations(user_id: str, output_file: str):
    """Export conversations to JSON file"""
    print(f"💾 Exporting conversations for user '{user_id}' to {output_file}")

    retriever = LanceDBConversationRetriever()
    await retriever.initialize()

    # Get all conversations (with large limit)
    result = await retriever.get_user_conversations(user_id, limit=1000)

    if result.get("status") == "success":
        conversations = result.get("conversations", [])

        # Prepare export data
        export_data = {
            "export_timestamp": datetime.now().isoformat(),
            "user_id": user_id,
            "total_conversations": len(conversations),
            "conversations": conversations,
        }

        # Write to file
        with open(output_file, "w", encoding="utf-8") as f:
            json.dump(export_data, f, indent=2, ensure_ascii=False)

        print(
            f"✅ Successfully exported {len(conversations)} conversations to {output_file}"
        )

    else:
        print(f"❌ Failed to export conversations: {result.get('message')}")


async def get_user_stats(user_id: str):
    """Get conversation statistics for a user"""
    print(f"📊 Getting conversation statistics for user: {user_id}")

    retriever = LanceDBConversationRetriever()
    await retriever.initialize()

    result = await retriever.get_conversation_stats(user_id)

    if result.get("status") == "success":
        stats = result.get("stats", {})

        print(f"📈 Conversation Statistics for {user_id}:")
        print(f"   Total Conversations: {stats.get('total_conversations', 0)}")
        print(f"   First Conversation: {stats.get('first_conversation', 'N/A')}")
        print(f"   Last Conversation: {stats.get('last_conversation', 'N/A')}")
        print(f"   Message Counts:")
        for role, count in stats.get("message_counts", {}).items():
            print(f"     - {role.capitalize()}: {count}")

    else:
        print(f"❌ Failed to get statistics: {result.get('message')}")


def main():
    """Main function with command line interface"""
    parser = argparse.ArgumentParser(description="Retrieve conversations from LanceDB")
    parser.add_argument(
        "--user-id", required=True, help="User ID to retrieve conversations for"
    )
    parser.add_argument(
        "--action",
        choices=["retrieve", "search", "export", "stats", "test"],
        default="retrieve",
        help="Action to perform",
    )
    parser.add_argument("--query", help="Search query (for search action)")
    parser.add_argument(
        "--limit", type=int, default=20, help="Number of conversations to retrieve"
    )
    parser.add_argument("--output", help="Output file for export")

    args = parser.parse_args()

    if not LANCEDB_AVAILABLE:
        print("❌ LanceDB is not available. Please ensure:")
        print("   - LanceDB is installed: pip install lancedb")
        print("   - The backend modules are accessible")
        sys.exit(1)

    # Perform the requested action
    if args.action == "test":
        asyncio.run(test_lancedb_connection())
    elif args.action == "retrieve":
        asyncio.run(retrieve_user_conversations(args.user_id, args.limit))
    elif args.action == "search":
        if not args.query:
            print("❌ Please provide a search query with --query")
            sys.exit(1)
        asyncio.run(search_user_conversations(args.user_id, args.query, args.limit))
    elif args.action == "export":
        output_file = (
            args.output
            or f"conversations_{args.user_id}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
        )
        asyncio.run(export_conversations(args.user_id, output_file))
    elif args.action == "stats":
        asyncio.run(get_user_stats(args.user_id))


if __name__ == "__main__":
    main()