Release of v4.0.0-alpha8

Add power path override option on component level. Fix the sql build feature. #1032.
This commit is contained in:
2024-04-06 23:29:23 +02:00
parent 23af2f0b29
commit 359b4dd92b
761 changed files with 1893 additions and 1235 deletions

View File

@@ -0,0 +1,139 @@
<?php
/**
* @package Joomla.Component.Builder
*
* @created 4th September, 2022
* @author Llewellyn van der Merwe <https://dev.vdm.io>
* @git Joomla Component Builder <https://git.vdm.dev/joomla/Component-Builder>
* @copyright Copyright (C) 2015 Vast Development Method. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace VDM\Joomla\Openai\Utilities;
use Joomla\CMS\Http\Http as JoomlaHttp;
use Joomla\Registry\Registry;
/**
* The Openai Http
*
* @since 3.2.0
*/
final class Http extends JoomlaHttp
{
/**
* The default Header
*
* @var array
* @since 3.2.0
*/
protected array $defaultHeaders = ['Content-Type' => 'application/json'];
/**
* Constructor.
*
* @param string|null $token The Openai API token.
* @param string|null $orgToken The Openai API Organization token.
*
* @since 3.2.0
* @throws \InvalidArgumentException
**/
public function __construct(?string $token, ?string $orgToken = null)
{
// add the token if given
if (is_string($token))
{
$this->defaultHeaders['Authorization'] = 'Bearer ' . $token;
}
// add the organization token if given
if (is_string($orgToken))
{
$this->defaultHeaders['OpenAI-Organization'] = $orgToken;
}
// setup config
$config = [
'userAgent' => 'JoomlaOpenai/3.0',
'headers' => $this->defaultHeaders
];
$options = new Registry($config);
// run parent constructor
parent::__construct($options);
}
/**
* Change the Tokens.
*
* @param string|null $token The Openai API token.
* @param string|null $orgToken The Openai API Organization token.
*
* @since 3.2.0
**/
public function setTokens(?string $token = null, ?string $orgToken = null)
{
// get the current headers
$this->defaultHeaders = (array) $this->getOption('headers',
$this->defaultHeaders
);
// add the token if given
if (is_string($token))
{
$this->defaultHeaders['Authorization'] = 'Bearer ' . $token;
}
// add the organization token if given
if (is_string($orgToken))
{
$this->defaultHeaders['OpenAI-Organization'] = $orgToken;
}
$this->setOption('headers', $this->defaultHeaders);
}
/**
* Change the User Token.
*
* @param string $token The API token.
*
* @since 3.2.0
**/
public function setToken(string $token)
{
// get the current headers
$this->defaultHeaders = (array) $this->getOption('headers',
$this->defaultHeaders
);
// add the token
$this->defaultHeaders['Authorization'] = 'Bearer ' . $token;
$this->setOption('headers', $this->defaultHeaders);
}
/**
* Change the Organization Token.
*
* @param string $token The Organization API token.
*
* @since 3.2.0
**/
public function setOrgToken(string $token)
{
// get the current headers
$this->defaultHeaders = (array) $this->getOption('headers',
$this->defaultHeader
);
// add the token
$this->defaultHeaders['OpenAI-Organization'] = $token;
$this->setOption('headers', $this->defaultHeaders);
}
}

View File

