Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
7a7b11b
Response.php and README.md file
charvimehradu Jun 7, 2025
f358113
Add AI provider interface and abstract provider files
charvimehradu Jun 9, 2025
ba8c0fd
Enhance abstract provider http factory handling
charvimehradu Jun 10, 2025
50ca86a
Add OpenAIProvider functionality and basic tests
charvimehradu Jun 11, 2025
d9d9140
Add OpenAIProvider chatwithVision method and Refactor existing structure
charvimehradu Jun 17, 2025
9324369
Add OpenAIProvider chatwithVision test
charvimehradu Jun 17, 2025
712bd33
Add model management capabilities
charvimehradu Jun 17, 2025
940f774
Add authentication for getting available models and test it
charvimehradu Jun 17, 2025
e9f8bab
Add image generation capability
charvimehradu Jun 18, 2025
7a4dd10
Add image generation capability-2
charvimehradu Jun 18, 2025
3dd5c7c
Response API works for browser settings only
charvimehradu Jun 18, 2025
c08b033
Add image generation capability and test for it
charvimehradu Jun 18, 2025
8ff42bb
Add image editing capabilities and test for it - 1
charvimehradu Jun 19, 2025
f6c08fe
Add the capability to create image variations and test for it
charvimehradu Jun 24, 2025
ca861fd
Add the capability to edit images
charvimehradu Jun 25, 2025
72820d5
Code cleanup and add the text to speech capability
charvimehradu Jun 26, 2025
c6dacd6
Add transcribe capability and test
charvimehradu Jun 28, 2025
222f228
Add translation capability and test
charvimehradu Jun 30, 2025
4ad02b9
Add embeddings creation capability
charvimehradu Jun 30, 2025
235c9c8
Add embeddings test and gpt-image-1 test
charvimehradu Jul 1, 2025
82f57c6
Add provision for custom openai servers
charvimehradu Jul 1, 2025
b4fc7df
Debug multi image capability for gpt-image-1 model
charvimehradu Jul 2, 2025
4837f78
Add Moderation capability and test/Restructure Test files
charvimehradu Jul 7, 2025
4c2aac6
Correct format
charvimehradu Jul 7, 2025
b378d47
Clean up code and refactor transcription and translation response par…
charvimehradu Jul 8, 2025
014e15b
Validate Image Inputs
charvimehradu Jul 8, 2025
14584cb
Validate Audio and Embeddings Inputs
charvimehradu Jul 9, 2025
448bd5e
Validate Chat Inputs
charvimehradu Jul 9, 2025
961fd62
Test chat input validation ad audio capability
charvimehradu Jul 10, 2025
faa6e9f
Add AIException class
charvimehradu Jul 12, 2025
f26ba30
Add InvalidArgumentException class and its implementation with OpenAI…
charvimehradu Jul 14, 2025
59eb85c
Resolve image generation test (default response_format=url)
charvimehradu Jul 14, 2025
72fcff4
Add AuthenticationException class and its implementation with OpenAI …
charvimehradu Jul 15, 2025
14c2274
Add RateLimit and QuotaExceeded tests
charvimehradu Jul 15, 2025
58d11ef
Add UnserializableResponse Exception and refactor other exceptions
charvimehradu Jul 16, 2025
2589a34
Add Provider Exception
charvimehradu Jul 16, 2025
dbb226b
Improve exception classes and keep defaults seet
charvimehradu Jul 17, 2025
7ff2d77
Resolve response_format errors with image payloads
charvimehradu Jul 21, 2025
82ee37b
Update composer to use http framework version 4.x
charvimehradu Jul 21, 2025
29fb7a0
Update composer to use filesystem framework and implement it
charvimehradu Jul 21, 2025
038c60b
Implement Anthropic provider
charvimehradu Jul 21, 2025
6db0bdf
To do in new branch
charvimehradu Jul 21, 2025
716f44f
Rename methods as vision and saveFile
charvimehradu Aug 6, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added README.md
Empty file.
105 changes: 105 additions & 0 deletions Tests/ChatCompletionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

