Release of v3.2.1-alpha1

Add power path override option on component level. Fix the sql build feature. #1032.
This commit is contained in:
2024-04-06 19:48:58 +02:00
parent ddb07c79d9
commit 94bc92357e
754 changed files with 1520 additions and 1179 deletions

View File

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

View File

@@ -0,0 +1,67 @@
<?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\Abstraction;
use VDM\Joomla\Openai\Utilities\Http;
use VDM\Joomla\Openai\Utilities\Uri;
use VDM\Joomla\Openai\Utilities\Response;
/**
* The Openai Api
*
* @since 3.2.0
*/
abstract class Api
{
/**
* The Http class
*
* @var Http
* @since 3.2.0
*/
protected Http $http;
/**
* The Uri class
*
* @var Uri
* @since 3.2.0
*/
protected Uri $uri;
/**
* The Response class
*
* @var Response
* @since 3.2.0
*/
protected Response $response;
/**
* Constructor.
*
* @param Http $http The http class.
* @param Uri $uri The uri class.
* @param Response $response The response class.
*
* @since 3.2.0
**/
public function __construct(Http $http, Uri $uri, Response $response)
{
$this->http = $http;
$this->uri = $uri;
$this->response = $response;
}
}

View File

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

View File

@@ -0,0 +1,138 @@
<?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;
use VDM\Joomla\Openai\Abstraction\Api;
/**
* The Openai Audio
*
* @since 3.2.0
*/
class Audio extends Api
{
/**
* Transcribes audio into the input language.
* API Ref: https://platform.openai.com/docs/api-reference/audio/create
*
* @param string $file The audio file to transcribe. Formats: mp3, mp4, mpeg, mpga, m4a, wav, or webm (required).
* @param string|null $prompt An optional text to guide the model's style (optional).
* @param string|null $responseFormat The format of the transcript output. Options: json, text, srt, verbose_json, or vtt (optional).
* @param float|null $temperature The sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic (optional).
* @param string|null $language The language of the input audio (optional).
* @param string $model ID of the model to use. Only "whisper-1" is currently available.
*
* @return object|null
* @since 3.2.0
**/
public function transcribe(
string $file,
?string $prompt = null,
?string $responseFormat = null,
?float $temperature = null,
?string $language = null,
string $model = 'whisper-1'
): ?object
{
// Build the request path.
$path = "/audio/transcriptions";
// Set the request data.
$data = new \stdClass();
$data->file = $file;
if ($prompt !== null)
{
$data->prompt = $prompt;
}
if ($responseFormat !== null)
{
$data->response_format = $responseFormat;
}
if ($temperature !== null)
{
$data->temperature = $temperature;
}
if ($language !== null)
{
$data->language = $language;
}
$data->model = $model;
// Send the post request.
return $this->response->get(
$this->http->post(
$this->uri->get($path), json_encode($data), ['Content-Type' => 'multipart/form-data']
)
);
}
/**
* Translate an audio file into English.
* API Ref: https://platform.openai.com/docs/api-reference/audio/create
*
* @param string $file The the audio file. Formats: mp3, mp4, mpeg, mpga, m4a, wav, or webm (required).
* @param string|null $prompt An optional text to guide the model's style or continue a previous audio segment. The prompt should be in English (optional).
* @param string|null $responseFormat The format of the transcript output. Options: json, text, srt, verbose_json, or vtt (optional).
* @param float|null $temperature The sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic (optional).
* @param string $model ID of the model to use. Only "whisper-1" is currently available.
*
* @return object|null
* @since 3.2.0
**/
public function translation(
string $file,
?string $prompt = null,
?string $responseFormat = null,
?float $temperature = null,
string $model = 'whisper-1'
): ?object
{
// Build the request path.
$path = "/audio/translations";
// Set the data.
$data = new \stdClass();
$data->file = $file;
if ($prompt !== null)
{
$data->prompt = $prompt;
}
if ($responseFormat !== null)
{
$data->response_format = $responseFormat;
}
if ($temperature !== null)
{
$data->temperature = $temperature;
}
$data->model = $model;
// Send the post request.
return $this->response->get(
$this->http->post(
$this->uri->get($path), json_encode($data), ['Content-Type' => 'multipart/form-data']
)
);
}
}

View File

