Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

feat(OpenAI): Add category applied input types to moderation response #572

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions 12 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';
}
2 changes: 1 addition & 1 deletion 2 src/Resources/Moderations.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ 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 6 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
17 changes: 13 additions & 4 deletions 17 src/Responses/Moderations/CreateResponseResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ 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 +42,13 @@ 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<int, string>>}
*/
public function toArray(): array
{
Expand All @@ -56,10 +59,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;
}
}
76 changes: 76 additions & 0 deletions 76 tests/Fixtures/Moderation.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,82 @@ 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'],
],
],
],
];
}

/**
* @return array<string, mixed>
*/
function moderationOmniWithTextAndImageResource(): array
{
return [
'id' => 'modr-5MWoLO',
'model' => 'omni-moderation-001',
'results' => [
[
'categories' => [
'hate' => false,
'hate/threatening' => false,
'harassment' => false,
'harassment/threatening' => false,
'illicit' => false,
'illicit/violent' => false,
'self-harm' => false,
'self-harm/intent' => false,
'self-harm/instructions' => false,
'sexual' => false,
'sexual/minors' => false,
'violence' => true,
'violence/graphic' => true,
],
'category_scores' => [
'hate' => 0.22714105248451233,
'hate/threatening' => 0.4132447838783264,
'illicit' => 0.1602763684674149,
'illicit/violent' => 0.9223177433013916,
'harassment' => 0.1602763684674149,
'harassment/threatening' => 0.1602763684674149,
'self-harm' => 0.005232391878962517,
'self-harm/intent' => 0.005134391873962517,
'self-harm/instructions' => 0.005132591874962517,
'sexual' => 0.01407341007143259,
'sexual/minors' => 0.0038522258400917053,
'violence' => 0.4132447838783264,
'violence/graphic' => 5.7929166992142E-5,
],
'flagged' => true,
'category_applied_input_types' => [
'hate' => ['text'],
'hate/threatening' => ['text'],
'harassment' => ['text'],
'harassment/threatening' => ['text'],
'self-harm' => ['text', 'image'],
'self-harm/intent' => ['text', 'image'],
'self-harm/instructions' => ['text', 'image'],
'sexual' => ['text', 'image'],
'sexual/minors' => ['text', 'image'],
'violence' => ['text', 'image'],
'violence/graphic' => ['text', 'image'],
'illicit' => ['text'],
'illicit/violent' => ['text'],
],
],
],
];
Expand Down
71 changes: 68 additions & 3 deletions 71 tests/Resources/Moderations.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
<?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;
use OpenAI\Responses\Moderations\CreateResponseResult;
use OpenAI\ValueObjects\Transporter\Response;

dataset('create omni inputs', [
'text_in_array' => [
['type' => 'text', 'text' => 'I love to kill...'],
],
'basic_text' => 'I want to kill them.',
]);

test('create legacy', closure: function () {
$client = mockClient('POST', 'moderations', [
'model' => 'text-moderation-latest',
Expand Down Expand Up @@ -44,15 +52,15 @@
->toBeInstanceOf(MetaInformation::class);
});

test('create omni', closure: function () {
test('create omni', closure: function ($input) {
$client = mockClient('POST', 'moderations', [
'model' => 'omni-moderation-latest',
'input' => 'I want to kill them.',
'input' => $input,
], Response::from(moderationOmniResource(), metaHeaders()));

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

expect($result)
Expand All @@ -77,6 +85,63 @@
->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);
})->with('create omni inputs');

test('create omni with image and text', closure: function () {
$client = mockClient('POST', 'moderations', [
'model' => 'omni-moderation-latest',
'input' => [
['type' => 'text', 'text' => '.. I want to kill...'],
[
'type' => 'image_url',
'image_url' => [
'url' => 'https://example.com/image.png',
],
],
],
], Response::from(moderationOmniWithTextAndImageResource(), metaHeaders()));

$result = $client->moderations()->create([
'model' => 'omni-moderation-latest',
'input' => [
['type' => 'text', 'text' => '.. I want to kill...'],
[
'type' => 'image_url',
'image_url' => [
'url' => 'https://example.com/image.png',
],
],
],
]);

expect($result)
->toBeInstanceOf(CreateResponse::class)
->id->toBe('modr-5MWoLO')
->model->toBe('omni-moderation-001')
->results->toBeArray()->toHaveCount(1)
->results->each->toBeInstanceOf(CreateResponseResult::class);

expect($result->results[0])
->flagged->toBeTrue()
->categories->toHaveCount(13)
->each->toBeInstanceOf(CreateResponseCategory::class)
->categoryAppliedInputTypes->toHaveCount(13);

expect($result->results[0]->categories[Category::ViolenceGraphic->value])
->category->toBe(Category::ViolenceGraphic)
->violated->toBe(true)
->score->toBe(5.7929166992142E-5);

expect($result->results[0]->categoryAppliedInputTypes[Category::IllicitViolent->value])
->toBe([CategoryAppliedInputType::Text->value]);

expect($result->results[0]->categoryAppliedInputTypes[Category::ViolenceGraphic->value])
->toBe([CategoryAppliedInputType::Text->value, CategoryAppliedInputType::Image->value]);

});
31 changes: 31 additions & 0 deletions 31 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';
});
});
Morty Proxy This is a proxified and sanitized view of the page, visit original site.