77 lines
3 KiB
Python
77 lines
3 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Test script to verify the fallback mechanism in interfaceAiObjects.py
|
|
"""
|
|
|
|
import asyncio
|
|
import sys
|
|
import os
|
|
import logging
|
|
from pathlib import Path
|
|
|
|
# Add the gateway directory to the Python path
|
|
gateway_dir = Path(__file__).parent
|
|
sys.path.insert(0, str(gateway_dir))
|
|
|
|
# Set up logging
|
|
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
logger = logging.getLogger(__name__)
|
|
|
|
async def test_fallback_mechanism():
|
|
"""Test the fallback mechanism by simulating a failing primary model."""
|
|
try:
|
|
from modules.interfaces.interfaceAiObjects import AiObjects
|
|
from modules.datamodels.datamodelAi import AiCallRequest, AiCallOptions, OperationType
|
|
|
|
logger.info("🧪 Testing fallback mechanism...")
|
|
|
|
# Create AiObjects instance
|
|
ai_objects = await AiObjects.create()
|
|
logger.info("✅ AiObjects created successfully")
|
|
|
|
# Test 1: Normal operation (should work with primary model)
|
|
logger.info("📝 Test 1: Normal operation")
|
|
request = AiCallRequest(
|
|
prompt="Hello, this is a test prompt. Please respond with 'Test successful'.",
|
|
context="",
|
|
options=AiCallOptions(operationType=OperationType.GENERAL)
|
|
)
|
|
|
|
try:
|
|
response = await ai_objects.call(request)
|
|
logger.info(f"✅ Test 1 successful: {response.modelName} - {response.content[:50]}...")
|
|
except Exception as e:
|
|
logger.warning(f"⚠️ Test 1 failed: {str(e)}")
|
|
|
|
# Test 2: Image analysis fallback
|
|
logger.info("🖼️ Test 2: Image analysis fallback")
|
|
try:
|
|
# Create a dummy image data (base64 encoded 1x1 pixel)
|
|
dummy_image = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg=="
|
|
result = await ai_objects.callImage(
|
|
prompt="Describe this image",
|
|
imageData=dummy_image,
|
|
mimeType="image/png",
|
|
options=AiCallOptions(operationType=OperationType.IMAGE_ANALYSIS)
|
|
)
|
|
logger.info(f"✅ Test 2 successful: {result[:50]}...")
|
|
except Exception as e:
|
|
logger.warning(f"⚠️ Test 2 failed: {str(e)}")
|
|
|
|
# Test 3: Test fallback model selection
|
|
logger.info("🔄 Test 3: Fallback model selection")
|
|
fallback_models = ai_objects._getFallbackModels(OperationType.GENERAL)
|
|
logger.info(f"✅ Fallback models for GENERAL: {fallback_models}")
|
|
|
|
fallback_models_image = ai_objects._getFallbackModels(OperationType.IMAGE_ANALYSIS)
|
|
logger.info(f"✅ Fallback models for IMAGE_ANALYSIS: {fallback_models_image}")
|
|
|
|
logger.info("🎉 Fallback mechanism test completed!")
|
|
|
|
except Exception as e:
|
|
logger.error(f"❌ Test failed: {str(e)}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(test_fallback_mechanism())
|