Files
railseek6/test_rerank_final.py
2026-01-13 09:51:35 +08:00

164 lines
6.0 KiB
Python
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import requests
import json
import time
import sys
def check_server_health():
"""Check if server is running"""
try:
response = requests.get("http://localhost:3015/health", timeout=5)
print(f"Server health: {response.status_code}")
if response.status_code == 200:
print("✅ Server is running")
return True
else:
print(f"❌ Server returned status {response.status_code}")
return False
except Exception as e:
print(f"❌ Server not reachable: {e}")
return False
def test_query_with_rerank():
"""Test query with rerank enabled"""
url = "http://localhost:3015/query"
headers = {
"Content-Type": "application/json",
"X-API-Key": "jleu1212",
"X-Workspace": "test1"
}
query = "what is odds"
print(f"\nTesting query: '{query}' for workspace: test1")
print("="*60)
# Test with rerank enabled
print("\n1. Testing WITH rerank enabled (enable_rerank=True):")
data_with_rerank = {
"query": query,
"enable_rerank": True,
"only_need_context": True
}
try:
start_time = time.time()
response = requests.post(url, headers=headers, json=data_with_rerank, timeout=30)
elapsed = time.time() - start_time
print(f" Status Code: {response.status_code}")
print(f" Response Time: {elapsed:.2f}s")
if response.status_code == 200:
result = response.json()
response_text = result.get('response', '')
# Check for rerank-related messages
if "Rerank is enabled but no rerank model is configured" in response_text:
print(" ⚠️ Rerank warning found: 'Rerank is enabled but no rerank model is configured'")
print(" This means the checkbox works but Jina API is not configured")
return False
elif "Successfully reranked" in response_text:
print(" ✅ Rerank success message found!")
return True
elif "jina" in response_text.lower():
print(" ✅ Jina-related content found!")
return True
else:
print(" No rerank indicators found in response")
# Check if we can find any rerank scores
if "rerank_score" in response_text.lower():
print(" ✅ Rerank scores found in response!")
return True
else:
print(" No rerank scores found")
return False
else:
print(f" ❌ Error: {response.status_code}")
print(f" Response: {response.text[:200]}")
return False
except Exception as e:
print(f" ❌ Error: {e}")
return False
def check_server_logs_for_rerank():
"""Check server logs for rerank configuration"""
print("\n2. Checking server logs for rerank configuration...")
try:
# Read the last few lines of the log file
with open("lightrag.log", "r", encoding="utf-8") as f:
lines = f.readlines()
last_lines = lines[-50:] # Last 50 lines
# Look for rerank-related messages
rerank_found = False
for line in last_lines:
if "rerank" in line.lower():
print(f" Found: {line.strip()}")
rerank_found = True
if "disabled" in line.lower():
print(" ❌ Rerank is disabled in server logs")
return False
elif "enabled" in line.lower():
print(" ✅ Rerank is enabled in server logs")
return True
if not rerank_found:
print(" No rerank-related messages found in recent logs")
return False
except Exception as e:
print(f" ❌ Error reading logs: {e}")
return False
def main():
print("="*60)
print("FINAL TEST: Jina Rerank Configuration Verification")
print("="*60)
# Step 1: Check server health
if not check_server_health():
print("\n❌ Server is not running. Please start the server first.")
return
# Wait a moment for server to fully initialize
print("\nWaiting 5 seconds for server initialization...")
time.sleep(5)
# Step 2: Check server logs
logs_ok = check_server_logs_for_rerank()
# Step 3: Test query with rerank
query_ok = test_query_with_rerank()
# Step 4: Final analysis
print("\n" + "="*60)
print("FINAL ANALYSIS:")
print("="*60)
if logs_ok and query_ok:
print("✅ SUCCESS: Jina rerank appears to be configured and working!")
print(" - Server logs show rerank is enabled")
print(" - Query with enable_rerank=True works without warnings")
elif not logs_ok and query_ok:
print("⚠️ PARTIAL SUCCESS: Query works but server logs don't show rerank")
print(" - The 'enable rerank' checkbox is functional")
print(" - Server may need to be restarted with --rerank-binding jina")
elif logs_ok and not query_ok:
print("⚠️ PARTIAL SUCCESS: Server configured but query shows warnings")
print(" - Server is configured for rerank")
print(" - Jina API key may be missing or invalid")
else:
print("❌ FAILURE: Rerank is not properly configured")
print(" - Server needs to be restarted with modified start_server.py")
print(" - Check that --rerank-binding jina is set")
print("\nNext steps:")
print("1. If 'Rerank is enabled but no rerank model is configured' appears,")
print(" the server needs a valid Jina API key")
print("2. Get a Jina API key from https://jina.ai/")
print("3. Update the JINA_API_KEY in start_server.py")
print("4. Restart the server")
if __name__ == "__main__":
main()