Skip to content

Commit bd9a8cd

Browse files
committed
tweaks
1 parent a89d465 commit bd9a8cd

File tree

3 files changed

+16
-32
lines changed

3 files changed

+16
-32
lines changed

src/LLM/AbstractLLM.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Cortex\ModelInfo\Data\ModelInfo;
2424
use Cortex\LLM\Data\ChatStreamResult;
2525
use Cortex\Exceptions\PipelineException;
26+
use Cortex\LLM\Data\ChatGenerationChunk;
2627
use Cortex\ModelInfo\Enums\ModelFeature;
2728
use Cortex\JsonSchema\Types\ObjectSchema;
2829
use Cortex\LLM\Data\Messages\UserMessage;
@@ -409,18 +410,19 @@ public function shouldParseOutput(bool $shouldParseOutput = true): static
409410
return $this;
410411
}
411412

412-
protected function applyOutputParserIfApplicable(ChatGeneration $generation): ChatGeneration
413-
{
413+
protected function applyOutputParserIfApplicable(
414+
ChatGeneration|ChatGenerationChunk $generationOrChunk,
415+
): ChatGeneration|ChatGenerationChunk {
414416
if ($this->shouldParseOutput && $this->outputParser !== null) {
415417
try {
416-
$parsedOutput = $this->outputParser->parse($generation);
417-
$generation = $generation->cloneWithParsedOutput($parsedOutput);
418+
$parsedOutput = $this->outputParser->parse($generationOrChunk);
419+
$generationOrChunk = $generationOrChunk->cloneWithParsedOutput($parsedOutput);
418420
} catch (OutputParserException $e) {
419421
$this->outputParserError = $e->getMessage();
420422
}
421423
}
422424

423-
return $generation;
425+
return $generationOrChunk;
424426
}
425427

426428
/**

src/LLM/Drivers/AnthropicChat.php

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
use Cortex\LLM\Data\Messages\ToolMessage;
3636
use Cortex\ModelInfo\Enums\ModelProvider;
3737
use Cortex\LLM\Data\Messages\SystemMessage;
38-
use Cortex\Exceptions\OutputParserException;
3938
use Cortex\Tasks\Enums\StructuredOutputMode;
4039
use Cortex\LLM\Data\Messages\AssistantMessage;
4140
use Cortex\LLM\Data\Messages\MessageCollection;
@@ -273,9 +272,7 @@ protected function mapStreamResponse(StreamResponse $response): ChatStreamResult
273272
);
274273
}
275274

276-
$isFinal = $finishReason !== null;
277-
278-
$generationChunk = new ChatGenerationChunk(
275+
$chunk = new ChatGenerationChunk(
279276
id: $messageId ?? 'unknown',
280277
message: new AssistantMessage(
281278
content: $chunkDelta,
@@ -293,25 +290,18 @@ protected function mapStreamResponse(StreamResponse $response): ChatStreamResult
293290
finishReason: $finishReason,
294291
usage: $usage,
295292
contentSoFar: $contentSoFar,
296-
isFinal: $isFinal,
293+
isFinal: $finishReason !== null,
297294
);
298295

299-
if ($this->outputParser !== null) {
300-
try {
301-
$parsedOutput = $this->outputParser->parse($generationChunk);
302-
$generationChunk = $generationChunk->cloneWithParsedOutput($parsedOutput);
303-
} catch (OutputParserException $e) {
304-
$this->outputParserError = $e->getMessage();
305-
}
306-
}
296+
$chunk = $this->applyOutputParserIfApplicable($chunk);
307297

308298
$this->dispatchEvent(
309-
$generationChunk->isFinal
310-
? new ChatModelEnd($generationChunk)
311-
: new ChatModelStream($generationChunk),
299+
$chunk->isFinal
300+
? new ChatModelEnd($chunk)
301+
: new ChatModelStream($chunk),
312302
);
313303

314-
yield $generationChunk;
304+
yield $chunk;
315305
}
316306
});
317307
}

src/LLM/Drivers/OpenAIChat.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,6 @@ protected function mapStreamResponse(StreamResponse $response): ChatStreamResult
213213
}
214214

215215
$finishReason = static::mapFinishReason($choice->finishReason ?? null);
216-
$isFinal = $finishReason !== null; // && $usage !== null;
217216

218217
$chunk = new ChatGenerationChunk(
219218
id: $chunk->id,
@@ -233,17 +232,10 @@ protected function mapStreamResponse(StreamResponse $response): ChatStreamResult
233232
finishReason: $finishReason,
234233
usage: $usage,
235234
contentSoFar: $contentSoFar,
236-
isFinal: $isFinal,
235+
isFinal: $finishReason !== null,
237236
);
238237

239-
if ($this->outputParser !== null) {
240-
try {
241-
$parsedOutput = $this->outputParser->parse($chunk);
242-
$chunk = $chunk->cloneWithParsedOutput($parsedOutput);
243-
} catch (OutputParserException $e) {
244-
$this->outputParserError = $e->getMessage();
245-
}
246-
}
238+
$chunk = $this->applyOutputParserIfApplicable($chunk);
247239

248240
$this->dispatchEvent(
249241
$chunk->isFinal

0 commit comments

Comments
 (0)