-
Notifications
You must be signed in to change notification settings - Fork 0
Output Formatter Metadata
The code implements two formatter classes: DtoOutputFormatter
and OutputFormatter
. These classes are part of the LLM (Language Model) Agents solution and are designed to format the output of an AI agent's response.
-
DtoOutputFormatter
: This class is used to instruct an agent to format its output according to a specific Data Transfer Object (DTO) or Enum class. It extends theOption
class and is marked as final and readonly. -
OutputFormatter
: This class is used to instruct an agent to use a specific output formatter. It also extends theOption
class and is marked as readonly.
Both classes are metadata options that can be added to an agent to control how its output is formatted.
a) Formatting API responses:
use LLM\Agents\Solution\Metadata\DtoOutputFormatter;
class ApiResponse
{
public function __construct(
public readonly int $status,
public readonly string $message,
public readonly mixed $data
) {}
}
$agent->addMetadata(new DtoOutputFormatter(ApiResponse::class));
b) Enforcing a specific output structure:
use LLM\Agents\Solution\Metadata\DtoOutputFormatter;
class UserProfile
{
public function __construct(
public readonly string $name,
public readonly int $age,
public readonly string $email
) {}
}
$agent->addMetadata(new DtoOutputFormatter(UserProfile::class));
c) Limiting output to predefined options:
use LLM\Agents\Solution\Metadata\DtoOutputFormatter;
enum Sentiment: string
{
case Positive = 'positive';
case Neutral = 'neutral';
case Negative = 'negative';
}
$agent->addMetadata(new DtoOutputFormatter(Sentiment::class));
d) Using a custom formatter:
use LLM\Agents\Solution\Metadata\OutputFormatter;
use LLM\Agents\LLM\Output\JsonSchemaFormatter;
$jsonFormatter = new JsonSchemaFormatter($schemaMapper);
$customFormatter = $jsonFormatter->withJsonSchema('{
"type": "object",
"properties": {
"summary": {"type": "string"},
"keywords": {"type": "array", "items": {"type": "string"}}
}
}');
$agent->addMetadata(new OutputFormatter($customFormatter));
e) Combining multiple formatters:
use LLM\Agents\Solution\Metadata\DtoOutputFormatter;
use LLM\Agents\Solution\Metadata\OutputFormatter;
class AnalysisResult
{
public function __construct(
public readonly string $text,
public readonly Sentiment $sentiment
) {}
}
$agent->addMetadata(new DtoOutputFormatter(AnalysisResult::class));
$agent->addMetadata(new OutputFormatter(new CustomSentimentFormatter()));
For DtoOutputFormatter
:
-
dtoClass
(required): The fully qualified class name of the DTO or Enum to use for formatting. Default: None (must be provided)use LLM\Agents\Solution\Metadata\DtoOutputFormatter; $formatter = new DtoOutputFormatter(dtoClass: MyDTO::class);
For OutputFormatter
:
-
formatter
(required): An instance ofFormatterInterface
to use for formatting. Default: None (must be provided)use LLM\Agents\Solution\Metadata\OutputFormatter; use LLM\Agents\LLM\Output\SelectFormatter; $formatter = new OutputFormatter(formatter: new SelectFormatter('option1', 'option2', 'option3'));
-
Option
: BothDtoOutputFormatter
andOutputFormatter
extend this class. It represents a configuration option for the agent. -
FormatterInterface
: TheOutputFormatter
class accepts an implementation of this interface. It defines the contract for output formatters. -
SchemaMapperInterface
: Used indirectly by theJsonSchemaFormatter
, which can be passed to theOutputFormatter
. -
SolutionMetadata
: The parent class ofOption
, representing metadata that can be added to a solution (like an agent).
Here's a Mermaid class diagram illustrating the relationships between the main features and related classes:
classDiagram
class SolutionMetadata {
+MetadataType type
+string key
+mixed content
}
class Option {
+string key
+mixed content
}
class DtoOutputFormatter {
+string dtoClass
}
class OutputFormatter {
+FormatterInterface formatter
}
class FormatterInterface {
+format(string|Stringable output): mixed
+getInstruction(): ?string
}
class SchemaMapperInterface {
+toJsonSchema(string class): array
+toObject(string json, ?string class): object
}
SolutionMetadata <|-- Option
Option <|-- DtoOutputFormatter
Option <|-- OutputFormatter
OutputFormatter --> FormatterInterface
FormatterInterface <|.. JsonSchemaFormatter
JsonSchemaFormatter --> SchemaMapperInterface
This diagram shows the inheritance hierarchy and associations between the classes involved in the output formatting feature of the LLM Agents solution.