@@ -0,0 +1,142 @@
<?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;
use VDM\Joomla\Openai\Abstraction\Api;
/**
* The Openai Chat
*
* @since 3.2.0
*/
class Chat extends Api
{
/**
* Create a chat completion with the OpenAI API.
* API Ref: https://platform.openai.com/docs/api-reference/chat/create
*
* @param string $model The model to use for completion.
* @param array $messages A list of messages describing the conversation so far.
*
* Each item in the array is an object with the following:
* - role (string) Required
* The role of the author of this message.
* One of system, user, or assistant.
* - content (string) Required
* The contents of the message.
* - name (string) Optional
* The name of the author of this message.
* May contain a-z, A-Z, 0-9, and underscores,
* with a maximum length of 64 characters.
*
* @param int|null $maxTokens Maximum number of tokens to generate (optional).
* @param float|null $temperature The sampling temperature to use (optional).
* @param float|null $topP The nucleus sampling parameter (optional).
* @param int|null $n The number of chat completion choices to generate (optional).
* @param bool|null $stream Partial message deltas (optional).
* @param mixed|null $stop Sequences where the API will stop generating tokens (optional).
* @param float|null $presencePenalty Penalty for new tokens based on whether they appear in the text (optional).
* @param float|null $frequencyPenalty Penalty for new tokens based on their frequency in the text (optional).
* @param array|null $logitBias Modify the likelihood of specified tokens appearing (optional).
* @param string|null $user A unique identifier representing the end-user (optional).
*
* @return object|null
* @since 3.2.0
**/
public function create(
string $model,
array $messages,
?int $maxTokens = null,
?float $temperature = null,
?float $topP = null,
?int $n = null,
?bool $stream = null,
$stop = null,
?float $presencePenalty = null,
?float $frequencyPenalty = null,
?array $logitBias = null,
?string $user = null
): ?object
{
// Build the request path.
$path = "/chat/completions";
// Set the request data.
$data = new \stdClass();
$data->model = $model;
$data->messages = $messages;
if ($maxTokens !== null)
{
$data->max_tokens = $maxTokens;
}
if ($temperature !== null)
{
$data->temperature = $temperature;
}
if ($topP !== null)
{
$data->top_p = $topP;
}
if ($n !== null)
{
$data->n = $n;
}
if ($stream !== null)
{
$data->stream = $stream;
}
if ($stop !== null)
{
$data->stop = $stop;
}
if ($presencePenalty !== null)
{
$data->presence_penalty = $presencePenalty;
}
if ($frequencyPenalty !== null)
{
$data->frequency_penalty = $frequencyPenalty;
}
if ($logitBias !== null)
{
$data->logit_bias = new \stdClass();
foreach ($logitBias as $key => $val)
{
$data->logit_bias->$key = $val;
}
}
if ($user !== null)
{
$data->user = $user;
}
// Send the post request.
return $this->response->get(
$this->http->post(
$this->uri->get($path), json_encode($data)
)
);
}
}

View File

