Skip to content

Commit

Permalink
refactor: introduce decoupled platform layer and convert models to st…
Browse files Browse the repository at this point in the history
…ate objects
  • Loading branch information
chr-hertel committed Oct 13, 2024
1 parent 8f87f8b commit 84f0546
Show file tree
Hide file tree
Showing 141 changed files with 1,400 additions and 955 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"mongodb/mongodb": "^1.20",
"php-cs-fixer/shim": "^3.64",
"phpstan/phpstan": "^1.12",
"phpstan/phpstan-webmozart-assert": "^1.2",
"phpunit/phpunit": "^11.3",
"probots-io/pinecone-php": "^1.0",
"rector/rector": "^1.2",
Expand Down
15 changes: 7 additions & 8 deletions examples/chat-claude-anthropic.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<?php

use PhpLlm\LlmChain\Bridge\Anthropic\Claude;
use PhpLlm\LlmChain\Bridge\Anthropic\PlatformFactory;
use PhpLlm\LlmChain\Chain;
use PhpLlm\LlmChain\Message\Message;
use PhpLlm\LlmChain\Message\MessageBag;
use PhpLlm\LlmChain\Model\Language\Claude;
use PhpLlm\LlmChain\Platform\Anthropic;
use PhpLlm\LlmChain\Model\Message\Message;
use PhpLlm\LlmChain\Model\Message\MessageBag;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\HttpClient\HttpClient;

require_once dirname(__DIR__).'/vendor/autoload.php';
(new Dotenv())->loadEnv(dirname(__DIR__).'/.env');
Expand All @@ -16,10 +15,10 @@
exit(1);
}

$platform = new Anthropic(HttpClient::create(), $_ENV['ANTHROPIC_API_KEY']);
$llm = new Claude($platform);
$platform = PlatformFactory::create($_ENV['ANTHROPIC_API_KEY']);
$llm = new Claude();

$chain = new Chain($llm);
$chain = new Chain($platform, $llm);
$messages = new MessageBag(
Message::forSystem('You are a pirate and you write funny.'),
Message::ofUser('What is the Symfony framework?'),
Expand Down
15 changes: 7 additions & 8 deletions examples/chat-gpt-azure.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<?php

use PhpLlm\LlmChain\Bridge\Azure\OpenAI\PlatformFactory;
use PhpLlm\LlmChain\Bridge\OpenAI\GPT;
use PhpLlm\LlmChain\Chain;
use PhpLlm\LlmChain\Message\Message;
use PhpLlm\LlmChain\Message\MessageBag;
use PhpLlm\LlmChain\Model\Language\Gpt;
use PhpLlm\LlmChain\Platform\OpenAI\Azure;
use PhpLlm\LlmChain\Model\Message\Message;
use PhpLlm\LlmChain\Model\Message\MessageBag;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\HttpClient\HttpClient;

require_once dirname(__DIR__).'/vendor/autoload.php';
(new Dotenv())->loadEnv(dirname(__DIR__).'/.env');
Expand All @@ -17,15 +16,15 @@
exit(1);
}

