-
-
Notifications
You must be signed in to change notification settings - Fork 4
AI Framework Foundation #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
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 f358113
Add AI provider interface and abstract provider files
charvimehradu ba8c0fd
Enhance abstract provider http factory handling
charvimehradu 50ca86a
Add OpenAIProvider functionality and basic tests
charvimehradu d9d9140
Add OpenAIProvider chatwithVision method and Refactor existing structure
charvimehradu 9324369
Add OpenAIProvider chatwithVision test
charvimehradu 712bd33
Add model management capabilities
charvimehradu 940f774
Add authentication for getting available models and test it
charvimehradu e9f8bab
Add image generation capability
charvimehradu 7a4dd10
Add image generation capability-2
charvimehradu 3dd5c7c
Response API works for browser settings only
charvimehradu c08b033
Add image generation capability and test for it
charvimehradu 8ff42bb
Add image editing capabilities and test for it - 1
charvimehradu f6c08fe
Add the capability to create image variations and test for it
charvimehradu ca861fd
Add the capability to edit images
charvimehradu 72820d5
Code cleanup and add the text to speech capability
charvimehradu c6dacd6
Add transcribe capability and test
charvimehradu 222f228
Add translation capability and test
charvimehradu 4ad02b9
Add embeddings creation capability
charvimehradu 235c9c8
Add embeddings test and gpt-image-1 test
charvimehradu 82f57c6
Add provision for custom openai servers
charvimehradu b4fc7df
Debug multi image capability for gpt-image-1 model
charvimehradu 4837f78
Add Moderation capability and test/Restructure Test files
charvimehradu 4c2aac6
Correct format
charvimehradu b378d47
Clean up code and refactor transcription and translation response par…
charvimehradu 014e15b
Validate Image Inputs
charvimehradu 14584cb
Validate Audio and Embeddings Inputs
charvimehradu 448bd5e
Validate Chat Inputs
charvimehradu 961fd62
Test chat input validation ad audio capability
charvimehradu faa6e9f
Add AIException class
charvimehradu f26ba30
Add InvalidArgumentException class and its implementation with OpenAI…
charvimehradu 59eb85c
Resolve image generation test (default response_format=url)
charvimehradu 72fcff4
Add AuthenticationException class and its implementation with OpenAI …
charvimehradu 14c2274
Add RateLimit and QuotaExceeded tests
charvimehradu 58d11ef
Add UnserializableResponse Exception and refactor other exceptions
charvimehradu 2589a34
Add Provider Exception
charvimehradu dbb226b
Improve exception classes and keep defaults seet
charvimehradu 7ff2d77
Resolve response_format errors with image payloads
charvimehradu 82ee37b
Update composer to use http framework version 4.x
charvimehradu 29fb7a0
Update composer to use filesystem framework and implement it
charvimehradu 038c60b
Implement Anthropic provider
charvimehradu 6db0bdf
To do in new branch
charvimehradu 716f44f
Rename methods as vision and saveFile
charvimehradu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Part of the Joomla Framework AI Package | ||
| * | ||
| */ | ||
|
|
||
| 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(); | ||
bembelimen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
| * 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) { | ||
|
||
| // 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; | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 = []); | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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