@@ -0,0 +1,158 @@
<?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;
use VDM\Joomla\Openai\Abstraction\Api;
/**
* The Openai Completions
*
* @since 3.2.0
*/
class Completions extends Api
{
/**
* Create a completion using the OpenAI API.
* API Ref: https://platform.openai.com/docs/api-reference/completions
*
* @param string $model The ID of the model to use.
* @param string|array $prompt The prompt(s) to generate completions for.
* @param int|null $maxTokens The maximum number of tokens to generate (optional).
* @param float|null $temperature The sampling temperature to use (optional).
* @param string $suffix The suffix that comes after a completion of inserted text. (optional).
* @param float|null $topP The top_p value for nucleus sampling (optional).
* @param int|null $n How many completions to generate (optional).
* @param bool|null $stream Whether to stream back partial progress (optional).
* @param int|null $logprobs Include the log probabilities on the most likely tokens (optional).
* @param bool|null $echo Echo back the prompt in addition to the completion (optional).
* @param string|null $stop Up to 4 sequences where the API will stop generating (optional).
* @param float|null $presencePenalty The presence penalty to use (optional).
* @param float|null $frequencyPenalty The frequency penalty to use (optional).
* @param int|null $bestOf Generates best_of completions server-side (optional).
* @param array|null $logitBias Modify the likelihood of specified tokens (optional).
* @param string|null $user A unique identifier representing your end-user (optional).
*
* @return object|null
* @since 3.2.0
**/
public function create(
string $model,
$prompt,
?int $maxTokens = null,
?string $suffix = null,
?float $temperature = null,
?float $topP = null,
?int $n = null,
?bool $stream = null,
?int $logprobs = null,
?bool $echo = null,
$stop = null,
?float $presencePenalty = null,
?float $frequencyPenalty = null,
?int $bestOf = null,
?array $logitBias = null,
?string $user = null
): ?object
{
// Build the request path.
$path = "/completions";
// Set the completion data.
$data = new \stdClass();
$data->model = $model;
$data->prompt = $prompt;
if ($maxTokens !== null)
{
$data->max_tokens = $maxTokens;
}
if ($temperature !== null)
{
$data->temperature = $temperature;
}
if ($suffix !== null)
{
$data->suffix = $suffix;
}
if ($topP !== null)
{
$data->top_p = $topP;
}
if ($n !== null)
{
$data->n = $n;
}
if ($stream !== null)
{
$data->stream = $stream;
}
if ($logprobs !== null)
{
$data->logprobs = $logprobs;
}
if ($echo !== null)
{
$data->echo = $echo;
}
if ($stop !== null)
{
$data->stop = $stop;
}
if ($presencePenalty !== null)
{
$data->presence_penalty = $presencePenalty;
}
if ($frequencyPenalty !== null)
{
$data->frequency_penalty = $frequencyPenalty;
}
if ($bestOf !== null)
{
$data->best_of = $bestOf;
}
if ($logitBias !== null)
{
$data->logit_bias = new \stdClass();
foreach ($logitBias as $key => $val)
{
$data->logit_bias->$key = $val;
}
}
if ($user !== null)
{
$data->user = $user;
}
// Send the post request.
return $this->response->get(
$this->http->post(
$this->uri->get($path), json_encode($data)
)
);
}
}

View File

@@ -0,0 +1,84 @@
<?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;
use VDM\Joomla\Openai\Abstraction\Api;
/**
* The Openai Edits
*
* @since 3.2.0
*/
class Edits extends Api
{
/**
* Create a new edit using OpenAI Edit API.
* API Ref: https://platform.openai.com/docs/api-reference/edits
*
* @param string $model The model to use.
* @param string $instruction The instruction for the edit.
* @param string|null $input The input text (optional).
* @param int|null $n How many edits to generate (optional).
* @param float|null $temperature The sampling temperature (optional).
* @param float|null $topP Nucleus sampling parameter (optional).
*
* @return object|null
* @since 3.2.0
**/
public function create(
string $model,
string $instruction,
?string $input = null,
?int $n = null,
?float $temperature = null,
?float $topP = null
): ?object
{
// Build the request path.
$path = "/edits";
// Set the data.
$data = new \stdClass();
$data->model = $model;
$data->instruction = $instruction;
if ($input !== null)
{
$data->input = $input;
}
if ($n !== null)
{
$data->n = $n;
}
if ($temperature !== null)
{
$data->temperature = $temperature;
}
if ($topP !== null)
{
$data->top_p = $topP;
}
// Send the post request.
return $this->response->get(
$this->http->post(
$this->uri->get($path), json_encode($data)
)
);
}
}

View File

@@ -0,0 +1,63 @@
<?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;
use VDM\Joomla\Openai\Abstraction\Api;
/**
* The Openai Embeddings
*
* @since 3.2.0
*/
class Embeddings extends Api
{
/**
* Create an embedding of a given input.
* API Ref: https://platform.openai.com/docs/api-reference/embeddings
*
* @param string $model The ID of the model to use.
* @param mixed $input The input text to get embeddings for, encoded as a string or array of tokens.
* @param string|null $user A unique identifier representing your end-user (optional).
*
* @return object|null
* @since 3.2.0
**/
public function create(
string $model,
$input,
?string $user = null
): ?object
{
// Build the request path.
$path = "/embeddings";
// Set the request data.
$data = new \stdClass();
$data->model = $model;
$data->input = $input;
if ($user !== null)
{
$data->user = $user;
}
// Send the post request.
return $this->response->get(
$this->http->post(
$this->uri->get($path), json_encode($data)
)
);
}
}

View File

