Skip to content

Commit 5286c54

Browse files
committed
improvements
1 parent 1a474d8 commit 5286c54

File tree

11 files changed

+133
-67
lines changed

11 files changed

+133
-67
lines changed

examples/skills-agent.php

Lines changed: 0 additions & 49 deletions
This file was deleted.

scratchpad.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
]);
4646

4747
// Get a generic agent builder from the prompt
48-
$agentBuilder = $prompt->agentBuilder();
48+
$agentBuilder = $prompt->agent();
4949

5050
$result = $agentBuilder->invoke(input: [
5151
'country' => 'France',
@@ -69,6 +69,10 @@
6969
new UserMessage('What is the capital of {country}?'),
7070
]);
7171

72+
$result = $prompt->agent()->invoke(input: [
73+
'country' => 'France',
74+
]);
75+
7276
// Uses default llm driver
7377
$result = Cortex::llm()->invoke([
7478
new SystemMessage('You are a helpful assistant'),

src/LLM/AbstractLLM.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,16 @@ public function getOutputParserError(): ?string
449449
return $this->outputParserError;
450450
}
451451

452+
public function getStructuredOutputConfig(): ?StructuredOutputConfig
453+
{
454+
return $this->structuredOutputConfig;
455+
}
456+
457+
public function getStructuredOutputMode(): StructuredOutputMode
458+
{
459+
return $this->structuredOutputMode;
460+
}
461+
452462
/**
453463
* @return array<\Cortex\ModelInfo\Enums\ModelFeature>
454464
*/

src/LLM/Contracts/LLM.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Cortex\JsonSchema\Types\ObjectSchema;
1515
use Cortex\ModelInfo\Enums\ModelProvider;
1616
use Cortex\LLM\Enums\StructuredOutputMode;
17+
use Cortex\LLM\Data\StructuredOutputConfig;
1718
use Cortex\LLM\Data\Messages\MessageCollection;
1819

1920
interface LLM extends Pipeable
@@ -164,6 +165,23 @@ public function getModelProvider(): ModelProvider;
164165
*/
165166
public function getModelInfo(): ?ModelInfo;
166167

168+
/**
169+
* Get the parameters for the LLM.
170+
*
171+
* @return array<string, mixed>
172+
*/
173+
public function getParameters(): array;
174+
175+
/**
176+
* Get the structured output config for the LLM.
177+
*/
178+
public function getStructuredOutputConfig(): ?StructuredOutputConfig;
179+
180+
/**
181+
* Get the structured output mode for the LLM.
182+
*/
183+
public function getStructuredOutputMode(): StructuredOutputMode;
184+
167185
/**
168186
* Set whether the raw provider response should be included in the result, if available.
169187
*/

src/LLM/Data/ToolCallCollection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function invokeAsToolMessages(Collection $availableTools, ?RuntimeConfig
3737
if ($matchingTool === null) {
3838
// If we didn't find a matching tool, and there is only one tool and
3939
// one tool call, we will assume it's the correct tool.
40-
if ($availableTools->containsOneItem() && $this->containsOneItem()) {
40+
if ($availableTools->hasSole() && $this->hasSole()) {
4141
/** @var \Cortex\LLM\Contracts\Tool $matchingTool */
4242
$matchingTool = $availableTools->first();
4343
} else {

src/OutputParsers/JsonOutputToolsParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function parse(ChatGeneration|ChatGenerationChunk|string $output): array
2929
throw OutputParserException::failed('Invalid input. Expected a message with tool calls.');
3030
}
3131

32-
if ($this->singleToolCall && $output->message->toolCalls->containsOneItem()) {
32+
if ($this->singleToolCall && $output->message->toolCalls->hasSole()) {
3333
return $output->message->toolCalls->first()->function->arguments;
3434
}
3535

src/Prompts/Builders/ChatPromptBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function messages(MessageCollection|array|string $messages): self
4444
/**
4545
* Convenience method to build a generic agent builder from the chat prompt builder.
4646
*/
47-
public function agentBuilder(): GenericAgentBuilder
47+
public function agent(): GenericAgentBuilder
4848
{
4949
return new GenericAgentBuilder()->withPrompt($this);
5050
}

src/Prompts/Templates/AbstractPromptTemplate.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Closure;
88
use Cortex\Pipeline;
99
use Cortex\Facades\LLM;
10+
use Cortex\Support\Utils;
1011
use Cortex\JsonSchema\Schema;
1112
use Cortex\Contracts\Pipeable;
1213
use Cortex\Pipeline\RuntimeConfig;
@@ -37,7 +38,7 @@ public function handlePipeable(mixed $payload, RuntimeConfig $config, Closure $n
3738

3839
// If there is only one variable, and the input is a string,
3940
// we can assume the user is passing the value directly.
40-
if (is_string($payload) && $variables->containsOneItem()) {
41+
if (is_string($payload) && $variables->hasSole()) {
4142
return $next($this->format([
4243
$variables->first() => $payload,
4344
]), $config);
@@ -79,17 +80,7 @@ public function llm(
7980
throw new PromptException('No LLM provider or metadata provided.');
8081
}
8182

82-
if ($provider instanceof LLMContract) {
83-
$llm = $provider;
84-
} elseif ($provider === null) {
85-
if (is_string($this->metadata?->provider)) {
86-
$llm = LLM::provider($this->metadata->provider);
87-
} else {
88-
$llm = $this->metadata->provider;
89-
}
90-
} else {
91-
$llm = LLM::provider($provider);
92-
}
83+
$llm = Utils::llm($provider ?? $this->metadata?->provider);
9384

9485
if (is_string($model)) {
9586
$llm->withModel($model);
@@ -128,6 +119,13 @@ public function llm(
128119
return $this->pipe($llm);
129120
}
130121

122+
public function withMetadata(PromptMetadata $metadata): self
123+
{
124+
$this->metadata = $metadata;
125+
126+
return $this;
127+
}
128+
131129
public function withCompiler(PromptCompiler $compiler): self
132130
{
133131
$this->compiler = $compiler;

src/Prompts/Templates/ChatPromptTemplate.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public function getInputSchema(): ObjectSchema
108108
/**
109109
* Convenience method to build a generic agent builder from the prompt template.
110110
*/
111-
public function agentBuilder(): GenericAgentBuilder
111+
public function agent(): GenericAgentBuilder
112112
{
113113
return new GenericAgentBuilder()->withPrompt($this);
114114
}

tests/Unit/Prompts/Builders/ChatPromptBuilderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
expect($result->first())->toBeInstanceOf(UserMessage::class);
4545
expect($result->first()->text())->toBe('What is the capital of France?');
4646

47-
$agentBuilder = $prompt->agentBuilder();
47+
$agentBuilder = $prompt->agent();
4848

4949
expect($agentBuilder)->toBeInstanceOf(GenericAgentBuilder::class)
5050
->and($agentBuilder->prompt())->toBe($prompt);

0 commit comments

Comments
 (0)