""" 測試意圖識別邏輯 - 不依賴外部套件 """ def test_product_intent_logic(): """測試商品意圖識別邏輯""" def analyze_query_intent_simple(message: str): """簡化版的意圖分析""" message_lower = message.lower() # 商品查詢關鍵字(擴展版) product_keywords = [ '推薦', '有沒有', '是否有', '請問有', '商品', '產品', '貨品', '查詢', '搜尋', '找', '庫存', '存貨', '價格', '多少錢', '貓砂', '狗糧', '寵物', '食品', '用品', '貓', '狗', '寵物用品', 'cat', 'dog', 'pet', 'litter', 'food' # 英文關鍵字 ] # 推薦查詢關鍵字 recommendation_keywords = ['推薦', '建議', '介紹', '有什麼', '哪些', '什麼好', '推薦一些'] # 庫存查詢關鍵字 inventory_keywords = ['庫存', '存貨', '剩餘', '還有', '現貨', '有多少', '剩多少'] is_product_query = any(keyword in message_lower for keyword in product_keywords) is_recommendation = any(keyword in message_lower for keyword in recommendation_keywords) is_inventory_check = any(keyword in message_lower for keyword in inventory_keywords) confidence = 0.5 if is_product_query: confidence += 0.3 if is_recommendation: confidence += 0.2 if is_inventory_check: confidence += 0.2 return { "is_product_query": is_product_query, "is_recommendation": is_recommendation, "is_inventory_check": is_inventory_check, "confidence": min(confidence, 1.0), "intent": "product_query" if is_product_query else "unknown" } def quick_intent_check_simple(message: str): """簡化版的快速意圖檢查""" message_lower = message.lower() # 幫助相關關鍵字(優先處理) help_keywords = ['幫助', 'help', '說明', '怎麼用', '指令', '功能', '統計', 'stats', '選單', 'menu'] # 非商品的業務查詢關鍵字(避免與商品查詢衝突) business_keywords = [ '訂單狀態', '訂單查詢', '我的訂單', '交易記錄', '購買記錄', '客戶資料', '會員資料', '帳戶資訊', '銷售報表', '財務報表' ] if any(keyword in message_lower for keyword in help_keywords): return "help" elif any(keyword in message_lower for keyword in business_keywords): return "search" else: # 預設返回 "chat",讓 Pydantic AI 有機會處理商品查詢 return "chat" # 測試案例 test_messages = [ "你好, 請問有沒有貓砂相關產品?", "是否有推薦貓砂?", "有什麼寵物用品?", "查詢狗糧庫存", "貓砂還有嗎?", "推薦一些好的貓砂", "你好!今天天氣如何?", # 非商品查詢 "/help", "統計", "我的訂單狀態" ] print("🧪 測試意圖識別邏輯") print("=" * 80) for i, message in enumerate(test_messages, 1): print(f"\n{i}. 測試訊息: '{message}'") # 1. 快速意圖檢查 quick_intent = quick_intent_check_simple(message) print(f" 快速意圖: {quick_intent}") # 2. Pydantic AI 意圖分析 product_intent = analyze_query_intent_simple(message) print(f" 商品查詢: {product_intent['is_product_query']}") print(f" 推薦查詢: {product_intent['is_recommendation']}") print(f" 庫存查詢: {product_intent['is_inventory_check']}") print(f" 信心度: {product_intent['confidence']:.2f}") # 3. 路由決策模擬 should_use_pydantic_ai = ( product_intent["is_product_query"] and product_intent["confidence"] > 0.5 ) if quick_intent == "help": final_route = "幫助模式" elif should_use_pydantic_ai: final_route = "🛍️ Pydantic AI 商品查詢" elif quick_intent == "search": final_route = "傳統搜尋模式" else: final_route = "聊天模式" print(f" 👉 最終路由: {final_route}") # 特別標記貓砂相關查詢 if "貓砂" in message or "貓" in message: print(f" 🐱 貓砂查詢檢測: {'✅ 應該被 Pydantic AI 處理' if should_use_pydantic_ai else '❌ 可能被錯誤路由'}") def test_keyword_extraction(): """測試關鍵字提取邏輯""" def extract_keywords_simple(query_text: str): """簡化版關鍵字提取""" # 移除常見的查詢詞彙 stop_words = ['推薦', '有沒有', '是否有', '請問', '想要', '需要', '找', '查詢', '搜尋', '?', '?'] # 分割並清理關鍵字 words = query_text.replace('?', '').replace('?', '').split() keywords = [word for word in words if word not in stop_words and len(word) > 1] return keywords if keywords else [query_text.strip()] print(f"\n\n🔍 測試關鍵字提取") print("=" * 50) test_queries = [ "你好, 請問有沒有貓砂相關產品?", "是否有推薦貓砂?", "有什麼寵物用品?", "查詢狗糧庫存" ] for query in test_queries: keywords = extract_keywords_simple(query) print(f"'{query}' → {keywords}") def main(): """主函數""" print("🚀 開始意圖識別測試\n") test_product_intent_logic() test_keyword_extraction() print("\n" + "=" * 80) print("✅ 測試完成!") print("\n💡 分析結果:") print("1. 檢查貓砂相關查詢是否被正確識別為商品查詢") print("2. 確認信心度是否超過 0.5 閾值") print("3. 驗證路由決策是否正確") if __name__ == "__main__": main()