CoolFace
Apppublic

creativesar/face

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
test_openrouter_rag.py93 linesDownload Raw Back to root
1#!/usr/bin/env python32"""3Test script to verify OpenRouter RAG functionality4"""5import asyncio6import os7from dotenv import load_dotenv8 9# Load environment variables10load_dotenv()11 12from services.openrouter_service import OpenRouterService13from services.optimized_openrouter_rag_service import OptimizedOpenRouterRAGService14from services.qdrant_service import QdrantService15 16async def test_openrouter_rag():17    """18    Test the OpenRouter RAG service connection and functionality19    """20    print("Testing OpenRouter RAG integration...")21 22    try:23        # Initialize the OpenRouter service24        openrouter_service = OpenRouterService()25        print("✓ OpenRouter service initialized successfully")26 27        # Test basic connection28        print("\nTesting basic response generation...")29        response = await openrouter_service.generate_response("Hello, how are you?")30        print(f"✓ Response received: {response[:100]}...")31 32        # Test embedding generation33        print("\nTesting embedding generation...")34        texts = ["This is a test sentence for embeddings.", "Another test sentence."]35        embeddings = await openrouter_service.generate_embeddings(texts)36        print(f"✓ Generated embeddings for {len(texts)} texts")37        print(f"  Embedding dimensions: {len(embeddings[0])}")38 39        # Test query embedding40        print("\nTesting query embedding...")41        query_embedding = await openrouter_service.generate_embeddings_query("What is artificial intelligence?")42        print(f"✓ Query embedding generated with {len(query_embedding)} dimensions")43 44        # Test connection method45        print("\nTesting connection method...")46        connection_ok = await openrouter_service.test_connection()47        print(f"✓ Connection test: {'PASSED' if connection_ok else 'FAILED'}")48 49        print("\n🎉 All OpenRouter tests passed! The integration is working correctly.")50 51        # Now test the full RAG service52        print("\nTesting Optimized OpenRouter RAG Service...")53        qdrant_service = QdrantService()54        rag_service = OptimizedOpenRouterRAGService(openrouter_service, qdrant_service)55 56        # Test initialization57        await rag_service.initialize_collection()58        print("✓ RAG service initialized and collection created")59 60        # Test indexing content61        print("\nTesting content indexing...")62        result = await rag_service.index_content(63            content="This is a test document about artificial intelligence and robotics.",64            chapter_id="test_chapter_001",65            section_title="Test Section",66            source_url="/test/source"67        )68        print(f"✓ Content indexed successfully: {result}")69 70        # Test querying71        print("\nTesting query processing...")72        query_result = await rag_service.process_query("What is this document about?")73        print(f"✓ Query processed: {query_result['answer'][:100]}...")74 75        # Get collection stats76        stats = await rag_service.get_collection_stats()77        print(f"✓ Collection stats: {stats}")78 79        print("\n🎉 All RAG tests passed! The OpenRouter RAG system is working correctly.")80        return True81 82    except Exception as e:83        print(f"\n❌ Error during OpenRouter RAG testing: {str(e)}")84        import traceback85        traceback.print_exc()86        return False87 88if __name__ == "__main__":89    success = asyncio.run(test_openrouter_rag())90    if success:91        print("\n✅ OpenRouter RAG is properly configured and responding!")92    else:93        print("\n❌ There are issues with the OpenRouter RAG configuration.")