davidtran999 commited on
Commit
bc197a5
·
verified ·
1 Parent(s): 1672c0c

Upload backend/test_api_mode_with_docs.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. backend/test_api_mode_with_docs.py +52 -0
backend/test_api_mode_with_docs.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """Test API mode with documents."""
3
+ import os
4
+ import sys
5
+
6
+ # Set environment
7
+ os.environ['LLM_PROVIDER'] = 'api'
8
+ os.environ['HF_API_BASE_URL'] = 'https://davidtran999-hue-portal-backend.hf.space/api'
9
+
10
+ # Add path
11
+ sys.path.insert(0, 'hue_portal')
12
+
13
+ # Setup Django
14
+ os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'hue_portal.settings')
15
+ import django
16
+ django.setup()
17
+
18
+ from hue_portal.chatbot.llm_integration import get_llm_generator
19
+ from hue_portal.core.models import Fine
20
+
21
+ # Get LLM
22
+ llm = get_llm_generator()
23
+ print(f"✅ LLM Provider: {llm.provider}")
24
+ print(f"✅ API URL: {llm.api_base_url}")
25
+ print(f"✅ Available: {llm.is_available()}\n")
26
+
27
+ # Get some documents
28
+ fines = Fine.objects.all()[:3]
29
+ print(f"📄 Found {len(fines)} documents\n")
30
+
31
+ # Test with documents
32
+ query = "Mức phạt vượt đèn đỏ là bao nhiêu?"
33
+ print(f"❓ Query: {query}\n")
34
+
35
+ # Build prompt
36
+ prompt = llm._build_prompt(query, None, list(fines))
37
+ print(f"📝 Prompt length: {len(prompt)} chars")
38
+ print(f"📝 Prompt preview:\n{prompt[:500]}...\n")
39
+
40
+ # Test API call
41
+ print("🔗 Calling HF Spaces API...\n")
42
+ result = llm._generate_api(prompt, None)
43
+
44
+ if result:
45
+ print(f"✅ Success! Response length: {len(result)}")
46
+ print(f"📥 Response:\n{result[:500]}...\n")
47
+ else:
48
+ print("❌ No response from API\n")
49
+
50
+
51
+
52
+