|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Mindwave\Mindwave\Prompts\OutputParsers; |
| 4 | + |
| 5 | +use Illuminate\Support\Collection; |
| 6 | +use Mindwave\Mindwave\Contracts\OutputParser; |
| 7 | +use ReflectionClass; |
| 8 | + |
| 9 | +class StructuredOutputParser implements OutputParser |
| 10 | +{ |
| 11 | + protected $schema; |
| 12 | + |
| 13 | + public function __construct($schema = null) |
| 14 | + { |
| 15 | + $this->schema = $schema; |
| 16 | + } |
| 17 | + |
| 18 | + public function fromClass($schema): self |
| 19 | + { |
| 20 | + $this->schema = $schema; |
| 21 | + |
| 22 | + return $this; |
| 23 | + } |
| 24 | + |
| 25 | + public function getSchemaStructure(): array |
| 26 | + { |
| 27 | + $reflectionClass = new ReflectionClass($this->schema); |
| 28 | + $properties = []; |
| 29 | + $required = []; |
| 30 | + |
| 31 | + foreach ($reflectionClass->getProperties() as $property) { |
| 32 | + $propertyName = $property->getName(); |
| 33 | + $propertyType = $property->getType()->getName(); |
| 34 | + |
| 35 | + if ($property->getType()->allowsNull() === false) { |
| 36 | + $required[] = $propertyName; |
| 37 | + } |
| 38 | + |
| 39 | + $properties[$propertyName] = [ |
| 40 | + 'type' => match ($propertyType) { |
| 41 | + 'string', 'int', 'float', 'bool' => $propertyType, |
| 42 | + 'array', Collection::class => 'array', |
| 43 | + default => 'object', |
| 44 | + }, |
| 45 | + ]; |
| 46 | + } |
| 47 | + |
| 48 | + return [ |
| 49 | + 'properties' => $properties, |
| 50 | + 'required' => $required, |
| 51 | + ]; |
| 52 | + } |
| 53 | + |
| 54 | + public function getFormatInstructions(): string |
| 55 | + { |
| 56 | + $schema = json_encode($this->getSchemaStructure()); |
| 57 | + |
| 58 | + return trim(' |
| 59 | +RESPONSE FORMAT INSTRUCTIONS |
| 60 | +---------------------------- |
| 61 | +The output should be formatted as a JSON instance that conforms to the JSON schema below. |
| 62 | +
|
| 63 | +As an example, for the schema {{"properties": {{"foo": {{"title": "Foo", "description": "a list of strings", "type": "array", "items": {{"type": "string"}}}}}}, "required": ["foo"]}}}} |
| 64 | +the object {{"foo": ["bar", "baz"]}} is a well-formatted instance of the schema. The object {{"properties": {{"foo": ["bar", "baz"]}}}} is not well-formatted. |
| 65 | +
|
| 66 | +Here is the output schema: |
| 67 | +```json |
| 68 | +'.$schema.' |
| 69 | +``` |
| 70 | +Remember to respond with a JSON blob, and NOTHING else.'); |
| 71 | + } |
| 72 | + |
| 73 | + public function parse(string $text): mixed |
| 74 | + { |
| 75 | + $reflectionClass = new ReflectionClass($this->schema); |
| 76 | + $data = json_decode($text, true); |
| 77 | + |
| 78 | + if (! $data) { |
| 79 | + // TODO(29 May 2023) ~ Helge: Throw custom exception |
| 80 | + return null; |
| 81 | + } |
| 82 | + |
| 83 | + $instance = new $this->schema(); |
| 84 | + |
| 85 | + foreach ($data as $key => $value) { |
| 86 | + |
| 87 | + $type = $reflectionClass->getProperty($key)->getType(); |
| 88 | + |
| 89 | + // TODO(29 May 2023) ~ Helge: There are probably libraries that do this in a more clever way, but this is fine for now. |
| 90 | + $instance->{$key} = match ($type->getName()) { |
| 91 | + 'bool' => boolval($value), |
| 92 | + 'int' => intval($value), |
| 93 | + 'float' => floatval($value), |
| 94 | + Collection::class => collect($value), |
| 95 | + default => $value, |
| 96 | + }; |
| 97 | + } |
| 98 | + |
| 99 | + return $instance; |
| 100 | + } |
| 101 | +} |
0 commit comments