diff --git a/src/Bridge/HuggingFace/ModelClient.php b/src/Bridge/HuggingFace/ModelClient.php index b955e06..6abd6fb 100644 --- a/src/Bridge/HuggingFace/ModelClient.php +++ b/src/Bridge/HuggingFace/ModelClient.php @@ -4,6 +4,7 @@ namespace PhpLlm\LlmChain\Bridge\HuggingFace; +use PhpLlm\LlmChain\Exception\InvalidArgumentException; use PhpLlm\LlmChain\Model\Message\Content\Audio; use PhpLlm\LlmChain\Model\Message\Content\Image; use PhpLlm\LlmChain\Model\Message\MessageBagInterface; @@ -99,6 +100,6 @@ private function getPayload(object|array|string $input, array $options): array return $payload; } - throw new \InvalidArgumentException('Unsupported input type: '.get_debug_type($input)); + throw new InvalidArgumentException('Unsupported input type: '.get_debug_type($input)); } } diff --git a/src/Bridge/HuggingFace/ResponseConverter.php b/src/Bridge/HuggingFace/ResponseConverter.php index 0ce2935..156790a 100644 --- a/src/Bridge/HuggingFace/ResponseConverter.php +++ b/src/Bridge/HuggingFace/ResponseConverter.php @@ -14,6 +14,8 @@ use PhpLlm\LlmChain\Bridge\HuggingFace\Output\TokenClassificationResult; use PhpLlm\LlmChain\Bridge\HuggingFace\Output\ZeroShotClassificationResult; use PhpLlm\LlmChain\Document\Vector; +use PhpLlm\LlmChain\Exception\InvalidArgumentException; +use PhpLlm\LlmChain\Exception\RuntimeException; use PhpLlm\LlmChain\Model\Model as BaseModel; use PhpLlm\LlmChain\Model\Response\BinaryResponse; use PhpLlm\LlmChain\Model\Response\ResponseInterface as LlmResponse; @@ -33,11 +35,11 @@ public function supports(BaseModel $model, array|string|object $input): bool public function convert(ResponseInterface $response, array $options = []): LlmResponse { if (503 === $response->getStatusCode()) { - return throw new \RuntimeException('Service unavailable.'); + return throw new RuntimeException('Service unavailable.'); } if (404 === $response->getStatusCode()) { - return throw new \InvalidArgumentException('Model, provider or task not found (404).'); + return throw new InvalidArgumentException('Model, provider or task not found (404).'); } $headers = $response->getHeaders(false); @@ -48,11 +50,11 @@ public function convert(ResponseInterface $response, array $options = []): LlmRe $message = is_string($content) ? $content : (is_array($content['error']) ? $content['error'][0] : $content['error']); - throw new \InvalidArgumentException(sprintf('API Client Error (%d): %s', $response->getStatusCode(), $message)); + throw new InvalidArgumentException(sprintf('API Client Error (%d): %s', $response->getStatusCode(), $message)); } if (200 !== $response->getStatusCode()) { - throw new \RuntimeException('Unhandled response code: '.$response->getStatusCode()); + throw new RuntimeException('Unhandled response code: '.$response->getStatusCode()); } $task = $options['task'] ?? null; @@ -78,7 +80,7 @@ public function convert(ResponseInterface $response, array $options = []): LlmRe Task::TRANSLATION => new TextResponse($content[0]['translation_text'] ?? ''), Task::ZERO_SHOT_CLASSIFICATION => new StructuredResponse(ZeroShotClassificationResult::fromArray($content)), - default => throw new \RuntimeException(sprintf('Unsupported task: %s', $task)), + default => throw new RuntimeException(sprintf('Unsupported task: %s', $task)), }; } } diff --git a/src/Bridge/OpenAI/DallE/ModelClient.php b/src/Bridge/OpenAI/DallE/ModelClient.php index 1b01365..39552d9 100644 --- a/src/Bridge/OpenAI/DallE/ModelClient.php +++ b/src/Bridge/OpenAI/DallE/ModelClient.php @@ -5,6 +5,7 @@ namespace PhpLlm\LlmChain\Bridge\OpenAI\DallE; use PhpLlm\LlmChain\Bridge\OpenAI\DallE; +use PhpLlm\LlmChain\Exception\RuntimeException; use PhpLlm\LlmChain\Model\Model; use PhpLlm\LlmChain\Model\Response\ResponseInterface as LlmResponse; use PhpLlm\LlmChain\Platform\ModelClient as PlatformResponseFactory; @@ -47,7 +48,7 @@ public function convert(HttpResponse $response, array $options = []): LlmRespons { $response = $response->toArray(); if (!isset($response['data'][0])) { - throw new \RuntimeException('No image generated.'); + throw new RuntimeException('No image generated.'); } $images = []; diff --git a/src/Bridge/TransformersPHP/Handler.php b/src/Bridge/TransformersPHP/Handler.php index 2308acf..f7bea77 100644 --- a/src/Bridge/TransformersPHP/Handler.php +++ b/src/Bridge/TransformersPHP/Handler.php @@ -5,6 +5,7 @@ namespace PhpLlm\LlmChain\Bridge\TransformersPHP; use Codewithkyrian\Transformers\Pipelines\Task; +use PhpLlm\LlmChain\Exception\InvalidArgumentException; use PhpLlm\LlmChain\Model\Model as BaseModel; use PhpLlm\LlmChain\Model\Response\ResponseInterface as LlmResponse; use PhpLlm\LlmChain\Model\Response\StructuredResponse; @@ -25,7 +26,7 @@ public function supports(BaseModel $model, object|array|string $input): bool public function request(BaseModel $model, object|array|string $input, array $options = []): ResponseInterface { if (!isset($options['task'])) { - throw new \InvalidArgumentException('The task option is required.'); + throw new InvalidArgumentException('The task option is required.'); } $pipeline = pipeline( @@ -44,7 +45,7 @@ public function request(BaseModel $model, object|array|string $input, array $opt public function convert(ResponseInterface $response, array $options = []): LlmResponse { if (!$response instanceof PipelineResponse) { - throw new \InvalidArgumentException('The response is not a valid TransformersPHP response.'); + throw new InvalidArgumentException('The response is not a valid TransformersPHP response.'); } $task = $options['task']; diff --git a/src/Bridge/TransformersPHP/PipelineResponse.php b/src/Bridge/TransformersPHP/PipelineResponse.php index 0ff8036..61fbd9c 100644 --- a/src/Bridge/TransformersPHP/PipelineResponse.php +++ b/src/Bridge/TransformersPHP/PipelineResponse.php @@ -5,6 +5,7 @@ namespace PhpLlm\LlmChain\Bridge\TransformersPHP; use Codewithkyrian\Transformers\Pipelines\Pipeline; +use PhpLlm\LlmChain\Exception\RuntimeException; use Symfony\Contracts\HttpClient\ResponseInterface; final class PipelineResponse implements ResponseInterface @@ -35,7 +36,7 @@ public function getHeaders(bool $throw = true): array public function getContent(bool $throw = true): string { - throw new \RuntimeException('Not implemented'); + throw new RuntimeException('Not implemented'); } /** @@ -48,11 +49,11 @@ public function toArray(bool $throw = true): array public function cancel(): void { - throw new \RuntimeException('Not implemented'); + throw new RuntimeException('Not implemented'); } public function getInfo(?string $type = null): mixed { - throw new \RuntimeException('Not implemented'); + throw new RuntimeException('Not implemented'); } } diff --git a/src/Bridge/TransformersPHP/PlatformFactory.php b/src/Bridge/TransformersPHP/PlatformFactory.php index 00a1bf7..fa013de 100644 --- a/src/Bridge/TransformersPHP/PlatformFactory.php +++ b/src/Bridge/TransformersPHP/PlatformFactory.php @@ -5,6 +5,7 @@ namespace PhpLlm\LlmChain\Bridge\TransformersPHP; use Codewithkyrian\Transformers\Transformers; +use PhpLlm\LlmChain\Exception\RuntimeException; use PhpLlm\LlmChain\Platform; final readonly class PlatformFactory @@ -12,7 +13,7 @@ public static function create(): Platform { if (!class_exists(Transformers::class)) { - throw new \RuntimeException('TransformersPHP is not installed. Please install it using "composer require codewithkyrian/transformers".'); + throw new RuntimeException('TransformersPHP is not installed. Please install it using "composer require codewithkyrian/transformers".'); } return new Platform([$handler = new Handler()], [$handler]); diff --git a/src/Chain/JsonSchema/Factory.php b/src/Chain/JsonSchema/Factory.php index c74d919..5f6b994 100644 --- a/src/Chain/JsonSchema/Factory.php +++ b/src/Chain/JsonSchema/Factory.php @@ -5,6 +5,7 @@ namespace PhpLlm\LlmChain\Chain\JsonSchema; use PhpLlm\LlmChain\Chain\JsonSchema\Attribute\With; +use PhpLlm\LlmChain\Exception\InvalidArgumentException; use Symfony\Component\TypeInfo\Type; use Symfony\Component\TypeInfo\Type\BuiltinType; use Symfony\Component\TypeInfo\Type\CollectionType; @@ -155,7 +156,7 @@ private function getTypeSchema(Type $type): array case $type->isIdentifiedBy(TypeIdentifier::OBJECT): if ($type instanceof BuiltinType) { - throw new \InvalidArgumentException('Cannot build schema from plain object type.'); + throw new InvalidArgumentException('Cannot build schema from plain object type.'); } assert($type instanceof ObjectType); if (in_array($type->getClassName(), ['DateTime', 'DateTimeImmutable', 'DateTimeInterface'], true)) { diff --git a/src/Chain/Toolbox/Tool/Crawler.php b/src/Chain/Toolbox/Tool/Crawler.php index 0bf7a05..5d347e1 100644 --- a/src/Chain/Toolbox/Tool/Crawler.php +++ b/src/Chain/Toolbox/Tool/Crawler.php @@ -5,6 +5,7 @@ namespace PhpLlm\LlmChain\Chain\Toolbox\Tool; use PhpLlm\LlmChain\Chain\Toolbox\Attribute\AsTool; +use PhpLlm\LlmChain\Exception\RuntimeException; use Symfony\Component\DomCrawler\Crawler as DomCrawler; use Symfony\Contracts\HttpClient\HttpClientInterface; @@ -15,7 +16,7 @@ public function __construct( private HttpClientInterface $httpClient, ) { if (!class_exists(DomCrawler::class)) { - throw new \RuntimeException('The DomCrawler component is not installed. Please install it using "composer require symfony/dom-crawler".'); + throw new RuntimeException('The DomCrawler component is not installed. Please install it using "composer require symfony/dom-crawler".'); } } diff --git a/src/Model/Message/Content/File.php b/src/Model/Message/Content/File.php index 312ce37..8b87be4 100644 --- a/src/Model/Message/Content/File.php +++ b/src/Model/Message/Content/File.php @@ -5,6 +5,7 @@ namespace PhpLlm\LlmChain\Model\Message\Content; use PhpLlm\LlmChain\Exception\InvalidArgumentException; +use PhpLlm\LlmChain\Exception\RuntimeException; use function Symfony\Component\String\u; @@ -32,7 +33,7 @@ public static function fromDataUrl(string $dataUrl): static public static function fromFile(string $path): static { if (!is_readable($path)) { - throw new \InvalidArgumentException(sprintf('The file "%s" does not exist or is not readable.', $path)); + throw new InvalidArgumentException(sprintf('The file "%s" does not exist or is not readable.', $path)); } return new static( @@ -68,7 +69,7 @@ public function asDataUrl(): string public function asResource() { if (null === $this->path) { - throw new \RuntimeException('You can only get a resource after creating fromFile.'); + throw new RuntimeException('You can only get a resource after creating fromFile.'); } return fopen($this->path, 'r'); diff --git a/src/Model/Response/BinaryResponse.php b/src/Model/Response/BinaryResponse.php index 2c0c411..6a1b1ff 100644 --- a/src/Model/Response/BinaryResponse.php +++ b/src/Model/Response/BinaryResponse.php @@ -4,6 +4,8 @@ namespace PhpLlm\LlmChain\Model\Response; +use PhpLlm\LlmChain\Exception\RuntimeException; + final class BinaryResponse extends BaseResponse { public function __construct( @@ -25,7 +27,7 @@ public function toBase64(): string public function toDataUri(): string { if (null === $this->mimeType) { - throw new \RuntimeException('Mime type is not set.'); + throw new RuntimeException('Mime type is not set.'); } return 'data:'.$this->mimeType.';base64,'.$this->toBase64();