Skip to content

Add support for multi-modal moderation inputs and category applied input types #1

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

Closed
12 changes: 12 additions & 0 deletions src/Enums/Moderations/CategoryAppliedInputType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace OpenAI\Enums\Moderations;

enum CategoryAppliedInputType: string
{
case Text = 'text';
case Image = 'image';
case Audio = 'audio';
}
13 changes: 12 additions & 1 deletion src/Resources/Moderations.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,18 @@ public function create(array $parameters): CreateResponse
{
$payload = Payload::create('moderations', $parameters);

/** @var Response<array{id: string, model: string, results: array<int, array{categories: array<string, bool>, category_scores: array<string, float>, flagged: bool}>}> $response */
/**
* @var Response<array{
* id: string,
* model: string,
* results: array<int, array{
* categories: array<string, bool>,
* category_scores: array<string, float>,
* flagged: bool,
* category_applied_input_types?: array<string, array<int, string>>
* }>
* }> $response
*/
$response = $this->transporter->requestObject($payload);

return CreateResponse::from($response->data(), $response->meta());
Expand Down
6 changes: 3 additions & 3 deletions src/Responses/Moderations/CreateResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
use OpenAI\Testing\Responses\Concerns\Fakeable;

/**
* @implements ResponseContract<array{id: string, model: string, results: array<int, array{categories: array<string, bool>, category_scores: array<string, float>, flagged: bool}>}>
* @implements ResponseContract<array{id: string, model: string, results: array<int, array{categories: array<string, bool>, category_scores: array<string, float>, flagged: bool, category_applied_input_types?: array<string, array<int, string>>}>}>
*/
final class CreateResponse implements ResponseContract, ResponseHasMetaInformationContract
{
/**
* @use ArrayAccessible<array{id: string, model: string, results: array<int, array{categories: array<string, bool>, category_scores: array<string, float>, flagged: bool}>}>
* @use ArrayAccessible<array{id: string, model: string, results: array<int, array{categories: array<string, bool>, category_scores: array<string, float>, flagged: bool, category_applied_input_types?: array<string, array<int, string>>}>}>
*/
use ArrayAccessible;

Expand All @@ -37,7 +37,7 @@ private function __construct(
/**
* Acts as static factory, and returns a new Response instance.
*
* @param array{id: string, model: string, results: array<int, array{categories: array<string, bool>, category_scores: array<string, float>, flagged: bool}>} $attributes
* @param array{id: string, model: string, results: array<int, array{categories: array<string, bool>, category_scores: array<string, float>, flagged: bool, category_applied_input_types?: array<string, array<int, string>>}>} $attributes
*/
public static function from(array $attributes, MetaInformation $meta): self
{
Expand Down
27 changes: 23 additions & 4 deletions src/Responses/Moderations/CreateResponseResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,23 @@ final class CreateResponseResult
{
/**
* @param array<string, CreateResponseCategory> $categories
* @param array<string, array<string>> $categoryAppliedInputTypes
*/
private function __construct(
public readonly array $categories,
public readonly bool $flagged,
public readonly ?array $categoryAppliedInputTypes,
) {
// ..
}

/**
* @param array{categories: array<string, bool>, category_scores: array<string, float>, flagged: bool} $attributes
* @param array{
* categories: array<string, bool>,
* category_scores: array<string, float>,
* flagged: bool,
* category_applied_input_types?: array<string, array<int, string>>
* } $attributes
*/
public static function from(array $attributes): self
{
Expand All @@ -40,12 +47,18 @@ public static function from(array $attributes): self

return new CreateResponseResult(
$categories,
$attributes['flagged']
$attributes['flagged'],
$attributes['category_applied_input_types'] ?? null,
);
}

/**
* @return array{categories: array<string, bool>, category_scores: array<string, float>, flagged: bool}
* @return array{
* categories: array<string, bool>,
* category_scores: array<string, float>,
* flagged: bool,
* category_applied_input_types?: array<string, array<string>>
* }
*/
public function toArray(): array
{
Expand All @@ -56,10 +69,16 @@ public function toArray(): array
$categoryScores[$category->category->value] = $category->score;
}

return [
$result = [
'categories' => $categories,
'category_scores' => $categoryScores,
'flagged' => $this->flagged,
];

if ($this->categoryAppliedInputTypes !== null) {
$result['category_applied_input_types'] = $this->categoryAppliedInputTypes;
}

return $result;
}
}
15 changes: 15 additions & 0 deletions tests/Fixtures/Moderation.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,21 @@ function moderationOmniResource(): array
'violence/graphic' => 0.036865197122097015,
],
'flagged' => true,
'category_applied_input_types' => [
'hate' => ['text'],
'hate/threatening' => ['text'],
'harassment' => ['text'],
'harassment/threatening' => ['text'],
'self-harm' => ['text'],
'self-harm/intent' => ['text'],
'self-harm/instructions' => ['text'],
'sexual' => ['text'],
'sexual/minors' => ['text'],
'violence' => ['text'],
'violence/graphic' => ['text'],
'illicit' => ['text'],
'illicit/violent' => ['text'],
],
],
],
];
Expand Down
13 changes: 11 additions & 2 deletions tests/Resources/Moderations.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use OpenAI\Enums\Moderations\Category;
use OpenAI\Enums\Moderations\CategoryAppliedInputType;
use OpenAI\Responses\Meta\MetaInformation;
use OpenAI\Responses\Moderations\CreateResponse;
use OpenAI\Responses\Moderations\CreateResponseCategory;
Expand Down Expand Up @@ -47,12 +48,16 @@
test('create omni', closure: function () {
$client = mockClient('POST', 'moderations', [
'model' => 'omni-moderation-latest',
'input' => 'I want to kill them.',
'input' => [
['type' => 'text', 'text' => '.. I want to kill...'],
],
], Response::from(moderationOmniResource(), metaHeaders()));

$result = $client->moderations()->create([
'model' => 'omni-moderation-latest',
'input' => 'I want to kill them.',
'input' => [
['type' => 'text', 'text' => '.. I want to kill...'],
],
]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably support both right? We need to support the string legacy option as well as array of multi-modal

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iBotPeaches Yes, It support both. Should I revert this changes and create new unit test ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just want to make sure both work. You changed both of them to the multi-modal approach. We should probably have:

  • A basic text only test
  • A basic text & image test
  • A basic text in array test


expect($result)
Expand All @@ -77,6 +82,10 @@
->violated->toBe(true)
->score->toBe(0.9223177433013916);

expect($result->results[0]->categoryAppliedInputTypes)
->toHaveCount(13)
->each->toBe([CategoryAppliedInputType::Text->value]);

expect($result->meta())
->toBeInstanceOf(MetaInformation::class);
});
31 changes: 31 additions & 0 deletions tests/Testing/Resources/ModerationsTestResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,34 @@
$parameters['input'] === 'I want to k*** them.';
});
});

it('records a multi-modal moderations create request', function () {
$fake = new ClientFake([
CreateResponse::fake(),
]);

$fake->moderations()->create([
'model' => 'text-moderation-omni',
'input' => [
[
'type' => 'text',
'text' => 'I want to k*** them.',
],
[
'type' => 'image_url',
'image_url' => [
'url' => 'https://example.com/potentially-harmful-image.jpg',
],
],
],
]);

$fake->assertSent(Moderations::class, function ($method, $parameters) {
return $method === 'create' &&
$parameters['model'] === 'text-moderation-omni' &&
$parameters['input'][0]['type'] === 'text' &&
$parameters['input'][0]['text'] === 'I want to k*** them.' &&
$parameters['input'][1]['type'] === 'image_url' &&
$parameters['input'][1]['image_url']['url'] === 'https://example.com/potentially-harmful-image.jpg';
});
});