Skip to content

Commit

Permalink
ref: throw custom exception for assertion violation
Browse files Browse the repository at this point in the history
  • Loading branch information
bernard-ng committed Dec 27, 2024
1 parent 079b7af commit 5f198f8
Show file tree
Hide file tree
Showing 19 changed files with 55 additions and 19 deletions.
30 changes: 30 additions & 0 deletions src/Assert.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the Lugha package.
*
* (c) Bernard Ngandu <bernard@devscast.tech>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Devscast\Lugha;

use Devscast\Lugha\Exception\InvalidArgumentException;

/**
* Class Assert.
*
* @author bernard-ng <bernard@devscast.tech>
*/
final class Assert extends \Webmozart\Assert\Assert
{
#[\Override]
protected static function reportInvalidArgument($message)
{
throw new InvalidArgumentException($message);
}
}
2 changes: 1 addition & 1 deletion src/Model/Completion/CompletionConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Devscast\Lugha\Model\Completion;

use Webmozart\Assert\Assert;
use Devscast\Lugha\Assert;

/**
* Class CompletionConfig.
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Completion/Prompt/PromptTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@

namespace Devscast\Lugha\Model\Completion\Prompt;

use Devscast\Lugha\Assert;
use Devscast\Lugha\Exception\UnformattedPromptTemplateException;
use Devscast\Lugha\Exception\UnreadableFileException;
use Webmozart\Assert\Assert;

/**
* Class PromptTemplate.
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Completion/Tools/FunctionInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Devscast\Lugha\Model\Completion\Tools;

use Webmozart\Assert\Assert;
use Devscast\Lugha\Assert;

/**
* Class FunctionInfo.
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Completion/Tools/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Devscast\Lugha\Model\Completion\Tools;

use Webmozart\Assert\Assert;
use Devscast\Lugha\Assert;

/**
* Class Parameter.
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Embedding/DimensionReducer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Devscast\Lugha\Model\Embedding;

use Webmozart\Assert\Assert;
use Devscast\Lugha\Assert;

/**
* Class DimensionReducer.
Expand Down
4 changes: 3 additions & 1 deletion src/Model/Embedding/Distance.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace Devscast\Lugha\Model\Embedding;

use Devscast\Lugha\Exception\InvalidArgumentException;

/**
* Class Distance.
* Represents the distance metric to use when comparing vectors.
Expand Down Expand Up @@ -48,7 +50,7 @@ enum Distance
public function compute(array $a, array $b): float
{
if (count($a) !== count($b)) {
throw new \InvalidArgumentException('Vectors must have the same dimension.');
throw new InvalidArgumentException('Vectors must have the same dimension.');
}

return match ($this) {
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Embedding/EmbeddingConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Devscast\Lugha\Model\Embedding;

use Webmozart\Assert\Assert;
use Devscast\Lugha\Assert;

/**
* Class EmbeddingConfig.
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Reranking/RerankingConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Devscast\Lugha\Model\Reranking;

use Webmozart\Assert\Assert;
use Devscast\Lugha\Assert;

/**
* Class RerankingConfig.
Expand Down
2 changes: 1 addition & 1 deletion src/Provider/ProviderConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Devscast\Lugha\Provider;

use Webmozart\Assert\Assert;
use Devscast\Lugha\Assert;

/**
* Class ProviderConfig.
Expand Down
2 changes: 1 addition & 1 deletion src/Provider/Service/Client/GoogleClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Devscast\Lugha\Provider\Service\Client;

use Devscast\Lugha\Assert;
use Devscast\Lugha\Exception\ServiceIntegrationException;
use Devscast\Lugha\Model\Completion\Chat\History;
use Devscast\Lugha\Model\Completion\CompletionConfig;
Expand All @@ -23,7 +24,6 @@
use Devscast\Lugha\Provider\Service\Client;
use Devscast\Lugha\Provider\Service\HasCompletionSupport;
use Devscast\Lugha\Provider\Service\HasEmbeddingSupport;
use Webmozart\Assert\Assert;

/**
* Class GoogleClient.
Expand Down
2 changes: 1 addition & 1 deletion src/Provider/Service/Client/MistralClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Devscast\Lugha\Provider\Service\Client;

use Devscast\Lugha\Assert;
use Devscast\Lugha\Exception\ServiceIntegrationException;
use Devscast\Lugha\Model\Completion\Chat\History;
use Devscast\Lugha\Model\Completion\CompletionConfig;
Expand All @@ -23,7 +24,6 @@
use Devscast\Lugha\Provider\Service\Client;
use Devscast\Lugha\Provider\Service\HasCompletionSupport;
use Devscast\Lugha\Provider\Service\HasEmbeddingSupport;
use Webmozart\Assert\Assert;

/**
* Class MistralClient.
Expand Down
2 changes: 1 addition & 1 deletion src/Provider/Service/Client/OllamaClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Devscast\Lugha\Provider\Service\Client;

use Devscast\Lugha\Assert;
use Devscast\Lugha\Exception\ServiceIntegrationException;
use Devscast\Lugha\Model\Completion\Chat\History;
use Devscast\Lugha\Model\Completion\CompletionConfig;
Expand All @@ -23,7 +24,6 @@
use Devscast\Lugha\Provider\Service\Client;
use Devscast\Lugha\Provider\Service\HasCompletionSupport;
use Devscast\Lugha\Provider\Service\HasEmbeddingSupport;
use Webmozart\Assert\Assert;

/**
* Class OllamaClient.
Expand Down
2 changes: 1 addition & 1 deletion src/Provider/Service/Client/OpenAIClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Devscast\Lugha\Provider\Service\Client;

use Devscast\Lugha\Assert;
use Devscast\Lugha\Exception\ServiceIntegrationException;
use Devscast\Lugha\Model\Completion\Chat\History;
use Devscast\Lugha\Model\Completion\CompletionConfig;
Expand All @@ -23,7 +24,6 @@
use Devscast\Lugha\Provider\Service\Client;
use Devscast\Lugha\Provider\Service\HasCompletionSupport;
use Devscast\Lugha\Provider\Service\HasEmbeddingSupport;
use Webmozart\Assert\Assert;

/**
* Class OpenAIClient.
Expand Down
2 changes: 1 addition & 1 deletion src/Provider/Service/Client/VoyagerClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Devscast\Lugha\Provider\Service\Client;

use Devscast\Lugha\Assert;
use Devscast\Lugha\Exception\ServiceIntegrationException;
use Devscast\Lugha\Model\Embedding\EmbeddingConfig;
use Devscast\Lugha\Model\Reranking\RankedDocument;
Expand All @@ -24,7 +25,6 @@
use Devscast\Lugha\Provider\Service\HasEmbeddingSupport;
use Devscast\Lugha\Provider\Service\HasRerankingSupport;
use Devscast\Lugha\Retrieval\Document;
use Webmozart\Assert\Assert;

/**
* Class VoyagerClient.
Expand Down
3 changes: 2 additions & 1 deletion src/Provider/Service/HasEmbeddingSupport.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Devscast\Lugha\Provider\Service;

use Devscast\Lugha\Exception\InvalidArgumentException;
use Devscast\Lugha\Exception\ServiceIntegrationException;
use Devscast\Lugha\Model\Embedding\EmbeddingConfig;
use Devscast\Lugha\Provider\Response\EmbeddingResponse;
Expand All @@ -29,7 +30,7 @@ interface HasEmbeddingSupport
* @param EmbeddingConfig $config The configuration to use for embeddings.
*
* @throws ServiceIntegrationException when any error occurs during the request.
* @throws \InvalidArgumentException when the prompt is empty.
* @throws InvalidArgumentException when the prompt is empty.
*/
public function embeddings(string $prompt, EmbeddingConfig $config): EmbeddingResponse;
}
3 changes: 2 additions & 1 deletion src/Provider/Service/HasRerankingSupport.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Devscast\Lugha\Provider\Service;