@@ -0,0 +1,78 @@
<?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;
use Joomla\DI\Container;
use VDM\Joomla\Openai\Service\Api;
use VDM\Joomla\Openai\Service\Utilities;
use VDM\Joomla\Interfaces\FactoryInterface;
/**
* Openai Factory
*
* @since 3.2.0
*/
abstract class Factory implements FactoryInterface
{
/**
* Global Package Container
*
* @var Container
* @since 3.2.0
**/
protected static $container = null;
/**
* Get any class from the package container
*
* @param string $key The container class key
*
* @return Mixed
* @since 3.2.0
*/
public static function _($key)
{
return self::getContainer()->get($key);
}
/**
* Get the global package container
*
* @return Container
* @since 3.2.0
*/
public static function getContainer(): Container
{
if (!self::$container)
{
self::$container = self::createContainer();
}
return self::$container;
}
/**
* Create a container object
*
* @return Container
* @since 3.2.0
*/
protected static function createContainer(): Container
{
return (new Container())
->registerServiceProvider(new Utilities())
->registerServiceProvider(new Api());
}
}

View File

@@ -0,0 +1,140 @@
<?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;
use VDM\Joomla\Openai\Abstraction\Api;
/**
* The Openai Files
*
* @since 3.2.0
*/
class Files extends Api
{
/**
* Fetches a list of files belonging to the user's organization.
* API Ref: https://platform.openai.com/docs/api-reference/files/list
*
* @return object|null The response from the OpenAI API, or null if an error occurred.
* @since 3.2.0
*/
public function list(): ?object
{
// Build the request path.
$path = "/files";
// Prepare the URI.
$uri = $this->uri->get($path);
// Send the GET request.
return $this->response->get(
$this->http->get($uri)
);
}
/**
* Upload a file that contains document(s) to be used across various endpoints/features.
* API Ref: https://platform.openai.com/docs/api-reference/files/upload
*
* @param string $file The file to upload.
* @param string $purpose The intended purpose of the uploaded documents.
*
* @return object|null
* @since 3.2.0
**/
public function upload(string $file, string $purpose): ?object
{
// Build the request path.
$path = "/files";
// Set the request data.
$data = new \stdClass();
$data->file = $file;
$data->purpose = $purpose;
// Send the post request.
return $this->response->get(
$this->http->post(
$this->uri->get($path), json_encode($data)
)
);
}
/**
* Returns information about a specific file.
* API Ref: https://platform.openai.com/docs/api-reference/files/retrieve
*
* @param string $fileID The file id to retrieve info
*
* @return object|null
* @since 3.2.0
**/
public function info(string $fileID): ?object
{
// Build the request path.
$path = "/files/{$fileID}";
// Send the post request.
return $this->response->get(
$this->http->get(
$this->uri->get($path)
)
);
}
/**
* Retrieve a specific file content.
* API Ref: https://platform.openai.com/docs/api-reference/files/retrieve-content
*
* @param string $fileID The file id to retrieve content
*
* @return mixed
* @since 3.2.0
**/
public function content(string $fileID)
{
// Build the request path.
$path = "/files/{$fileID}/content";
// Send the post request.
return $this->response->get(
$this->http->get(
$this->uri->get($path)
)
);
}
/**
* Delete a file.
* API Ref: https://platform.openai.com/docs/api-reference/files/delete
*
* @param string $fileID The file id to delete
*
* @return object|null
* @since 3.2.0
**/
public function delete(string $fileID): ?object
{
// Build the request path.
$path = "/files/{$fileID}";
// Send the post request.
return $this->response->get(
$this->http->delete(
$this->uri->get($path)
)
);
}
}

View File

@@ -0,0 +1,50 @@
<?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;
use VDM\Joomla\Openai\Abstraction\Api;
/**
* The Openai Fine Tunes
*
* @since 3.2.0
*/
class FineTunes extends Api
{
/**
* List your organization's fine-tuning jobs
* API Ref: https://platform.openai.com/docs/api-reference/fine-tunes/list
*
* @return object|null The response from the OpenAI API, or null if an error occurred.
* @since 3.2.0
*/
public function list(): ?object
{
// Build the request path.
$path = "/fine-tunes";
// Prepare the URI.
$uri = $this->uri->get($path);
// Send the GET request.
return $this->response->get(
$this->http->get($uri)
);
}
/**
* More to follow: https://platform.openai.com/docs/api-reference/fine-tunes
**/
}

