@@ -45,9 +45,7 @@ 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;use PhpLlm\LlmChain\Platform\Bridge\OpenAI\GPT;use PhpLlm\LlmChain\Platform\Bridge\OpenAI\PlatformFactory;
51
49
52
50
// Platform: OpenAI
53
51
$platform = PlatformFactory::create($_ENV['OPENAI_API_KEY']);
@@ -90,13 +88,11 @@ have different content types, like `Text`, `Image` or `Audio`.
90
88
#### Example Chain call with messages
91
89
92
90
``` php
93
- use PhpLlm\LlmChain\Chain;
94
- use PhpLlm\LlmChain\Model\Message\Message;
95
- use PhpLlm\LlmChain\Model\Message\MessageBag;
91
+ use PhpLlm\LlmChain\Chain\Chain;use PhpLlm\LlmChain\Platform\Message\Message;use PhpLlm\LlmChain\Platform\Message\MessageBag;
96
92
97
93
// Platform & LLM instantiation
98
94
99
- $chain = new Chain($platform, $llm );
95
+ $chain = new Chain($platform, $model );
100
96
$messages = new MessageBag(
101
97
Message::forSystem('You are a helpful chatbot answering questions about LLM Chain.'),
102
98
Message::ofUser('Hello, how are you?'),
@@ -156,7 +152,7 @@ $yourTool = new YourTool();
156
152
$toolbox = Toolbox::create($yourTool);
157
153
$toolProcessor = new ChainProcessor($toolbox);
158
154
159
- $chain = new Chain($platform, $llm , inputProcessor: [$toolProcessor], outputProcessor: [$toolProcessor]);
155
+ $chain = new Chain($platform, $model , inputProcessor: [$toolProcessor], outputProcessor: [$toolProcessor]);
160
156
```
161
157
162
158
Custom tools can basically be any class, but must configure by the ` #[AsTool] ` attribute.
@@ -219,8 +215,7 @@ partially support by LLMs like GPT.
219
215
To leverage this, configure the ` #[With] ` attribute on the method arguments of your tool:
220
216
221
217
``` php
222
- use PhpLlm\LlmChain\Chain\JsonSchema\Attribute\With;
223
- use PhpLlm\LlmChain\Chain\Toolbox\Attribute\AsTool;
218
+ use PhpLlm\LlmChain\Chain\Toolbox\Attribute\AsTool;use PhpLlm\LlmChain\Platform\Contract\JsonSchema\Attribute\With;
224
219
225
220
#[AsTool('my_tool', 'Example tool with parameters requirements.')]
226
221
final class MyTool
@@ -252,10 +247,10 @@ attribute to the class is not possible in those cases, but you can explicitly re
252
247
253
248
``` php
254
249
use PhpLlm\LlmChain\Chain\Toolbox\Toolbox;
255
- use PhpLlm\LlmChain\Chain\Toolbox\MetadataFactory\MemoryFactory ;
250
+ use PhpLlm\LlmChain\Chain\Toolbox\ToolFactory\MemoryToolFactory ;
256
251
use Symfony\Component\Clock\Clock;
257
252
258
- $metadataFactory = (new MemoryFactory ())
253
+ $metadataFactory = (new MemoryToolFactory ())
259
254
->addTool(Clock::class, 'clock', 'Get the current date and time', 'now');
260
255
$toolbox = new Toolbox($metadataFactory, [new Clock()]);
261
256
```
@@ -268,12 +263,12 @@ tools in the same chain - which even enables you to overwrite the pre-existing c
268
263
269
264
``` php
270
265
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 ;
266
+ use PhpLlm\LlmChain\Chain\Toolbox\ToolFactory \ChainFactory;
267
+ use PhpLlm\LlmChain\Chain\Toolbox\ToolFactory\MemoryToolFactory ;
268
+ use PhpLlm\LlmChain\Chain\Toolbox\ToolFactory\ReflectionToolFactory ;
274
269
275
- $reflectionFactory = new ReflectionFactory (); // Register tools with #[AsTool] attribute
276
- $metadataFactory = (new MemoryFactory ()) // Register or overwrite tools explicitly
270
+ $reflectionFactory = new ReflectionToolFactory (); // Register tools with #[AsTool] attribute
271
+ $metadataFactory = (new MemoryToolFactory ()) // Register or overwrite tools explicitly
277
272
->addTool(...);
278
273
$toolbox = new Toolbox(new ChainFactory($metadataFactory, $reflectionFactory), [...]);
279
274
```
@@ -287,14 +282,14 @@ Similar to third-party tools, you can also use a chain as a tool in another chai
287
282
complex logic or to reuse a chain in multiple places or hide sub-chains from the LLM.
288
283
289
284
``` php
290
- use PhpLlm\LlmChain\Chain\Toolbox\MetadataFactory\MemoryFactory ;
285
+ use PhpLlm\LlmChain\Chain\Toolbox\ToolFactory\MemoryToolFactory ;
291
286
use PhpLlm\LlmChain\Chain\Toolbox\Toolbox;
292
287
use PhpLlm\LlmChain\Chain\Toolbox\Tool\Chain;
293
288
294
289
// Chain was initialized before
295
290
296
291
$chainTool = new Chain($chain);
297
- $metadataFactory = (new MemoryFactory ())
292
+ $metadataFactory = (new MemoryToolFactory ())
298
293
->addTool($chainTool, 'research_agent', 'Meaningful description for sub-chain');
299
294
$toolbox = new Toolbox($metadataFactory, [$chainTool]);
300
295
```
@@ -314,7 +309,7 @@ use PhpLlm\LlmChain\Chain\Toolbox\FaultTolerantToolbox;
314
309
$toolbox = new FaultTolerantToolbox($innerToolbox);
315
310
$toolProcessor = new ChainProcessor($toolbox);
316
311
317
- $chain = new Chain($platform, $llm , inputProcessor: [$toolProcessor], outputProcessor: [$toolProcessor]);
312
+ $chain = new Chain($platform, $model , inputProcessor: [$toolProcessor], outputProcessor: [$toolProcessor]);
318
313
```
319
314
320
315
#### Tool Filtering
@@ -362,12 +357,7 @@ For populating a vector store, LLM Chain provides the service `Embedder`, which
362
357
` EmbeddingsModel ` and one of ` StoreInterface ` , and works with a collection of ` Document ` objects as input:
363
358
364
359
``` 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;
369
- use Probots\Pinecone\Pinecone;
370
- use Symfony\Component\HttpClient\HttpClient;
360
+ use PhpLlm\LlmChain\Platform\Bridge\OpenAI\Embeddings;use PhpLlm\LlmChain\Platform\Bridge\OpenAI\PlatformFactory;use PhpLlm\LlmChain\Store\Bridge\Pinecone\Store;use PhpLlm\LlmChain\Store\Embedder;use Probots\Pinecone\Pinecone;
371
361
372
362
$embedder = new Embedder(
373
363
PlatformFactory::create($_ENV['OPENAI_API_KEY']),
@@ -380,8 +370,7 @@ $embedder->embed($documents);
380
370
The collection of ` Document ` instances is usually created by text input of your domain entities:
381
371
382
372
``` php
383
- use PhpLlm\LlmChain\Document\Metadata;
384
- use PhpLlm\LlmChain\Document\TextDocument;
373
+ use PhpLlm\LlmChain\Store\Document\Metadata;use PhpLlm\LlmChain\Store\Document\TextDocument;
385
374
386
375
foreach ($entities as $entity) {
387
376
$documents[] = new TextDocument(
@@ -399,19 +388,14 @@ In the end the chain is used in combination with a retrieval tool on top of the
399
388
` SimilaritySearch ` tool provided by the library:
400
389
401
390
``` php
402
- use PhpLlm\LlmChain\Chain;
403
- use PhpLlm\LlmChain\Model\Message\Message;
404
- use PhpLlm\LlmChain\Model\Message\MessageBag;
405
- use PhpLlm\LlmChain\Chain\Toolbox\ChainProcessor;
406
- use PhpLlm\LlmChain\Chain\Toolbox\Tool\SimilaritySearch;
407
- use PhpLlm\LlmChain\Chain\Toolbox\Toolbox;
391
+ use PhpLlm\LlmChain\Chain\Chain;use PhpLlm\LlmChain\Chain\Toolbox\ChainProcessor;use PhpLlm\LlmChain\Chain\Toolbox\Tool\SimilaritySearch;use PhpLlm\LlmChain\Chain\Toolbox\Toolbox;use PhpLlm\LlmChain\Platform\Message\Message;use PhpLlm\LlmChain\Platform\Message\MessageBag;
408
392
409
393
// Initialize Platform & Models
410
394
411
395
$similaritySearch = new SimilaritySearch($embeddings, $store);
412
396
$toolbox = Toolbox::create($similaritySearch);
413
397
$processor = new ChainProcessor($toolbox);
414
- $chain = new Chain($platform, $llm , [$processor], [$processor]);
398
+ $chain = new Chain($platform, $model , [$processor], [$processor]);
415
399
416
400
$messages = new MessageBag(
417
401
Message::forSystem(<<<PROMPT
@@ -450,22 +434,15 @@ LLM Chain supports that use-case by abstracting the hustle of defining and provi
450
434
the response back to PHP objects.
451
435
452
436
To achieve this, a specific chain processor needs to be registered:
437
+
453
438
```php
454
- use PhpLlm\LlmChain\Chain;
455
- use PhpLlm\LlmChain\Model\Message\Message;
456
- use PhpLlm\LlmChain\Model\Message\MessageBag;
457
- use PhpLlm\LlmChain\Chain\StructuredOutput\ChainProcessor;
458
- use PhpLlm\LlmChain\Chain\StructuredOutput\ResponseFormatFactory;
459
- use PhpLlm\LlmChain\Tests\Chain\StructuredOutput\Data\MathReasoning;
460
- use Symfony\Component\Serializer\Encoder\JsonEncoder;
461
- use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
462
- use Symfony\Component\Serializer\Serializer;
439
+ use PhpLlm\LlmChain\Chain\Chain;use PhpLlm\LlmChain\Chain\StructuredOutput\ChainProcessor;use PhpLlm\LlmChain\Chain\StructuredOutput\ResponseFormatFactory;use PhpLlm\LlmChain\Platform\Message\Message;use PhpLlm\LlmChain\Platform\Message\MessageBag;use PhpLlm\LlmChain\Tests\Chain\StructuredOutput\Data\MathReasoning;use Symfony\Component\Serializer\Encoder\JsonEncoder;use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;use Symfony\Component\Serializer\Serializer;
463
440
464
441
// Initialize Platform and LLM
465
442
466
443
$serializer = new Serializer([new ObjectNormalizer()], [new JsonEncoder()]);
467
444
$processor = new ChainProcessor(new ResponseFormatFactory(), $serializer);
468
- $chain = new Chain($platform, $llm , [$processor], [$processor]);
445
+ $chain = new Chain($platform, $model , [$processor], [$processor]);
469
446
470
447
$messages = new MessageBag(
471
448
Message::forSystem(' You are a helpful math tutor. Guide the user through the solution step by step. ' ),
@@ -481,8 +458,7 @@ dump($response->getContent()); // returns an instance of `MathReasoning` class
481
458
Also PHP array structures as `response_format` are supported, which also requires the chain processor mentioned above:
482
459
483
460
```php
484
- use PhpLlm\LlmChain\Model\Message\Message;
485
- use PhpLlm\LlmChain\Model\Message\MessageBag;
461
+ use PhpLlm\LlmChain\Platform\Message\Message;use PhpLlm\LlmChain\Platform\Message\MessageBag;
486
462
487
463
// Initialize Platform, LLM and Chain with processors and Clock tool
488
464
@@ -518,13 +494,11 @@ Since LLMs usually generate a response word by word, most of them also support s
518
494
Events. LLM Chain supports that by abstracting the conversion and returning a Generator as content of the response.
519
495
520
496
```php
521
- use PhpLlm\LlmChain\Chain;
522
- use PhpLlm\LlmChain\Message\Message;
523
- use PhpLlm\LlmChain\Message\MessageBag;
497
+ use PhpLlm\LlmChain\Chain\Chain;use PhpLlm\LlmChain\Message\Message;use PhpLlm\LlmChain\Message\MessageBag;
524
498
525
499
// Initialize Platform and LLM
526
500
527
- $chain = new Chain($llm );
501
+ $chain = new Chain($model );
528
502
$messages = new MessageBag(
529
503
Message::forSystem(' You are a thoughtful philosopher. ' ),
530
504
Message::ofUser(' What is the purpose of an ant? ' ),
@@ -551,9 +525,7 @@ needs to be used.
551
525
Some LLMs also support images as input, which LLM Chain supports as `Content` type within the `UserMessage`:
552
526
553
527
```php
554
- use PhpLlm\LlmChain\Model\Message\Content\Image;
555
- use PhpLlm\LlmChain\Model\Message\Message;
556
- use PhpLlm\LlmChain\Model\Message\MessageBag;
528
+ use PhpLlm\LlmChain\Platform\Message\Content\Image;use PhpLlm\LlmChain\Platform\Message\Message;use PhpLlm\LlmChain\Platform\Message\MessageBag;
557
529
558
530
// Initialize Platform, LLM & Chain
559
531
@@ -579,9 +551,7 @@ $response = $chain->call($messages);
579
551
Similar to images, some LLMs also support audio as input, which is just another `Content` type within the `UserMessage`:
580
552
581
553
```php
582
- use PhpLlm\LlmChain\Model\Message\Content\Audio;
583
- use PhpLlm\LlmChain\Model\Message\Message;
584
- use PhpLlm\LlmChain\Model\Message\MessageBag;
554
+ use PhpLlm\LlmChain\Platform\Message\Content\Audio;use PhpLlm\LlmChain\Platform\Message\Message;use PhpLlm\LlmChain\Platform\Message\MessageBag;
585
555
586
556
// Initialize Platform, LLM & Chain
587
557
@@ -606,7 +576,7 @@ therefore LLM Chain implements a `EmbeddingsModel` interface with various models
606
576
The standalone usage results in an `Vector` instance:
607
577
608
578
```php
609
- use PhpLlm\LlmChain\Bridge\OpenAI\Embeddings;
579
+ use PhpLlm\LlmChain\Platform\ Bridge\OpenAI\Embeddings;
610
580
611
581
// Initialize Platform
612
582
@@ -655,11 +625,11 @@ The behavior of the Chain is extendable with services that implement `InputProce
655
625
interface. They are provided while instantiating the Chain instance:
656
626
657
627
```php
658
- use PhpLlm\LlmChain\Chain;
628
+ use PhpLlm\LlmChain\Chain\Chain ;
659
629
660
630
// Initialize Platform, LLM and processors
661
631
662
- $chain = new Chain($platform, $llm , $inputProcessors, $outputProcessors);
632
+ $chain = new Chain($platform, $model , $inputProcessors, $outputProcessors);
663
633
```
664
634
665
635
#### InputProcessor
@@ -668,11 +638,9 @@ $chain = new Chain($platform, $llm, $inputProcessors, $outputProcessors);
668
638
able to mutate both on top of the `Input` instance provided.
669
639
670
640
```php
671
- use PhpLlm\LlmChain\Chain\Input;
672
- use PhpLlm\LlmChain\Chain\InputProcessor;
673
- use PhpLlm\LlmChain\Model\Message\AssistantMessage
641
+ use PhpLlm\LlmChain\Chain\Input;use PhpLlm\LlmChain\Chain\InputProcessorInterface;use PhpLlm\LlmChain\Platform\Message\AssistantMessage;
674
642
675
- final class MyProcessor implements InputProcessor
643
+ final class MyProcessor implements InputProcessorInterface
676
644
{
677
645
public function processInput(Input $input): void
678
646
{
@@ -693,11 +661,9 @@ final class MyProcessor implements InputProcessor
693
661
mutate or replace the given response:
694
662
695
663
```php
696
- use PhpLlm\LlmChain\Chain\Output;
697
- use PhpLlm\LlmChain\Chain\OutputProcessor;
698
- use PhpLlm\LlmChain\Model\Message\AssistantMessage
664
+ use PhpLlm\LlmChain\Chain\Output;use PhpLlm\LlmChain\Chain\OutputProcessorInterface;
699
665
700
- final class MyProcessor implements OutputProcessor
666
+ final class MyProcessor implements OutputProcessorInterface
701
667
{
702
668
public function processOutput(Output $out): void
703
669
{
@@ -716,13 +682,9 @@ provided, in case the processor implemented the `ChainAwareProcessor` interface,
716
682
`ChainAwareTrait`:
717
683
718
684
```php
719
- use PhpLlm\LlmChain\Chain\ChainAwareProcessor;
720
- use PhpLlm\LlmChain\Chain\ChainAwareTrait;
721
- use PhpLlm\LlmChain\Chain\Output;
722
- use PhpLlm\LlmChain\Chain\OutputProcessor;
723
- use PhpLlm\LlmChain\Model\Message\AssistantMessage
685
+ use PhpLlm\LlmChain\Chain\ChainAwareProcessorInterface;use PhpLlm\LlmChain\Chain\ChainAwareTrait;use PhpLlm\LlmChain\Chain\Output;use PhpLlm\LlmChain\Chain\OutputProcessorInterface;
724
686
725
- final class MyProcessor implements OutputProcessor, ChainAwareProcessor
687
+ final class MyProcessor implements OutputProcessorInterface, ChainAwareProcessorInterface
726
688
{
727
689
use ChainAwareTrait;
728
690
@@ -740,11 +702,9 @@ LLM Chain comes out of the box with an integration for [HuggingFace](https://hug
740
702
hosting and sharing all kinds of models, including LLMs, embeddings, image generation, and classification models.
741
703
742
704
You can just instantiate the Platform with the corresponding HuggingFace bridge and use it with the `task` option:
705
+
743
706
```php
744
- 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;
707
+ use PhpLlm\LlmChain\Bridge\HuggingFace\Model;use PhpLlm\LlmChain\Platform\Bridge\HuggingFace\PlatformFactory;use PhpLlm\LlmChain\Platform\Bridge\HuggingFace\Task;use PhpLlm\LlmChain\Platform\Message\Content\Image;
748
708
749
709
$platform = PlatformFactory::create($apiKey);
750
710
$model = new Model(' facebook /detr-resnet-50 ' );
@@ -788,9 +748,7 @@ and comes with an extra setup, see [TransformersPHP's Getting Starter](https://t
788
748
The usage with LLM Chain is similar to the HuggingFace integration, and also requires the `task` option to be set:
789
749
790
750
```php
791
- use Codewithkyrian\Transformers\Pipelines\Task;
792
- use PhpLlm\LlmChain\Bridge\TransformersPHP\Model;
793
- use PhpLlm\LlmChain\Bridge\TransformersPHP\PlatformFactory;
751
+ use Codewithkyrian\Transformers\Pipelines\Task;use PhpLlm\LlmChain\Bridge\TransformersPHP\Model;use PhpLlm\LlmChain\Platform\Bridge\TransformersPHP\PlatformFactory;
794
752
795
753
$platform = PlatformFactory::create();
796
754
$model = new Model( ' Xenova/LaMini-Flan-T5-783M' );
0 commit comments