@@ -45,9 +45,9 @@ Those models are provided by different **platforms**, like OpenAI, Azure, Google
45
45
#### Example Instantiation
46
46
47
47
``` php
48
- use PhpLlm\LlmChain\Bridge\OpenAI\Embeddings;
49
- use PhpLlm\LlmChain\Bridge\OpenAI\GPT;
50
- use PhpLlm\LlmChain\Bridge\OpenAI\PlatformFactory;
48
+ use PhpLlm\LlmChain\Platform\ Bridge\OpenAI\Embeddings;
49
+ use PhpLlm\LlmChain\Platform\ Bridge\OpenAI\GPT;
50
+ use PhpLlm\LlmChain\Platform\ Bridge\OpenAI\PlatformFactory;
51
51
52
52
// Platform: OpenAI
53
53
$platform = PlatformFactory::create($_ENV['OPENAI_API_KEY']);
@@ -90,13 +90,13 @@ have different content types, like `Text`, `Image` or `Audio`.
90
90
#### Example Chain call with messages
91
91
92
92
``` php
93
- use PhpLlm\LlmChain\Chain;
94
- use PhpLlm\LlmChain\Model \Message\Message;
95
- use PhpLlm\LlmChain\Model \Message\MessageBag;
93
+ use PhpLlm\LlmChain\Chain\Chain ;
94
+ use PhpLlm\LlmChain\Platform \Message\Message;
95
+ use PhpLlm\LlmChain\Platform \Message\MessageBag;
96
96
97
97
// Platform & LLM instantiation
98
98
99
- $chain = new Chain($platform, $llm );
99
+ $chain = new Chain($platform, $model );
100
100
$messages = new MessageBag(
101
101
Message::forSystem('You are a helpful chatbot answering questions about LLM Chain.'),
102
102
Message::ofUser('Hello, how are you?'),
@@ -146,6 +146,7 @@ Tools are services that can be called by the LLM to provide additional features
146
146
Tool calling can be enabled by registering the processors in the chain:
147
147
148
148
``` php
149
+ use PhpLlm\LlmChain\Chain\Chain;
149
150
use PhpLlm\LlmChain\Chain\Toolbox\ChainProcessor;
150
151
use PhpLlm\LlmChain\Chain\Toolbox\Toolbox;
151
152
@@ -156,7 +157,7 @@ $yourTool = new YourTool();
156
157
$toolbox = Toolbox::create($yourTool);
157
158
$toolProcessor = new ChainProcessor($toolbox);
158
159
159
- $chain = new Chain($platform, $llm , inputProcessor: [$toolProcessor], outputProcessor: [$toolProcessor]);
160
+ $chain = new Chain($platform, $model , inputProcessor: [$toolProcessor], outputProcessor: [$toolProcessor]);
160
161
```
161
162
162
163
Custom tools can basically be any class, but must configure by the ` #[AsTool] ` attribute.
@@ -219,8 +220,8 @@ partially support by LLMs like GPT.
219
220
To leverage this, configure the ` #[With] ` attribute on the method arguments of your tool:
220
221
221
222
``` php
222
- use PhpLlm\LlmChain\Chain\JsonSchema\Attribute\With;
223
223
use PhpLlm\LlmChain\Chain\Toolbox\Attribute\AsTool;
224
+ use PhpLlm\LlmChain\Platform\Contract\JsonSchema\Attribute\With;
224
225
225
226
#[AsTool('my_tool', 'Example tool with parameters requirements.')]
226
227
final class MyTool
@@ -252,10 +253,10 @@ attribute to the class is not possible in those cases, but you can explicitly re
252
253
253
254
``` php
254
255
use PhpLlm\LlmChain\Chain\Toolbox\Toolbox;
255
- use PhpLlm\LlmChain\Chain\Toolbox\MetadataFactory\MemoryFactory ;
256
+ use PhpLlm\LlmChain\Chain\Toolbox\ToolFactory\MemoryToolFactory ;
256
257
use Symfony\Component\Clock\Clock;
257
258
258
- $metadataFactory = (new MemoryFactory ())
259
+ $metadataFactory = (new MemoryToolFactory ())
259
260
->addTool(Clock::class, 'clock', 'Get the current date and time', 'now');
260
261
$toolbox = new Toolbox($metadataFactory, [new Clock()]);
261
262
```
@@ -268,12 +269,12 @@ tools in the same chain - which even enables you to overwrite the pre-existing c
268
269
269
270
``` php
270
271
use PhpLlm\LlmChain\Chain\Toolbox\Toolbox;
271
- use PhpLlm\LlmChain\Chain\Toolbox\MetadataFactory \ChainFactory;
272
- use PhpLlm\LlmChain\Chain\Toolbox\MetadataFactory\MemoryFactory ;
273
- use PhpLlm\LlmChain\Chain\Toolbox\MetadataFactory\ReflectionFactory ;
272
+ use PhpLlm\LlmChain\Chain\Toolbox\ToolFactory \ChainFactory;
273
+ use PhpLlm\LlmChain\Chain\Toolbox\ToolFactory\MemoryToolFactory ;
274
+ use PhpLlm\LlmChain\Chain\Toolbox\ToolFactory\ReflectionToolFactory ;
274
275
275
- $reflectionFactory = new ReflectionFactory (); // Register tools with #[AsTool] attribute
276
- $metadataFactory = (new MemoryFactory ()) // Register or overwrite tools explicitly
276
+ $reflectionFactory = new ReflectionToolFactory (); // Register tools with #[AsTool] attribute
277
+ $metadataFactory = (new MemoryToolFactory ()) // Register or overwrite tools explicitly
277
278
->addTool(...);
278
279
$toolbox = new Toolbox(new ChainFactory($metadataFactory, $reflectionFactory), [...]);
279
280
```
@@ -287,14 +288,14 @@ Similar to third-party tools, you can also use a chain as a tool in another chai
287
288
complex logic or to reuse a chain in multiple places or hide sub-chains from the LLM.
288
289
289
290
``` php
290
- use PhpLlm\LlmChain\Chain\Toolbox\MetadataFactory\MemoryFactory ;
291
+ use PhpLlm\LlmChain\Chain\Toolbox\ToolFactory\MemoryToolFactory ;
291
292
use PhpLlm\LlmChain\Chain\Toolbox\Toolbox;
292
293
use PhpLlm\LlmChain\Chain\Toolbox\Tool\Chain;
293
294
294
295
// Chain was initialized before
295
296
296
297
$chainTool = new Chain($chain);
297
- $metadataFactory = (new MemoryFactory ())
298
+ $metadataFactory = (new MemoryToolFactory ())
298
299
->addTool($chainTool, 'research_agent', 'Meaningful description for sub-chain');
299
300
$toolbox = new Toolbox($metadataFactory, [$chainTool]);
300
301
```
@@ -306,6 +307,7 @@ To gracefully handle errors that occur during tool calling, e.g. wrong tool name
306
307
to the LLM.
307
308
308
309
``` php
310
+ use PhpLlm\LlmChain\Chain\Chain;
309
311
use PhpLlm\LlmChain\Chain\Toolbox\ChainProcessor;
310
312
use PhpLlm\LlmChain\Chain\Toolbox\FaultTolerantToolbox;
311
313
@@ -314,7 +316,7 @@ use PhpLlm\LlmChain\Chain\Toolbox\FaultTolerantToolbox;
314
316
$toolbox = new FaultTolerantToolbox($innerToolbox);
315
317
$toolProcessor = new ChainProcessor($toolbox);
316
318
317
- $chain = new Chain($platform, $llm , inputProcessor: [$toolProcessor], outputProcessor: [$toolProcessor]);
319
+ $chain = new Chain($platform, $model , inputProcessor: [$toolProcessor], outputProcessor: [$toolProcessor]);
318
320
```
319
321
320
322
#### Tool Filtering
@@ -362,12 +364,11 @@ For populating a vector store, LLM Chain provides the service `Embedder`, which
362
364
` EmbeddingsModel ` and one of ` StoreInterface ` , and works with a collection of ` Document ` objects as input:
363
365
364
366
``` php
365
- use PhpLlm\LlmChain\Embedder ;
366
- use PhpLlm\LlmChain\Bridge\OpenAI\Embeddings ;
367
- use PhpLlm\LlmChain\Bridge\OpenAI\PlatformFactory ;
368
- use PhpLlm\LlmChain\Bridge\Pinecone\ Store;
367
+ use PhpLlm\LlmChain\Platform\Bridge\OpenAI\Embeddings ;
368
+ use PhpLlm\LlmChain\Platform\ Bridge\OpenAI\PlatformFactory ;
369
+ use PhpLlm\LlmChain\Store\ Bridge\Pinecone\Store ;
370
+ use PhpLlm\LlmChain\Store\Embedder ;
369
371
use Probots\Pinecone\Pinecone;
370
- use Symfony\Component\HttpClient\HttpClient;
371
372
372
373
$embedder = new Embedder(
373
374
PlatformFactory::create($_ENV['OPENAI_API_KEY']),
@@ -380,8 +381,8 @@ $embedder->embed($documents);
380
381
The collection of ` Document ` instances is usually created by text input of your domain entities:
381
382
382
383
``` php
383
- use PhpLlm\LlmChain\Document\Metadata;
384
- use PhpLlm\LlmChain\Document\TextDocument;
384
+ use PhpLlm\LlmChain\Store\ Document\Metadata;
385
+ use PhpLlm\LlmChain\Store\ Document\TextDocument;
385
386
386
387
foreach ($entities as $entity) {
387
388
$documents[] = new TextDocument(
@@ -399,19 +400,19 @@ In the end the chain is used in combination with a retrieval tool on top of the
399
400
` SimilaritySearch ` tool provided by the library:
400
401
401
402
``` php
402
- use PhpLlm\LlmChain\Chain;
403
- use PhpLlm\LlmChain\Model\Message\Message;
404
- use PhpLlm\LlmChain\Model\Message\MessageBag;
403
+ use PhpLlm\LlmChain\Chain\Chain;
405
404
use PhpLlm\LlmChain\Chain\Toolbox\ChainProcessor;
406
405
use PhpLlm\LlmChain\Chain\Toolbox\Tool\SimilaritySearch;
407
406
use PhpLlm\LlmChain\Chain\Toolbox\Toolbox;
407
+ use PhpLlm\LlmChain\Platform\Message\Message;
408
+ use PhpLlm\LlmChain\Platform\Message\MessageBag;
408
409
409
410
// Initialize Platform & Models
410
411
411
412
$similaritySearch = new SimilaritySearch($embeddings, $store);
412
413
$toolbox = Toolbox::create($similaritySearch);
413
- $processor = new ChainProcessor ($toolbox);
414
- $chain = new Chain($platform, $llm , [$processor], [$processor]);
414
+ $processor = new Chain ($toolbox);
415
+ $chain = new Chain($platform, $model , [$processor], [$processor]);
415
416
416
417
$messages = new MessageBag(
417
418
Message::forSystem(<<<PROMPT
@@ -425,8 +426,8 @@ $response = $chain->call($messages);
425
426
426
427
#### Code Examples
427
428
428
- 1. [MongoDB Store](examples/store- mongodb-similarity-search.php)
429
- 1. [Pinecone Store](examples/store- pinecone-similarity-search.php)
429
+ 1. [MongoDB Store](examples/store/ mongodb-similarity-search.php)
430
+ 1. [Pinecone Store](examples/store/ pinecone-similarity-search.php)
430
431
431
432
#### Supported Stores
432
433
@@ -450,12 +451,13 @@ LLM Chain supports that use-case by abstracting the hustle of defining and provi
450
451
the response back to PHP objects.
451
452
452
453
To achieve this, a specific chain processor needs to be registered:
454
+
453
455
```php
454
- use PhpLlm\LlmChain\Chain;
455
- use PhpLlm\LlmChain\Model\Message\Message;
456
- use PhpLlm\LlmChain\Model\Message\MessageBag;
456
+ use PhpLlm\LlmChain\Chain\Chain;
457
457
use PhpLlm\LlmChain\Chain\StructuredOutput\ChainProcessor;
458
458
use PhpLlm\LlmChain\Chain\StructuredOutput\ResponseFormatFactory;
459
+ use PhpLlm\LlmChain\Platform\Message\Message;
460
+ use PhpLlm\LlmChain\Platform\Message\MessageBag;
459
461
use PhpLlm\LlmChain\Tests\Chain\StructuredOutput\Data\MathReasoning;
460
462
use Symfony\Component\Serializer\Encoder\JsonEncoder;
461
463
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
@@ -465,7 +467,7 @@ use Symfony\Component\Serializer\Serializer;
465
467
466
468
$serializer = new Serializer([new ObjectNormalizer()], [new JsonEncoder()]);
467
469
$processor = new ChainProcessor(new ResponseFormatFactory(), $serializer);
468
- $chain = new Chain($platform, $llm , [$processor], [$processor]);
470
+ $chain = new Chain($platform, $model , [$processor], [$processor]);
469
471
470
472
$messages = new MessageBag(
471
473
Message::forSystem(' You are a helpful math tutor. Guide the user through the solution step by step. ' ),
@@ -481,8 +483,8 @@ dump($response->getContent()); // returns an instance of `MathReasoning` class
481
483
Also PHP array structures as `response_format` are supported, which also requires the chain processor mentioned above:
482
484
483
485
```php
484
- use PhpLlm\LlmChain\Model \Message\Message;
485
- use PhpLlm\LlmChain\Model \Message\MessageBag;
486
+ use PhpLlm\LlmChain\Platform \Message\Message;
487
+ use PhpLlm\LlmChain\Platform \Message\MessageBag;
486
488
487
489
// Initialize Platform, LLM and Chain with processors and Clock tool
488
490
@@ -518,13 +520,13 @@ Since LLMs usually generate a response word by word, most of them also support s
518
520
Events. LLM Chain supports that by abstracting the conversion and returning a Generator as content of the response.
519
521
520
522
```php
521
- use PhpLlm\LlmChain\Chain;
523
+ use PhpLlm\LlmChain\Chain\Chain ;
522
524
use PhpLlm\LlmChain\Message\Message;
523
525
use PhpLlm\LlmChain\Message\MessageBag;
524
526
525
527
// Initialize Platform and LLM
526
528
527
- $chain = new Chain($llm );
529
+ $chain = new Chain($model );
528
530
$messages = new MessageBag(
529
531
Message::forSystem(' You are a thoughtful philosopher. ' ),
530
532
Message::ofUser(' What is the purpose of an ant? ' ),
@@ -551,9 +553,9 @@ needs to be used.
551
553
Some LLMs also support images as input, which LLM Chain supports as `Content` type within the `UserMessage`:
552
554
553
555
```php
554
- use PhpLlm\LlmChain\Model \Message\Content\Image;
555
- use PhpLlm\LlmChain\Model \Message\Message;
556
- use PhpLlm\LlmChain\Model \Message\MessageBag;
556
+ use PhpLlm\LlmChain\Platform \Message\Content\Image;
557
+ use PhpLlm\LlmChain\Platform \Message\Message;
558
+ use PhpLlm\LlmChain\Platform \Message\MessageBag;
557
559
558
560
// Initialize Platform, LLM & Chain
559
561
@@ -579,9 +581,9 @@ $response = $chain->call($messages);
579
581
Similar to images, some LLMs also support audio as input, which is just another `Content` type within the `UserMessage`:
580
582
581
583
```php
582
- use PhpLlm\LlmChain\Model \Message\Content\Audio;
583
- use PhpLlm\LlmChain\Model \Message\Message;
584
- use PhpLlm\LlmChain\Model \Message\MessageBag;
584
+ use PhpLlm\LlmChain\Platform \Message\Content\Audio;
585
+ use PhpLlm\LlmChain\Platform \Message\Message;
586
+ use PhpLlm\LlmChain\Platform \Message\MessageBag;
585
587
586
588
// Initialize Platform, LLM & Chain
587
589
@@ -606,7 +608,7 @@ therefore LLM Chain implements a `EmbeddingsModel` interface with various models
606
608
The standalone usage results in an `Vector` instance:
607
609
608
610
```php
609
- use PhpLlm\LlmChain\Bridge\OpenAI\Embeddings;
611
+ use PhpLlm\LlmChain\Platform\ Bridge\OpenAI\Embeddings;
610
612
611
613
// Initialize Platform
612
614
@@ -655,11 +657,11 @@ The behavior of the Chain is extendable with services that implement `InputProce
655
657
interface. They are provided while instantiating the Chain instance:
656
658
657
659
```php
658
- use PhpLlm\LlmChain\Chain;
660
+ use PhpLlm\LlmChain\Chain\Chain ;
659
661
660
662
// Initialize Platform, LLM and processors
661
663
662
- $chain = new Chain($platform, $llm , $inputProcessors, $outputProcessors);
664
+ $chain = new Chain($platform, $model , $inputProcessors, $outputProcessors);
663
665
```
664
666
665
667
#### InputProcessor
@@ -669,10 +671,10 @@ able to mutate both on top of the `Input` instance provided.
669
671
670
672
```php
671
673
use PhpLlm\LlmChain\Chain\Input;
672
- use PhpLlm\LlmChain\Chain\InputProcessor ;
673
- use PhpLlm\LlmChain\Model \Message\AssistantMessage
674
+ use PhpLlm\LlmChain\Chain\InputProcessorInterface ;
675
+ use PhpLlm\LlmChain\Platform \Message\AssistantMessage;
674
676
675
- final class MyProcessor implements InputProcessor
677
+ final class MyProcessor implements InputProcessorInterface
676
678
{
677
679
public function processInput(Input $input): void
678
680
{
@@ -694,10 +696,9 @@ mutate or replace the given response:
694
696
695
697
```php
696
698
use PhpLlm\LlmChain\Chain\Output;
697
- use PhpLlm\LlmChain\Chain\OutputProcessor;
698
- use PhpLlm\LlmChain\Model\Message\AssistantMessage
699
+ use PhpLlm\LlmChain\Chain\OutputProcessorInterface;
699
700
700
- final class MyProcessor implements OutputProcessor
701
+ final class MyProcessor implements OutputProcessorInterface
701
702
{
702
703
public function processOutput(Output $out): void
703
704
{
@@ -716,13 +717,12 @@ provided, in case the processor implemented the `ChainAwareProcessor` interface,
716
717
`ChainAwareTrait`:
717
718
718
719
```php
719
- use PhpLlm\LlmChain\Chain\ChainAwareProcessor ;
720
+ use PhpLlm\LlmChain\Chain\ChainAwareInterface ;
720
721
use PhpLlm\LlmChain\Chain\ChainAwareTrait;
721
722
use PhpLlm\LlmChain\Chain\Output;
722
- use PhpLlm\LlmChain\Chain\OutputProcessor;
723
- use PhpLlm\LlmChain\Model\Message\AssistantMessage
723
+ use PhpLlm\LlmChain\Chain\OutputProcessorInterface;
724
724
725
- final class MyProcessor implements OutputProcessor, ChainAwareProcessor
725
+ final class MyProcessor implements OutputProcessorInterface, ChainAwareInterface
726
726
{
727
727
use ChainAwareTrait;
728
728
@@ -740,11 +740,12 @@ LLM Chain comes out of the box with an integration for [HuggingFace](https://hug
740
740
hosting and sharing all kinds of models, including LLMs, embeddings, image generation, and classification models.
741
741
742
742
You can just instantiate the Platform with the corresponding HuggingFace bridge and use it with the `task` option:
743
+
743
744
```php
744
745
use PhpLlm\LlmChain\Bridge\HuggingFace\Model;
745
- use PhpLlm\LlmChain\Bridge\HuggingFace\PlatformFactory;
746
- use PhpLlm\LlmChain\Bridge\HuggingFace\Task;
747
- use PhpLlm\LlmChain\Model \Message\Content\Image;
746
+ use PhpLlm\LlmChain\Platform\ Bridge\HuggingFace\PlatformFactory;
747
+ use PhpLlm\LlmChain\Platform\ Bridge\HuggingFace\Task;
748
+ use PhpLlm\LlmChain\Platform \Message\Content\Image;
748
749
749
750
$platform = PlatformFactory::create($apiKey);
750
751
$model = new Model(' facebook /detr-resnet-50 ' );
@@ -790,7 +791,7 @@ The usage with LLM Chain is similar to the HuggingFace integration, and also req
790
791
```php
791
792
use Codewithkyrian\Transformers\Pipelines\Task;
792
793
use PhpLlm\LlmChain\Bridge\TransformersPHP\Model;
793
- use PhpLlm\LlmChain\Bridge\TransformersPHP\PlatformFactory;
794
+ use PhpLlm\LlmChain\Platform\ Bridge\TransformersPHP\PlatformFactory;
794
795
795
796
$platform = PlatformFactory::create();
796
797
$model = new Model( ' Xenova/LaMini-Flan-T5-783M' );
0 commit comments