View File

@@ -0,0 +1,203 @@
<?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;
use VDM\Joomla\Openai\Abstraction\Api;
/**
* The Openai Images
*
* @since 3.2.0
*/
class Images extends Api
{
/**
* Generate an image given a text prompt.
* API Ref: https://platform.openai.com/docs/api-reference/images/create
*
* @param string $prompt The text description of the desired image(s).
* @param string|null $size The size of the generated images (optional).
* @param string|null $responseFormat The format in which the images are returned (optional).
* @param int|null $n The number of images to generate (optional).
* @param string|null $user A unique identifier representing the end-user (optional).
*
* @return object|null
* @since 3.2.0
**/
public function generate(
string $prompt,
?string $size = null,
?string $responseFormat = null,
?int $n = null,
?string $user = null
): ?object
{
// Build the request path.
$path = "/images/generations";
// Set the request data.
$data = new \stdClass();
$data->prompt = $prompt;
if ($size !== null)
{
$data->size = $size;
}
if ($responseFormat !== null)
{
$data->response_format = $responseFormat;
}
if ($n !== null)
{
$data->n = $n;
}
if ($user !== null)
{
$data->user = $user;
}
// Send the post request.
return $this->response->get(
$this->http->post(
$this->uri->get($path), json_encode($data)
)
);
}
/**
* Create an image edit with extended options.
* API Ref: https://platform.openai.com/docs/api-reference/images/create-edit
*
* @param string $image The original image to edit. Must be a valid PNG file, less than 4MB, and square.
* @param string|null $mask An additional image for editing (optional).
* @param string $prompt A text description of the desired image(s).
* @param string|null $size The size of the generated images (optional).
* @param string|null $responseFormat The format in which the images are returned (optional).
* @param int|null $n The number of images to generate (optional).
* @param string|null $user A unique identifier representing your end-user (optional).
*
* @return object|null
* @since 3.2.0
**/
public function edit(
string $image,
string $prompt,
?string $mask = null,
?string $size = null,
?string $responseFormat = null,
?int $n = null,
?string $user = null
): ?object
{
// Build the request path.
$path = "/images/edits";
// Set the image edit data.
$data = new \stdClass();
$data->image = $image;
$data->prompt = $prompt;
if ($mask !== null)
{
$data->mask = $mask;
}
if ($size !== null)
{
$data->size = $size;
}
if ($responseFormat !== null)
{
$data->response_format = $responseFormat;
}
if ($n !== null)
{
$data->n = $n;
}
if ($user !== null)
{
$data->user = $user;
}
// Send the post request.
return $this->response->get(
$this->http->post(
$this->uri->get($path), json_encode($data)
)
);
}
/**
* Create a variation of a given image.
* API Ref: https://platform.openai.com/docs/api-reference/images/create-variation
*
* @param string $image The image to use as the basis for the variation(s).
* @param string|null $size The size of the generated images (optional).
* @param string|null $responseFormat The format in which the generated images are returned (optional).
* @param int|null $n The number of images to generate (optional).
* @param string|null $user A unique identifier representing your end-user (optional).
*
* @return object|null
* @since 3.2.0
**/
public function variation(
string $image,
?string $size = null,
?string $responseFormat = null,
?int $n = null,
?string $user = null
): ?object
{
// Build the request path.
$path = "/images/variations";
// Set the image variation data.
$data = new \stdClass();
$data->image = $image;
if ($size !== null)
{
$data->size = $size;
}
if ($responseFormat !== null)
{
$data->response_format = $responseFormat;
}
if ($n !== null)
{
$data->n = $n;
}
if ($user !== null)
{
$data->user = $user;
}
// Send the post request.
return $this->response->get(
$this->http->post(
$this->uri->get($path), json_encode($data)
)
);
}
}

View File

@@ -0,0 +1,44 @@
<?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;
use VDM\Joomla\Openai\Abstraction\Api;
/**
* The Openai Models
*
* @since 3.2.0
*/
class Models extends Api
{
/**
* List the available models from OpenAI API.
*
* @return object|null
* @since 3.2.0
**/
public function list(): ?object
{
// Build the request path.
$path = "/models";
// Send the get request.
return $this->response->get(
$this->http->get(
$this->uri->get($path)
)
);
}
}

View File

