Skip to content

Commit af38a2a

Browse files
committed
New JSON Schema types handling
1 parent 10970b5 commit af38a2a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1406
-694
lines changed

examples/A05_Extras/TranscriptionToTasks/run.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,16 @@ class Tasks {
6262

6363
// Step 3: Extract structured data using default language model API (OpenAI)
6464
print("Extracting structured data using LLM...\n\n");
65-
$tasks = (new StructuredOutput)->with(
66-
messages: $text,
67-
responseModel: Tasks::class,
68-
model: 'gpt-4o',
69-
mode: OutputMode::Json,
70-
)->get();
65+
$tasks = (new StructuredOutput)
66+
->withDebugPreset('on')
67+
->with(
68+
messages: $text,
69+
responseModel: Tasks::class,
70+
//model: 'gpt-4o',
71+
mode: OutputMode::Json,
72+
)
73+
->get();
74+
dd($tasks);
7175

7276
// Step 4: Now you can use the extracted data in your application
7377
print("Extracted data:\n");

examples/C07_Misc/ClassificationMulticlass/run.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,22 @@ class TicketLabels {
3333
?>
3434
```
3535

36-
## Classifying Text
36+
## Classifying Text0
3737

3838
The function `multi_classify` executes multi-label classification using LLM.
3939

4040
```php
4141
<?php
4242
// Perform single-label classification on the input text.
4343
function multi_classify(string $data) : TicketLabels {
44-
return (new StructuredOutput)
45-
->with(
46-
messages: [[
47-
"role" => "user",
48-
"content" => "Label following support ticket: {$data}",
49-
]],
50-
responseModel: TicketLabels::class,
51-
)
52-
->get();
44+
$x = (new StructuredOutput)
45+
//->withDebugPreset('on')
46+
->wiretap(fn($e) => $e->printDebug())
47+
->withMessages("Label following support ticket: {$data}")
48+
->withResponseModel(TicketLabels::class)
49+
->create();
50+
dd($x);
51+
// ->get();
5352
}
5453
?>
5554
```

packages/polyglot/docs/advanced/json-schema.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ Available methods include:
268268
JsonSchema provides various methods to access schema properties:
269269

270270
```php
271-
$schema->type(); // Get schema type (string, object, array, etc.)
271+
$schema->type(); // Get schema type (e.g., 'object')
272272
$schema->name(); // Get schema name
273273
$schema->isNullable(); // Check if schema is nullable
274274
$schema->requiredProperties(); // Get array of required properties

packages/polyglot/src/Embeddings/Drivers/EmbeddingsDriverFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function makeDriver(EmbeddingsConfig $config, HttpClient $httpClient) : C
5151
$this->events->dispatch(new EmbeddingsDriverBuilt([
5252
'driver' => get_class($driver),
5353
'config' => $config->toArray(),
54-
'httpClient' => print_r($httpClient, true),
54+
'httpClient' => $httpClient ? get_class($httpClient) : null,
5555
]));
5656

5757
return $driver($config, $httpClient, $this->events);

packages/polyglot/src/Inference/Drivers/InferenceDriverFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function makeDriver(LLMConfig $config, ?HttpClient $httpClient = null): C
9292
$this->events->dispatch(new InferenceDriverBuilt([
9393
'driverClass' => get_class($driver),
9494
'config' => $config->toArray(),
95-
'httpClient' => print_r($httpClient, true),
95+
'httpClient' => $httpClient ? get_class($httpClient) : null,
9696
]));
9797

9898
return $driver;

packages/schema/src/Data/Traits/TypeDetails/HandlesFactoryMethods.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Cognesy\Schema\Data\TypeDetails;
66
use Cognesy\Schema\Factories\TypeDetailsFactory;
7-
use Cognesy\Utils\JsonSchema\JsonSchema;
87

98
trait HandlesFactoryMethods
109
{
@@ -60,9 +59,9 @@ public static function fromPhpDocTypeString(string $typeString) : TypeDetails {
6059
return (new TypeDetailsFactory)->fromPhpDocTypeString($typeString);
6160
}
6261

63-
public static function fromJsonSchema(JsonSchema $jsonSchema) : TypeDetails {
64-
return (new TypeDetailsFactory)->fromJsonSchema($jsonSchema);
65-
}
62+
// public static function fromJsonSchema(JsonSchema $jsonSchema) : TypeDetails {
63+
// return (new TypeDetailsFactory)->fromJsonSchema($jsonSchema);
64+
// }
6665

6766
static public function fromValue(mixed $value) : TypeDetails {
6867
return (new TypeDetailsFactory)->fromValue($value);

packages/schema/src/Data/Traits/TypeDetails/HandlesJsonTypes.php

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,59 @@
22

33
namespace Cognesy\Schema\Data\Traits\TypeDetails;
44

5+
use Cognesy\Schema\Data\TypeDetails;
56
use Cognesy\Utils\JsonSchema\JsonSchema;
7+
use Cognesy\Utils\JsonSchema\JsonSchemaType;
68

79
trait HandlesJsonTypes
810
{
9-
public function jsonType() : string {
11+
public function toJsonType() : JsonSchemaType {
1012
return match ($this->type) {
11-
self::PHP_OBJECT => JsonSchema::JSON_OBJECT,
12-
self::PHP_ENUM => ($this->enumType === self::PHP_INT ? JsonSchema::JSON_INTEGER : JsonSchema::JSON_STRING),
13-
self::PHP_COLLECTION => JsonSchema::JSON_ARRAY,
14-
self::PHP_ARRAY => JsonSchema::JSON_ARRAY,
15-
self::PHP_INT => JsonSchema::JSON_INTEGER,
16-
self::PHP_FLOAT => JsonSchema::JSON_NUMBER,
17-
self::PHP_STRING => JsonSchema::JSON_STRING,
18-
self::PHP_BOOL => JsonSchema::JSON_BOOLEAN,
13+
self::PHP_OBJECT => JsonSchemaType::object(),
14+
self::PHP_ENUM => ($this->enumType === self::PHP_INT ? JsonSchemaType::integer() : JsonSchemaType::string()),
15+
self::PHP_COLLECTION => JsonSchemaType::array(),
16+
self::PHP_ARRAY => JsonSchemaType::array(),
17+
self::PHP_INT => JsonSchemaType::integer(),
18+
self::PHP_FLOAT => JsonSchemaType::number(),
19+
self::PHP_STRING => JsonSchemaType::string(),
20+
self::PHP_BOOL => JsonSchemaType::boolean(),
1921
default => throw new \Exception('Type not supported: '.$this->type),
2022
};
2123
}
2224

23-
static public function toPhpType(string $jsonType) : string {
24-
return match ($jsonType) {
25-
JsonSchema::JSON_OBJECT => self::PHP_OBJECT,
26-
JsonSchema::JSON_ARRAY => self::PHP_ARRAY,
27-
JsonSchema::JSON_INTEGER => self::PHP_INT,
28-
JsonSchema::JSON_NUMBER => self::PHP_FLOAT,
29-
JsonSchema::JSON_STRING => self::PHP_STRING,
30-
JsonSchema::JSON_BOOLEAN => self::PHP_BOOL,
31-
default => throw new \Exception('Unknown type: ' . $jsonType),
25+
static public function jsonToPhpType(JsonSchemaType $jsonType) : string {
26+
return match (true) {
27+
$jsonType->isObject() => self::PHP_OBJECT,
28+
$jsonType->isArray() => self::PHP_ARRAY,
29+
$jsonType->isInteger() => self::PHP_INT,
30+
$jsonType->isNumber() => self::PHP_FLOAT,
31+
$jsonType->isString() => self::PHP_STRING,
32+
$jsonType->isBoolean() => self::PHP_BOOL,
33+
default => throw new \Exception('Unknown type: ' . $jsonType->toString()),
34+
};
35+
}
36+
37+
static public function fromJson(JsonSchema $json) : TypeDetails {
38+
return match (true) {
39+
$json->isOption() => TypeDetails::option($json->enumValues()),
40+
$json->isObject() && !$json->hasObjectClass() => throw new \Exception('Object must have x-php-class field with the target class name'),
41+
$json->isObject() => TypeDetails::object($json->objectClass()),
42+
$json->isEnum() => TypeDetails::enum($json->objectClass(), self::jsonToPhpType($json->type()), $json->enumValues()),
43+
$json->isCollection() => TypeDetails::collection(match (true) {
44+
$json->itemSchema()?->isOption() => TypeDetails::option($json->itemSchema()?->enumValues()),
45+
$json->itemSchema()?->isEnum() => $json->itemSchema()?->objectClass(),
46+
$json->itemSchema()?->isObject() => $json->itemSchema()?->objectClass(),
47+
$json->itemSchema()?->isScalar() => self::jsonToPhpType($json->itemSchema()?->type()),
48+
$json->itemSchema()?->isAny() => TypeDetails::mixed(),
49+
default => throw new \Exception('Collection item type must be scalar, object or enum: ' . $json->itemSchema()?->type()->toString()),
50+
}),
51+
$json->isArray() => TypeDetails::array(),
52+
$json->isString() => TypeDetails::string(),
53+
$json->isBoolean() => TypeDetails::bool(),
54+
$json->isInteger() => TypeDetails::int(),
55+
$json->isNumber() => TypeDetails::float(),
56+
$json->isAny() => TypeDetails::mixed(),
57+
default => throw new \Exception('Unknown type: ' . $json->type()->toString()),
3258
};
3359
}
3460
}

packages/schema/src/Data/Traits/TypeDetails/HandlesPhpTypes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
trait HandlesPhpTypes
66
{
7-
static public function getType(mixed $variable) : ?string {
7+
static public function getPhpType(mixed $variable) : ?string {
88
$type = gettype($variable);
99
return self::TYPE_MAP[$type] ?? self::PHP_UNSUPPORTED;
1010
}

0 commit comments

Comments
 (0)