use Devscast\Lugha\Exception\InvalidArgumentException;
use Devscast\Lugha\Exception\ServiceIntegrationException;
use Devscast\Lugha\Model\Reranking\RerankingConfig;
use Devscast\Lugha\Provider\Response\RerankingResponse;
Expand All @@ -31,7 +32,7 @@ interface HasRerankingSupport
* @param RerankingConfig $config The configuration to use for reranking.
*
* @throws ServiceIntegrationException when any error occurs during the request.
* @throws \InvalidArgumentException when the prompt or documents are empty.
* @throws InvalidArgumentException when the prompt or documents are empty.
*/
public function rerank(string $prompt, array $documents, RerankingConfig $config): RerankingResponse;
}
5 changes: 3 additions & 2 deletions tests/Model/Completion/Prompt/PromptTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Devscast\Lugha\Tests\Model\Completion\Prompt;

use Devscast\Lugha\Exception\InvalidArgumentException;
use Devscast\Lugha\Exception\UnformattedPromptTemplateException;
use Devscast\Lugha\Model\Completion\Prompt\PromptTemplate;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -56,15 +57,15 @@ public function testItCanFormatAPromptTemplateWithMultiplePlaceholders(): void

public function testItFailsToCreateAPromptTemplateWithEmptyTemplate(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Template cannot be empty');

new PromptTemplate('');
}

public function testItFailsToFormatAPromptTemplateWithEmptyValues(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Values cannot be empty');

$template = PromptTemplate::from('Hello {context}');
Expand Down
3 changes: 2 additions & 1 deletion tests/Model/Embedding/DistanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Devscast\Lugha\Tests\Model\Embedding;

use Devscast\Lugha\Exception\InvalidArgumentException;
use Devscast\Lugha\Model\Embedding\Distance;
use PHPUnit\Framework\TestCase;

Expand All @@ -25,7 +26,7 @@ final class DistanceTest extends TestCase
{
public function testVectorWithDifferentDimensions(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Vectors must have the same dimension.');

$distance = Distance::COSINE;
Expand Down

0 comments on commit 5f198f8

Please sign in to comment.