require_once '../vendor/autoload.php';

use Joomla\AI\Provider\OpenAIProvider;

echo "Testing Real OpenAI API Calls...\n\n";

$configFile = __DIR__ . '/../config.json';
$config = json_decode(file_get_contents($configFile), true);
$api_key = $config['openai_api_key'] ?? null;

try {
// Create provider with your API key
$provider = new OpenAIProvider([
'api_key' => $api_key
]);

echo "Provider created with API key\n";
echo "Provider name: " . $provider->getName() . "\n\n";

// To Do: Check if the provider is supported. Currently key set as env variables only
// if (!OpenAIProvider::isSupported()) {
// throw new \Exception('OpenAI API is not supported or API key is missing.');
// }

// Test 1: Simple prompt
echo "Test 1: Simple prompt\n";
echo str_repeat('-', 50) . "\n";

$response = $provider->chat("Hello! How are you?");

echo "API call successful!\n";
echo "Response: " . $response->getContent() . "\n";
echo "Provider: " . $response->getProvider() . "\n";
echo "Status: " . $response->getStatusCode() . "\n";

$metadata = $response->getMetadata();
if (!empty($metadata)) {
echo "Model used: " . ($metadata['model']) . "\n";
if (isset($metadata['usage'])) {
echo "Tokens used: " . ($metadata['usage']['total_tokens']) . "\n";
}
}
echo "\n";

// Test 2: Multiple Response Choices (n parameter)
echo "Test 2: Multiple Response Choices (n parameter)\n";
echo str_repeat('-', 50) . "\n";
$response = $provider->chat("Suggest a name for a movie based on pilots and astronauts", [
'n' => 3,
]);
echo "Model: " . $response->getMetadata()['model'] . "\n";
echo "Response: " . $response->getContent() . "\n";

$metadata = $response->getMetadata();
if (isset($metadata['choices']) && is_array($metadata['choices'])) {
echo "Number of choices returned: " . count($metadata['choices']) . "\n";
for ($i = 0; $i < count($metadata['choices']); $i++) {
echo "Choice " . ($i + 1) . ": " . ($metadata['choices'][$i]['message']['content'] ?? 'No content') . "\n";
}
} else {
echo "Expected multiple choices but got single response. Check OpenAI provider implementation.\n";
}
echo "\n";

// Test 3:Test chat completions audio capability
echo "Test 3: Test chat completions audio capability\n";
echo str_repeat('-', 50) . "\n";
$response = $provider->chat("Say a few words on Joomla! for about 30 seconds in english.", [
'model' => 'gpt-4o-audio-preview',
'modalities' => ['text', 'audio'],
'audio' => [
'voice' => 'alloy',
'format' => 'wav'
],
]);

$metadata = $response->getMetadata();
$debugFile = "output/full_audio_response_structure.json";
$fullStructure = [
'response_class' => get_class($response),
'content' => $response->getContent(),
'status_code' => $response->getStatusCode(),
'provider' => $response->getProvider(),
'metadata' => $metadata
];
file_put_contents($debugFile, json_encode($fullStructure, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
echo "Full response structure saved to: $debugFile\n";

if (isset($metadata['choices'][0]['message']['audio']['data'])) {
$audioData = $metadata['choices'][0]['message']['audio']['data'];
$audioDatab64 = base64_decode($audioData, true);
$audioFile = file_put_contents("output/chat_completions_audio.wav", $audioDatab64);
echo "Audio file found and saved to: \"output/chat_completions_audio.wav\".\n";
} else {
echo "Audio file not found.\n";
}
echo "\n";

echo "\n" . str_repeat('=', 60) . "\n";
echo "All Chat Completions API tests completed successfully!\n";
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}
128 changes: 128 additions & 0 deletions Tests/DefaultModels.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php

require_once '../vendor/autoload.php';

use Joomla\AI\Provider\OpenAIProvider;

echo "Testing Real OpenAI API Calls...\n\n";

$configFile = __DIR__ . '/../config.json';
$config = json_decode(file_get_contents($configFile), true);
$api_key = $config['openai_api_key'] ?? null;

try {
// Create provider with your API key
$provider = new OpenAIProvider([
'api_key' => $api_key
]);

echo "Provider created with API key\n";
echo "Provider name: " . $provider->getName() . "\n\n";

// To Do: Check if the provider is supported. Currently key set as env variables only
// // if (!OpenAIProvider::isSupported()) {
// throw new \Exception('OpenAI API is not supported or API key is missing.');
// }

// Set default model for all subsequent calls
$provider->setDefaultModel('gpt-3.5-turbo');
echo "Default model: " . $provider->getDefaultModel() . "\n\n";

// Test 1: Will use default model since ('gpt-3.5-turbo')
// no model is specified in the options
// and the default model is set
echo "Test 1: Simple prompt- Will use default model gpt-3.5-turbo\n";
echo str_repeat('-', 50) . "\n";
$response = $provider->chat("Hello! How are you?");
echo "Model: " . $response->getMetadata()['model'] . "\n";
echo "Response: " . $response->getContent() . "\n";
echo "\n";

// Test 2: Will use default model for the next call again since ('gpt-3.5-turbo')
// no model is specified in the options
// and the default model is set
echo "Test 2: Multiple Response Choices- Will use default model gpt-3.5-turbo\n";
echo str_repeat('-', 50) . "\n";

$response = $provider->chat("Suggest a name for a movie based on pilots and astronauts");
echo "Model: " . $response->getMetadata()['model'] . "\n";
echo "Response: " . $response->getContent() . "\n";
echo "\n";

// Test 3: This will override the default model since
// model is specified in the options ('gpt-4o-audio-preview')
echo "Test 3: Test chat completions audio capability- Will override the default and use gpt-4o-audio-preview model\n";
echo str_repeat('-', 50) . "\n";
$response = $provider->chat("Say a few words on Joomla! for about 30 seconds in english.", [
'model' => 'gpt-4o-audio-preview',
'modalities' => ['text', 'audio'],
'audio' => [
'voice' => 'alloy',
'format' => 'wav'
],
]);

$metadata = $response->getMetadata();
echo "Model: " . $response->getMetadata()['model'] . "\n";
if (isset($metadata['choices'][0]['message']['audio']['data'])) {
$audioData = $metadata['choices'][0]['message']['audio']['data'];
$audioDatab64 = base64_decode($audioData, true);
$audioFile = file_put_contents("output/chat_completions_audio.wav", $audioDatab64);
echo "Audio file found and saved to: \"output/chat_completions_audio.wav\".\n";
} else {
echo "Audio file not found.\n";
}
echo "\n";

// Test 4: Will use default model for the next call again since ('gpt-3.5-turbo')
// no model is specified in the options
// and the default model was never unset
echo "Test 4: Simple prompt- Will use default model gpt-3.5-turbo because default model was not unset\n";
echo str_repeat('-', 50) . "\n";

$response = $provider->chat("What is the capital of France?");
echo "Model: " . $response->getMetadata()['model'] . "\n";
echo "Response: " . $response->getContent() . "\n";
echo "\n";

// Unset default model
$provider->unsetDefaultModel();
echo "Default model unset\n\n";

// Test 5: Uses method's default
// (no model in options,
// no default model,
// no config model)
echo "Test 5: generateImage with method's default model (should use 'dall-e-2')\n";
echo str_repeat('-', 50) . "\n";

$response = $provider->generateImage("Generate an image of a dog playing chess.");
$response->saveFile("output/test5_image.png");

echo "Model: " . ($response->getMetadata()['model']) . "\n";
echo "File saved to: output/test5_image.png\n";
echo "\n";

// Test 6: Uses provider's config default
// (no model in options,
// no default model,
// config has model)
$providerWithConfig = new OpenAIProvider([
'api_key' => $api_key,
'model' => 'dall-e-3' // Set default model in config
]);

echo "Test 6: generateImage with provider's config default model (should use 'dall-e-3')\n";
echo str_repeat('-', 50) . "\n";
$response = $providerWithConfig->generateImage("Generate an image of gray tabby cat hugging an otter with an orange scarf. Make it look realistic.");

$response->saveFile("output/test6_image.png");
echo "Model: " . ($response->getMetadata()['model']) . "\n";
echo "File saved to: output/test6_image.png\n";
echo "\n";

echo "\n" . str_repeat('=', 60) . "\n";
echo "All Chat Completions API tests completed successfully!\n";
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}
112 changes: 112 additions & 0 deletions Tests/DefaultsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

require_once '../vendor/autoload.php';

use Joomla\AI\Provider\OpenAIProvider;

$configFile = __DIR__ . '/../config.json';
$config = json_decode(file_get_contents($configFile), true);
$api_key = $config['openai_api_key'] ?? null;
$gpt_image_model_key = $config['gpt_image_model_key'];
$base_url = $config['openai_base_url'];

try {
$provider = new OpenAIProvider([
'api_key' => $api_key,
]);

// ===========================================
// Basic Chat Tests
// ===========================================

// Test 1: Basic Prompt Testing
echo "Test 1: Basic Prompt Testing\n";
echo str_repeat("-", 40) . "\n";

$response1 = $provider->chat("Hi, Can you write a paragraph on the importance of AI in modern technology?");
echo $response1->getContent();
$response1->saveFile('output/chat.txt');

echo "\n" . str_repeat("=", 50) . "\n";

// ===========================================
// Image Generation Tests
// ===========================================

// Test 2: Basic Image Generation
echo "Test 2: Basic Image Generation\n";
echo str_repeat("-", 40) . "\n";

$response2 = $provider->generateImage("Please generate an image for my blog post about my mount fuji hiking trip");
$response2->saveFile('output/mount_fuji.png');

echo "\n" . str_repeat("=", 50) . "\n";

// Test 3: DALL-E 3 with URL response
echo "Test 3: DALL-E 3 with URL response...\n";
echo str_repeat("-", 40) . "\n";

$response3 = $provider->generateImage(
"Can you generate an image of a slice of pizza riding a bicycle for my shopping website?",
[
'model' => 'dall-e-2',
'response_format' => 'url',
'n' => 3,
]
);
$response3->saveFile('output/thin_pizza.txt');

echo "\n" . str_repeat("=", 50) . "\n\n";

// ===========================================
// Speech Generation Tests
// ===========================================

// Test 4: Basic Speech Generation
echo "Test 4: Basic Speech Generation\n";
echo str_repeat("-", 40) . "\n";

$text = "Hello world! This is a test of the OpenAI text-to-speech capability.";

$response4 = $provider->speech($text);
$response4->saveFile('output/speech_4.mp3');

echo str_repeat("=", 50) . "\n\n";

// ===========================================
// Transcription Tests
// ===========================================

// Test 5: Basic Transcription
echo "Test 5: Basic Transcription \n";
echo str_repeat("-", 40) . "\n";

$audioFile = 'test_files/test_audio.wav';

$response5 = $provider->transcribe($audioFile);
$response5->getContent();
$response5->saveFile('output/transcribed.txt');

echo str_repeat("=", 50) . "\n\n";

// ===========================================
// Translation Tests
// ===========================================

// Test 6: Basic Translation
echo "Test 6: Basic Translation \n";
echo str_repeat("-", 40) . "\n";

$testAudioFile = 'test_files/test_german_audio.wav';

$response6 = $provider->translate($testAudioFile);
echo $response6->getContent();
$response6->saveFile('output/translated.txt');

echo "\n" . str_repeat("=", 50) . "\n\n";

echo "=== All Tests Completed Successfully! ===\n";

} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}
Loading