|
""" |
|
通過 API 測試貓砂查詢功能 |
|
""" |
|
|
|
import requests |
|
import json |
|
|
|
def test_api_endpoints(): |
|
"""測試 API 端點""" |
|
base_url = "http://localhost:7860" |
|
|
|
print("🚀 開始 API 測試\n") |
|
|
|
|
|
print("1. 測試健康檢查...") |
|
try: |
|
response = requests.get(f"{base_url}/health", timeout=10) |
|
print(f" 狀態碼: {response.status_code}") |
|
if response.status_code == 200: |
|
print(f" 回應: {response.json()}") |
|
print(" ✅ 健康檢查通過\n") |
|
except Exception as e: |
|
print(f" ❌ 健康檢查失敗: {str(e)}\n") |
|
return False |
|
|
|
|
|
print("2. 測試智能路由 API...") |
|
test_messages = [ |
|
"你好, 請問有沒有貓砂相關產品?", |
|
"是否有推薦貓砂?", |
|
"有什麼寵物用品?", |
|
"查詢狗糧庫存", |
|
"你好!今天天氣如何?" |
|
] |
|
|
|
for i, message in enumerate(test_messages, 1): |
|
print(f"\n 測試 {i}: '{message}'") |
|
try: |
|
payload = { |
|
"message": message, |
|
"user_id": "test_user_api" |
|
} |
|
|
|
response = requests.post( |
|
f"{base_url}/route", |
|
json=payload, |
|
timeout=30, |
|
headers={"Content-Type": "application/json"} |
|
) |
|
|
|
print(f" 狀態碼: {response.status_code}") |
|
|
|
if response.status_code == 200: |
|
result = response.json() |
|
print(f" 模式: {result.get('mode', 'unknown')}") |
|
print(f" 成功: {result.get('success', False)}") |
|
|
|
|
|
response_text = result.get('text', 'No response') |
|
if len(response_text) > 150: |
|
print(f" 回應: {response_text[:150]}...") |
|
else: |
|
print(f" 回應: {response_text}") |
|
|
|
|
|
if result.get('products_found') is not None: |
|
print(f" 找到商品: {result['products_found']}") |
|
if result.get('has_recommendations'): |
|
print(f" 包含推薦: {result['has_recommendations']}") |
|
|
|
else: |
|
print(f" ❌ API 錯誤: {response.text}") |
|
|
|
except Exception as e: |
|
print(f" ❌ 請求失敗: {str(e)}") |
|
|
|
|
|
print(f"\n3. 測試專門的商品查詢 API...") |
|
|
|
product_queries = [ |
|
"是否有推薦貓砂?", |
|
"有什麼寵物用品?", |
|
"查詢貓相關產品" |
|
] |
|
|
|
for i, query in enumerate(product_queries, 1): |
|
print(f"\n 商品查詢 {i}: '{query}'") |
|
try: |
|
payload = { |
|
"message": query, |
|
"user_id": "test_user_product" |
|
} |
|
|
|
response = requests.post( |
|
f"{base_url}/product-query", |
|
json=payload, |
|
timeout=30, |
|
headers={"Content-Type": "application/json"} |
|
) |
|
|
|
print(f" 狀態碼: {response.status_code}") |
|
|
|
if response.status_code == 200: |
|
result = response.json() |
|
print(f" 成功: {result.get('success', False)}") |
|
|
|
response_text = result.get('text', 'No response') |
|
if len(response_text) > 150: |
|
print(f" 回應: {response_text[:150]}...") |
|
else: |
|
print(f" 回應: {response_text}") |
|
|
|
if result.get('products_found') is not None: |
|
print(f" 找到商品: {result['products_found']}") |
|
if result.get('error'): |
|
print(f" 錯誤: {result['error']}") |
|
|
|
else: |
|
print(f" ❌ API 錯誤: {response.text}") |
|
|
|
except Exception as e: |
|
print(f" ❌ 請求失敗: {str(e)}") |
|
|
|
|
|
print(f"\n4. 測試傳統搜尋 API...") |
|
|
|
search_queries = [ |
|
"貓砂", |
|
"寵物用品", |
|
"貓" |
|
] |
|
|
|
for i, query in enumerate(search_queries, 1): |
|
print(f"\n 搜尋 {i}: '{query}'") |
|
try: |
|
payload = { |
|
"message": query, |
|
"user_id": "test_user_search" |
|
} |
|
|
|
response = requests.post( |
|
f"{base_url}/search", |
|
json=payload, |
|
timeout=30, |
|
headers={"Content-Type": "application/json"} |
|
) |
|
|
|
print(f" 狀態碼: {response.status_code}") |
|
|
|
if response.status_code == 200: |
|
result = response.json() |
|
print(f" 成功: {result.get('success', False)}") |
|
|
|
response_text = result.get('text', 'No response') |
|
if len(response_text) > 150: |
|
print(f" 回應: {response_text[:150]}...") |
|
else: |
|
print(f" 回應: {response_text}") |
|
|
|
else: |
|
print(f" ❌ API 錯誤: {response.text}") |
|
|
|
except Exception as e: |
|
print(f" ❌ 請求失敗: {str(e)}") |
|
|
|
def main(): |
|
"""主函數""" |
|
print("🧪 貓砂查詢 API 測試") |
|
print("=" * 50) |
|
|
|
test_api_endpoints() |
|
|
|
print("\n" + "=" * 50) |
|
print("✅ API 測試完成!") |
|
print("\n💡 分析建議:") |
|
print("1. 檢查智能路由是否正確識別商品查詢意圖") |
|
print("2. 比較不同 API 端點的回應差異") |
|
print("3. 確認是否真的沒有貓砂相關商品,還是查詢邏輯問題") |
|
|
|
if __name__ == "__main__": |
|
main() |
|
|