-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from llm-agents-php/feature/dynamic-memory-tool
Adds dynamic memory tool
- Loading branch information
Showing
7 changed files
with
183 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Agents\DynamicMemoryTool; | ||
|
||
use Spiral\JsonSchemaGenerator\Attribute\Field; | ||
|
||
final class DynamicMemoryInput | ||
{ | ||
public function __construct( | ||
#[Field(title: 'Session ID', description: 'The unique identifier for the current session')] | ||
public string $sessionId, | ||
#[Field(title: 'User preference', description: 'The user preference to add or update')] | ||
public string $preference, | ||
) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Agents\DynamicMemoryTool; | ||
|
||
use App\Application\Entity\Uuid; | ||
use LLM\Agents\Solution\SolutionMetadata; | ||
use Psr\SimpleCache\CacheInterface; | ||
use Spiral\Core\Attribute\Singleton; | ||
|
||
#[Singleton] | ||
final readonly class DynamicMemoryService | ||
{ | ||
public function __construct( | ||
private CacheInterface $cache, | ||
) {} | ||
|
||
public function addMemory(Uuid $sessionUuid, SolutionMetadata $metadata): void | ||
{ | ||
$memories = $this->getCurrentMemory($sessionUuid); | ||
|
||
$memories->addMemory($metadata); | ||
|
||
$this->cache->set($this->getKey($sessionUuid), $memories); | ||
} | ||
|
||
public function updateMemory(Uuid $sessionUuid, SolutionMetadata $metadata): void | ||
{ | ||
$memories = $this->getCurrentMemory($sessionUuid); | ||
$memories->updateMemory($metadata); | ||
|
||
$this->cache->set($this->getKey($sessionUuid), $memories); | ||
} | ||
|
||
public function getCurrentMemory(Uuid $sessionUuid): Memories | ||
{ | ||
return $this->cache->get($this->getKey($sessionUuid)) ?? new Memories( | ||
\Ramsey\Uuid\Uuid::uuid4(), | ||
); | ||
} | ||
|
||
private function getKey(Uuid $sessionUuid): string | ||
{ | ||
return 'user_memory'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Agents\DynamicMemoryTool; | ||
|
||
use App\Application\Entity\Uuid; | ||
use App\Domain\Tool\PhpTool; | ||
use LLM\Agents\Solution\MetadataType; | ||
use LLM\Agents\Solution\SolutionMetadata; | ||
|
||
final class DynamicMemoryTool extends PhpTool | ||
{ | ||
public const NAME = 'dynamic_memory'; | ||
|
||
public function __construct( | ||
private readonly DynamicMemoryService $memoryService, | ||
) { | ||
parent::__construct( | ||
name: self::NAME, | ||
inputSchema: DynamicMemoryInput::class, | ||
description: 'Use this tool to add or update a important memory about the user. This memory will be used in the future to provide a better experience.', | ||
); | ||
} | ||
|
||
public function execute(object $input): string | ||
{ | ||
$metadata = new SolutionMetadata( | ||
type: MetadataType::Memory, | ||
key: 'user_memory', | ||
content: $input->preference, | ||
); | ||
|
||
$sessionUuid = Uuid::fromString($input->sessionId); | ||
$this->memoryService->addMemory($sessionUuid, $metadata); | ||
|
||
return 'Memory updated'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Agents\DynamicMemoryTool; | ||
|
||
use LLM\Agents\Solution\SolutionMetadata; | ||
use Ramsey\Uuid\UuidInterface; | ||
use Traversable; | ||
|
||
final class Memories implements \IteratorAggregate | ||
{ | ||
public function __construct( | ||
public readonly UuidInterface $uuid, | ||
/** @var array<SolutionMetadata> */ | ||
public array $memories = [], | ||
) {} | ||
|
||
public function addMemory(SolutionMetadata $metadata): void | ||
{ | ||
$this->memories[] = $metadata; | ||
} | ||
|
||
public function getIterator(): Traversable | ||
{ | ||
yield from $this->memories; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters