-
-
Notifications
You must be signed in to change notification settings - Fork 680
[0.x] Adds Moderations::create typed response
#5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nunomaduro
merged 4 commits into
openai-php:main
from
gehrisandro:poc-strong-typed-requests-and-responses
Oct 3, 2022
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4933cb5
POC: strong typed requests and responses
gehrisandro a4c02b1
POC: strong typed requests and responses
gehrisandro b9614ed
rename namespaces and class names, toArray() now returns the original…
gehrisandro 2770d89
add missing strict_types declarations
gehrisandro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OpenAI\Contracts; | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| interface DataObject | ||
| { | ||
| /** | ||
| * Return the data as array. | ||
|
gehrisandro marked this conversation as resolved.
Outdated
|
||
| * | ||
| * @return array<array-key, mixed> | ||
| */ | ||
| public function toArray(): array; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <?php | ||
|
|
||
| namespace OpenAI\DataObjects\Moderation; | ||
|
gehrisandro marked this conversation as resolved.
Outdated
|
||
|
|
||
| use OpenAI\Contracts\DataObject; | ||
|
|
||
| final class Moderation implements DataObject | ||
|
gehrisandro marked this conversation as resolved.
Outdated
|
||
| { | ||
| /** | ||
| * @param array<array-key, ModerationResult> $results | ||
| */ | ||
| public function __construct( | ||
| public readonly string $id, | ||
| public readonly string $model, | ||
| public readonly array $results, | ||
| ) { | ||
| } | ||
|
|
||
| /** | ||
| * @return array<string, mixed> | ||
| */ | ||
| public function toArray(): array | ||
| { | ||
| return [ | ||
| 'id' => $this->id, | ||
| 'model' => $this->model, | ||
| 'results' => array_map(fn (ModerationResult $result): array => $result->toArray(), $this->results), | ||
| ]; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <?php | ||
|
|
||
| namespace OpenAI\DataObjects\Moderation; | ||
|
gehrisandro marked this conversation as resolved.
Outdated
|
||
|
|
||
| use OpenAI\Contracts\DataObject; | ||
| use OpenAI\Enums\Moderation\Category; | ||
|
|
||
| final class ModerationCategory implements DataObject | ||
|
gehrisandro marked this conversation as resolved.
Outdated
|
||
| { | ||
| public function __construct( | ||
| public readonly Category $category, | ||
| public readonly bool $violated, | ||
| public readonly float $score, | ||
| ) { | ||
| } | ||
|
|
||
| /** | ||
| * @return array<string, string|float|bool> | ||
| */ | ||
| public function toArray(): array | ||
| { | ||
| return [ | ||
| 'category' => $this->category->value, | ||
| 'violated' => $this->violated, | ||
| 'score' => $this->score, | ||
| ]; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <?php | ||
|
|
||
| namespace OpenAI\DataObjects\Moderation; | ||
|
|
||
| use OpenAI\Contracts\DataObject; | ||
|
|
||
| final class ModerationResult implements DataObject | ||
| { | ||
| /** | ||
| * @param array<array-key, ModerationCategory> $categories | ||
| */ | ||
| public function __construct( | ||
| public readonly array $categories, | ||
| public readonly bool $flagged, | ||
| ) { | ||
| } | ||
|
|
||
| /** | ||
| * @return array<string, mixed> | ||
| */ | ||
| public function toArray(): array | ||
| { | ||
| return [ | ||
| 'categories' => array_map(fn (ModerationCategory $category): array => $category->toArray(), $this->categories), | ||
| 'flagged' => $this->flagged, | ||
| ]; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <?php | ||
|
|
||
| namespace OpenAI\Enums\Moderation; | ||
|
|
||
| enum Category: string | ||
| { | ||
| case Hate = 'hate'; | ||
| case HateThreatening = 'hate/threatening'; | ||
| case SelfHarm = 'self-harm'; | ||
| case Sexual = 'sexual'; | ||
| case SexualMinors = 'sexual/minors'; | ||
| case Violence = 'violence'; | ||
| case ViolenceGraphic = 'violence/graphic'; | ||
| } |
40 changes: 40 additions & 0 deletions
40
src/Factories/DataObjects/Moderation/ModerationCategoryFactory.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| <?php | ||
|
|
||
| namespace OpenAI\Factories\DataObjects\Moderation; | ||
|
|
||
| use OpenAI\DataObjects\Moderation\ModerationCategory; | ||
| use OpenAI\Enums\Moderation\Category; | ||
|
|
||
| final class ModerationCategoryFactory | ||
| { | ||
| /** | ||
| * @param array<array-key, array<string, bool|float|string>> $results | ||
| * @return ModerationCategory[] | ||
| */ | ||
| public static function collection(array $results): array | ||
| { | ||
| return array_map(fn ($result): ModerationCategory => static::new($result), $results); | ||
| } | ||
|
|
||
| /** | ||
| * @param array<string, bool|float|string> $attributes | ||
| */ | ||
| public static function new(array $attributes): ModerationCategory | ||
| { | ||
| return (new self)->make( | ||
| attributes: $attributes, | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @param array<string, bool|float|string> $attributes | ||
| */ | ||
| public function make(array $attributes): ModerationCategory | ||
| { | ||
| return new ModerationCategory( | ||
| category: Category::from((string) $attributes['category']), | ||
| violated: (bool) $attributes['violated'], | ||
| score: (float) $attributes['score'], | ||
| ); | ||
| } | ||
| } |
30 changes: 30 additions & 0 deletions
30
src/Factories/DataObjects/Moderation/ModerationFactory.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <?php | ||
|
|
||
| namespace OpenAI\Factories\DataObjects\Moderation; | ||
|
|
||
| use OpenAI\DataObjects\Moderation\Moderation; | ||
|
|
||
| final class ModerationFactory | ||
| { | ||
| /** | ||
| * @param array<string, array<array-key, array<string, array<string, bool|float>>>|string> $attributes | ||
| */ | ||
| public static function new(array $attributes): Moderation | ||
| { | ||
| return (new self)->make( | ||
| attributes: $attributes, | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @param array<string, array<array-key, array<string, array<string, bool|float>>>|string> $attributes | ||
| */ | ||
| public function make(array $attributes): Moderation | ||
| { | ||
| return new Moderation( | ||
| id: strval($attributes['id']), | ||
| model: strval($attributes['model']), | ||
| results: ModerationResultFactory::collection($attributes['results']), /** @phpstan-ignore-line */ | ||
| ); | ||
| } | ||
| } |
45 changes: 45 additions & 0 deletions
45
src/Factories/DataObjects/Moderation/ModerationResultFactory.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| <?php | ||
|
|
||
| namespace OpenAI\Factories\DataObjects\Moderation; | ||
|
|
||
| use OpenAI\DataObjects\Moderation\ModerationResult; | ||
| use OpenAI\Enums\Moderation\Category; | ||
|
|
||
| final class ModerationResultFactory | ||
| { | ||
| /** | ||
| * @param array<array-key, array<string, array<string, bool|float>>> $results | ||
| * @return ModerationResult[] | ||
| */ | ||
| public static function collection(array $results): array | ||
| { | ||
| return array_map(fn ($result): ModerationResult => static::new($result), $results); | ||
| } | ||
|
|
||
| /** | ||
| * @param array<string, array<string, bool|float>> $attributes | ||
| */ | ||
| public static function new(array $attributes): ModerationResult | ||
| { | ||
| return (new self)->make( | ||
| attributes: $attributes, | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @param array<string, array<string, bool|float>> $attributes | ||
| */ | ||
| public function make(array $attributes): ModerationResult | ||
| { | ||
| $categories = array_map(fn (Category $category): array => [ | ||
| 'category' => $category->value, | ||
| 'violated' => $attributes['categories'][$category->value], | ||
| 'score' => $attributes['category_scores'][$category->value], | ||
| ], Category::cases()); | ||
|
|
||
| return new ModerationResult( | ||
| categories: ModerationCategoryFactory::collection($categories), | ||
| flagged: (bool) ($attributes['flagged']), | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| <?php | ||
|
|
||
| use OpenAI\DataObjects\Moderation\Moderation; | ||
| use OpenAI\DataObjects\Moderation\ModerationResult; | ||
| use OpenAI\Factories\DataObjects\Moderation\ModerationFactory; | ||
|
|
||
| test('build new moderation', function () { | ||
| $moderation = ModerationFactory::new(moderationResource()); | ||
|
|
||
| expect($moderation) | ||
| ->toBeInstanceOf(Moderation::class) | ||
| ->id->toBe('modr-5MWoLO') | ||
| ->model->toBe('text-moderation-001') | ||
| ->results->toBeArray()->toHaveCount(1) | ||
| ->results->each->toBeInstanceOf(ModerationResult::class); | ||
| }); | ||
|
|
||
| test('create array from moderation', function () { | ||
| $moderation = ModerationFactory::new(moderationResource()); | ||
|
|
||
| expect($moderation->toArray()) | ||
| ->toBeArray() | ||
| ->id->toBe('modr-5MWoLO') | ||
| ->model->toBe('text-moderation-001') | ||
| ->results->toBeArray()->toHaveCount(1) | ||
| ->results->each->toBeArray(); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| <?php | ||
|
|
||
| use OpenAI\Enums\Moderation\Category; | ||
| use OpenAI\Factories\DataObjects\Moderation\ModerationCategoryFactory; | ||
|
|
||
| test('build new moderation category', function () { | ||
| $category = ModerationCategoryFactory::new([ | ||
| 'category' => Category::Hate->value, | ||
| 'violated' => true, | ||
| 'score' => 0.1234, | ||
| ]); | ||
|
|
||
| expect($category) | ||
| ->category->toBe(Category::Hate) | ||
| ->violated->toBe(true) | ||
| ->score->toBe(0.1234); | ||
| }); | ||
|
|
||
| test('create array from moderation category', function () { | ||
| $raw = [ | ||
| 'category' => Category::Hate->value, | ||
| 'violated' => true, | ||
| 'score' => 0.1234, | ||
| ]; | ||
|
|
||
| $category = ModerationCategoryFactory::new($raw); | ||
|
|
||
| expect($category->toArray()) | ||
| ->toBeArray() | ||
| ->toBe($raw); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <?php | ||
|
|
||
| use OpenAI\DataObjects\Moderation\ModerationCategory; | ||
| use OpenAI\Factories\DataObjects\Moderation\ModerationResultFactory; | ||
|
|
||
| test('build new moderation result', function () { | ||
| $result = ModerationResultFactory::new(moderationResource()['results'][0]); | ||
|
|
||
| expect($result) | ||
| ->flagged->toBeTrue() | ||
| ->categories->toHaveCount(7) | ||
| ->each->toBeInstanceOf(ModerationCategory::class); | ||
| }); | ||
|
|
||
| test('create array from moderation result', function () { | ||
| $result = ModerationResultFactory::new(moderationResource()['results'][0]); | ||
|
|
||
| expect($result->toArray()) | ||
| ->flagged->toBeTrue() | ||
| ->categories->toHaveCount(7) | ||
| ->each->toBeArray(); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.