Skip to content

Commit

Permalink
Merge pull request #17 from marcelooblan2016/feature/phpstan
Browse files Browse the repository at this point in the history
fixes: based on phpstan
  • Loading branch information
marcelooblan2016 authored Nov 24, 2023
2 parents 3c3e3c4 + f7609c0 commit 5f51140
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 26 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"orchestra/testbench": "^5.0|^6.0|^7.0"
"orchestra/testbench": "^5.0|^6.0|^7.0",
"nunomaduro/larastan": "^2.6"
},
"extra": {
"laravel": {
Expand All @@ -31,4 +32,4 @@
"test": "phpunit"
},
"license": "MIT"
}
}
8 changes: 8 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
includes:
- ./vendor/nunomaduro/larastan/extension.neon
parameters:
paths:
- src
level: 8
ignoreErrors:
excludePaths:
28 changes: 13 additions & 15 deletions src/Services/Ai/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,31 @@

namespace Marxolity\OpenAi\Services\Ai;
use Illuminate\Support\Facades\Http;
use Illuminate\Http\Client\Response;

class Base
{
// Response
public $responseRaw;
public $responseMessage;
// Api
protected $apiKey;
// Models
protected $model;
protected $allowedModels = [
// GPT 3
/** @var ?array<mixed> */
public ?array $responseRaw;
public ?string $responseMessage;
protected string $apiKey = '';
protected string $model = '';
/** @var array<string> */
protected array $allowedModels = [
'gpt-3.5-turbo', 'gpt-3.5-turbo-1106',
// GPT 4 (Tier 1 & Up)
'gpt-4', 'gpt-4-vision-preview'
];
// Content
protected $contentQuery;
// End points
protected $endPoint = 'https://api.openai.com/v1/chat/completions';
/** @var string */
protected string $contentQuery;
/** @var string */
protected string $endPoint = 'https://api.openai.com/v1/chat/completions';

public function __construct() {
$this->apiKey = config('open-ai.api_key');
$this->model = config('open-ai.default_model');
}

public function sendRequest()
public function sendRequest(): Response
{
return Http::withToken($this->apiKey)->acceptJson()->post($this->endPoint, [
"model" => $this->model,
Expand Down
3 changes: 1 addition & 2 deletions src/Services/Ai/OpenAi.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class OpenAi extends Base implements AiInterface, ResponseInterface, ValidationI
use Validation, Response;
/**
* send the request build to openai endpoint
* @return string (XML or JSON)
**/
public function send(): self
{
Expand All @@ -25,7 +24,7 @@ public function send(): self
// Parse Message
$responseMessage = Arr::get($this->responseRaw, 'choices.0.message.content');
// Error Message
if (empty($responseMessage)) $responseMessage = Arr::get($this->responseRaw, 'error.message');
if (empty($responseMessage)) $responseMessage = Arr::get($this->responseRaw, 'error.message'); // @phpstan-ignore argument.type
// Set response message
$this->responseMessage = $responseMessage;

Expand Down
9 changes: 8 additions & 1 deletion src/Traits/Interfaces/ResponseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
interface ResponseInterface
{
public function toJson(): string;
public function toXml(): string;
/**
* @return string|false Return XML string or false.
**/
public function toXml();
/**
* $this->responseRaw
* @return array<mixed>
**/
public function toArray(): array;
}
7 changes: 3 additions & 4 deletions src/Traits/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,15 @@ public function toJson(): string
return collect($this->responseRaw)->toJson();
}
/**
* $this->responseRaw
* @return string
* @return string|false Return XML string or false.
**/
public function toXml(): string
public function toXml()
{
return $this->arrayToXml($this->responseRaw);
}
/**
* $this->responseRaw
* @return array
* @return array<mixed>
**/
public function toArray(): array
{
Expand Down
11 changes: 9 additions & 2 deletions src/Traits/XmlParser.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
<?php

namespace Marxolity\OpenAi\Traits;
use \SimpleXMLElement;

trait XmlParser
{
public function arrayToXml($array, $xml = null) {
if ($xml === null) $xml = new \SimpleXMLElement('<root/>');
/**
* @param ?array<string> $array,?SimpleXMLElement
* @return string|false Return XML string or false.
**/
public function arrayToXml(?array $array, ?SimpleXMLElement $xml = null)
{
if ($xml === null) $xml = new SimpleXMLElement('<root/>');

collect($array)
->each( function ($value, $key) use (&$xml) {
/** @phpstan-ignore-next-line */
if (is_array($value)) {
if (!is_numeric($key)) {
$child = $xml->addChild($key);
Expand Down

0 comments on commit 5f51140

Please sign in to comment.