Skip to content

Commit

Permalink
Config Instructor for Predict module via constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
ddebowczyk committed Jun 6, 2024
1 parent 358dc13 commit 98741d8
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 59 deletions.
9 changes: 7 additions & 2 deletions examples/02_Advanced/LanguagePrograms2/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@
```php
<?php

use Cognesy\Instructor\Contracts\CanProvideSchema;
use Cognesy\Instructor\Extras\Module\Addons\Predict\Predict;
use Cognesy\Instructor\Extras\Module\CallData\Contracts\HasInputOutputData;
use Cognesy\Instructor\Extras\Module\CallData\Traits\AutoSignature;
use Cognesy\Instructor\Extras\Module\Core\Module;
use Cognesy\Instructor\Extras\Module\Signature\Attributes\InputField;
use Cognesy\Instructor\Extras\Module\Signature\Attributes\OutputField;
use Cognesy\Instructor\Extras\Module\CallData\SignatureData;
use Cognesy\Instructor\Instructor;
use Cognesy\Instructor\Schema\Attributes\Description;

$loader = require 'vendor/autoload.php';
$loader->add('Cognesy\\Instructor\\', __DIR__ . '../../src/');
Expand Down Expand Up @@ -63,7 +65,10 @@ class FixedEmail extends SignatureData {
public string $fixedBody;
}

class EmailTranslation extends SignatureData {
// Alternative way to define the module signature data without extending a class
class EmailTranslation implements HasInputOutputData, CanProvideSchema {
use AutoSignature;

#[InputField('subject of email')]
public string $subject;
#[InputField('body of email')]
Expand Down
33 changes: 26 additions & 7 deletions src/Extras/Module/Addons/Predict/Predict.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use BackedEnum;
use Cognesy\Instructor\Data\Example;
use Cognesy\Instructor\Enums\Mode;
use Cognesy\Instructor\Extras\Module\Core\DynamicModule;
use Cognesy\Instructor\Extras\Module\Signature\Contracts\HasSignature;
use Cognesy\Instructor\Extras\Module\Signature\Signature;
Expand All @@ -16,18 +17,30 @@

class Predict extends DynamicModule
{
private Instructor $instructor;
protected Instructor $instructor;

protected string $prompt;
protected string $defaultPrompt = 'Your job is to infer output argument values in input data based on specification: {signature} {description}';
protected $options = [];
protected $model = 'gpt-4o';
protected $mode = Mode::Tools;
protected array $examples = [];
protected int $maxRetries = 3;

protected string $defaultPrompt = 'Your job is to infer output argument values in input data based on specification: {signature} {description}';

protected string|Signature|HasSignature $defaultSignature;

protected ?object $signatureCarrier;

public function __construct(
string|Signature|HasSignature $signature,
Instructor $instructor,
string $model = 'gpt-4o',
int $maxRetries = 3,
array $options = [],
array $examples = [],
string $prompt = '',
Mode $mode = Mode::Tools,
) {
if ($signature instanceof HasSignature) {
$this->signatureCarrier = $signature;
Expand All @@ -37,6 +50,12 @@ public function __construct(
default => $signature,
};
$this->instructor = $instructor;
$this->model = $model;
$this->maxRetries = $maxRetries;
$this->options = $options;
$this->examples = $examples;
$this->prompt = $prompt;
$this->mode = $mode;
}

public function signature(): string|Signature {
Expand Down Expand Up @@ -75,8 +94,12 @@ public function forward(array $args, object $targetObject): mixed {
$response = $this->instructor->respond(
messages: $this->toMessages($input),
responseModel: $targetObject,
model: 'gpt-4o', // TODO: needs to be configurable
model: $this->model,
maxRetries: $this->maxRetries,
options: $this->options,
examples: $this->examples,
prompt: $this->prompt(),
mode: $this->mode,
);

return $response;
Expand All @@ -89,10 +112,6 @@ public function prompt() : string {
return $this->prompt;
}

public function setPrompt(string $prompt) : void {
$this->prompt = $prompt;
}

// INTERNAL ////////////////////////////////////////////////////////////////////////////////////

private function toMessages(string|array|object $input) : array {
Expand Down
10 changes: 2 additions & 8 deletions src/Extras/Module/CallData/CallData.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
<?php
namespace Cognesy\Instructor\Extras\Module\CallData;

use Cognesy\Instructor\Extras\Module\DataAccess\Contracts\DataAccess;
use Cognesy\Instructor\Extras\Module\DataAccess\ObjectDataAccess;
use Cognesy\Instructor\Extras\Module\Signature\Signature;
use Cognesy\Instructor\Extras\Module\CallData\Contracts\HasInputOutputData;

class CallData implements HasInputOutputData
{
use Traits\CallData\HandlesSignature;
use Traits\CallData\ProvidesSignature;
use Traits\CallData\ProvidesSchema;
use Traits\CallData\HandlesInputOutputData;

protected DataAccess $input;
protected DataAccess $output;
protected Signature $signature;
protected string $description = '';

public function __construct(
object $input,
object $output,
Signature $signature,
) {
$this->signature = $signature;
$this->description = $signature->getDescription();
$this->input = new ObjectDataAccess($input, $signature->toInputSchema()->getPropertyNames());
$this->output = new ObjectDataAccess($output, $signature->toOutputSchema()->getPropertyNames());
}
Expand Down
10 changes: 2 additions & 8 deletions src/Extras/Module/CallData/SignatureData.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
namespace Cognesy\Instructor\Extras\Module\CallData;

use Cognesy\Instructor\Contracts\CanProvideSchema;
use Cognesy\Instructor\Extras\Module\DataAccess\Contracts\DataAccess;
use Cognesy\Instructor\Extras\Module\Signature\Signature;
use Cognesy\Instructor\Extras\Module\CallData\Contracts\HasInputOutputData;

/**
Expand All @@ -18,10 +16,6 @@
class SignatureData implements HasInputOutputData, CanProvideSchema
{
use Traits\CallDataClass\HandlesInputOutputData;
use Traits\CallDataClass\HandlesSchema;
use Traits\CallDataClass\HandlesSignature;

private Signature $signature;
private DataAccess $input;
private DataAccess $output;
use Traits\CallDataClass\ProvidesSchema;
use Traits\CallDataClass\ProvidesSignature;
}
10 changes: 10 additions & 0 deletions src/Extras/Module/CallData/Traits/AutoSignature.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Cognesy\Instructor\Extras\Module\CallData\Traits;

trait AutoSignature
{
use CallDataClass\HandlesInputOutputData;
use CallDataClass\ProvidesSchema;
use CallDataClass\ProvidesSignature;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

trait HandlesInputOutputData
{
use ProvidesSchema;

protected DataAccess $input;
protected DataAccess $output;

public function withArgs(mixed ...$args): static {
$this->input->setValues(
InputOutputMapper::fromInputs($args, $this->inputNames())
Expand All @@ -28,14 +33,4 @@ public function toArray(): array {
$this->output->getValues(),
);
}

// CONVENIENCE METHODS ////////////////////////////////////////////////////////////////

public function inputNames(): array {
return $this->signature->toInputSchema()->getPropertyNames();
}

public function outputNames(): array {
return $this->signature->toOutputSchema()->getPropertyNames();
}
}
30 changes: 30 additions & 0 deletions src/Extras/Module/CallData/Traits/CallData/ProvidesSchema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Cognesy\Instructor\Extras\Module\CallData\Traits\CallData;

use Cognesy\Instructor\Schema\Data\Schema\Schema;

trait ProvidesSchema
{
use ProvidesSignature;

public function toSchema(): Schema {
return $this->toOutputSchema();
}

public function toInputSchema(): Schema {
return $this->signature()->toInputSchema();
}

public function toOutputSchema(): Schema {
return $this->signature()->toOutputSchema();
}

public function inputNames(): array {
return $this->toInputSchema()->getPropertyNames();
}

public function outputNames(): array {
return $this->toOutputSchema()->getPropertyNames();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

use Cognesy\Instructor\Extras\Module\Signature\Signature;

trait HandlesSignature
trait ProvidesSignature
{
protected Signature $signature;

public function signature(): Signature {
return $this->signature;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

trait HandlesInputOutputData
{
use ProvidesSchema;

private DataAccess $input;
private DataAccess $output;

static public function fromArgs(...$args): static {
$instance = new static();
$instance->withArgs(...$args);
Expand Down Expand Up @@ -41,14 +46,4 @@ public function toArray(): array {
$this->output()->getValues(),
);
}

// CONVENIENCE METHODS ////////////////////////////////////////////////////////////////

public function inputNames(): array {
return $this->signature()->toInputSchema()->getPropertyNames();
}

public function outputNames(): array {
return $this->signature()->toOutputSchema()->getPropertyNames();
}
}
12 changes: 0 additions & 12 deletions src/Extras/Module/CallData/Traits/CallDataClass/HandlesSchema.php

This file was deleted.

30 changes: 30 additions & 0 deletions src/Extras/Module/CallData/Traits/CallDataClass/ProvidesSchema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Cognesy\Instructor\Extras\Module\CallData\Traits\CallDataClass;

use Cognesy\Instructor\Schema\Data\Schema\Schema;

trait ProvidesSchema
{
use ProvidesSignature;

public function toSchema(): Schema {
return $this->toOutputSchema();
}

public function toInputSchema(): Schema {
return $this->signature()->toInputSchema();
}

public function toOutputSchema(): Schema {
return $this->signature()->toOutputSchema();
}

public function inputNames(): array {
return $this->toInputSchema()->getPropertyNames();
}

public function outputNames(): array {
return $this->toOutputSchema()->getPropertyNames();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
use Cognesy\Instructor\Extras\Module\Signature\Signature;
use Cognesy\Instructor\Extras\Module\Signature\SignatureFactory;

trait HandlesSignature
trait ProvidesSignature
{
private Signature $signature;

public function signature(): Signature {
if (!isset($this->signature)) {
$this->signature = SignatureFactory::fromClassMetadata(static::class);
Expand Down

0 comments on commit 98741d8

Please sign in to comment.