$platform = new Azure(HttpClient::create(),
$platform = PlatformFactory::create(
$_ENV['AZURE_OPENAI_BASEURL'],
$_ENV['AZURE_OPENAI_DEPLOYMENT'],
$_ENV['AZURE_OPENAI_VERSION'],
$_ENV['AZURE_OPENAI_KEY'],
);
$llm = new Gpt($platform, Gpt::GPT_4O_MINI);
$llm = new GPT(GPT::GPT_4O_MINI);

$chain = new Chain($llm);
$chain = new Chain($platform, $llm);
$messages = new MessageBag(
Message::forSystem('You are a pirate and you write funny.'),
Message::ofUser('What is the Symfony framework?'),
Expand Down
15 changes: 7 additions & 8 deletions examples/chat-gpt-openai.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<?php

use PhpLlm\LlmChain\Bridge\OpenAI\GPT;
use PhpLlm\LlmChain\Bridge\OpenAI\PlatformFactory;
use PhpLlm\LlmChain\Chain;
use PhpLlm\LlmChain\Message\Message;
use PhpLlm\LlmChain\Message\MessageBag;
use PhpLlm\LlmChain\Model\Language\Gpt;
use PhpLlm\LlmChain\Platform\OpenAI\OpenAI;
use PhpLlm\LlmChain\Model\Message\Message;
use PhpLlm\LlmChain\Model\Message\MessageBag;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\HttpClient\HttpClient;

require_once dirname(__DIR__).'/vendor/autoload.php';
(new Dotenv())->loadEnv(dirname(__DIR__).'/.env');
Expand All @@ -16,12 +15,12 @@
exit(1);
}

$platform = new OpenAI(HttpClient::create(), $_ENV['OPENAI_API_KEY']);
$llm = new Gpt($platform, Gpt::GPT_4O_MINI, [
$platform = PlatformFactory::create($_ENV['OPENAI_API_KEY']);
$llm = new GPT(GPT::GPT_4O_MINI, [
'temperature' => 0.5, // default options for the model
]);

$chain = new Chain($llm);
$chain = new Chain($platform, $llm);
$messages = new MessageBag(
Message::forSystem('You are a pirate and you write funny.'),
Message::ofUser('What is the Symfony framework?'),
Expand Down
15 changes: 7 additions & 8 deletions examples/chat-llama-ollama.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<?php

use PhpLlm\LlmChain\Bridge\Meta\Llama;
use PhpLlm\LlmChain\Bridge\Ollama\PlatformFactory;
use PhpLlm\LlmChain\Chain;
use PhpLlm\LlmChain\Message\Message;
use PhpLlm\LlmChain\Message\MessageBag;
use PhpLlm\LlmChain\Model\Language\Llama;
use PhpLlm\LlmChain\Platform\Ollama;
use PhpLlm\LlmChain\Model\Message\Message;
use PhpLlm\LlmChain\Model\Message\MessageBag;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\HttpClient\HttpClient;

require_once dirname(__DIR__).'/vendor/autoload.php';
(new Dotenv())->loadEnv(dirname(__DIR__).'/.env');
Expand All @@ -16,10 +15,10 @@
exit(1);
}

$platform = new Ollama(HttpClient::create(), $_ENV['OLLAMA_HOST_URL']);
$llm = new Llama($platform);
$platform = PlatformFactory::create($_ENV['OLLAMA_HOST_URL']);
$llm = new Llama('llama3.2');

$chain = new Chain($llm);
$chain = new Chain($platform, $llm);
$messages = new MessageBag(
Message::forSystem('You are a helpful assistant.'),
Message::ofUser('Tina has one brother and one sister. How many sisters do Tina\'s siblings have?'),
Expand Down
15 changes: 7 additions & 8 deletions examples/chat-llama-replicate.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<?php

use PhpLlm\LlmChain\Bridge\Meta\Llama;
use PhpLlm\LlmChain\Bridge\Replicate\PlatformFactory;
use PhpLlm\LlmChain\Chain;
use PhpLlm\LlmChain\Message\Message;
use PhpLlm\LlmChain\Message\MessageBag;
use PhpLlm\LlmChain\Model\Language\Llama;
use PhpLlm\LlmChain\Platform\Replicate;
use PhpLlm\LlmChain\Model\Message\Message;
use PhpLlm\LlmChain\Model\Message\MessageBag;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\HttpClient\HttpClient;

require_once dirname(__DIR__).'/vendor/autoload.php';
(new Dotenv())->loadEnv(dirname(__DIR__).'/.env');
Expand All @@ -16,10 +15,10 @@
exit(1);
}

$platform = new Replicate(HttpClient::create(), $_ENV['REPLICATE_API_KEY']);
$llm = new Llama($platform);
$platform = PlatformFactory::create($_ENV['REPLICATE_API_KEY']);
$llm = new Llama();

$chain = new Chain($llm);
$chain = new Chain($platform, $llm);
$messages = new MessageBag(
Message::forSystem('You are a helpful assistant.'),
Message::ofUser('Tina has one brother and one sister. How many sisters do Tina\'s siblings have?'),
Expand Down
15 changes: 7 additions & 8 deletions examples/chat-o1-openai.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<?php

use PhpLlm\LlmChain\Bridge\OpenAI\GPT;
use PhpLlm\LlmChain\Bridge\OpenAI\PlatformFactory;
use PhpLlm\LlmChain\Chain;
use PhpLlm\LlmChain\Message\Message;
use PhpLlm\LlmChain\Message\MessageBag;
use PhpLlm\LlmChain\Model\Language\Gpt;
use PhpLlm\LlmChain\Platform\OpenAI\OpenAI;
use PhpLlm\LlmChain\Model\Message\Message;
use PhpLlm\LlmChain\Model\Message\MessageBag;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\HttpClient\HttpClient;

require_once dirname(__DIR__).'/vendor/autoload.php';
(new Dotenv())->loadEnv(dirname(__DIR__).'/.env');
Expand All @@ -21,8 +20,8 @@
exit(134);
}

$platform = new OpenAI(HttpClient::create(), $_ENV['OPENAI_API_KEY']);
$llm = new Gpt($platform, Gpt::O1_PREVIEW);
$platform = PlatformFactory::create($_ENV['OPENAI_API_KEY']);
$llm = new GPT(GPT::O1_PREVIEW);

$prompt = <<<PROMPT
I want to build a Symfony app in PHP 8.2 that takes user questions and looks them
Expand All @@ -33,6 +32,6 @@
at the beginning and end, not throughout the code.
PROMPT;

$response = (new Chain($llm))->call(new MessageBag(Message::ofUser($prompt)));
$response = (new Chain($platform, $llm))->call(new MessageBag(Message::ofUser($prompt)));

echo $response->getContent().PHP_EOL;
13 changes: 6 additions & 7 deletions examples/embeddings-openai.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<?php

use PhpLlm\LlmChain\Model\Embeddings\OpenAI as Embeddings;
use PhpLlm\LlmChain\Platform\OpenAI\OpenAI as Platform;
use PhpLlm\LlmChain\Bridge\OpenAI\Embeddings;
use PhpLlm\LlmChain\Bridge\OpenAI\PlatformFactory;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\HttpClient\HttpClient;

require_once dirname(__DIR__).'/vendor/autoload.php';
(new Dotenv())->loadEnv(dirname(__DIR__).'/.env');
Expand All @@ -13,13 +12,13 @@
exit(1);
}

$platform = new Platform(HttpClient::create(), $_ENV['OPENAI_API_KEY']);
$embeddings = new Embeddings($platform);
$platform = PlatformFactory::create($_ENV['OPENAI_API_KEY']);
$embeddings = new Embeddings();

$vector = $embeddings->create(<<<TEXT
$response = $platform->request($embeddings, <<<TEXT
Once upon a time, there was a country called Japan. It was a beautiful country with a lot of mountains and rivers.
The people of Japan were very kind and hardworking. They loved their country very much and took care of it. The
country was very peaceful and prosperous. The people lived happily ever after.
TEXT);

echo 'Dimensions: '.$vector->getDimensions().PHP_EOL;
echo 'Dimensions: '.$response->getContent()->getDimensions().PHP_EOL;

Check failure on line 24 in examples/embeddings-openai.php

View workflow job for this annotation

GitHub Actions / qa

Cannot call method getDimensions() on iterable|object|string.
13 changes: 6 additions & 7 deletions examples/embeddings-voyage.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<?php

use PhpLlm\LlmChain\Model\Embeddings\Voyage;
use PhpLlm\LlmChain\Platform\Voyage as Platform;
use PhpLlm\LlmChain\Bridge\Voyage\PlatformFactory;
use PhpLlm\LlmChain\Bridge\Voyage\Voyage;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\HttpClient\HttpClient;

require_once dirname(__DIR__).'/vendor/autoload.php';
(new Dotenv())->loadEnv(dirname(__DIR__).'/.env');
Expand All @@ -13,13 +12,13 @@
exit(1);
}

$platform = new Platform(HttpClient::create(), $_ENV['VOYAGE_API_KEY']);
$embeddings = new Voyage($platform);
$platform = PlatformFactory::create($_ENV['VOYAGE_API_KEY']);
$embeddings = new Voyage();

$vector = $embeddings->create(<<<TEXT
$response = $platform->request($embeddings, <<<TEXT
Once upon a time, there was a country called Japan. It was a beautiful country with a lot of mountains and rivers.
The people of Japan were very kind and hardworking. They loved their country very much and took care of it. The
country was very peaceful and prosperous. The people lived happily ever after.
TEXT);

echo 'Dimensions: '.$vector->getDimensions().PHP_EOL;
echo 'Dimensions: '.$response->getContent()->getDimensions().PHP_EOL;

Check failure on line 24 in examples/embeddings-voyage.php

View workflow job for this annotation

GitHub Actions / qa

Cannot call method getDimensions() on iterable|object|string.
17 changes: 8 additions & 9 deletions examples/image-describer-binary.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
<?php

use PhpLlm\LlmChain\Bridge\OpenAI\GPT;
use PhpLlm\LlmChain\Bridge\OpenAI\PlatformFactory;
use PhpLlm\LlmChain\Chain;
use PhpLlm\LlmChain\Message\Content\Image;
use PhpLlm\LlmChain\Message\Message;
use PhpLlm\LlmChain\Message\MessageBag;
use PhpLlm\LlmChain\Model\Language\Gpt;
use PhpLlm\LlmChain\Platform\OpenAI\OpenAI;
use PhpLlm\LlmChain\Model\Message\Content\Image;
use PhpLlm\LlmChain\Model\Message\Message;
use PhpLlm\LlmChain\Model\Message\MessageBag;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\HttpClient\HttpClient;

require_once dirname(__DIR__).'/vendor/autoload.php';
(new Dotenv())->loadEnv(dirname(__DIR__).'/.env');
Expand All @@ -17,10 +16,10 @@
exit(1);
}

$platform = new OpenAI(HttpClient::create(), $_ENV['OPENAI_API_KEY']);
$llm = new Gpt($platform, Gpt::GPT_4O_MINI);
$platform = PlatformFactory::create($_ENV['OPENAI_API_KEY']);
$llm = new GPT(GPT::GPT_4O_MINI);

$chain = new Chain($llm);
$chain = new Chain($platform, $llm);
$messages = new MessageBag(
Message::forSystem('You are an image analyzer bot that helps identify the content of images.'),
Message::ofUser(
Expand Down
17 changes: 8 additions & 9 deletions examples/image-describer-url.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
<?php

use PhpLlm\LlmChain\Bridge\OpenAI\GPT;
use PhpLlm\LlmChain\Bridge\OpenAI\PlatformFactory;
use PhpLlm\LlmChain\Chain;
use PhpLlm\LlmChain\Message\Content\Image;
use PhpLlm\LlmChain\Message\Message;
use PhpLlm\LlmChain\Message\MessageBag;
use PhpLlm\LlmChain\Model\Language\Gpt;
use PhpLlm\LlmChain\Platform\OpenAI\OpenAI;
use PhpLlm\LlmChain\Model\Message\Content\Image;
use PhpLlm\LlmChain\Model\Message\Message;
use PhpLlm\LlmChain\Model\Message\MessageBag;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\HttpClient\HttpClient;

require_once dirname(__DIR__).'/vendor/autoload.php';
(new Dotenv())->loadEnv(dirname(__DIR__).'/.env');
Expand All @@ -17,10 +16,10 @@
exit(1);
}

$platform = new OpenAI(HttpClient::create(), $_ENV['OPENAI_API_KEY']);
$llm = new Gpt($platform, Gpt::GPT_4O_MINI);
$platform = PlatformFactory::create($_ENV['OPENAI_API_KEY']);
$llm = new GPT(GPT::GPT_4O_MINI);

$chain = new Chain($llm);
$chain = new Chain($platform, $llm);
$messages = new MessageBag(
Message::forSystem('You are an image analyzer bot that helps identify the content of images.'),
Message::ofUser(
Expand Down
33 changes: 16 additions & 17 deletions examples/store-mongodb-similarity-search.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
<?php

use MongoDB\Client as MongoDBClient;
use PhpLlm\LlmChain\Bridge\MongoDB\Store;
use PhpLlm\LlmChain\Bridge\OpenAI\Embeddings;
use PhpLlm\LlmChain\Bridge\OpenAI\GPT;
use PhpLlm\LlmChain\Bridge\OpenAI\PlatformFactory;
use PhpLlm\LlmChain\Chain;
use PhpLlm\LlmChain\Chain\ToolBox\ChainProcessor;
use PhpLlm\LlmChain\Chain\ToolBox\Tool\SimilaritySearch;
use PhpLlm\LlmChain\Chain\ToolBox\ToolAnalyzer;
use PhpLlm\LlmChain\Chain\ToolBox\ToolBox;
use PhpLlm\LlmChain\Document\Metadata;
use PhpLlm\LlmChain\Document\TextDocument;
use PhpLlm\LlmChain\DocumentEmbedder;
use PhpLlm\LlmChain\Message\Message;
use PhpLlm\LlmChain\Message\MessageBag;
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\MongoDB\Store;
use PhpLlm\LlmChain\ToolBox\ChainProcessor;
use PhpLlm\LlmChain\ToolBox\Tool\SimilaritySearch;
use PhpLlm\LlmChain\ToolBox\ToolAnalyzer;
use PhpLlm\LlmChain\ToolBox\ToolBox;
use PhpLlm\LlmChain\Embedder;
use PhpLlm\LlmChain\Model\Message\Message;
use PhpLlm\LlmChain\Model\Message\MessageBag;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\Uid\Uuid;

require_once dirname(__DIR__).'/vendor/autoload.php';
Expand Down Expand Up @@ -53,19 +52,19 @@
}

// create embeddings for documents
$platform = new Platform(HttpClient::create(), $_ENV['OPENAI_API_KEY']);
$embedder = new DocumentEmbedder($embeddings = new Embeddings($platform), $store);
$platform = PlatformFactory::create($_ENV['OPENAI_API_KEY']);
$embedder = new Embedder($platform, $embeddings = new Embeddings(), $store);
$embedder->embed($documents);

// initialize the index
$store->initialize();

$llm = new Gpt($platform, Gpt::GPT_4O_MINI);
$llm = new GPT(GPT::GPT_4O_MINI);

$similaritySearch = new SimilaritySearch($embeddings, $store);
$similaritySearch = new SimilaritySearch($platform, $embeddings, $store);
$toolBox = new ToolBox(new ToolAnalyzer(), [$similaritySearch]);
$processor = new ChainProcessor($toolBox);
$chain = new Chain($llm, [$processor], [$processor]);
$chain = new Chain($platform, $llm, [$processor], [$processor]);

$messages = new MessageBag(
Message::forSystem('Please answer all user questions only using SimilaritySearch function.'),
Expand Down
Loading

0 comments on commit 84f0546

Please sign in to comment.