Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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.
147 changes: 147 additions & 0 deletions src/AbstractProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php

/**
* Part of the Joomla Framework AI Package
*
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check the headers Joomla is using

Copy link
Collaborator Author

@charvimehradu charvimehradu Jun 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have changed them with commit#4. Please check. I am not sure what should be the copyright for 2025, could you please confirm and I will update it in my next commit


namespace Joomla\AI;

use Joomla\Http\HttpFactory;

/**
* Abstract provider class.
*
* @since __DEPLOY_VERSION__
*/
abstract class AbstractProvider implements ProviderInterface
{
/**
* The provider options.
*
* @var array|\ArrayAccess
*/
protected $options;

/**
* The HTTP factory instance.
*
* @var HttpFactory
*/
protected $httpFactory;

/**
* Constructor.
*
* @param array|\ArrayAccess $options Provider options array.
* @param HttpFactory $httpFactory The http factory
*
* @throws \InvalidArgumentException If options is not an array or does not implement ArrayAccess.
* @since ___DEPLOY_VERSION___
*/
public function __construct($options = [], ?HttpFactory $httpFactory = null)
{
// To do: Exception Handeling Code
// Validate provider is suported
// Validate that $options is an array or implements ArrayAccess
if (!\is_array($options) && !($options instanceof \ArrayAccess)) {
throw new \InvalidArgumentException(
'The options param must be an array or implement the ArrayAccess interface.'
);
}

$this->options = $options;
$this->httpFactory = $httpFactory ?: new HttpFactory();
}

/**
* Get an option from the AI provider.
*
* @param string $key The name of the option to get.
* @param mixed $default The default value if the option is not set.
*
* @return mixed The option value.
*/
protected function getOption(string $key, $default = null)
{
return $this->options[$key] ?? $default;
}

/**
* Make HTTP GET request to AI provider API.
*
* @param string $url API endpoint URL
* @param array $headers Additional HTTP headers
* @param integer $timeout Request timeout in seconds
*
* @return \Joomla\Http\Response
* @throws \Exception
* @since ___DEPLOY_VERSION___
*/
protected function makeGetRequest(string $url, array $headers = [], $timeout = null)
{
try {
$response = $this->httpFactory->getHttp([])->get($url, $headers, $timeout);
} catch (\Exception $e) {
throw new \Exception('AI API GET request failed: ' . $e->getMessage(), 0, $e);
}

return $response;
}

/**
* Make HTTP POST request.
*
* @param string $url API endpoint URL
* @param mixed $data POST data
* @param array $headers HTTP headers
* @param integer $timeout Request timeout
*
* @return \Joomla\Http\Response
* @throws \Exception
* @since ___DEPLOY_VERSION___
*/
protected function makePostRequest(string $url, $data, array $headers = [], $timeout = null)
{
try {
$response = $this->httpFactory->getHttp([])->post($url, $data, $headers, $timeout);
} catch (\Exception $e) {
throw new \Exception('AI API POST request failed: ' . $e->getMessage(), 0, $e);
}

return $response;
}

/**
* Check response code and handle errors
*
* @param \Joomla\Http\Response $response HTTP response
*
* @return boolean True if successful
* @throws \Exception
*/
protected function validateResponse($response): bool
{
if ($response->code !== 200) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is only a 200 a valid response?

// To Do: Error Handling Code
throw new \Exception('AI API Error: HTTP ' . $response->code . ' - ' . $response->body);
}

return true;
}

/**
* Parse JSON response safely
*/
protected function parseJsonResponse(string $jsonString): array
{
$decoded = json_decode($jsonString, true);

if (json_last_error() !== JSON_ERROR_NONE) {
throw new \Exception('Invalid JSON response: ' . json_last_error_msg());
}

return $decoded;
}

}
53 changes: 53 additions & 0 deletions src/ProviderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/**
* Part of the Joomla Framework Http Package
*
*/

namespace Joomla\AI;

/**
* AI provider class interface.
*
* @since __DEPLOY_VERSION__
*/
interface ProviderInterface
{
/**
* Method to check if AI provider is available for using
*
* @return boolean True if available else false
*
*/
public static function isSupported();

/**
* Method to get the name of the AI provider.
*
* @return string The name of the AI provider.
*
*/
public function getName();

/**
* Send a prompt to the AI provider and return a Response object with the response.
*
* @param string $prompt The prompt to send to the AI provider.
* @param array $options An associative array of options to send with the request.
* @return Response
*
*/
public function prompt($prompt, $options = []);

/**
* Ask a question to the AI provider and return a Response object with the response.
*
* @param string $question The question to send to the AI provider.
* @param array $options An associative array of options to send with the request.
* @return Response
*
*/
public function ask($question, $options = []);

}
157 changes: 157 additions & 0 deletions src/Response.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?php

/**
* Part of the Joomla Framework AI Package
*
* @copyright Copyright (C) 2005 - 2025 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/

namespace Joomla\AI;

/**
* AI response data object class.
*
* @since __DEPLOY_VERSION__
*/
class Response
{
/**
* The content of the response.
*
* @var string
* @since __DEPLOY_VERSION__
*/
private $content;

/**
* The status code of the response.
*
* @var int
* @since __DEPLOY_VERSION__
*/
private $statusCode;

/**
* The metadata of the response.
*
* @var array
* @since __DEPLOY_VERSION__
*/
private $metadata;

/**
* The provider of the response.
*
* @var string
* @since __DEPLOY_VERSION__
*/
private $provider;

/**
* Constructor.
*
* @param string $content The content of the response.
* @param string $provider The provider of the response.
* @param array $metadata The metadata of the response.
* @param int $status The status code of the response.
*
* @since __DEPLOY_VERSION__
*/
public function __construct(string $content, string $provider, array $metadata = [], int $status = 200)
{
$this->content = $content;
$this->provider = $provider;
$this->metadata = $metadata;
$this->statusCode = $status;
}

/**
* Get the content of the response.
*
* @return string The content of the response.
*
* @since __DEPLOY_VERSION__
*/
public function getContent(): string
{
return $this->content;
}

/**
* Get the metadata of the response.
*
* @return array The metadata of the response.
*
* @since __DEPLOY_VERSION__
*/
public function getMetadata(): array
{
return $this->metadata;
}

/**
* Get the provider of the response.
*
* @return string The provider of the response.
*
* @since __DEPLOY_VERSION__
*/
public function getProvider(): string
{
return $this->provider;
}

/**
* Get the status code of the response.
*
* @return int The status code of the response.
*
* @since __DEPLOY_VERSION__
*/
public function getStatusCode(): int
{
return $this->statusCode;
}

/**
* Magic method to access properties of the response object.
*
* @param string $name The name of the property to get.
*
* @return mixed The value of the property.
*
* @since __DEPLOY_VERSION__
*/
public function __get($name)
{
switch (strtolower($name)) {
case 'content':
return $this->getContent();

case 'metadata':
return $this->getMetadata();

case 'provider':
return $this->getProvider();

case 'statuscode':
return $this->getStatusCode();

default:
$trace = debug_backtrace();

trigger_error(
sprintf(
'Undefined property via __get(): %s in %s on line %s',
$name,
$trace[0]['file'],
$trace[0]['line']
),
E_USER_NOTICE
);

break;
}
}
}