@@ -0,0 +1,60 @@
<?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;
use VDM\Joomla\Openai\Abstraction\Api;
/**
* The Openai Moderate
*
* @since 3.2.0
*/
class Moderate extends Api
{
/**
* Classify if text violates OpenAI's Content Policy.
* API Ref: https://platform.openai.com/docs/api-reference/moderations/create
*
* @param string|array $input The input text to classify.
* @param string|null $model The moderation model (optional).
*
* @return object|null
* @since 3.2.0
**/
public function text(
$input,
?string $model = null
): ?object
{
// Build the request path.
$path = "/moderations";
// Set the moderation data.
$data = new \stdClass();
$data->input = $input;
if ($model !== null)
{
$data->model = $model;
}
// Send the post request.
return $this->response->get(
$this->http->post(
$this->uri->get($path), json_encode($data)
)
);
}
}

View File

@@ -0,0 +1,247 @@
<?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\Service;
use Joomla\DI\Container;
use Joomla\DI\ServiceProviderInterface;
use VDM\Joomla\Openai\Audio;
use VDM\Joomla\Openai\Chat;
use VDM\Joomla\Openai\Completions;
use VDM\Joomla\Openai\Edits;
use VDM\Joomla\Openai\Embeddings;
use VDM\Joomla\Openai\Files;
use VDM\Joomla\Openai\FineTunes;
use VDM\Joomla\Openai\Images;
use VDM\Joomla\Openai\Models;
use VDM\Joomla\Openai\Moderate;
/**
* The Openai Api Service
*
* @since 3.2.0
*/
class Api implements ServiceProviderInterface
{
/**
* Registers the service provider with a DI container.
*
* @param Container $container The DI container.
*
* @return void
* @since 3.2.0
*/
public function register(Container $container)
{
$container->alias(Audio::class, 'Openai.Audio')
->share('Openai.Audio', [$this, 'getAudio'], true);
$container->alias(Chat::class, 'Openai.Chat')
->share('Openai.Chat', [$this, 'getChat'], true);
$container->alias(Completions::class, 'Openai.Completions')
->share('Openai.Completions', [$this, 'getCompletions'], true);
$container->alias(Edits::class, 'Openai.Edits')
->share('Openai.Edits', [$this, 'getEdits'], true);
$container->alias(Embeddings::class, 'Openai.Embeddings')
->share('Openai.Embeddings', [$this, 'getEmbeddings'], true);
$container->alias(Files::class, 'Openai.Files')
->share('Openai.Files', [$this, 'getFiles'], true);
$container->alias(FineTunes::class, 'Openai.FineTunes')
->share('Openai.FineTunes', [$this, 'getFineTunes'], true);
$container->alias(Images::class, 'Openai.Images')
->share('Openai.Images', [$this, 'getImages'], true);
$container->alias(Models::class, 'Openai.Models')
->share('Openai.Models', [$this, 'getModels'], true);
$container->alias(Moderate::class, 'Openai.Moderate')
->share('Openai.Moderate', [$this, 'getModerate'], true);
}
/**
* Get the Audio class
*
* @param Container $container The DI container.
*
* @return Audio
* @since 3.2.0
*/
public function getAudio(Container $container): Audio
{
return new Audio(
$container->get('Openai.Utilities.Http'),
$container->get('Openai.Utilities.Uri'),
$container->get('Openai.Utilities.Response')
);
}
/**
* Get the Chat class
*
* @param Container $container The DI container.
*
* @return Chat
* @since 3.2.0
*/
public function getChat(Container $container): Chat
{
return new Chat(
$container->get('Openai.Utilities.Http'),
$container->get('Openai.Utilities.Uri'),
$container->get('Openai.Utilities.Response')
);
}
/**
* Get the Completions class
*
* @param Container $container The DI container.
*
* @return Completions
* @since 3.2.0
*/
public function getCompletions(Container $container): Completions
{
return new Completions(
$container->get('Openai.Utilities.Http'),
$container->get('Openai.Utilities.Uri'),
$container->get('Openai.Utilities.Response')
);
}
/**
* Get the Edits class
*
* @param Container $container The DI container.
*
* @return Edits
* @since 3.2.0
*/
public function getEdits(Container $container): Edits
{
return new Edits(
$container->get('Openai.Utilities.Http'),
$container->get('Openai.Utilities.Uri'),
$container->get('Openai.Utilities.Response')
);
}
/**
* Get the Embeddings class
*
* @param Container $container The DI container.
*
* @return Embeddings
* @since 3.2.0
*/
public function getEmbeddings(Container $container): Embeddings
{
return new Embeddings(
$container->get('Openai.Utilities.Http'),
$container->get('Openai.Utilities.Uri'),
$container->get('Openai.Utilities.Response')
);
}
/**
* Get the Files class
*
* @param Container $container The DI container.
*
* @return Files
* @since 3.2.0
*/
public function getFiles(Container $container): Files
{
return new Files(
$container->get('Openai.Utilities.Http'),
$container->get('Openai.Utilities.Uri'),
$container->get('Openai.Utilities.Response')
);
}
/**
* Get the FineTunes class
*
* @param Container $container The DI container.
*
* @return FineTunes
* @since 3.2.0
*/
public function getFineTunes(Container $container): FineTunes
{
return new FineTunes(
$container->get('Openai.Utilities.Http'),
$container->get('Openai.Utilities.Uri'),
$container->get('Openai.Utilities.Response')
);
}
/**
* Get the Images class
*
* @param Container $container The DI container.
*
* @return Images
* @since 3.2.0
*/
public function getImages(Container $container): Images
{
return new Images(
$container->get('Openai.Utilities.Http'),
$container->get('Openai.Utilities.Uri'),
$container->get('Openai.Utilities.Response')
);
}
/**
* Get the Models class
*
* @param Container $container The DI container.
*
* @return Models
* @since 3.2.0
*/
public function getModels(Container $container): Models
{
return new Models(
$container->get('Openai.Utilities.Http'),
$container->get('Openai.Utilities.Uri'),
$container->get('Openai.Utilities.Response')
);
}
/**
* Get the Moderate class
*
* @param Container $container The DI container.
*
* @return Moderate
* @since 3.2.0
*/
public function getModerate(Container $container): Moderate
{
return new Moderate(
$container->get('Openai.Utilities.Http'),
$container->get('Openai.Utilities.Uri'),
$container->get('Openai.Utilities.Response')
);
}
}

