|
| 1 | +<?php |
| 2 | + |
| 3 | +require_once '../vendor/autoload.php'; |
| 4 | + |
| 5 | +use Joomla\AI\Provider\AnthropicProvider; |
| 6 | + |
| 7 | +echo "=== Anthropic Provider Comprehensive Test ===\n\n"; |
| 8 | + |
| 9 | +$configFile = __DIR__ . '/../config.json'; |
| 10 | +$config = json_decode(file_get_contents($configFile), true); |
| 11 | +$api_key = $config['anthropic_api_key'] ?? null; |
| 12 | + |
| 13 | +try { |
| 14 | + // Create provider |
| 15 | + $provider = new AnthropicProvider([ |
| 16 | + 'api_key' => $api_key |
| 17 | + ]); |
| 18 | + |
| 19 | + echo "Provider: " . $provider->getName() . "\n"; |
| 20 | + echo "Provider created successfully!\n\n"; |
| 21 | + |
| 22 | + // Test 1: Get all available models |
| 23 | + echo "=== Test 1: Get Available Models ===\n"; |
| 24 | + |
| 25 | + $availableModels = $provider->getAvailableModels(); |
| 26 | + echo "Available Models: " . implode(', ', $availableModels) . "\n"; |
| 27 | + echo "Total: " . count($availableModels) . " models\n"; |
| 28 | + |
| 29 | + echo "\n" . str_repeat("=", 60) . "\n\n"; |
| 30 | + |
| 31 | + // Test 2: Test with a known model ID (if different from available models) |
| 32 | + echo "=== Test 2: Get Known Model (claude-3-haiku-20240307) ===\n"; |
| 33 | + |
| 34 | + try { |
| 35 | + $validModelResponse = $provider->getModel('claude-3-haiku-20240307'); |
| 36 | + |
| 37 | + echo "Model Information:\n"; |
| 38 | + echo $validModelResponse->getContent() . "\n"; |
| 39 | + |
| 40 | + } catch (Exception $e) { |
| 41 | + echo "Error getting known model: " . $e->getMessage() . "\n"; |
| 42 | + } |
| 43 | + |
| 44 | + echo "\n" . str_repeat("=", 60) . "\n\n"; |
| 45 | + |
| 46 | + // Test 3: Test with invalid model ID (error handling) |
| 47 | + echo "=== Test 3: Error Handling - Invalid Model ID ===\n"; |
| 48 | + |
| 49 | + try { |
| 50 | + $invalidModelResponse = $provider->getModel('invalid-model-12345'); |
| 51 | + echo "Unexpected success with invalid model\n"; |
| 52 | + } catch (Exception $e) { |
| 53 | + echo "Expected error caught: " . $e->getMessage() . "\n"; |
| 54 | + } |
| 55 | + |
| 56 | + echo "\n" . str_repeat("=", 60) . "\n\n"; |
| 57 | + |
| 58 | + // Test 4: Basic Chat with saveFile |
| 59 | + echo "=== Test 4: Basic Chat with saveFile ===\n"; |
| 60 | + echo str_repeat("-", 50) . "\n"; |
| 61 | + |
| 62 | + $chatResponse = $provider->chat( |
| 63 | + "Hi", |
| 64 | + [ |
| 65 | + 'model' => 'claude-3-haiku-20240307', |
| 66 | + 'max_tokens' => 150 |
| 67 | + ] |
| 68 | + ); |
| 69 | + |
| 70 | + echo "Chat Response:\n"; |
| 71 | + echo $chatResponse->getContent() . "\n"; |
| 72 | + echo "Status: " . $chatResponse->getStatusCode() . "\n"; |
| 73 | + echo "Provider: " . $chatResponse->getProvider() . "\n"; |
| 74 | + |
| 75 | + $metadata = $chatResponse->getMetadata(); |
| 76 | + echo "Model: " . $metadata['model'] . "\n"; |
| 77 | + echo "Input Tokens: " . $metadata['input_tokens'] . "\n"; |
| 78 | + echo "Output Tokens: " . $metadata['output_tokens'] . "\n"; |
| 79 | + echo "Stop Reason: " . $metadata['stop_reason'] . "\n"; |
| 80 | + |
| 81 | + echo "\n" . str_repeat("=", 60) . "\n\n"; |
| 82 | + |
| 83 | + // Test 5: Vision Analysis |
| 84 | + echo "=== Test 5: Vision Analysis ===\n"; |
| 85 | + echo str_repeat("-", 50) . "\n"; |
| 86 | + |
| 87 | + $testImage = "test_files/fish.png"; |
| 88 | + $visionResponse = $provider->vision( |
| 89 | + "What do you see in this image? Describe in one line.", |
| 90 | + $testImage, |
| 91 | + ); |
| 92 | + |
| 93 | + echo "Vision Response:\n"; |
| 94 | + echo $visionResponse->getContent() . "\n"; |
| 95 | + echo "Status: " . $visionResponse->getStatusCode() . "\n"; |
| 96 | + |
| 97 | + $visionMetadata = $visionResponse->getMetadata(); |
| 98 | + echo "Model: " . $visionMetadata['model'] . "\n"; |
| 99 | + echo "Input Tokens: " . $visionMetadata['input_tokens'] . "\n"; |
| 100 | + echo "Output Tokens: " . $visionMetadata['output_tokens'] . "\n"; |
| 101 | + |
| 102 | + echo "\n" . str_repeat("=", 60) . "\n\n"; |
| 103 | + |
| 104 | + // Test 6: Advanced Chat with Options |
| 105 | + echo "=== Test 6: Advanced Chat with Options ===\n"; |
| 106 | + echo str_repeat("-", 50) . "\n"; |
| 107 | + |
| 108 | + $advancedResponse = $provider->chat( |
| 109 | + "What is the full form of AI?", |
| 110 | + [ |
| 111 | + 'model' => 'claude-3-haiku-20240307', |
| 112 | + 'max_tokens' => 200, |
| 113 | + 'temperature' => 0.7, |
| 114 | + 'top_p' => 0.9, |
| 115 | + 'stop_sequences' => ['\n\n\n'] |
| 116 | + ] |
| 117 | + ); |
| 118 | + |
| 119 | + echo "Advanced Chat Response:\n"; |
| 120 | + echo $advancedResponse->getContent() . "\n"; |
| 121 | + |
| 122 | + $advancedMetadata = $advancedResponse->getMetadata(); |
| 123 | + echo "Model: " . $advancedMetadata['model'] . "\n"; |
| 124 | + echo "Input Tokens: " . $advancedMetadata['input_tokens'] . "\n"; |
| 125 | + echo "Output Tokens: " . $advancedMetadata['output_tokens'] . "\n"; |
| 126 | + echo "Stop Reason: " . $advancedMetadata['stop_reason'] . "\n"; |
| 127 | + |
| 128 | + echo "\n" . str_repeat("=", 60) . "\n\n"; |
| 129 | + |
| 130 | + echo "\nAll Anthropic tests completed successfully!\n"; |
| 131 | + |
| 132 | +} catch (Exception $e) { |
| 133 | + echo "Test failed with error: " . $e->getMessage() . "\n"; |
| 134 | +} |
0 commit comments