From bbd9afc692082b028d17501491ad2e259d3f8d66 Mon Sep 17 00:00:00 2001 From: Christopher Hertel Date: Sun, 29 Sep 2024 01:14:57 +0200 Subject: [PATCH] refactor: from vendor to structural namespaces for models and platforms --- examples/chat-claude-anthropic.php | 4 +- examples/chat-gpt-azure.php | 7 +-- examples/chat-gpt-openai.php | 7 +-- examples/embeddings-openai.php | 9 ++- examples/embeddings-voyage.php | 6 +- examples/image-describer-binary.php | 7 +-- examples/image-describer-url.php | 7 +-- examples/reasoning-openai.php | 7 +-- examples/store-mongodb-similarity-search.php | 11 ++-- examples/store-pinecone-similarity-search.php | 11 ++-- examples/stream-claude-anthropic.php | 4 +- examples/stream-gpt-openai.php | 7 +-- examples/structured-output-clock.php | 7 +-- examples/structured-output-math.php | 7 +-- examples/toolbox-clock.php | 7 +-- examples/toolbox-serpapi.php | 7 +-- examples/toolbox-weather.php | 7 +-- examples/toolbox-wikipedia.php | 7 +-- examples/toolbox-youtube.php | 7 +-- src/Anthropic/Model/Claude/Version.php | 36 ----------- src/Anthropic/Platform.php | 15 ----- .../Embeddings/OpenAI.php} | 18 +++--- .../Model => Model/Embeddings}/Voyage.php | 21 ++++--- .../Model => Model/Language}/Claude.php | 21 ++++--- src/{OpenAI/Model => Model/Language}/Gpt.php | 36 ++++++++--- src/OpenAI/Model/Embeddings/Version.php | 34 ----------- src/OpenAI/Model/Gpt/Version.php | 61 ------------------- src/{Anthropic => }/Platform/Anthropic.php | 5 +- .../OpenAI}/AbstractPlatform.php | 3 +- .../Platform => Platform/OpenAI}/Azure.php | 3 +- .../Platform => Platform/OpenAI}/OpenAI.php | 3 +- src/{ => Platform}/OpenAI/Platform.php | 2 +- src/{Voyage => }/Platform/Voyage.php | 10 ++- src/Voyage/Model/Voyage/Version.php | 46 -------------- src/Voyage/Platform.php | 15 ----- 35 files changed, 134 insertions(+), 331 deletions(-) delete mode 100644 src/Anthropic/Model/Claude/Version.php delete mode 100644 src/Anthropic/Platform.php rename src/{OpenAI/Model/Embeddings.php => Model/Embeddings/OpenAI.php} (71%) rename src/{Voyage/Model => Model/Embeddings}/Voyage.php (51%) rename src/{Anthropic/Model => Model/Language}/Claude.php (75%) rename src/{OpenAI/Model => Model/Language}/Gpt.php (79%) delete mode 100644 src/OpenAI/Model/Embeddings/Version.php delete mode 100644 src/OpenAI/Model/Gpt/Version.php rename src/{Anthropic => }/Platform/Anthropic.php (91%) rename src/{OpenAI/Platform => Platform/OpenAI}/AbstractPlatform.php (95%) rename src/{OpenAI/Platform => Platform/OpenAI}/Azure.php (93%) rename src/{OpenAI/Platform => Platform/OpenAI}/OpenAI.php (92%) rename src/{ => Platform}/OpenAI/Platform.php (90%) rename src/{Voyage => }/Platform/Voyage.php (78%) delete mode 100644 src/Voyage/Model/Voyage/Version.php delete mode 100644 src/Voyage/Platform.php diff --git a/examples/chat-claude-anthropic.php b/examples/chat-claude-anthropic.php index 84a449a3..7aa2516c 100755 --- a/examples/chat-claude-anthropic.php +++ b/examples/chat-claude-anthropic.php @@ -1,10 +1,10 @@ 0.5, // default options for the model ]); diff --git a/examples/embeddings-openai.php b/examples/embeddings-openai.php index 0b4f833e..5c875677 100755 --- a/examples/embeddings-openai.php +++ b/examples/embeddings-openai.php @@ -1,8 +1,7 @@ create(<<create(<<embed($documents); // initialize the index $store->initialize(); -$llm = new Gpt($platform, Version::gpt4oMini()); +$llm = new Gpt($platform, Gpt::GPT_4O_MINI); $similaritySearch = new SimilaritySearch($embeddings, $store); $toolBox = new ToolBox(new ToolAnalyzer(), [$similaritySearch]); diff --git a/examples/store-pinecone-similarity-search.php b/examples/store-pinecone-similarity-search.php index 547beb7b..5aa09144 100755 --- a/examples/store-pinecone-similarity-search.php +++ b/examples/store-pinecone-similarity-search.php @@ -6,10 +6,9 @@ use PhpLlm\LlmChain\DocumentEmbedder; use PhpLlm\LlmChain\Message\Message; use PhpLlm\LlmChain\Message\MessageBag; -use PhpLlm\LlmChain\OpenAI\Model\Embeddings; -use PhpLlm\LlmChain\OpenAI\Model\Gpt; -use PhpLlm\LlmChain\OpenAI\Model\Gpt\Version; -use PhpLlm\LlmChain\OpenAI\Platform\OpenAI; +use PhpLlm\LlmChain\Model\Embeddings\OpenAI as Embeddings; +use PhpLlm\LlmChain\Model\Language\Gpt; +use PhpLlm\LlmChain\Platform\OpenAI\OpenAI as Platform; use PhpLlm\LlmChain\Store\Pinecone\Store; use PhpLlm\LlmChain\ToolBox\ChainProcessor; use PhpLlm\LlmChain\ToolBox\Tool\SimilaritySearch; @@ -48,11 +47,11 @@ } // create embeddings for documents -$platform = new OpenAI(HttpClient::create(), $_ENV['OPENAI_API_KEY']); +$platform = new Platform(HttpClient::create(), $_ENV['OPENAI_API_KEY']); $embedder = new DocumentEmbedder($embeddings = new Embeddings($platform), $store); $embedder->embed($documents); -$llm = new Gpt($platform, Version::gpt4oMini()); +$llm = new Gpt($platform, Gpt::GPT_4O_MINI); $similaritySearch = new SimilaritySearch($embeddings, $store); $toolBox = new ToolBox(new ToolAnalyzer(), [$similaritySearch]); diff --git a/examples/stream-claude-anthropic.php b/examples/stream-claude-anthropic.php index 08f12fcf..9c6836ea 100644 --- a/examples/stream-claude-anthropic.php +++ b/examples/stream-claude-anthropic.php @@ -1,10 +1,10 @@ $body - * - * @return array - */ - public function request(array $body): iterable; -} diff --git a/src/OpenAI/Model/Embeddings.php b/src/Model/Embeddings/OpenAI.php similarity index 71% rename from src/OpenAI/Model/Embeddings.php rename to src/Model/Embeddings/OpenAI.php index 249c83c8..ad9b37fd 100644 --- a/src/OpenAI/Model/Embeddings.php +++ b/src/Model/Embeddings/OpenAI.php @@ -2,20 +2,22 @@ declare(strict_types=1); -namespace PhpLlm\LlmChain\OpenAI\Model; +namespace PhpLlm\LlmChain\Model\Embeddings; use PhpLlm\LlmChain\Document\Vector; use PhpLlm\LlmChain\EmbeddingsModel; -use PhpLlm\LlmChain\OpenAI\Model\Embeddings\Version; -use PhpLlm\LlmChain\OpenAI\Platform; +use PhpLlm\LlmChain\Platform\OpenAI\Platform; -final class Embeddings implements EmbeddingsModel +final readonly class OpenAI implements EmbeddingsModel { + public const TEXT_ADA_002 = 'text-embedding-ada-002'; + public const TEXT_3_LARGE = 'text-embedding-3-large'; + public const TEXT_3_SMALL = 'text-embedding-3-small'; + public function __construct( - private readonly Platform $platform, - private ?Version $version = null, + private Platform $platform, + private string $version = self::TEXT_3_SMALL, ) { - $this->version ??= Version::textEmbedding3Small(); } public function create(string $text, array $options = []): Vector @@ -43,7 +45,7 @@ public function multiCreate(array $texts, array $options = []): array private function createBody(string $text): array { return [ - 'model' => $this->version->name, + 'model' => $this->version, 'input' => $text, ]; } diff --git a/src/Voyage/Model/Voyage.php b/src/Model/Embeddings/Voyage.php similarity index 51% rename from src/Voyage/Model/Voyage.php rename to src/Model/Embeddings/Voyage.php index 98f694fa..2cae5262 100644 --- a/src/Voyage/Model/Voyage.php +++ b/src/Model/Embeddings/Voyage.php @@ -2,20 +2,25 @@ declare(strict_types=1); -namespace PhpLlm\LlmChain\Voyage\Model; +namespace PhpLlm\LlmChain\Model\Embeddings; use PhpLlm\LlmChain\Document\Vector; use PhpLlm\LlmChain\EmbeddingsModel; -use PhpLlm\LlmChain\Voyage\Model\Voyage\Version; -use PhpLlm\LlmChain\Voyage\Platform; +use PhpLlm\LlmChain\Platform\Voyage as Platform; -final class Voyage implements EmbeddingsModel +final readonly class Voyage implements EmbeddingsModel { + public const VERSION_V3 = 'voyage-3'; + public const VERSION_V3_LITE = 'voyage-3-lite'; + public const VERSION_FINANCE_2 = 'voyage-finance-2'; + public const VERSION_MULTILINGUAL_2 = 'voyage-multilingual-2'; + public const VERSION_LAW_2 = 'voyage-law-2'; + public const VERSION_CODE_2 = 'voyage-code-2'; + public function __construct( - private readonly Platform $platform, - private ?Version $version = null, + private Platform $platform, + private string $version = self::VERSION_V3, ) { - $this->version ??= Version::v3(); } public function create(string $text, array $options = []): Vector @@ -28,7 +33,7 @@ public function create(string $text, array $options = []): Vector public function multiCreate(array $texts, array $options = []): array { $response = $this->platform->request(array_merge($options, [ - 'model' => $this->version->name, + 'model' => $this->version, 'input' => $texts, ])); diff --git a/src/Anthropic/Model/Claude.php b/src/Model/Language/Claude.php similarity index 75% rename from src/Anthropic/Model/Claude.php rename to src/Model/Language/Claude.php index fda96696..baa0e733 100644 --- a/src/Anthropic/Model/Claude.php +++ b/src/Model/Language/Claude.php @@ -2,27 +2,30 @@ declare(strict_types=1); -namespace PhpLlm\LlmChain\Anthropic\Model; +namespace PhpLlm\LlmChain\Model\Language; -use PhpLlm\LlmChain\Anthropic\Model\Claude\Version; -use PhpLlm\LlmChain\Anthropic\Platform; use PhpLlm\LlmChain\LanguageModel; use PhpLlm\LlmChain\Message\MessageBag; +use PhpLlm\LlmChain\Platform\Anthropic; use PhpLlm\LlmChain\Response\ResponseInterface; use PhpLlm\LlmChain\Response\StreamResponse; use PhpLlm\LlmChain\Response\TextResponse; -final class Claude implements LanguageModel +final readonly class Claude implements LanguageModel { + public const VERSION_3_HAIKU = 'claude-3-haiku-20240307'; + public const VERSION_3_SONNET = 'claude-3-sonnet-20240229'; + public const VERSION_35_SONNET = 'claude-3-5-sonnet-20240620'; + public const VERSION_3_OPUS = 'claude-3-opus-20240229'; + /** * @param array $options The default options for the model usage */ public function __construct( - private readonly Platform $platform, - private ?Version $version = null, - private readonly array $options = ['temperature' => 1.0, 'max_tokens' => 1000], + private Anthropic $platform, + private string $version = self::VERSION_35_SONNET, + private array $options = ['temperature' => 1.0, 'max_tokens' => 1000], ) { - $this->version ??= Version::sonnet35(); } /** @@ -33,7 +36,7 @@ public function call(MessageBag $messages, array $options = []): ResponseInterfa { $system = $messages->getSystemMessage(); $body = array_merge($this->options, $options, [ - 'model' => $this->version->name, + 'model' => $this->version, 'system' => $system->content, 'messages' => $messages->withoutSystemMessage(), ]); diff --git a/src/OpenAI/Model/Gpt.php b/src/Model/Language/Gpt.php similarity index 79% rename from src/OpenAI/Model/Gpt.php rename to src/Model/Language/Gpt.php index 2389a386..425b318a 100644 --- a/src/OpenAI/Model/Gpt.php +++ b/src/Model/Language/Gpt.php @@ -2,13 +2,12 @@ declare(strict_types=1); -namespace PhpLlm\LlmChain\OpenAI\Model; +namespace PhpLlm\LlmChain\Model\Language; use PhpLlm\LlmChain\Exception\RuntimeException; use PhpLlm\LlmChain\LanguageModel; use PhpLlm\LlmChain\Message\MessageBag; -use PhpLlm\LlmChain\OpenAI\Model\Gpt\Version; -use PhpLlm\LlmChain\OpenAI\Platform; +use PhpLlm\LlmChain\Platform\OpenAI\Platform; use PhpLlm\LlmChain\Response\Choice; use PhpLlm\LlmChain\Response\ChoiceResponse; use PhpLlm\LlmChain\Response\ResponseInterface; @@ -19,25 +18,42 @@ final class Gpt implements LanguageModel { + public const GPT_35_TURBO = 'gpt-3.5-turbo'; + public const GPT_35_TURBO_INSTRUCT = 'gpt-3.5-turbo-instruct'; + public const GPT_4 = 'gpt-4'; + public const GPT_4_TURBO = 'gpt-4-turbo'; + public const GPT_4O = 'gpt-4o'; + public const GPT_4O_MINI = 'gpt-4o-mini'; + public const O1_MINI = 'o1-mini'; + public const O1_PREVIEW = 'o1-preview'; + /** * @param array $options The default options for the model usage */ public function __construct( private readonly Platform $platform, - private ?Version $version = null, + private readonly string $version = self::GPT_4O, private readonly array $options = ['temperature' => 1.0], + private bool $supportsImageInput = false, + private bool $supportsStructuredOutput = false, ) { - $this->version ??= Version::gpt4o(); + if (false === $this->supportsImageInput) { + $this->supportsImageInput = in_array($this->version, [self::GPT_4_TURBO, self::GPT_4O, self::GPT_4O_MINI, self::O1_MINI, self::O1_PREVIEW], true); + } + + if (false === $this->supportsStructuredOutput) { + $this->supportsStructuredOutput = in_array($this->version, [self::GPT_4O, self::GPT_4O_MINI], true); + } } /** - * @param array $options The options to be used for this specific call. - * Can overwrite default options. + * @param array $options The options to be used for this specific call. + * Can overwrite default options. */ public function call(MessageBag $messages, array $options = []): ResponseInterface { $body = array_merge($this->options, $options, [ - 'model' => $this->version->name, + 'model' => $this->version, 'messages' => $messages, ]); @@ -76,12 +92,12 @@ public function supportsToolCalling(): bool public function supportsImageInput(): bool { - return $this->version->supportImageInput; + return $this->supportsImageInput; } public function supportsStructuredOutput(): bool { - return $this->version->supportStructuredOutput; + return $this->supportsStructuredOutput; } private function streamIsToolCall(\Generator $response): bool diff --git a/src/OpenAI/Model/Embeddings/Version.php b/src/OpenAI/Model/Embeddings/Version.php deleted file mode 100644 index f56c0ff8..00000000 --- a/src/OpenAI/Model/Embeddings/Version.php +++ /dev/null @@ -1,34 +0,0 @@ - $body + * + * @return array + */ public function request(array $body): array { $response = $this->httpClient->request('POST', 'https://api.voyageai.com/v1/embeddings', [ diff --git a/src/Voyage/Model/Voyage/Version.php b/src/Voyage/Model/Voyage/Version.php deleted file mode 100644 index 77ffd2f3..00000000 --- a/src/Voyage/Model/Voyage/Version.php +++ /dev/null @@ -1,46 +0,0 @@ - $body - * - * @return array - */ - public function request(array $body): array; -}