-
Notifications
You must be signed in to change notification settings - Fork 0
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
c8d7412
commit 079b7af
Showing
32 changed files
with
374 additions
and
49 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Devscast\Lugha\Exception; | ||
|
||
/** | ||
* Class InvalidArgumentException. | ||
* | ||
* @author bernard-ng <[email protected]> | ||
*/ | ||
final class InvalidArgumentException extends \InvalidArgumentException | ||
{ | ||
} |
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,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Devscast\Lugha\Exception; | ||
|
||
/** | ||
* Class RuntimeException. | ||
* | ||
* @author bernard-ng <[email protected]> | ||
*/ | ||
final class RuntimeException extends \RuntimeException | ||
{ | ||
} |
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,7 +1,7 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Lugha package. | ||
* This file is part of the Lugha package. | ||
* | ||
* (c) Bernard Ngandu <[email protected]> | ||
* | ||
|
@@ -11,17 +11,17 @@ | |
|
||
declare(strict_types=1); | ||
|
||
namespace Devscast\Lugha\Provider\Service; | ||
namespace Devscast\Lugha\Exception; | ||
|
||
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; | ||
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; | ||
|
||
/** | ||
* Class IntegrationException. | ||
* Class ServiceIntegrationException. | ||
* | ||
* @author bernard-ng <[email protected]> | ||
*/ | ||
class IntegrationException extends \Exception | ||
class ServiceIntegrationException extends \Exception | ||
{ | ||
public function __construct(string $message = '', int $code = 0, ?\Throwable $previous = null) | ||
{ | ||
|
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,26 @@ | ||
<?php | ||
/* | ||
* This file is part of the Lugha package. | ||
* | ||
* (c) Bernard Ngandu <[email protected]> | ||
* | ||
* 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\Exception; | ||
|
||
/** | ||
* Class UnformattedPromptTemplateException. | ||
* | ||
* @author bernard-ng <[email protected]> | ||
*/ | ||
final class UnformattedPromptTemplateException extends \RuntimeException | ||
{ | ||
public function __construct() | ||
{ | ||
parent::__construct('The prompt template has not been formatted yet'); | ||
} | ||
} |
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,7 +1,7 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Lugha package. | ||
* This file is part of the Lugha package. | ||
* | ||
* (c) Bernard Ngandu <[email protected]> | ||
* | ||
|
@@ -11,7 +11,7 @@ | |
|
||
declare(strict_types=1); | ||
|
||
namespace Devscast\Lugha\Retrieval\Loader\Reader\Exception; | ||
namespace Devscast\Lugha\Exception; | ||
|
||
/** | ||
* Class UnsupportedFileException. | ||
|
@@ -22,7 +22,7 @@ final class UnreadableFileException extends \RuntimeException | |
{ | ||
public function __construct(string $file, int $code = 0, \Throwable $previous = null) | ||
{ | ||
$message = sprintf('Unable to read the content of %s', $file); | ||
$message = sprintf('Unable to read or write the content of %s', $file); | ||
parent::__construct($message, $code, $previous); | ||
} | ||
} |
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,7 +1,7 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Lugha package. | ||
* This file is part of the Lugha package. | ||
* | ||
* (c) Bernard Ngandu <[email protected]> | ||
* | ||
|
@@ -11,7 +11,7 @@ | |
|
||
declare(strict_types=1); | ||
|
||
namespace Devscast\Lugha\Retrieval\Loader\Reader\Exception; | ||
namespace Devscast\Lugha\Exception; | ||
|
||
/** | ||
* Class UnsupportedFileException. | ||
|
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,80 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Lugha package. | ||
* | ||
* (c) Bernard Ngandu <[email protected]> | ||
* | ||
* 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\Model\Completion\Tools; | ||
|
||
use Devscast\Lugha\Exception\InvalidArgumentException; | ||
use Devscast\Lugha\Exception\RuntimeException; | ||
use Devscast\Lugha\Provider\Provider; | ||
|
||
/** | ||
* Class FunctionBuilder. | ||
* | ||
* @author bernard-ng <[email protected]> | ||
*/ | ||
abstract class FunctionBuilder | ||
{ | ||
/** | ||
* @param class-string<object>|object $function | ||
*/ | ||
public static function build(object|string $function, Provider $provider = Provider::OPENAI): array | ||
{ | ||
$functionInfo = self::getFunctionInfo($function); | ||
|
||
return match ($provider) { | ||
Provider::OPENAI => self::buildOpenAICompatible($functionInfo), | ||
default => throw new InvalidArgumentException('Unsupported provider.') | ||
}; | ||
} | ||
|
||
private static function buildOpenAICompatible(FunctionInfo $functionInfo): array | ||
{ | ||
return [ | ||
'type' => 'function', | ||
'function' => [ | ||
'name' => $functionInfo->name, | ||
'description' => $functionInfo->description, | ||
'parameters' => [ | ||
'type' => 'object', | ||
'required' => $functionInfo->getRequiredParameters(), | ||
'properties' => array_map( | ||
fn (Parameter $parameter): array => $parameter->build(), | ||
$functionInfo->parameters | ||
), | ||
], | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @param class-string<object>|object $function | ||
*/ | ||
private static function getFunctionInfo(object|string $function): FunctionInfo | ||
{ | ||
try { | ||
$class = new \ReflectionClass($function); | ||
$attributes = $class->getAttributes(FunctionInfo::class); | ||
|
||
if (! isset($attributes[0])) { | ||
$functionFqcn = is_string($function) ? $function : $function::class; | ||
throw new InvalidArgumentException( | ||
sprintf('%s does not have the required %s attribute.', $functionFqcn, FunctionInfo::class) | ||
); | ||
} | ||
|
||
return $attributes[0]->newInstance(); | ||
} catch (\ReflectionException $e) { | ||
throw new RuntimeException($e->getMessage(), previous: $e); | ||
} | ||
} | ||
} |
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,46 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Lugha package. | ||
* | ||
* (c) Bernard Ngandu <[email protected]> | ||
* | ||
* 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\Model\Completion\Tools; | ||
|
||
use Webmozart\Assert\Assert; | ||
|
||
/** | ||
* Class FunctionInfo. | ||
* | ||
* @author bernard-ng <[email protected]> | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_CLASS)] | ||
readonly class FunctionInfo | ||
{ | ||
/** | ||
* @param array<Parameter> $parameters | ||
*/ | ||
public function __construct( | ||
public string $name, | ||
public string $description, | ||
public array $parameters | ||
) { | ||
Assert::notEmpty($name); | ||
Assert::notEmpty($description); | ||
Assert::allIsInstanceOf($parameters, Parameter::class); | ||
} | ||
|
||
public function getRequiredParameters(): array | ||
{ | ||
return array_map( | ||
fn (Parameter $parameter) => $parameter->name, | ||
array_filter($this->parameters, fn (Parameter $parameter) => $parameter->required) | ||
); | ||
} | ||
} |
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,48 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Lugha package. | ||
* | ||
* (c) Bernard Ngandu <[email protected]> | ||
* | ||
* 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\Model\Completion\Tools; | ||
|
||
use Webmozart\Assert\Assert; | ||
|
||
/** | ||
* Class Parameter. | ||
* | ||
* @author bernard-ng <[email protected]> | ||
*/ | ||
final readonly class Parameter | ||
{ | ||
public const array SUPPORTED_TYPES = ['string', 'integer', 'number', 'boolean', 'array']; | ||
|
||
public function __construct( | ||
public string $name, | ||
public string $type, | ||
public ?string $description = null, | ||
public ?array $enum = null, | ||
public bool $required = false | ||
) { | ||
Assert::notEmpty($name); | ||
Assert::oneOf($type, self::SUPPORTED_TYPES); | ||
Assert::notEmpty($description); | ||
} | ||
|
||
public function build(): array | ||
{ | ||
return array_filter([ | ||
'name' => $this->name, | ||
'type' => $this->type, | ||
'enum' => $this->enum, | ||
'description' => $this->description, | ||
], fn ($value) => $value !== null); | ||
} | ||
} |
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.