Skip to content

Commit 1f5763c

Browse files
Merge pull request #42 from WordPress/remove-candidate-token-count
2 parents 61af8b6 + 562524b commit 1f5763c

File tree

5 files changed

+17
-112
lines changed

5 files changed

+17
-112
lines changed

src/Results/DTO/Candidate.php

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,14 @@
1919
*
2020
* @phpstan-import-type MessageArrayShape from Message
2121
*
22-
* @phpstan-type CandidateArrayShape array{message: MessageArrayShape, finishReason: string, tokenCount: int}
22+
* @phpstan-type CandidateArrayShape array{message: MessageArrayShape, finishReason: string}
2323
*
2424
* @extends AbstractDataTransferObject<CandidateArrayShape>
2525
*/
2626
class Candidate extends AbstractDataTransferObject
2727
{
2828
public const KEY_MESSAGE = 'message';
2929
public const KEY_FINISH_REASON = 'finishReason';
30-
public const KEY_TOKEN_COUNT = 'tokenCount';
3130
/**
3231
* @var Message The generated message.
3332
*/
@@ -38,21 +37,15 @@ class Candidate extends AbstractDataTransferObject
3837
*/
3938
private FinishReasonEnum $finishReason;
4039

41-
/**
42-
* @var int The number of tokens in this candidate.
43-
*/
44-
private int $tokenCount;
45-
4640
/**
4741
* Constructor.
4842
*
4943
* @since n.e.x.t
5044
*
5145
* @param Message $message The generated message.
5246
* @param FinishReasonEnum $finishReason The reason generation stopped.
53-
* @param int $tokenCount The number of tokens in this candidate.
5447
*/
55-
public function __construct(Message $message, FinishReasonEnum $finishReason, int $tokenCount)
48+
public function __construct(Message $message, FinishReasonEnum $finishReason)
5649
{
5750
if (!$message->getRole()->isModel()) {
5851
throw new InvalidArgumentException(
@@ -62,7 +55,6 @@ public function __construct(Message $message, FinishReasonEnum $finishReason, in
6255

6356
$this->message = $message;
6457
$this->finishReason = $finishReason;
65-
$this->tokenCount = $tokenCount;
6658
}
6759

6860
/**
@@ -89,18 +81,6 @@ public function getFinishReason(): FinishReasonEnum
8981
return $this->finishReason;
9082
}
9183

92-
/**
93-
* Gets the token count.
94-
*
95-
* @since n.e.x.t
96-
*
97-
* @return int The token count.
98-
*/
99-
public function getTokenCount(): int
100-
{
101-
return $this->tokenCount;
102-
}
103-
10484
/**
10585
* {@inheritDoc}
10686
*
@@ -117,12 +97,8 @@ public static function getJsonSchema(): array
11797
'enum' => FinishReasonEnum::getValues(),
11898
'description' => 'The reason generation stopped.',
11999
],
120-
self::KEY_TOKEN_COUNT => [
121-
'type' => 'integer',
122-
'description' => 'The number of tokens in this candidate.',
123-
],
124100
],
125-
'required' => [self::KEY_MESSAGE, self::KEY_FINISH_REASON, self::KEY_TOKEN_COUNT],
101+
'required' => [self::KEY_MESSAGE, self::KEY_FINISH_REASON],
126102
];
127103
}
128104

@@ -138,7 +114,6 @@ public function toArray(): array
138114
return [
139115
self::KEY_MESSAGE => $this->message->toArray(),
140116
self::KEY_FINISH_REASON => $this->finishReason->value,
141-
self::KEY_TOKEN_COUNT => $this->tokenCount,
142117
];
143118
}
144119

@@ -149,14 +124,13 @@ public function toArray(): array
149124
*/
150125
public static function fromArray(array $array): self
151126
{
152-
static::validateFromArrayData($array, [self::KEY_MESSAGE, self::KEY_FINISH_REASON, self::KEY_TOKEN_COUNT]);
127+
static::validateFromArrayData($array, [self::KEY_MESSAGE, self::KEY_FINISH_REASON]);
153128

154129
$messageData = $array[self::KEY_MESSAGE];
155130

156131
return new self(
157132
Message::fromArray($messageData),
158133
FinishReasonEnum::from($array[self::KEY_FINISH_REASON]),
159-
$array[self::KEY_TOKEN_COUNT]
160134
);
161135
}
162136
}

tests/unit/Operations/DTO/GenerativeAiOperationTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,6 @@ public function testFromArraySucceededState(): void
403403
]
404404
],
405405
Candidate::KEY_FINISH_REASON => FinishReasonEnum::stop()->value,
406-
Candidate::KEY_TOKEN_COUNT => 30
407406
]
408407
],
409408
GenerativeAiResult::KEY_TOKEN_USAGE => [

tests/unit/Results/DTO/CandidateTest.php

Lines changed: 13 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,10 @@ public function testCreateWithBasicProperties(): void
3939
$candidate = new Candidate(
4040
$message,
4141
FinishReasonEnum::stop(),
42-
25
4342
);
4443

4544
$this->assertSame($message, $candidate->getMessage());
4645
$this->assertEquals(FinishReasonEnum::stop(), $candidate->getFinishReason());
47-
$this->assertEquals(25, $candidate->getTokenCount());
4846
}
4947

