Skip to content
This repository was archived by the owner on Jul 16, 2025. It is now read-only.

Commit b95883d

Browse files
committed
Change memory provider signature returning array of memory
1 parent 8b6dc44 commit b95883d

File tree

7 files changed

+64
-24
lines changed

7 files changed

+64
-24
lines changed

src/Chain/Memory/EmbeddingProvider.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ public function __construct(
2525
) {
2626
}
2727

28-
public function loadMemory(Input $input): ?Memory
28+
public function loadMemory(Input $input): array
2929
{
3030
$messages = $input->messages->getMessages();
3131
/** @var MessageInterface|null $userMessage */
3232
$userMessage = $messages[array_key_last($messages)] ?? null;
3333

3434
if (!$userMessage instanceof UserMessage) {
35-
return null;
35+
return [];
3636
}
3737

3838
$userMessageTextContent = array_filter(
@@ -41,7 +41,7 @@ public function loadMemory(Input $input): ?Memory
4141
);
4242

4343
if (0 === \count($userMessageTextContent)) {
44-
return null;
44+
return [];
4545
}
4646

4747
$userMessageTextContent = array_shift($userMessageTextContent);
@@ -50,14 +50,14 @@ public function loadMemory(Input $input): ?Memory
5050
$vectors = $this->platform->request($this->model, $userMessageTextContent->text)->asVectors();
5151
$foundEmbeddingContent = $this->vectorStore->query($vectors[0]);
5252
if (0 === \count($foundEmbeddingContent)) {
53-
return null;
53+
return [];
5454
}
5555

5656
$content = '## Dynamic memories fitting user message'.\PHP_EOL.\PHP_EOL;
5757
foreach ($foundEmbeddingContent as $document) {
5858
$content .= json_encode($document->metadata);
5959
}
6060

61-
return new Memory($content);
61+
return [new Memory($content)];
6262
}
6363
}

src/Chain/Memory/MemoryInputProcessor.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,17 @@ public function processInput(Input $input): void
4545

4646
$memory = '';
4747
foreach ($this->memoryProviders as $provider) {
48-
$memoryMessage = $provider->loadMemory($input);
48+
$memoryMessages = $provider->loadMemory($input);
4949

50-
if (null === $memoryMessage) {
50+
if (0 === \count($memoryMessages)) {
5151
continue;
5252
}
5353

54-
$memory .= \PHP_EOL.\PHP_EOL.$memoryMessage->content;
54+
$memory .= \PHP_EOL.\PHP_EOL;
55+
$memory .= implode(
56+
\PHP_EOL,
57+
array_map(static fn (Memory $memory): string => $memory->content, $memoryMessages),
58+
);
5559
}
5660

5761
if ('' === $memory) {

src/Chain/Memory/MemoryProviderInterface.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,8 @@
1111
*/
1212
interface MemoryProviderInterface
1313
{
14-
public function loadMemory(Input $input): ?Memory;
14+
/**
15+
* @return list<Memory>
16+
*/
17+
public function loadMemory(Input $input): array;
1518
}

src/Chain/Memory/StaticMemoryProvider.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ public function __construct(string ...$memory)
2121
$this->memory = $memory;
2222
}
2323

24-
public function loadMemory(Input $input): ?Memory
24+
public function loadMemory(Input $input): array
2525
{
2626
if (0 === \count($this->memory)) {
27-
return null;
27+
return [];
2828
}
2929

3030
$content = '## Static Memory'.\PHP_EOL;
@@ -33,6 +33,6 @@ public function loadMemory(Input $input): ?Memory
3333
$content .= \PHP_EOL.'- '.$memory;
3434
}
3535

36-
return new Memory($content);
36+
return [new Memory($content)];
3737
}
3838
}

tests/Chain/Memory/EmbeddingProviderTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public function itIsNotCreatingMemoryWhenNoVectorsFound(): void
133133
[],
134134
));
135135

136-
self::assertNull($memory);
136+
self::assertCount(0, $memory);
137137
}
138138

139139
#[Test]
@@ -171,14 +171,14 @@ public function itIsCreatingMemoryWithFoundVectors(): void
171171
[],
172172
));
173173

174-
self::assertNotNull($memory);
174+
self::assertCount(1, $memory);
175175
self::assertSame(
176176
<<<MARKDOWN
177177
## Dynamic memories fitting user message
178178
179179
{"fact":"The sky is blue"}{"fact":"Water is wet"}
180180
MARKDOWN,
181-
$memory->content,
181+
$memory[0]->content,
182182
);
183183
}
184184
}

tests/Chain/Memory/MemoryInputProcessorTest.php

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@ public function itIsAddingMemoryToSystemPrompt(): void
6262
$firstMemoryProvider = $this->createMock(MemoryProviderInterface::class);
6363
$firstMemoryProvider->expects($this->once())
6464
->method('loadMemory')
65-
->willReturn(new Memory('First memory content'));
65+
->willReturn([new Memory('First memory content')]);
6666

