Skip to content

Commit

Permalink
test: augment tests for omni vs legacy moderation
Browse files Browse the repository at this point in the history
  • Loading branch information
iBotPeaches committed Nov 6, 2024
1 parent e4bc4e8 commit d4a7818
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
46 changes: 46 additions & 0 deletions tests/Fixtures/Moderation.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,49 @@ function moderationResource(): array
],
];
}

/**
* @return array<string, mixed>
*/
function moderationOmniResource(): array
{
return [
'id' => 'modr-5MWoLO',
'model' => 'omni-moderation-001',
'results' => [
[
'categories' => [
'hate' => false,
'hate/threatening' => true,
'harassment' => false,
'harassment/threatening' => false,
'illicit' => false,
'illicit/violent' => true,
'self-harm' => false,
'self-harm/intent' => false,
'self-harm/instructions' => false,
'sexual' => false,
'sexual/minors' => false,
'violence' => false,
'violence/graphic' => false,
],
'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' => 0.036865197122097015,
],
'flagged' => true,
],
],
];
}
39 changes: 38 additions & 1 deletion tests/Resources/Moderations.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use OpenAI\Responses\Moderations\CreateResponseResult;
use OpenAI\ValueObjects\Transporter\Response;

test('create', closure: function () {
test('create legacy', closure: function () {
$client = mockClient('POST', 'moderations', [
'model' => 'text-moderation-latest',
'input' => 'I want to kill them.',
Expand Down Expand Up @@ -43,3 +43,40 @@
expect($result->meta())
->toBeInstanceOf(MetaInformation::class);
});

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

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

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);

expect($result->results[0]->categories[Category::Illicit->value])
->category->toBe(Category::Illicit)
->violated->toBe(false)
->score->toBe(0.1602763684674149);

expect($result->results[0]->categories[Category::IllicitViolent->value])
->category->toBe(Category::IllicitViolent)
->violated->toBe(true)
->score->toBe(0.9223177433013916);

expect($result->meta())
->toBeInstanceOf(MetaInformation::class);
});

0 comments on commit d4a7818

Please sign in to comment.