5048
/**
@@ -58,7 +56,7 @@ public function testWithDifferentFinishReasons(FinishReasonEnum $finishReason):
5856
{
5957
$message = new ModelMessage([new MessagePart('Response')]);
6058

61-
$candidate = new Candidate($message, $finishReason, 10);
59+
$candidate = new Candidate($message, $finishReason);
6260

6361
$this->assertEquals($finishReason, $candidate->getFinishReason());
6462
}
@@ -103,13 +101,11 @@ public function testWithComplexMessage(): void
103101

104102
$candidate = new Candidate(
105103
$message,
106-
FinishReasonEnum::toolCalls(),
107-
150
104+
FinishReasonEnum::toolCalls()
108105
);
109106

110107
$this->assertCount(6, $candidate->getMessage()->getParts());
111108
$this->assertTrue($candidate->getFinishReason()->isToolCalls());
112-
$this->assertEquals(150, $candidate->getTokenCount());
113109
}
114110

115111
/**
@@ -134,7 +130,6 @@ public function testWithMessageContainingFiles(): void
134130
$candidate = new Candidate(
135131
$message,
136132
FinishReasonEnum::stop(),
137-
85
138133
);
139134

140135
$parts = $candidate->getMessage()->getParts();
@@ -143,42 +138,6 @@ public function testWithMessageContainingFiles(): void
143138
$this->assertEquals('The image shows a flowchart of the process.', $parts[2]->getText());
144139
}
145140

146-
/**
147-
* Tests candidate with different token counts.
148-
*
149-
* @dataProvider tokenCountProvider
150-
* @param int $tokenCount
151-
* @return void
152-
*/
153-
public function testWithDifferentTokenCounts(int $tokenCount): void
154-
{
155-
$message = new ModelMessage([new MessagePart('Response')]);
156-
157-
$candidate = new Candidate(
158-
$message,
159-
FinishReasonEnum::stop(),
160-
$tokenCount
161-
);
162-
163-
$this->assertEquals($tokenCount, $candidate->getTokenCount());
164-
}
165-
166-
/**
167-
* Provides different token counts.
168-
*
169-
* @return array
170-
*/
171-
public function tokenCountProvider(): array
172-
{
173-
return [
174-
'zero' => [0],
175-
'small' => [10],
176-
'medium' => [500],
177-
'large' => [4000],
178-
'very_large' => [100000],
179-
];
180-
}
181-
182141
/**
183142
* Tests candidate rejects non-model message.
184143
*
@@ -195,8 +154,7 @@ public function testRejectsNonModelMessage(): void
195154

196155
new Candidate(
197156
$userMessage,
198-
FinishReasonEnum::stop(),
199-
10
157+
FinishReasonEnum::stop()
200158
);
201159
}
202160

@@ -217,8 +175,7 @@ public function testRejectsMessageWithDifferentRole(): void
217175

218176
new Candidate(
219177
$message,
220-
FinishReasonEnum::stop(),
221-
10
178+
FinishReasonEnum::stop()
222179
);
223180
}
224181

@@ -238,7 +195,6 @@ public function testJsonSchema(): void
238195
$this->assertArrayHasKey('properties', $schema);
239196
$this->assertArrayHasKey(Candidate::KEY_MESSAGE, $schema['properties']);
240197
$this->assertArrayHasKey(Candidate::KEY_FINISH_REASON, $schema['properties']);
241-
$this->assertArrayHasKey(Candidate::KEY_TOKEN_COUNT, $schema['properties']);
242198

243199
// Check finishReason property
244200
$finishReasonSchema = $schema['properties'][Candidate::KEY_FINISH_REASON];
@@ -250,16 +206,9 @@ public function testJsonSchema(): void
250206
$this->assertContains('tool_calls', $finishReasonSchema['enum']);
251207
$this->assertContains('error', $finishReasonSchema['enum']);
252208

253-
// Check tokenCount property
254-
$tokenCountSchema = $schema['properties'][Candidate::KEY_TOKEN_COUNT];
255-
$this->assertEquals('integer', $tokenCountSchema['type']);
256-
257209
// Check required fields
258210
$this->assertArrayHasKey('required', $schema);
259-
$this->assertEquals(
260-
[Candidate::KEY_MESSAGE, Candidate::KEY_FINISH_REASON, Candidate::KEY_TOKEN_COUNT],
261-
$schema['required']
262-
);
211+
$this->assertEquals([Candidate::KEY_MESSAGE, Candidate::KEY_FINISH_REASON], $schema['required']);
263212
}
264213

265214
/**
@@ -273,12 +222,10 @@ public function testWithEmptyMessageParts(): void
273222

274223
$candidate = new Candidate(
275224
$message,
276-
FinishReasonEnum::stop(),
277-
0
225+
FinishReasonEnum::stop()
278226
);
279227

280228
$this->assertCount(0, $candidate->getMessage()->getParts());
281-
$this->assertEquals(0, $candidate->getTokenCount());
282229
}
283230

284231
/**
@@ -294,12 +241,10 @@ public function testWithMaxLengthFinishReason(): void
294241

295242
$candidate = new Candidate(
296243
$message,
297-
FinishReasonEnum::length(),
298-
4096
244+
FinishReasonEnum::length()
299245
);
300246

301247
$this->assertTrue($candidate->getFinishReason()->isLength());
302-
$this->assertEquals(4096, $candidate->getTokenCount());
303248
}
304249

305250
/**
@@ -315,8 +260,7 @@ public function testWithContentFilterFinishReason(): void
315260

316261
$candidate = new Candidate(
317262
$message,
318-
FinishReasonEnum::contentFilter(),
319-
8
263+
FinishReasonEnum::contentFilter()
320264
);
321265

322266
$this->assertTrue($candidate->getFinishReason()->isContentFilter());
@@ -335,8 +279,7 @@ public function testWithErrorFinishReason(): void
335279

336280
$candidate = new Candidate(
337281
$message,
338-
FinishReasonEnum::error(),
339-
9
282+
FinishReasonEnum::error()
340283
);
341284

342285
$this->assertTrue($candidate->getFinishReason()->isError());
@@ -356,19 +299,14 @@ public function testToArray(): void
356299

357300
$candidate = new Candidate(
358301
$message,
359-
FinishReasonEnum::stop(),
360-
45
302+
FinishReasonEnum::stop()
361303
);
362304

363305
$json = $this->assertToArrayReturnsArray($candidate);
364306

365-
$this->assertArrayHasKeys(
366-
$json,
367-
[Candidate::KEY_MESSAGE, Candidate::KEY_FINISH_REASON, Candidate::KEY_TOKEN_COUNT]
368-
);
307+
$this->assertArrayHasKeys($json, [Candidate::KEY_MESSAGE, Candidate::KEY_FINISH_REASON]);
369308
$this->assertIsArray($json[Candidate::KEY_MESSAGE]);
370309
$this->assertEquals(FinishReasonEnum::stop()->value, $json[Candidate::KEY_FINISH_REASON]);
371-
$this->assertEquals(45, $json[Candidate::KEY_TOKEN_COUNT]);
372310
}
373311

374312
/**
@@ -393,14 +331,12 @@ public function testFromArray(): void
393331
]
394332
],
395333
Candidate::KEY_FINISH_REASON => FinishReasonEnum::stop()->value,
396-
Candidate::KEY_TOKEN_COUNT => 75
397334
];
398335

399336
$candidate = Candidate::fromArray($json);
400337

401338
$this->assertInstanceOf(Candidate::class, $candidate);
402339
$this->assertEquals(FinishReasonEnum::stop(), $candidate->getFinishReason());
403-
$this->assertEquals(75, $candidate->getTokenCount());
404340
$this->assertCount(2, $candidate->getMessage()->getParts());
405341
$this->assertEquals('Response text 1', $candidate->getMessage()->getParts()[0]->getText());
406342
$this->assertEquals('Response text 2', $candidate->getMessage()->getParts()[1]->getText());
@@ -419,12 +355,10 @@ public function testArrayRoundTrip(): void
419355
new MessagePart('Generated response'),
420356
new MessagePart(new FunctionCall('call_123', 'search', ['q' => 'test']))
421357
]),
422-
FinishReasonEnum::toolCalls(),
423-
120
358+
FinishReasonEnum::toolCalls()
424359
),
425360
function ($original, $restored) {
426361
$this->assertEquals($original->getFinishReason()->value, $restored->getFinishReason()->value);
427-
$this->assertEquals($original->getTokenCount(), $restored->getTokenCount());
428362
$this->assertCount(
429363
count($original->getMessage()->getParts()),
430364
$restored->getMessage()->getParts()
@@ -450,8 +384,7 @@ public function testImplementsWithArrayTransformationInterface(): void
450384
{
451385
$candidate = new Candidate(
452386
new ModelMessage([new MessagePart('test')]),
453-
FinishReasonEnum::stop(),
454-
10
387+
FinishReasonEnum::stop()
455388
);
456389
$this->assertImplementsArrayTransformation($candidate);
457390
}

tests/unit/Results/DTO/GenerativeAiResultTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,6 @@ public function testFromArray(): void
673673
]
674674
],
675675
Candidate::KEY_FINISH_REASON => FinishReasonEnum::stop()->value,
676-
Candidate::KEY_TOKEN_COUNT => 20
677676
]
678677
],
679678
GenerativeAiResult::KEY_TOKEN_USAGE => [

0 commit comments

Comments
 (0)