You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jul 16, 2025. It is now read-only.
*[MongoDB Atlas Search](https://mongodb.com/products/platform/atlas-vector-search) (requires `mongodb/mongodb` as additional dependency)
502
502
*[Pinecone](https://pinecone.io) (requires `probots-io/pinecone-php` as additional dependency)
503
503
504
-
See [issue #28](https://github.com/php-llm/llm-chain/issues/28) for planned support of other models and platforms.
504
+
See [issue #28](https://github.com/php-llm/llm-chain/issues/28) for planned support of other models and platforms.
505
505
506
506
## Advanced Usage & Features
507
507
@@ -749,7 +749,7 @@ final class MyProcessor implements InputProcessorInterface
749
749
$options = $input->getOptions();
750
750
$options['foo'] = 'bar';
751
751
$input->setOptions($options);
752
-
752
+
753
753
// mutate MessageBag
754
754
$input->messages->append(new AssistantMessage(sprintf('Please answer using the locale %s', $this->locale)));
755
755
}
@@ -801,6 +801,81 @@ final class MyProcessor implements OutputProcessorInterface, ChainAwareInterface
801
801
}
802
802
```
803
803
804
+
805
+
## Memory
806
+
807
+
LLM Chain supports adding contextual memory to your conversations, which allows the model to recall past interactions or relevant information from different sources. Memory providers inject information into the system prompt, providing the model with context without changing your application logic.
808
+
809
+
### Using Memory
810
+
811
+
Memory integration is handled through the `MemoryInputProcessor` and one or more `MemoryProviderInterface` implementations. Here's how to set it up:
812
+
813
+
```php
814
+
<?php
815
+
use PhpLlm\LlmChain\Chain\Chain;
816
+
use PhpLlm\LlmChain\Chain\Memory\MemoryInputProcessor;
817
+
use PhpLlm\LlmChain\Chain\Memory\StaticMemoryProvider;
818
+
819
+
// Platform & LLM instantiation
820
+
821
+
$personalFacts = new StaticMemoryProvider(
822
+
'My name is Wilhelm Tell',
823
+
'I wish to be a swiss national hero',
824
+
'I am struggling with hitting apples but want to be professional with the bow and arrow',
825
+
);
826
+
$memoryProcessor = new MemoryInputProcessor($personalFacts);
827
+
828
+
$chain = new Chain($platform, $model, [$memoryProcessor]);
829
+
$messages = new MessageBag(Message::ofUser('What do we do today?'));
830
+
$response = $chain->call($messages);
831
+
```
832
+
833
+
### Memory Providers
834
+
835
+
The library includes some implementations that are usable out of the box.
836
+
837
+
#### Static Memory
838
+
839
+
The static memory can be utilized to provide static information form, for example, user settings, basic knowledge of your application
840
+
or any other thing that should be remembered als always there without the need of having it statically added to the system prompt by
841
+
yourself.
842
+
843
+
```php
844
+
use PhpLlm\LlmChain\Chain\Memory\StaticMemoryProvider;
845
+
846
+
$staticMemory = new StaticMemoryProvider(
847
+
'The user is allergic to nuts',
848
+
'The user prefers brief explanations',
849
+
);
850
+
```
851
+
852
+
#### Embedding Provider
853
+
854
+
Based on an embedding storage the given user message is utilized to inject knowledge from the storage. This could be general knowledge that was stored there and could fit the users input without the need for tools or past conversation pieces that should be recalled for
855
+
the current message bag.
856
+
857
+
```php
858
+
use PhpLlm\LlmChain\Chain\Memory\EmbeddingProvider;
859
+
860
+
$embeddingsMemory = new EmbeddingProvider(
861
+
$platform,
862
+
$embeddings, // Your embeddings model to use for vectorizing the users message
863
+
$store // Your vector store to query for fitting context
864
+
);
865
+
866
+
```
867
+
868
+
### Dynamically Memory Usage
869
+
870
+
The memory configuration is globally given for the chain. Sometimes there is the need to explicit disable the memory when it is not needed for some calls or calls are not in the wanted context for a call. So there is the option `use_memory` that is enabled by default but can be disabled on premise.
871
+
872
+
```php
873
+
$response = $chain->call($messages, [
874
+
'use_memory' => false,
875
+
]);
876
+
```
877
+
878
+
804
879
## HuggingFace
805
880
806
881
LLM Chain comes out of the box with an integration for [HuggingFace](https://huggingface.co/) which is a platform for
['role' => 'user', 'timestamp' => '2024-12-14 12:00:00', 'content' => 'My friends John and Emma are friends, too, are there hints why?'],
40
+
['role' => 'assistant', 'timestamp' => '2024-12-14 12:00:01', 'content' => 'Based on the found documents i would expect they are friends since childhood, this can give a deep bound!'],
41
+
['role' => 'user', 'timestamp' => '2024-12-14 12:02:02', 'content' => 'Yeah but how does this bound? I know John was once there with a wound dressing as Emma fell, could this be a hint?'],
42
+
['role' => 'assistant', 'timestamp' => '2024-12-14 12:02:03', 'content' => 'Yes, this could be a hint that they have been through difficult times together, which can strengthen their bond.'],
0 commit comments