|
1 | | -# J |
| 1 | +# JSON Schema Mapper for LLM Agents |
| 2 | + |
| 3 | +This package is a super handy JSON Schema Mapper for the LLM Agents project. |
| 4 | + |
| 5 | +## What's it all about? |
| 6 | + |
| 7 | +This package gives you a nifty SchemaMapper that can: |
| 8 | + |
| 9 | +- Convert PHP classes to JSON schemas |
| 10 | +- Turn JSON data into PHP objects |
| 11 | + |
| 12 | +## Class Diagram |
| 13 | + |
| 14 | +Here's a quick look at how the main components fit together: |
| 15 | + |
| 16 | +```mermaid |
| 17 | +classDiagram |
| 18 | + class SchemaMapperInterface { |
| 19 | + <<interface>> |
| 20 | + +toJsonSchema(string $class): array |
| 21 | + +toObject(string $json, ?string $class): object |
| 22 | + } |
| 23 | + class SchemaMapper { |
| 24 | + -generator: JsonSchemaGenerator |
| 25 | + -mapper: TreeMapper |
| 26 | + +toJsonSchema(string $class): array |
| 27 | + +toObject(string $json, ?string $class): object |
| 28 | + } |
| 29 | + SchemaMapperInterface <|.. SchemaMapper |
| 30 | + SchemaMapper --> JsonSchemaGenerator |
| 31 | + SchemaMapper --> TreeMapper |
| 32 | +``` |
| 33 | + |
| 34 | +## Getting Started |
| 35 | + |
| 36 | +### Installation |
| 37 | + |
| 38 | +First things first, let's get this package installed: |
| 39 | + |
| 40 | +```bash |
| 41 | +composer require llm-agents/json-schema-mapper |
| 42 | +``` |
| 43 | + |
| 44 | +### Setting it up in Spiral |
| 45 | + |
| 46 | +If you're using the Spiral framework (and why wouldn't you be? It's awesome!), you'll need to register the bootloader. |
| 47 | +Here's how: |
| 48 | + |
| 49 | +1. Open up your `app/src/Application/Kernel.php` file. |
| 50 | +2. Add the SchemaMapperBootloader to the `defineBootloaders()` method: |
| 51 | + |
| 52 | +```php |
| 53 | +class Kernel extends \Spiral\Framework\Kernel |
| 54 | +{ |
| 55 | + // ... |
| 56 | + |
| 57 | + public function defineBootloaders(): array |
| 58 | + { |
| 59 | + return [ |
| 60 | + // ... other bootloaders ... |
| 61 | + \LLM\Agents\JsonSchema\Mapper\Bootloader\SchemaMapperBootloader::class, |
| 62 | + ]; |
| 63 | + } |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +And that's it! The bootloader will take care of registering the SchemaMapper for you. |
| 68 | + |
| 69 | +## How to Use It |
| 70 | + |
| 71 | +### Converting a PHP Class to JSON Schema |
| 72 | + |
| 73 | +Let's say you have a `User` class and you want to get its JSON schema: |
| 74 | + |
| 75 | +```php |
| 76 | +use LLM\Agents\JsonSchema\Mapper\SchemaMapperInterface; |
| 77 | + |
| 78 | +class UserController |
| 79 | +{ |
| 80 | + public function __construct( |
| 81 | + private SchemaMapperInterface $schemaMapper |
| 82 | + ) {} |
| 83 | + |
| 84 | + public function getUserSchema(): array |
| 85 | + { |
| 86 | + return $this->schemaMapper->toJsonSchema(User::class); |
| 87 | + } |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +### Converting JSON to a PHP Object |
| 92 | + |
| 93 | +Got some JSON data that you want to turn into a PHP object? No problem: |
| 94 | + |
| 95 | +```php |
| 96 | +use LLM\Agents\JsonSchema\Mapper\SchemaMapperInterface; |
| 97 | + |
| 98 | +class UserService |
| 99 | +{ |
| 100 | + public function __construct( |
| 101 | + private SchemaMapperInterface $schemaMapper |
| 102 | + ) {} |
| 103 | + |
| 104 | + public function createUserFromJson(string $json): User |
| 105 | + { |
| 106 | + return $this->schemaMapper->toObject($json, User::class); |
| 107 | + } |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +## Contributing |
| 112 | + |
| 113 | +We'd love your help to make this package even better! Here's how you can contribute: |
| 114 | + |
| 115 | +1. Fork the repository |
| 116 | +2. Write some awesome code |
| 117 | +3. Create a new Pull Request |
| 118 | + |
| 119 | +Please make sure your code follows PSR-12 coding standards and include tests for any new features. |
| 120 | + |
| 121 | +## License |
| 122 | + |
| 123 | +This package is open-sourced software licensed under the MIT license. Feel free to use it, modify it, and share it! |
| 124 | + |
| 125 | +--- |
| 126 | + |
| 127 | +That's all, folks! If you have any questions or run into any issues, don't hesitate to open an issue on GitHub. |
0 commit comments