66
77class Schema
88{
9- public const MODEL_OPENAI = 'openai ' ;
10-
11- public const MODEL_ANTHROPIC = 'anthropic ' ;
12-
139 protected string $ name ;
1410
1511 protected string $ description ;
@@ -69,49 +65,19 @@ public function getRequired(): array
6965 }
7066
7167 /**
72- * Convert the schema parameters to a JSON Schema object
73- *
74- * @param string $model - the model to use (anthropic, openai) default is openai
7568 * @return array<string, mixed>
7669 */
77- public function toSchema ( string $ model = self :: MODEL_OPENAI ): array
70+ public function getProperties ( ): array
7871 {
79- if (! in_array ($ model , $ this ->getValidModels ())) {
80- throw new \InvalidArgumentException (
81- 'Invalid model selected. Must be one of: ' .implode (', ' , $ this ->getValidModels ())
82- );
83- }
84-
85- if ($ model === self ::MODEL_ANTHROPIC ) {
86- return [
87- 'name ' => $ this ->name ,
88- 'description ' => $ this ->description ,
89- 'input_schema ' => [
90- 'type ' => 'object ' ,
91- 'properties ' => $ this ->object ->getProperties (),
92- 'required ' => $ this ->required ,
93- ],
94- ];
95- }
96-
97- return [
98- 'name ' => $ this ->name ,
99- 'strict ' => true ,
100- 'schema ' => [
101- 'type ' => 'object ' ,
102- 'properties ' => $ this ->object ->getProperties (),
103- 'required ' => $ this ->required ,
104- 'additionalProperties ' => false ,
105- ],
106- ];
72+ return $ this ->object ->getProperties ();
10773 }
10874
10975 /**
110- * Convert the schema parameters to a simple JSON object
76+ * Convert the schema parameters to a simple JSON string
11177 *
112- * @return array< string, string>
78+ * @return string
11379 */
114- public function toJson (): array
80+ public function toJson (): string
11581 {
11682 $ json = [];
11783 foreach ($ this ->object ->getProperties () as $ property => $ value ) {
@@ -120,19 +86,10 @@ public function toJson(): array
12086 $ json [$ property ] = $ description .' ( ' .$ type .') ' ;
12187 }
12288
123- return $ json ;
124- }
89+ if (! json_encode ($ json )) {
90+ throw new \Exception ('Invalid JSON: ' .json_last_error_msg ());
91+ }
12592
126- /**
127- * Returns an array of valid models
128- *
129- * @return array<int, string>
130- */
131- public function getValidModels (): array
132- {
133- return [
134- self ::MODEL_OPENAI ,
135- self ::MODEL_ANTHROPIC ,
136- ];
93+ return json_encode ($ json );
13794 }
13895}
0 commit comments