Skip to content

Commit

Permalink
Merge pull request #21 from llm-agents-php/feature/metadata-aware-int…
Browse files Browse the repository at this point in the history
…erface

Adds MetadataAwareInterface interface
  • Loading branch information
butschster authored Sep 15, 2024
2 parents 5b9da48 + c21dfc8 commit c0ca3fb
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 14 deletions.
32 changes: 21 additions & 11 deletions src/Agent/AgentAggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@
class AgentAggregate implements AgentInterface,
HasLinkedAgentsInterface,
HasLinkedToolsInterface,
HasLinkedContextSourcesInterface
HasLinkedContextSourcesInterface,
MetadataAwareInterface
{
/**
* @var array<TAssociation>
*/
/** @var array<TAssociation> */
private array $associations = [];

public function __construct(
Expand Down Expand Up @@ -92,7 +91,7 @@ public function getMemory(): array
{
return \array_values(
\array_filter(
$this->agent->getMetadata(),
$this->getMetadata(),
static fn(SolutionMetadata $metadata): bool => $metadata->type === MetadataType::Memory,
),
);
Expand All @@ -102,7 +101,7 @@ public function getPrompts(): array
{
return \array_values(
\array_filter(
$this->agent->getMetadata(),
$this->getMetadata(),
static fn(SolutionMetadata $metadata): bool => $metadata->type === MetadataType::Prompt,
),
);
Expand All @@ -115,7 +114,7 @@ public function getConfiguration(): array
{
return \array_values(
\array_filter(
$this->agent->getMetadata(),
$this->getMetadata(),
static fn(SolutionMetadata $metadata): bool => $metadata->type === MetadataType::Configuration,
),
);
Expand All @@ -128,11 +127,9 @@ public function addAssociation(Solution $association): void
$this->associations[] = $association;
}

public function addMetadata(SolutionMetadata ...$metadatum): void
public function addMetadata(SolutionMetadata ...$metadata): void
{
foreach ($metadatum as $metadata) {
$this->agent->addMetadata($metadata);
}
$this->agent->addMetadata(...$metadata);
}

private function validateDependency(Solution $association): void
Expand All @@ -145,4 +142,17 @@ private function validateDependency(Solution $association): void
}
}
}

public function getMetadata(): array
{
$metadata = $this->agent->getMetadata();

foreach ($this->associations as $association) {
if ($association instanceof MetadataAwareInterface) {
$metadata = \array_merge($metadata, $association->getMetadata());
}
}

return $metadata;
}
}
14 changes: 14 additions & 0 deletions src/Agent/MetadataAwareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace LLM\Agents\Agent;

use LLM\Agents\Solution\SolutionMetadata;

interface MetadataAwareInterface
{
public function getMetadata(): array;

public function addMetadata(SolutionMetadata ...$metadata): void;
}
10 changes: 7 additions & 3 deletions src/Solution/Solution.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace LLM\Agents\Solution;

abstract class Solution
use LLM\Agents\Agent\MetadataAwareInterface;

abstract class Solution implements MetadataAwareInterface
{
/**
* @var array<SolutionMetadata>
Expand All @@ -17,9 +19,11 @@ public function __construct(
public readonly ?string $description = null,
) {}

public function addMetadata(SolutionMetadata $metadata): void
public function addMetadata(SolutionMetadata ...$metadata): void
{
$this->metadata[] = $metadata;
foreach ($metadata as $meta) {
$this->metadata[] = $meta;
}
}

public function getMetadata(): array
Expand Down

0 comments on commit c0ca3fb

Please sign in to comment.