@@ -0,0 +1,141 @@
<?php
/**
* @package Joomla.Component.Builder
*
* @created 4th September, 2022
* @author Llewellyn van der Merwe <https://dev.vdm.io>
* @git Joomla Component Builder <https://git.vdm.dev/joomla/Component-Builder>
* @copyright Copyright (C) 2015 Vast Development Method. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace VDM\Joomla\Openai\Utilities;
use Joomla\Http\Response as JoomlaResponse;
use VDM\Joomla\Utilities\JsonHelper;
use VDM\Joomla\Utilities\StringHelper;
/**
* The Openai Response
*
* @since 3.2.0
*/
final class Response
{
/**
* Process the response and decode it.
*
* @param JoomlaResponse $response The response.
* @param integer $expectedCode The expected "good" code.
* @param mixed $default The default if body not have length
*
* @return mixed
*
* @since 3.2.0
* @throws \DomainException
**/
public function get($response, int $expectedCode = 200, $default = null)
{
// Validate the response code.
if ($response->code != $expectedCode)
{
// Decode the error response and throw an exception.
$message = $this->error($response);
// Throw an exception with the OpenAI error message and code.
throw new \DomainException($message, $response->code);
}
return $this->body($response, $default);
}
/**
* Process the response and decode it. (when we have multiple success codes)
*
* @param JoomlaResponse $response The response.
* @param array [$expectedCode => $default] The expected "good" code. and The default if body not have length
*
* @return mixed
*
* @since 3.2.0
* @throws \DomainException
**/
public function get_($response, array $validate = [200 => null])
{
// Validate the response code.
if (!isset($validate[$response->code]))
{
// Decode the error response and throw an exception.
$message = $this->error($response);
// Throw an exception with the OpenAI error message and code.
throw new \DomainException($message, $response->code);
}
return $this->body($response, $validate[$response->code]);
}
/**
* Return the body from the response
*
* @param JoomlaResponse $response The response.
* @param mixed $default The default if body not have length
*
* @return mixed
* @since 3.2.0
**/
protected function body($response, $default = null)
{
$body = $response->body ?? null;
// check that we have a body
if (StringHelper::check($body))
{
if (JsonHelper::check($body))
{
$body = json_decode((string) $body);
}
return $body;
}
return $default;
}
/**
* Get the error message from the OpenAI API response
*
* @param JoomlaResponse $response The response.
*
* @return string
* @since 3.2.0
**/
protected function error($response): string
{
// do we have a json string
if (isset($response->body) && JsonHelper::check($response->body))
{
$error = json_decode($response->body);
}
else
{
return 'Invalid or empty response body.';
}
// check if OpenAI returned an error object
if (isset($error->error))
{
// error object found, extract message and code
$errorMessage = isset($error->error->message) ? $error->error->message : 'Unknown error.';
$errorCode = isset($error->error->code) ? $error->error->code : 'Unknown error code.';
// return formatted error message
return 'OpenAI Error: ' . $errorMessage . ' Code: ' . $errorCode;
}
return 'No error information found in response.';
}
}

View File

@@ -0,0 +1,115 @@
<?php
/**
* @package Joomla.Component.Builder
*
* @created 4th September, 2022
* @author Llewellyn van der Merwe <https://dev.vdm.io>
* @git Joomla Component Builder <https://git.vdm.dev/joomla/Component-Builder>
* @copyright Copyright (C) 2015 Vast Development Method. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace VDM\Joomla\Openai\Utilities;
use Joomla\Uri\Uri as JoomlaUri;
/**
* The Openai Uri
*
* @since 3.2.0
*/
final class Uri
{
/**
* The api version
*
* @var string
* @since 3.2.0
*/
private string $version;
/**
* The api URL
*
* @var string
* @since 3.2.0
*/
private string $url;
/**
* Constructor
*
* @param string $url URL to the openai system
* example: https://api.openai.com
* @param string $version Version to the openai system
*
* @since 3.2.0
**/
public function __construct(
string $url = 'https://api.openai.com',
string $version = 'v1')
{
// set the API details
$this->setUrl($url);
$this->setVersion($version);
}
/**
* Method to build and return a full request URL for the request. This method will
* add appropriate pagination details if necessary and also prepend the API url
* to have a complete URL for the request.
*
* @param string $path URL to inflect
*
* @return JoomlaUri
* @since 3.2.0
**/
public function get(string $path): JoomlaUri
{
// Get a new Uri object focusing the api url and given path.
$uri = new JoomlaUri($this->api() . $path);
return $uri;
}
/**
* Get the full API URL
*
* @return string
* @since 3.2.0
**/
public function api(): string
{
return $this->url . '/' . $this->version;
}
/**
* Set the URL of the API
*
* @param string $url URL to your openai system
* example: https://api.openai.com
*
* @return void
* @since 3.2.0
**/
private function setUrl(string $url)
{
return $this->url = $url;
}
/**
* Set the version of the API
*
* @param string $version version to your openai API
*
* @return void
* @since 3.2.0
**/
private function setVersion($version)
{
return $this->version = $version;
}
}

View File

@@ -0,0 +1 @@
<html><body bgcolor="#FFFFFF"></body></html>