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c75
 
d52b84f
 
 
 
 
 
b222c75
d52b84f
 
 
 
b222c75
d52b84f
 
 
 
 
 
 
 
 
 
163baa3
 
97c403c
 
 
163baa3
 
97c403c
163baa3
97c403c
 
d52b84f
97c403c
 
 
 
 
 
 
 
 
d52b84f
 
 
97c403c
d52b84f
 
 
97c403c
d52b84f
 
 
 
 
 
 
 
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
"""
Agent Service - Central Brain for Sales & Feedback Agents
Manages LLM conversation loop with native tool calling
"""
from typing import Dict, Any, List, Optional
import os
import json
from tools_service import ToolsService


class AgentService:
    """
    Manages the conversation loop between User -> LLM -> Tools -> Response
    Uses native tool calling via HuggingFace Inference API
    """
    
    def __init__(
        self,
        tools_service: ToolsService,
        embedding_service,
        qdrant_service,
        advanced_rag,
        hf_token: str,
        feedback_tracking=None  # Optional feedback tracking
    ):
        self.tools_service = tools_service
        self.embedding_service = embedding_service
        self.qdrant_service = qdrant_service
        self.advanced_rag = advanced_rag
        self.hf_token = hf_token
        self.feedback_tracking = feedback_tracking
        
        # Load system prompts
        self.prompts = self._load_prompts()
    
    def _load_prompts(self) -> Dict[str, str]:
        """Load system prompts from files"""
        prompts = {}
        prompts_dir = "prompts"
        
        for mode in ["sales_agent", "feedback_agent"]:
            filepath = os.path.join(prompts_dir, f"{mode}.txt")
            try:
                with open(filepath, 'r', encoding='utf-8') as f:
                    prompts[mode] = f.read()
                print(f"✓ Loaded prompt: {mode}")
            except Exception as e:
                print(f"⚠️ Error loading {mode} prompt: {e}")
                prompts[mode] = ""
        
        return prompts
    
    def _get_native_tools(self, mode: str = "sales") -> List[Dict]:
        """
        Get tools formatted for native tool calling API.
        Returns OpenAI-compatible tool definitions.
        """
        common_tools = [
            {
                "type": "function",
                "function": {
                    "name": "search_events",
                    "description": "Tìm kiếm sự kiện phù hợp theo từ khóa, vibe, hoặc thời gian.",
                    "parameters": {
                        "type": "object",
                        "properties": {
                            "query": {"type": "string", "description": "Từ khóa tìm kiếm (VD: 'nhạc rock', 'hài kịch')"}
                        }
                    }
                }
            },
            {
                "type": "function",
                "function": {
                    "name": "get_event_details",
                    "description": "Lấy thông tin chi tiết (giá, địa điểm, thời gian) của sự kiện.",
                    "parameters": {
                        "type": "object",
                        "properties": {
                            "event_id": {"type": "string", "description": "ID của sự kiện (MongoDB ID)"}
                        },
                        "required": ["event_id"]
                    }
                }
            }
        ]
        
        sales_tools = [
            {
                "type": "function",
                "function": {
                    "name": "save_lead",
                    "description": "Lưu thông tin khách hàng quan tâm (Lead).",
                    "parameters": {
                        "type": "object",
                        "properties": {
                            "email": {"type": "string", "description": "Email address"},
                            "phone": {"type": "string", "description": "Phone number"},
                            "interest": {"type": "string", "description": "What they're interested in"}
                        }
                    }
                }
            }
        ]
        
        feedback_tools = [
            {
                "type": "function",
                "function": {
                    "name": "get_purchased_events",
                    "description": "Kiểm tra lịch sử các sự kiện user đã mua vé hoặc tham gia.",
                    "parameters": {
                        "type": "object",
                        "properties": {
                            "user_id": {"type": "string", "description": "ID của user"}
                        },
                        "required": ["user_id"]
                    }
                }
            },
            {
                "type": "function",
                "function": {
                    "name": "save_feedback",
                    "description": "Lưu đánh giá/feedback của user về sự kiện.",
                    "parameters": {
                        "type": "object",
                        "properties": {
                            "event_id": {"type": "string", "description": "ID sự kiện"},
                            "rating": {"type": "integer", "description": "Số sao đánh giá (1-5)"},
                            "comment": {"type": "string", "description": "Nội dung nhận xét"}
                        },
                        "required": ["event_id", "rating"]
                    }
                }
            }
        ]
        
        if mode == "feedback":
            return common_tools + feedback_tools
        else:
            return common_tools + sales_tools
    
    async def chat(
        self,
        user_message: str,
        conversation_history: List[Dict],
        mode: str = "sales",  # "sales" or "feedback"
        user_id: Optional[str] = None,
        access_token: Optional[str] = None,  # For authenticated API calls
        max_iterations: int = 3
    ) -> Dict[str, Any]:
        """
        Main conversation loop with native tool calling
        
        Args:
            user_message: User's input
            conversation_history: Previous messages [{"role": "user", "content": ...}, ...]
            mode: "sales" or "feedback"
            user_id: User ID (for feedback mode to check purchase history)
            access_token: JWT token for authenticated API calls
            max_iterations: Maximum tool call iterations to prevent infinite loops
        
        Returns:
            {
                "message": "Bot response",
                "tool_calls": [...],  # List of tools called (for debugging)
                "mode": mode
            }
        """
        print(f"\n🤖 Agent Mode: {mode}")
        print(f"👤 User Message: {user_message}")
        print(f"🔑 Auth Info:")
        print(f"  - User ID: {user_id}")
        print(f"  - Access Token: {'✅ Received' if access_token else '❌ None'}")
        
        # Store user_id and access_token for tool calls
        self.current_user_id = user_id
        self.current_access_token = access_token
        if access_token:
            print(f"  - Stored access_token for tools: {access_token[:20]}...")
        if user_id:
            print(f"  - Stored user_id for tools: {user_id}")
        
        # Select system prompt (without tool instructions - native tools handle this)
        system_prompt = self._get_system_prompt(mode)
        
        # Get native tools for this mode
        tools = self._get_native_tools(mode)
        
        # Build conversation context
        messages = self._build_messages(system_prompt, conversation_history, user_message)
        
        # Agentic loop: LLM may call tools multiple times
        tool_calls_made = []
        current_response = None
        
        for iteration in range(max_iterations):
            print(f"\n🔄 Iteration {iteration + 1}")
            
            # Call LLM with native tools
            llm_result = await self._call_llm_with_tools(messages, tools)
            
            # Check if this is a final text response or a tool call
            if llm_result["type"] == "text":
                current_response = llm_result["content"]
                print(f"🧠 LLM Final Response: {current_response[:200]}...")
                break
            
            elif llm_result["type"] == "tool_calls":
                # Process each tool call
                for tool_call in llm_result["tool_calls"]:
                    tool_name = tool_call["function"]["name"]
                    arguments = json.loads(tool_call["function"]["arguments"])
                    
                    print(f"🔧 Tool Called: {tool_name}")
                    print(f"   Arguments: {arguments}")
                    
                    # Auto-inject real user_id for get_purchased_events
                    if tool_name == 'get_purchased_events' and self.current_user_id:
                        print(f"🔄 Auto-injecting real user_id: {self.current_user_id}")
                        arguments['user_id'] = self.current_user_id
                    
                    # Execute tool
                    tool_result = await self.tools_service.execute_tool(
                        tool_name,
                        arguments,
                        access_token=self.current_access_token
                    )
                    
                    # Record tool call
                    tool_calls_made.append({
                        "function": tool_name,
                        "arguments": arguments,
                        "result": tool_result
                    })
                    
                    # Handle RAG search specially
                    if isinstance(tool_result, dict) and tool_result.get("action") == "run_rag_search":
                        tool_result = await self._execute_rag_search(tool_result["query"])
                    
                    # Add assistant's tool call to messages
                    messages.append({
                        "role": "assistant",
                        "content": None,
                        "tool_calls": [{
                            "id": tool_call.get("id", f"call_{iteration}"),
                            "type": "function",
                            "function": {
                                "name": tool_name,
                                "arguments": json.dumps(arguments)
                            }
                        }]
                    })
                    
                    # Add tool result to messages
                    messages.append({
                        "role": "tool",
                        "tool_call_id": tool_call.get("id", f"call_{iteration}"),
                        "content": self._format_tool_result({"result": tool_result})
                    })
            
            elif llm_result["type"] == "error":
                print(f"⚠️ LLM Error: {llm_result['content']}")
                current_response = "Xin lỗi, tôi đang gặp chút vấn đề kỹ thuật. Bạn thử lại sau nhé!"
                break
        
        # Get final response if we hit max iterations
        final_response = current_response or "Tôi cần thêm thông tin để hỗ trợ bạn."
        
        return {
            "message": final_response,
            "tool_calls": tool_calls_made,
            "mode": mode
        }
    
    def _get_system_prompt(self, mode: str) -> str:
        """Get system prompt for selected mode (without tool instructions)"""
        prompt_key = f"{mode}_agent" if mode in ["sales", "feedback"] else "sales_agent"
        return self.prompts.get(prompt_key, "")
    
    def _build_messages(
        self,
        system_prompt: str,
        history: List[Dict],
        user_message: str
    ) -> List[Dict]:
        """Build messages array for LLM"""
        messages = [{"role": "system", "content": system_prompt}]
        
        # Add conversation history
        messages.extend(history)
        
        # Add current user message
        messages.append({"role": "user", "content": user_message})
        
        return messages
    
    async def _call_llm_with_tools(self, messages: List[Dict], tools: List[Dict]) -> Dict:
        """
        Call HuggingFace LLM with native tool calling support
        
        Returns:
            {"type": "text", "content": "..."} for text responses
            {"type": "tool_calls", "tool_calls": [...]} for tool call requests
            {"type": "error", "content": "..."} for errors
        """
        try:
            from huggingface_hub import AsyncInferenceClient
            
            # Create async client - Qwen2.5 works on default HuggingFace API
            client = AsyncInferenceClient(token=self.hf_token)
            
            # Call HF API with chat completion and native tools
            # Qwen2.5-72B-Instruct: Best for Vietnamese - state-of-the-art performance
            response = await client.chat_completion(
                messages=messages,
                model="Qwen/Qwen2.5-72B-Instruct",  # Best for Vietnamese + tool calling
                max_tokens=1024,  # Increased to prevent truncation
                temperature=0.7,
                tools=tools,
                tool_choice="auto"  # Let model decide when to use tools
            )
            
            # Check if the model made tool calls
            message = response.choices[0].message
            
            if message.tool_calls:
                print(f"🔧 Native tool calls detected: {len(message.tool_calls)}")
                return {
                    "type": "tool_calls",
                    "tool_calls": [
                        {
                            "id": tc.id,
                            "function": {
                                "name": tc.function.name,
                                "arguments": tc.function.arguments
                            }
                        }
                        for tc in message.tool_calls
                    ]
                }
            else:
                # Regular text response
                return {
                    "type": "text",
                    "content": message.content or ""
                }
                
        except Exception as e:
            print(f"⚠️ LLM Call Error: {e}")
            return {
                "type": "error",
                "content": str(e)
            }
    
    def _format_tool_result(self, tool_result: Dict) -> str:
        """Format tool result for feeding back to LLM"""
        result = tool_result.get("result", {})
        
        # Special handling for purchased events list
        if isinstance(result, list):
            print(f"\n🔍 Formatting {len(result)} items for LLM")
            if not result:
                return "Không tìm thấy dữ liệu nào phù hợp."
            
            # Format each event clearly
            formatted_events = []
            for i, event in enumerate(result, 1):
                # Handle both object/dict and string results
                if isinstance(event, str):
                    formatted_events.append(f"{i}. {event}")
                    continue
                    
                event_info = []
                event_info.append(f"Event {i}:")
                
                # Extract key fields
                if 'eventName' in event:
                    event_info.append(f"  Name: {event['eventName']}")
                if 'eventCode' in event:
                    event_info.append(f"  Code: {event['eventCode']}")
                if '_id' in event:
                    event_info.append(f"  ID: {event['_id']}")
                if 'startTimeEventTime' in event:
                    event_info.append(f"  Date: {event['startTimeEventTime']}")
                # Handle RAG result payload structure
                if 'texts' in event: # Flat text from RAG
                     event_info.append(f"  Content: {event['texts']}")
                if 'id_use' in event:
                     event_info.append(f"  ID: {event['id_use']}")
                
                formatted_events.append("\n".join(event_info))
            
            formatted = "Tool Results:\n\n" + "\n\n".join(formatted_events)
            # print(f"📤 Sending to LLM:\n{formatted}") # Reduce noise
            return formatted
        
        # Default formatting for other results
        if isinstance(result, dict):
            # Pretty print key info
            formatted = []
            for key, value in result.items():
                if key not in ["success", "error"]:
                    formatted.append(f"{key}: {value}")
            return "\n".join(formatted) if formatted else json.dumps(result)
        
        return str(result)
    
    async def _execute_rag_search(self, query_params: Dict) -> str:
        """
        Execute RAG search with Strict Relevance Filtering
        Called when LLM wants to search_events
        """
        query = query_params.get("query", "").strip()
        vibe = query_params.get("vibe", "").strip()
        time = query_params.get("time", "").strip()
        
        # Score threshold to filter weak/irrelevant matches
        SCORE_THRESHOLD = 0.5  # Tune this value (0.4-0.6)
        
        # Build search strategies
        search_strategies = []
        user_gave_specific_keyword = bool(query)  # If user gave a keyword, don't fallback to vibe
        
        # 1. Full combination (Specific)
        full_query = f"{query} {vibe} {time}".strip()
        if full_query:
            search_strategies.append(("Full Context", full_query))
            
        # 2. Main keyword only (Broad) - Critical for terms like "rượu"
        if query and query != full_query:
            search_strategies.append(("Keyword Only", query))
            
        # 3. Vibe only (Fallback) - ONLY if user didn't provide specific keyword
        if vibe and not user_gave_specific_keyword and vibe != full_query:
            search_strategies.append(("Vibe Only", vibe))
            
        print(f"[SEARCH] Plan: {[s[0] for s in search_strategies]}")
        print(f"[SEARCH] User gave specific keyword: {user_gave_specific_keyword}")
        
        final_results = []
        seen_ids = set()
        
        for strategy_name, search_text in search_strategies:
            if not search_text:
                continue
                
            print(f"[SEARCH] Trying: {strategy_name} ('{search_text}')")
            
            # Use embedding + qdrant with score threshold
            embedding = self.embedding_service.encode_text(search_text)
            results = self.qdrant_service.search(
                query_embedding=embedding,
                limit=5,
                score_threshold=SCORE_THRESHOLD  # Filter weak matches
            )
            
            # Deduplicate and add results
            count = 0
            for res in results:
                doc_id = res['id']
                score = res.get('confidence', 0)
                if doc_id not in seen_ids:
                    seen_ids.add(doc_id)
                    final_results.append(res)
                    count += 1
                    print(f"   + Added: {doc_id[:20]}... (score: {score:.3f})")
            
            print(f"   Found {count} new results (Total: {len(final_results)})")
            
            # If we have enough results, stop
            if len(final_results) >= 5:
                break
        
        # Format results
        formatted = []
        for i, result in enumerate(final_results[:5], 1): # Limit to top 5
            # NOTE: qdrant_service returns "metadata" not "payload"
            payload = result.get("metadata", {})
            
            # DEBUG: Log payload keys for first result
            if i == 1:
                print(f"[DEBUG] First Result Keys: {list(result.keys())}")
                print(f"[DEBUG] Metadata Keys: {list(payload.keys())}")
                if 'texts' not in payload and 'text' in payload:
                     print(f"[INFO] Found 'text' but not 'texts'. Using 'text' field.")
            
            # Robust extraction of text content
            texts = payload.get("texts", [])
            if isinstance(texts, list) and texts:
                text = texts[0] # List of chunks
            elif isinstance(texts, str):
                text = texts # Single string
            elif 'text' in payload:
                text = payload.get('text') # Fallback to 'text' field
            else:
                text = ""
                
            event_id = payload.get("id_use", "")
            
            if not text:
                print(f"⚠️ Result {i} skipped: Empty text content. ID: {event_id}")
                continue

            # Clean and truncate text for context window
            clean_text = str(text).replace("\n", " ").strip()
            formatted.append(f"Event Found: {clean_text[:300]}... (ID: {event_id})")
        
        if not formatted:
            print("❌ RAG Search returned 0 usable results after all strategies")
            return "SYSTEM_MESSAGE: Không tìm thấy sự kiện nào trong cơ sở dữ liệu phù hợp với yêu cầu. Hãy báo lại cho khách hàng: 'Hiện tại mình chưa tìm thấy sự kiện nào phù hợp với yêu cầu này, bạn thử đổi tiêu chí xem sao nhé?'"
            
        print(f"✅ Returning {len(formatted)} events to LLM")
        return "\n\n".join(formatted)