Skip to content

Commit

Permalink
Merge pull request #68 from outl1ne/Virtual-branch
Browse files Browse the repository at this point in the history
refactor: Add JsonBoolean and JsonInteger classes and update ChatTest
  • Loading branch information
allantatter authored Oct 21, 2024
2 parents 37d8a96 + 47217ff commit d3655b7
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 9 deletions.
25 changes: 25 additions & 0 deletions src/Capabilities/Chat/Parameters/JsonSchema/JsonAnyOf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Outl1ne\NovaOpenAI\Capabilities\Chat\Parameters\JsonSchema;

use Outl1ne\NovaOpenAI\Traits\StaticMake;

class JsonAnyOf implements Arrayable
{
use StaticMake;

protected array $schemas = [];

public function schema(array|Arrayable $schema): self
{
$this->schemas[] = $schema instanceof Arrayable ? $schema->toArray() : $schema;
return $this;
}

public function toArray(): array
{
return [
'anyOf' => $this->schemas,
];
}
}
25 changes: 25 additions & 0 deletions src/Capabilities/Chat/Parameters/JsonSchema/JsonBoolean.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Outl1ne\NovaOpenAI\Capabilities\Chat\Parameters\JsonSchema;

use Outl1ne\NovaOpenAI\Traits\StaticMake;

class JsonBoolean implements Arrayable
{
use StaticMake;

public function __construct(protected ?string $description = null) {}

public function toArray(): array
{
$return = [
'type' => 'boolean',
];

if ($this->description) {
$return['description'] = $this->description;
}

return $return;
}
}
40 changes: 40 additions & 0 deletions src/Capabilities/Chat/Parameters/JsonSchema/JsonEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Outl1ne\NovaOpenAI\Capabilities\Chat\Parameters\JsonSchema;

use Exception;
use Outl1ne\NovaOpenAI\Traits\StaticMake;

class JsonEnum implements Arrayable
{
use StaticMake;

public function __construct(protected ?string $description = null, protected ?array $enum = null) {}

public function enums(array $enum): self
{
$this->enum = $enum;
return $this;
}

public function toArray(): array
{
if (!$this->enum) {
throw new Exception('Enum values are required for JsonEnum');
}

$return = [
'type' => 'string',
];

if ($this->description) {
$return['description'] = $this->description;
}

if ($this->enum) {
$return['enum'] = $this->enum;
}

return $return;
}
}
25 changes: 25 additions & 0 deletions src/Capabilities/Chat/Parameters/JsonSchema/JsonInteger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Outl1ne\NovaOpenAI\Capabilities\Chat\Parameters\JsonSchema;

use Outl1ne\NovaOpenAI\Traits\StaticMake;

class JsonInteger implements Arrayable
{
use StaticMake;

public function __construct(protected ?string $description = null) {}

public function toArray(): array
{
$return = [
'type' => 'integer',
];

if ($this->description) {
$return['description'] = $this->description;
}

return $return;
}
}
8 changes: 2 additions & 6 deletions src/Capabilities/Chat/Parameters/JsonSchema/JsonNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,18 @@ class JsonNumber implements Arrayable
{
use StaticMake;

public function __construct(protected ?string $description = null, protected ?array $enum = null) {}
public function __construct(protected ?string $description = null) {}

public function toArray(): array
{
$return = [
'type' => 'string',
'type' => 'number',
];

if ($this->description) {
$return['description'] = $this->description;
}

if ($this->enum) {
$return['enum'] = $this->enum;
}

return $return;
}
}
27 changes: 24 additions & 3 deletions tests/Unit/ChatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@

use Outl1ne\NovaOpenAI\Facades\OpenAI;
use Orchestra\Testbench\Concerns\WithWorkbench;
use Outl1ne\NovaOpenAI\Capabilities\Chat\Parameters\JsonSchema\JsonAnyOf;
use Outl1ne\NovaOpenAI\Capabilities\Chat\Parameters\Messages;
use Outl1ne\NovaOpenAI\Capabilities\Chat\Responses\ChatResponse;
use Outl1ne\NovaOpenAI\Capabilities\Chat\Parameters\ResponseFormat;
use Outl1ne\NovaOpenAI\Capabilities\Chat\Parameters\JsonSchema\JsonEnum;
use Outl1ne\NovaOpenAI\Capabilities\Chat\Responses\StreamedChatResponse;
use Outl1ne\NovaOpenAI\Capabilities\Chat\Parameters\JsonSchema\JsonArray;
use Outl1ne\NovaOpenAI\Capabilities\Chat\Parameters\JsonSchema\JsonNumber;
use Outl1ne\NovaOpenAI\Capabilities\Chat\Parameters\JsonSchema\JsonObject;
use Outl1ne\NovaOpenAI\Capabilities\Chat\Parameters\JsonSchema\JsonSchema;
use Outl1ne\NovaOpenAI\Capabilities\Chat\Parameters\JsonSchema\JsonString;
use Outl1ne\NovaOpenAI\Capabilities\Chat\Parameters\JsonSchema\JsonBoolean;
use Outl1ne\NovaOpenAI\Capabilities\Chat\Parameters\JsonSchema\JsonInteger;

class ChatTest extends \Orchestra\Testbench\TestCase
{
Expand Down Expand Up @@ -50,11 +54,28 @@ public function test_chat_json_schema_response(): void
{
$response = OpenAI::chat()->create(
model: 'gpt-4o-mini',
messages: Messages::make()->system('You are a helpful assistant.')->user('Suggest me tasty fruits.'),
responseFormat: ResponseFormat::make()->jsonSchema(JsonObject::make()->property('fruits', JsonArray::make()->items(JsonString::make()))),
messages: Messages::make()->system('You are a helpful assistant.')->user('Suggest me 10 tasty fruits.'),
responseFormat: ResponseFormat::make()->jsonSchema(
JsonObject::make()
->property('fruits', JsonArray::make()->items(JsonString::make()))
->property('number_of_fruits_in_response', JsonInteger::make())
->property('number_of_fruits_in_response_divided_by_three', JsonNumber::make())
->property('is_number_of_fruits_in_response_even', JsonBoolean::make())
->property('fruit_most_occurring_color', JsonEnum::make()->enums(['red', 'green', 'blue']))
->property(
'random_integer_or_string_max_one_character',
JsonAnyOf::make()
->schema(JsonInteger::make())
->schema(JsonString::make())
),
),
);
$this->assertTrue($response instanceof ChatResponse);
$this->assertIsArray($response->json()?->fruits);
$this->assertIsInt($response->json()?->number_of_fruits_in_response);
$this->assertIsFloat($response->json()?->number_of_fruits_in_response_divided_by_three);
$this->assertIsBool($response->json()?->is_number_of_fruits_in_response_even);
$this->assertIsString($response->json()?->fruit_most_occurring_color);

$response = OpenAI::chat()->create(
model: 'gpt-4o-mini',
Expand Down

0 comments on commit d3655b7

Please sign in to comment.