6767
$secondMemoryProvider = $this->createMock(MemoryProviderInterface::class);
6868
$secondMemoryProvider->expects($this->once())
6969
->method('loadMemory')
70-
->willReturn(null);
70+
->willReturn([]);
7171

7272
$memoryInputProcessor = new MemoryInputProcessor(
7373
$firstMemoryProvider,
@@ -86,7 +86,7 @@ public function itIsAddingMemoryToSystemPrompt(): void
8686
You are a helpful and kind assistant.
8787
8888
# Conversation Memory
89-
This is the memory i have found for this conversation. The memory has more weight to answer user input,
89+
This is the memory I have found for this conversation. The memory has more weight to answer user input,
9090
so try to answer utilizing the memory as much as possible. Your answer must be changed to fit the given
9191
memory. If the memory is irrelevant, ignore it. Do not reply to the this section of the prompt and do not
9292
reference it as this is just for your reference.
@@ -103,7 +103,7 @@ public function itIsAddingMemoryToSystemPromptEvenItIsEmpty(): void
103103
$firstMemoryProvider = $this->createMock(MemoryProviderInterface::class);
104104
$firstMemoryProvider->expects($this->once())
105105
->method('loadMemory')
106-
->willReturn(new Memory('First memory content'));
106+
->willReturn([new Memory('First memory content')]);
107107

108108
$memoryInputProcessor = new MemoryInputProcessor($firstMemoryProvider);
109109

@@ -117,7 +117,7 @@ public function itIsAddingMemoryToSystemPromptEvenItIsEmpty(): void
117117
self::assertSame(
118118
<<<MARKDOWN
119119
# Conversation Memory
120-
This is the memory i have found for this conversation. The memory has more weight to answer user input,
120+
This is the memory I have found for this conversation. The memory has more weight to answer user input,
121121
so try to answer utilizing the memory as much as possible. Your answer must be changed to fit the given
122122
memory. If the memory is irrelevant, ignore it. Do not reply to the this section of the prompt and do not
123123
reference it as this is just for your reference.
@@ -128,13 +128,45 @@ public function itIsAddingMemoryToSystemPromptEvenItIsEmpty(): void
128128
);
129129
}
130130

131+
#[Test]
132+
public function itIsAddingMultipleMemoryFromSingleProviderToSystemPrompt(): void
133+
{
134+
$firstMemoryProvider = $this->createMock(MemoryProviderInterface::class);
135+
$firstMemoryProvider->expects($this->once())
136+
->method('loadMemory')
137+
->willReturn([new Memory('First memory content'), new Memory('Second memory content')]);
138+
139+
$memoryInputProcessor = new MemoryInputProcessor($firstMemoryProvider);
140+
141+
$memoryInputProcessor->processInput($input = new Input(
142+
self::createStub(Model::class),
143+
new MessageBag(),
144+
[]
145+
));
146+
147+
self::assertArrayNotHasKey('use_memory', $input->getOptions());
148+
self::assertSame(
149+
<<<MARKDOWN
150+
# Conversation Memory
151+
This is the memory I have found for this conversation. The memory has more weight to answer user input,
152+
so try to answer utilizing the memory as much as possible. Your answer must be changed to fit the given
153+
memory. If the memory is irrelevant, ignore it. Do not reply to the this section of the prompt and do not
154+
reference it as this is just for your reference.
155+
156+
First memory content
157+
Second memory content
158+
MARKDOWN,
159+
$input->messages->getSystemMessage()->content,
160+
);
161+
}
162+
131163
#[Test]
132164
public function itIsNotAddingAnythingIfMemoryWasEmpty(): void
133165
{
134166
$firstMemoryProvider = $this->createMock(MemoryProviderInterface::class);
135167
$firstMemoryProvider->expects($this->once())
136168
->method('loadMemory')
137-
->willReturn(null);
169+
->willReturn([]);
138170

139171
$memoryInputProcessor = new MemoryInputProcessor($firstMemoryProvider);
140172

tests/Chain/Memory/StaticMemoryProviderTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function itsReturnsNullWhenNoFactsAreProvided(): void
3434
[]
3535
));
3636

37-
self::assertNull($memory);
37+
self::assertCount(0, $memory);
3838
}
3939

4040
#[Test]
@@ -51,8 +51,9 @@ public function itDeliversFormattedFacts(): void
5151
[]
5252
));
5353

54-
self::assertInstanceOf(Memory::class, $memory);
54+
self::assertCount(1, $memory);
55+
self::assertInstanceOf(Memory::class, $memory[0]);
5556
$expectedContent = "## Static Memory\n\n- {$fact1}\n- {$fact2}";
56-
self::assertSame($expectedContent, $memory->content);
57+
self::assertSame($expectedContent, $memory[0]->content);
5758
}
5859
}

0 commit comments

Comments
 (0)