Skip to content

Commit

Permalink
ToolUse - cont.
Browse files Browse the repository at this point in the history
  • Loading branch information
ddebowczyk committed Nov 25, 2024
1 parent c23d657 commit 45a433e
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 13 deletions.
1 change: 1 addition & 0 deletions examples/A05_Extras/LLMToolUse/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ function subtract_numbers(int $a, int $b) : int {
$toolUse = (new ToolUse)
->withDriver($driver)
->withDefaultContinuationCriteria()
->withDefaultProcessors()
->withMessages('Add 2455 and 3558 then subtract 4344 from the result.')
->withTools([
$addTool,
Expand Down
2 changes: 1 addition & 1 deletion src/Extras/ToolUse/Contracts/CanProcessStep.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@

interface CanProcessStep
{
public function processStep(ToolUseContext $context, ToolUseStep $step): void;
public function processStep(ToolUseStep $step, ToolUseContext $context): ToolUseStep;
}
15 changes: 15 additions & 0 deletions src/Extras/ToolUse/Processors/AccumulateTokenUsage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Cognesy\Instructor\Extras\ToolUse\Processors;

use Cognesy\Instructor\Extras\ToolUse\Contracts\CanProcessStep;
use Cognesy\Instructor\Extras\ToolUse\ToolUseContext;
use Cognesy\Instructor\Extras\ToolUse\ToolUseStep;

class AccumulateTokenUsage implements CanProcessStep
{
public function processStep(ToolUseStep $step, ToolUseContext $context): ToolUseStep {
$context->accumulateUsage($step->usage());
return $step;
}
}
3 changes: 2 additions & 1 deletion src/Extras/ToolUse/Processors/AppendStepMessages.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

class AppendStepMessages implements CanProcessStep
{
public function processStep(ToolUseContext $context, ToolUseStep $step): void {
public function processStep(ToolUseStep $step, ToolUseContext $context): ToolUseStep {
$context->appendMessages($step->messages());
return $step;
}
}
16 changes: 16 additions & 0 deletions src/Extras/ToolUse/Processors/UpdateStep.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Cognesy\Instructor\Extras\ToolUse\Processors;

use Cognesy\Instructor\Extras\ToolUse\Contracts\CanProcessStep;
use Cognesy\Instructor\Extras\ToolUse\ToolUseContext;
use Cognesy\Instructor\Extras\ToolUse\ToolUseStep;

class UpdateStep implements CanProcessStep
{
public function processStep(ToolUseStep $step, ToolUseContext $context): ToolUseStep {
$context->addStep($step);
$context->setCurrentStep($step);
return $step;
}
}
10 changes: 1 addition & 9 deletions src/Extras/ToolUse/ToolUse.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,7 @@ public function context() : ToolUseContext {

public function nextStep() : ToolUseStep {
$step = $this->driver->useTools($this->context);

// process step results
$this->context->accumulateUsage($step->usage());
$this->context->addStep($step);
$this->context->setCurrentStep($step);

$this->context->appendMessages($step->messages());

return $step;
return $this->processStep($step, $this->context);
}

public function finalStep() : ToolUseStep {
Expand Down
9 changes: 7 additions & 2 deletions src/Extras/ToolUse/Traits/ToolUse/HandlesStepProcessors.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Cognesy\Instructor\Extras\ToolUse\Traits\ToolUse;

use Cognesy\Instructor\Extras\ToolUse\Contracts\CanProcessStep;
use Cognesy\Instructor\Extras\ToolUse\Processors\AccumulateTokenUsage;
use Cognesy\Instructor\Extras\ToolUse\Processors\AppendStepMessages;
use Cognesy\Instructor\Extras\ToolUse\Processors\UpdateStep;
use Cognesy\Instructor\Extras\ToolUse\ToolUseContext;
use Cognesy\Instructor\Extras\ToolUse\ToolUseStep;

Expand All @@ -18,14 +20,17 @@ public function withProcessors(CanProcessStep ...$processors): self {

public function withDefaultProcessors(): self {
$this->withProcessors(
new AccumulateTokenUsage(),
new UpdateStep(),
new AppendStepMessages(),
);
return $this;
}

public function processStep(ToolUseContext $context, ToolUseStep $step): void {
public function processStep(ToolUseStep $step, ToolUseContext $context): ToolUseStep {
foreach ($this->processors as $processor) {
$processor->processStep($context, $step);
$step = $processor->processStep($step, $context);
}
return $step;
}
}

0 comments on commit 45a433e

Please sign in to comment.