|
| 1 | +<?php |
| 2 | + |
| 3 | +require_once '../vendor/autoload.php'; |
| 4 | + |
| 5 | +use Joomla\AI\Provider\OpenAIProvider; |
| 6 | + |
| 7 | +echo "Testing Real OpenAI API Calls...\n\n"; |
| 8 | + |
| 9 | +$configFile = __DIR__ . '/../config.json'; |
| 10 | +$config = json_decode(file_get_contents($configFile), true); |
| 11 | +$api_key = $config['openai_api_key'] ?? null; |
| 12 | + |
| 13 | +try { |
| 14 | + // Create provider with your API key |
| 15 | + $provider = new OpenAIProvider([ |
| 16 | + 'api_key' => $api_key |
| 17 | + ]); |
| 18 | + |
| 19 | + echo "Provider created with API key\n"; |
| 20 | + echo "Provider name: " . $provider->getName() . "\n\n"; |
| 21 | + |
| 22 | + // To Do: Check if the provider is supported. Currently key set as env variables only |
| 23 | + // // if (!OpenAIProvider::isSupported()) { |
| 24 | + // throw new \Exception('OpenAI API is not supported or API key is missing.'); |
| 25 | + // } |
| 26 | + |
| 27 | + // Set default model for all subsequent calls |
| 28 | + $provider->setDefaultModel('gpt-3.5-turbo'); |
| 29 | + echo "Default model: " . $provider->getDefaultModel() . "\n\n"; |
| 30 | + |
| 31 | + // Test 1: Will use default model since ('gpt-3.5-turbo') |
| 32 | + // no model is specified in the options |
| 33 | + // and the default model is set |
| 34 | + echo "Test 1: Simple prompt- Will use default model gpt-3.5-turbo\n"; |
| 35 | + echo str_repeat('-', 50) . "\n"; |
| 36 | + $response = $provider->chat("Hello! How are you?"); |
| 37 | + echo "Model: " . $response->getMetadata()['model'] . "\n"; |
| 38 | + echo "Response: " . $response->getContent() . "\n"; |
| 39 | + echo "\n"; |
| 40 | + |
| 41 | + // Test 2: Will use default model for the next call again since ('gpt-3.5-turbo') |
| 42 | + // no model is specified in the options |
| 43 | + // and the default model is set |
| 44 | + echo "Test 2: Multiple Response Choices- Will use default model gpt-3.5-turbo\n"; |
| 45 | + echo str_repeat('-', 50) . "\n"; |
| 46 | + |
| 47 | + $response = $provider->chat("Suggest a name for a movie based on pilots and astronauts"); |
| 48 | + echo "Model: " . $response->getMetadata()['model'] . "\n"; |
| 49 | + echo "Response: " . $response->getContent() . "\n"; |
| 50 | + echo "\n"; |
| 51 | + |
| 52 | + // Test 3: This will override the default model since |
| 53 | + // model is specified in the options ('gpt-4o-audio-preview') |
| 54 | + echo "Test 3: Test chat completions audio capability- Will override the default and use gpt-4o-audio-preview model\n"; |
| 55 | + echo str_repeat('-', 50) . "\n"; |
| 56 | + $response = $provider->chat("Say a few words on Joomla! for about 30 seconds in english.", [ |
| 57 | + 'model' => 'gpt-4o-audio-preview', |
| 58 | + 'modalities' => ['text', 'audio'], |
| 59 | + 'audio' => [ |
| 60 | + 'voice' => 'alloy', |
| 61 | + 'format' => 'wav' |
| 62 | + ], |
| 63 | + ]); |
| 64 | + |
| 65 | + $metadata = $response->getMetadata(); |
| 66 | + echo "Model: " . $response->getMetadata()['model'] . "\n"; |
| 67 | + if (isset($metadata['choices'][0]['message']['audio']['data'])) { |
| 68 | + $audioData = $metadata['choices'][0]['message']['audio']['data']; |
| 69 | + $audioDatab64 = base64_decode($audioData, true); |
| 70 | + $audioFile = file_put_contents("output/chat_completions_audio.wav", $audioDatab64); |
| 71 | + echo "Audio file found and saved to: \"output/chat_completions_audio.wav\".\n"; |
| 72 | + } else { |
| 73 | + echo "Audio file not found.\n"; |
| 74 | + } |
| 75 | + echo "\n"; |
| 76 | + |
| 77 | + // Test 4: Will use default model for the next call again since ('gpt-3.5-turbo') |
| 78 | + // no model is specified in the options |
| 79 | + // and the default model was never unset |
| 80 | + echo "Test 4: Simple prompt- Will use default model gpt-3.5-turbo because default model was not unset\n"; |
| 81 | + echo str_repeat('-', 50) . "\n"; |
| 82 | + |
| 83 | + $response = $provider->chat("What is the capital of France?"); |
| 84 | + echo "Model: " . $response->getMetadata()['model'] . "\n"; |
| 85 | + echo "Response: " . $response->getContent() . "\n"; |
| 86 | + echo "\n"; |
| 87 | + |
| 88 | + // Unset default model |
| 89 | + $provider->unsetDefaultModel(); |
| 90 | + echo "Default model unset\n\n"; |
| 91 | + |
| 92 | + // Test 5: Uses method's default |
| 93 | + // (no model in options, |
| 94 | + // no default model, |
| 95 | + // no config model) |
| 96 | + echo "Test 5: generateImage with method's default model (should use 'dall-e-2')\n"; |
| 97 | + echo str_repeat('-', 50) . "\n"; |
| 98 | + |
| 99 | + $response = $provider->generateImage("Generate an image of a dog playing chess."); |
| 100 | + $response->saveFile("output/test5_image.png"); |
| 101 | + |
| 102 | + echo "Model: " . ($response->getMetadata()['model']) . "\n"; |
| 103 | + echo "File saved to: output/test5_image.png\n"; |
| 104 | + echo "\n"; |
| 105 | + |
| 106 | + // Test 6: Uses provider's config default |
| 107 | + // (no model in options, |
| 108 | + // no default model, |
| 109 | + // config has model) |
| 110 | + $providerWithConfig = new OpenAIProvider([ |
| 111 | + 'api_key' => $api_key, |
| 112 | + 'model' => 'dall-e-3' // Set default model in config |
| 113 | + ]); |
| 114 | + |
| 115 | + echo "Test 6: generateImage with provider's config default model (should use 'dall-e-3')\n"; |
| 116 | + echo str_repeat('-', 50) . "\n"; |
| 117 | + $response = $providerWithConfig->generateImage("Generate an image of gray tabby cat hugging an otter with an orange scarf. Make it look realistic."); |
| 118 | + |
| 119 | + $response->saveFile("output/test6_image.png"); |
| 120 | + echo "Model: " . ($response->getMetadata()['model']) . "\n"; |
| 121 | + echo "File saved to: output/test6_image.png\n"; |
| 122 | + echo "\n"; |
| 123 | + |
| 124 | + echo "\n" . str_repeat('=', 60) . "\n"; |
| 125 | + echo "All Chat Completions API tests completed successfully!\n"; |
| 126 | +} catch (Exception $e) { |
| 127 | + echo "Error: " . $e->getMessage() . "\n"; |
| 128 | +} |
0 commit comments