| |
| """ |
| Test script for the enhanced narrator analyzer agent that uses Shamela scraper. |
| """ |
|
|
| import asyncio |
| import os |
| import sys |
| from pathlib import Path |
|
|
| |
| project_root = Path(__file__).parent |
| sys.path.insert(0, str(project_root)) |
|
|
| from app.agent.services import LLMService |
| from pprint import pprint |
|
|
|
|
| async def test_single_narrator(): |
| """Test analyzing a single narrator.""" |
| print("🔍 Testing Single Narrator Analysis") |
| print("=" * 50) |
| |
| llm_service = LLMService() |
| |
| |
| narrator_name = "زرارة بن أعين" |
| print(f"Analyzing: {narrator_name}") |
| |
| result = await llm_service.analyze_narrator(narrator_name) |
| |
| print(f"\n📊 Results for {narrator_name}:") |
| print(f"Name: {result.narrator_name}") |
| print(f"Grade: {result.reliability_grade}") |
| print(f"Confidence: {result.confidence_level}") |
| print(f"Success: {result.success}") |
| print(f"Message: {result.message}") |
| print(f"\nReasoning:\n{result.reasoning}") |
| print(f"\nScholarly Consensus:\n{result.scholarly_consensus}") |
| print(f"\nBiographical Info:\n{result.biographical_info}") |
| if result.known_issues: |
| print(f"\nKnown Issues:\n{result.known_issues}") |
| print(f"\nRecommendation:\n{result.recommendation}") |
|
|
|
|
| async def test_narrator_chain(): |
| """Test analyzing a chain of narrators.""" |
| print("\n\n🔗 Testing Narrator Chain Analysis") |
| print("=" * 50) |
| |
| llm_service = LLMService() |
| |
| |
| narrator_chain = [ |
| "زرارة بن أعين", |
| "أبو جعفر الباقر", |
| "جابر بن عبد الله" |
| ] |
| |
| print(f"Analyzing chain: {' ← '.join(narrator_chain)}") |
| |
| |
| chain_results = await llm_service.analyze_narrator_chain(narrator_chain) |
| |
| print("\n📋 Individual Results:") |
| for narrator, result in chain_results.items(): |
| print(f"\n{narrator}:") |
| print(f" Grade: {result.reliability_grade}") |
| print(f" Confidence: {result.confidence_level}") |
| print(f" Success: {result.success}") |
| if not result.success: |
| print(f" Error: {result.message}") |
| |
| |
| synthesis = await llm_service.synthesize_chain_analysis(chain_results) |
| |
| print("\n🔬 Chain Synthesis:") |
| print(f"Success: {synthesis['success']}") |
| print(f"Chain Length: {synthesis['chain_length']}") |
| print(f"\nOverall Assessment:\n{synthesis['overall_assessment']}") |
|
|
|
|
| async def test_unknown_narrator(): |
| """Test analyzing an unknown/obscure narrator.""" |
| print("\n\n❓ Testing Unknown Narrator Analysis") |
| print("=" * 50) |
| |
| llm_service = LLMService() |
| |
| |
| narrator_name = "محمد بن غير مشهور" |
| print(f"Analyzing unknown narrator: {narrator_name}") |
| |
| result = await llm_service.analyze_narrator(narrator_name) |
| |
| print(f"\n📊 Results for {narrator_name}:") |
| print(f"Grade: {result.reliability_grade}") |
| print(f"Confidence: {result.confidence_level}") |
| print(f"Success: {result.success}") |
| print(f"Message: {result.message}") |
| print(f"\nReasoning:\n{result.reasoning}") |
|
|
|
|
| async def main(): |
| """Run all tests.""" |
| print("🤖 Enhanced Narrator Analyzer Agent Test Suite") |
| print("=" * 60) |
| print("This agent combines Shamela.ws scraping with LLM analysis") |
| print("=" * 60) |
| |
| try: |
| |
| if not os.getenv("GOOGLE_API_KEY"): |
| print("❌ Warning: GOOGLE_API_KEY not found in environment") |
| print(" Make sure you have set up your .env file with the API key") |
| return |
| |
| |
| await test_single_narrator() |
| await test_narrator_chain() |
| await test_unknown_narrator() |
| |
| print("\n✅ All tests completed!") |
| print("\n💡 The enhanced agent now:") |
| print(" • Scrapes data from Shamela.ws automatically") |
| print(" • Combines scraped data with LLM knowledge") |
| print(" • Provides detailed reasoning and analysis") |
| print(" • Can analyze complete chains of narrators") |
| print(" • Synthesizes chain-level assessments") |
| |
| except Exception as e: |
| print(f"❌ Test failed: {str(e)}") |
| import traceback |
| traceback.print_exc() |
|
|
|
|
| if __name__ == "__main__": |
| asyncio.run(main()) |
|
|