View File

@@ -0,0 +1,113 @@
<?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\Service;
use Joomla\DI\Container;
use Joomla\DI\ServiceProviderInterface;
use VDM\Joomla\Openai\Utilities\Uri;
use VDM\Joomla\Openai\Utilities\Response;
use VDM\Joomla\Openai\Utilities\Http;
use VDM\Joomla\Utilities\Component\Helper;
/**
* The Openai Utilities Service
*
* @since 3.2.0
*/
class Utilities implements ServiceProviderInterface
{
/**
* Registers the service provider with a DI container.
*
* @param Container $container The DI container.
*
* @return void
* @since 3.2.0
*/
public function register(Container $container)
{
$container->alias(Uri::class, 'Openai.Utilities.Uri')
->share('Openai.Utilities.Uri', [$this, 'getUri'], true);
$container->alias(Response::class, 'Openai.Utilities.Response')
->share('Openai.Utilities.Response', [$this, 'getResponse'], true);
$container->alias(Http::class, 'Openai.Utilities.Http')
->share('Openai.Utilities.Http', [$this, 'getHttp'], true);
}
/**
* Get the Uri class
*
* @param Container $container The DI container.
*
* @return Uri
* @since 3.2.0
*/
public function getUri(Container $container): Uri
{
return new Uri();
}
/**
* Get the Response class
*
* @param Container $container The DI container.
*
* @return Response
* @since 3.2.0
*/
public function getResponse(Container $container): Response
{
return new Response();
}
/**
* Get the Http class
*
* @param Container $container The DI container.
*
* @return Http
* @since 3.2.0
*/
public function getHttp(Container $container): Http
{
$openai_token = null;
$openai_org_token = null;
if (Helper::getParams()->get('enable_open_ai') == 1)
{
$openai_token = Helper::getParams()->get('openai_token');
if (Helper::getParams()->get('enable_open_ai_org') == 1)
{
$openai_org_token = Helper::getParams()->get('openai_org_token');
}
if ($openai_token === 'secret')
{
$openai_token = null;
}
if ($openai_org_token === 'secret')
{
$openai_org_token = null;
}
}
return new Http(
$openai_token,
$openai_org_token
);
}
}

View File

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

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>

View File

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