This repository has been archived by the owner on Oct 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ac96f7d
commit cd3d8a1
Showing
91 changed files
with
2,797 additions
and
197 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"libraryVersion": "1.0.0", | ||
"apiVersion": "1.0.5", | ||
"buildVersion": "1.1.3" | ||
"libraryVersion": "1.0.1", | ||
"apiVersion": "1.0.6", | ||
"buildVersion": "1.1.4" | ||
} |
This file contains 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
This file contains 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,180 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright (c) 2022 Tectalic (https://tectalic.com) | ||
* | ||
* For copyright and license information, please view the LICENSE file that was distributed with this source code. | ||
* | ||
* Please see the README.md file for usage instructions. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tectalic\OpenAi\Handlers; | ||
|
||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Tectalic\OpenAi\Client; | ||
use Tectalic\OpenAi\ClientException; | ||
use Tectalic\OpenAi\Manager; | ||
use Tectalic\OpenAi\Models\AbstractModel; | ||
use Tectalic\OpenAi\Models\AbstractModelCollection; | ||
use Tectalic\OpenAi\Models\Moderations\CreateRequest; | ||
use Tectalic\OpenAi\Models\Moderations\CreateResponse; | ||
use Throwable; | ||
|
||
final class Moderations | ||
{ | ||
/** @var Client */ | ||
private $client; | ||
|
||
/** @var RequestInterface|null */ | ||
private $request = null; | ||
|
||
/** @var ResponseInterface|null */ | ||
private $response = null; | ||
|
||
/** @var class-string<AbstractModel|AbstractModelCollection> */ | ||
private $modelType; | ||
|
||
public function __construct(?Client $client = null) | ||
{ | ||
$this->client = $client ?? Manager::access(); | ||
} | ||
|
||
/** | ||
* Classifies if text violates OpenAI's Content Policy | ||
* | ||
* Operation URL: POST /moderations | ||
* Operation ID: createModeration | ||
* | ||
* @param CreateRequest|array $body | ||
* | ||
* @api | ||
* @return self | ||
*/ | ||
public function create($body): self | ||
{ | ||
$url = '/moderations'; | ||
$this->setRequest($this->client->post( | ||
$url, | ||
\is_array($body) ? new CreateRequest($body) : $body, | ||
['Content-Type' => 'application/json'] | ||
)); | ||
$this->modelType = CreateResponse::class; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Convert response body to an array. | ||
* | ||
* @return array | ||
* @throws ClientException | ||
*/ | ||
private function parseResponse(ResponseInterface $response): array | ||
{ | ||
$contentType = \strtolower($response->getHeaderLine('Content-Type')); | ||
if (substr($contentType, 0, 16) !== 'application/json' && \strlen($contentType) !== 0) { | ||
throw new ClientException(\sprintf('Unsupported content type: %s', $contentType)); | ||
} | ||
|
||
$body = (string) $response->getBody(); | ||
$body = \strlen($body) === 0 ? '[]' : $body; | ||
$data = (array) \json_decode($body, true); | ||
|
||
if (\json_last_error()) { | ||
throw new ClientException( | ||
'Failed to parse JSON response body: ' . \json_last_error_msg() | ||
); | ||
} | ||
return $data; | ||
} | ||
|
||
/** | ||
* Sets the PSR 7 Response object. | ||
* Also removes the previous response. | ||
* | ||
* @param RequestInterface $request | ||
* | ||
* @return void | ||
*/ | ||
private function setRequest(RequestInterface $request): void | ||
{ | ||
$this->request = $request; | ||
$this->response = null; | ||
} | ||
|
||
/** | ||
* Returns the PSR 7 Response object. | ||
* | ||
* @internal | ||
* @return RequestInterface | ||
* @throws ClientException | ||
*/ | ||
public function getRequest(): RequestInterface | ||
{ | ||
if (\is_null($this->request)) { | ||
throw new ClientException('Request not configured.'); | ||
} | ||
return $this->request; | ||
} | ||
|
||
/** | ||
* Returns the PSR 7 Response object. | ||
* | ||
* @api | ||
* @return ResponseInterface | ||
*/ | ||
public function getResponse(): ResponseInterface | ||
{ | ||
if (!\is_null($this->response)) { | ||
return $this->response; | ||
} | ||
|
||
$this->response = $this->client->sendRequest($this->getRequest()); | ||
$this->request = null; | ||
return $this->response; | ||
} | ||
|
||
/** | ||
* Returns the response body as a model/DTO. | ||
* | ||
* @api | ||
* @return AbstractModel|AbstractModelCollection | ||
* @throws ClientException if an unsuccessful HTTP response occurs. | ||
* @throws ClientException if the response body cannot be parsed. | ||
*/ | ||
public function toModel(): object | ||
{ | ||
if ($this->getResponse()->getStatusCode() < 200 || $this->getResponse()->getStatusCode() >= 300) { | ||
throw new ClientException( | ||
\sprintf( | ||
'Unsuccessful response. HTTP status code: %s (%s).', | ||
$this->getResponse()->getStatusCode(), | ||
$this->getResponse()->getReasonPhrase() | ||
) | ||
); | ||
} | ||
$class = $this->modelType; | ||
try { | ||
return new $class($this->parseResponse($this->getResponse())); | ||
} catch (Throwable $e) { | ||
throw new ClientException( | ||
'Response body parse failed. See previous exception for details.', | ||
0, | ||
$e | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Returns the response body as an associative array. | ||
* | ||
* @api | ||
* @return array<string,mixed> | ||
*/ | ||
public function toArray(): array | ||
{ | ||
return $this->parseResponse($this->getResponse()); | ||
} | ||
} |
This file contains 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 |
---|---|---|
|
@@ -118,7 +118,7 @@ final class CreateRequest extends AbstractModel | |
* 5 most likely tokens. The API will always return the logprob of the sampled | ||
* token, so there may be up to logprobs+1 elements in the response. | ||
* The maximum value for logprobs is 5. If you need more than this, please contact | ||
* [email protected] and describe your use case. | ||
* us through our Help center and describe your use case. | ||
* When logprobs is set, completion will be automatically added into expand to get | ||
* the logprobs. | ||
* | ||
|
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -89,10 +89,10 @@ final class CreateRequest extends AbstractModel | |
* risks. Try 0.9 for more creative applications, and 0 (argmax sampling) for ones | ||
* with a well-defined answer. | ||
* | ||
* Example: 0 | ||
* | ||
* Default Value: 0 | ||
* | ||
* Example: 0 | ||
* | ||
* @var float|int|null | ||
*/ | ||
public $temperature; | ||
|
@@ -103,7 +103,7 @@ final class CreateRequest extends AbstractModel | |
* 5 most likely tokens. The API will always return the logprob of the sampled | ||
* token, so there may be up to logprobs+1 elements in the response. | ||
* The maximum value for logprobs is 5. If you need more than this, please contact | ||
* [email protected] and describe your use case. | ||
* us through our Help center and describe your use case. | ||
* When logprobs is set, completion will be automatically added into expand to get | ||
* the logprobs. | ||
* | ||
|
This file contains 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
This file contains 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
Oops, something went wrong.