Skip to content

Commit 153161b

Browse files
committed
wip
1 parent b60b6ce commit 153161b

File tree

13 files changed

+133
-26
lines changed

13 files changed

+133
-26
lines changed

src/Agents/Agent.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ public function __construct(
7373

7474
$this->memory = new ChatMemory(new InMemoryStore($this->prompt->messages->withoutPlaceholders()));
7575
$this->usage = Usage::empty();
76-
$this->llm = Utils::toLLM($llm);
76+
77+
$this->llm = $llm !== null
78+
? Utils::toLLM($llm)
79+
: $this->prompt->metadata?->toLLM() ?? Utils::toLLM($llm);
7780

7881
if ($this->tools !== []) {
7982
$this->llm->withTools($this->tools, $this->toolChoice);

src/LLM/AbstractLLM.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ abstract class AbstractLLM implements LLM
8080

8181
protected bool $ignoreModelFeatures = false;
8282

83+
protected bool $includeRaw = false;
84+
8385
public function __construct(
8486
protected string $model,
8587
protected ModelProvider $modelProvider,
@@ -414,6 +416,16 @@ public function shouldParseOutput(bool $shouldParseOutput = true): static
414416
return $this;
415417
}
416418

419+
/**
420+
* Set whether the raw provider response should be included in the result.
421+
*/
422+
public function includeRaw(bool $includeRaw = true): static
423+
{
424+
$this->includeRaw = $includeRaw;
425+
426+
return $this;
427+
}
428+
417429
protected function applyOutputParserIfApplicable(
418430
ChatGeneration|ChatGenerationChunk $generationOrChunk,
419431
): ChatGeneration|ChatGenerationChunk {

src/LLM/Data/ChatGenerationChunk.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616
readonly class ChatGenerationChunk implements Arrayable
1717
{
18+
/**
19+
* @param array<string, mixed>|null $rawChunk
20+
*/
1821
public function __construct(
1922
public string $id,
2023
public AssistantMessage $message,
@@ -26,6 +29,7 @@ public function __construct(
2629
public bool $isFinal = false,
2730
public mixed $parsedOutput = null,
2831
public ?string $outputParserError = null,
32+
public ?array $rawChunk = null,
2933
) {}
3034

3135
public function cloneWithParsedOutput(mixed $parsedOutput): self
@@ -57,6 +61,7 @@ public function toArray(): array
5761
'parsed_output' => $this->parsedOutput,
5862
'output_parser_error' => $this->outputParserError,
5963
'created_at' => $this->createdAt,
64+
'raw_chunk' => $this->rawChunk,
6065
];
6166
}
6267
}

src/LLM/Data/ChatResult.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
public mixed $parsedOutput;
1515

1616
/**
17-
* @param array<int, mixed> $rawResponse
17+
* @param array<int, mixed>|null $rawResponse
1818
*/
1919
public function __construct(
2020
public ChatGeneration $generation,
2121
public Usage $usage,
22-
public array $rawResponse = [],
22+
public ?array $rawResponse = null,
2323
) {
2424
$this->parsedOutput = $this->generation->parsedOutput;
2525
}
@@ -38,6 +38,7 @@ public function toArray(): array
3838
return [
3939
'generation' => $this->generation,
4040
'usage' => $this->usage,
41+
'raw_response' => $this->rawResponse,
4142
];
4243
}
4344
}

src/LLM/Drivers/OpenAI/Chat/Concerns/MapsResponse.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,15 @@ protected function mapResponse(CreateResponse $response): ChatResult
4747

4848
$generation = $this->applyOutputParserIfApplicable($generation);
4949

50+
/** @var array<string, mixed>|null $rawResponse */
51+
$rawResponse = $this->includeRaw
52+
? $response->toArray()
53+
: null;
54+
5055
return new ChatResult(
5156
$generation,
5257
$usage,
53-
$response->toArray(), // @phpstan-ignore argument.type
58+
$rawResponse, // @phpstan-ignore argument.type
5459
);
5560
}
5661
}

src/LLM/Drivers/OpenAI/Chat/Concerns/MapsStreamResponse.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ protected function mapStreamResponse(StreamResponse $response): ChatStreamResult
155155
finishReason: $finishReason,
156156
usage: $usage,
157157
contentSoFar: $contentSoFar,
158-
isFinal: $isLastContentChunk,
158+
isFinal: $isLastContentChunk && $usage !== null,
159+
rawChunk: $this->includeRaw ? $chunk->toArray() : null,
159160
);
160161

161162
$chatGenerationChunk = $this->applyOutputParserIfApplicable($chatGenerationChunk);

src/LLM/Drivers/OpenAI/Responses/Concerns/MapsResponse.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,10 @@ protected function mapResponse(CreateResponse $response): ChatResult
9494

9595
$generation = $this->applyOutputParserIfApplicable($generation);
9696

97-
/** @var array<string, mixed> $rawResponse */
98-
$rawResponse = $response->toArray();
97+
/** @var array<string, mixed>|null $rawResponse */
98+
$rawResponse = $this->includeRaw
99+
? $response->toArray()
100+
: null;
99101

100102
return new ChatResult(
101103
$generation,

src/LLM/Drivers/OpenAI/Responses/Concerns/MapsStreamResponse.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,11 @@ protected function mapStreamResponse(StreamResponse $response): ChatStreamResult
176176
// Determine chunk type
177177
$chunkType = $this->resolveResponsesChunkType($event, $currentDelta, $contentSoFar, $finishReason);
178178

179+
/** @var array<string, mixed>|null $rawChunk */
180+
$rawChunk = $this->includeRaw
181+
? $streamChunk->toArray()
182+
: null;
183+
179184
$chatGenerationChunk = new ChatGenerationChunk(
180185
id: $responseId ?? 'unknown',
181186
message: new AssistantMessage(
@@ -198,6 +203,7 @@ protected function mapStreamResponse(StreamResponse $response): ChatStreamResult
198203
usage: $responseUsage,
199204
contentSoFar: $contentSoFar,
200205
isFinal: $isFinal,
206+
rawChunk: $rawChunk,
201207
);
202208

203209
$chatGenerationChunk = $this->applyOutputParserIfApplicable($chatGenerationChunk);

src/LLM/Drivers/OpenAI/Responses/Concerns/MapsUsage.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ protected function mapUsage(?CreateResponseUsage $usage): ?Usage
2222
return new Usage(
2323
promptTokens: $usage->inputTokens,
2424
completionTokens: $usage->outputTokens,
25-
cachedTokens: $usage->inputTokensDetails?->cachedTokens,
26-
reasoningTokens: $usage->outputTokensDetails?->reasoningTokens,
25+
cachedTokens: $usage->inputTokensDetails->cachedTokens,
26+
reasoningTokens: $usage->outputTokensDetails->reasoningTokens,
2727
totalTokens: $usage->totalTokens,
2828
inputCost: $this->modelProvider->inputCostForTokens($this->model, $usage->inputTokens),
2929
outputCost: $this->modelProvider->outputCostForTokens($this->model, $usage->outputTokens),

src/Prompts/Builders/Concerns/BuildsPrompts.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public function initialVariables(array $initialVariables): self
4040
}
4141

4242
/**
43+
* Define the metadata for the prompt.
44+
*
4345
* @param array<string, mixed> $parameters
4446
* @param array<int, \Cortex\LLM\Contracts\Tool|\Closure|string> $tools
4547
* @param array<string, mixed> $additional

0 commit comments

Comments
 (0)