Skip to content

Commit

Permalink
Fixes in examples
Browse files Browse the repository at this point in the history
  • Loading branch information
ddebowczyk committed Mar 17, 2024
1 parent 9c64147 commit 8fd02ac
Show file tree
Hide file tree
Showing 23 changed files with 107 additions and 68 deletions.
5 changes: 2 additions & 3 deletions examples/ArbitraryProperties/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class UserDetail
Now we can use this data model to extract arbitrary properties from a text message
in a form that is easier for future processing.


```php
<?php
$text = <<<TEXT
Expand All @@ -41,10 +40,10 @@ class UserDetail
responseModel: UserDetail::class
);

dump($user);

assert($user->age === 25);
assert($user->name === "Jason");
assert(!empty($user->properties));

dump($user);
?>
```
3 changes: 2 additions & 1 deletion examples/ArbitraryPropertiesConsistency/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class UserDetails
responseModel: UserDetails::class
);

assert(!empty($list->users));
dump($list);

assert(!empty($list->users));
?>
```
2 changes: 2 additions & 0 deletions examples/ChainOfThought/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,7 @@ class Employee {
);

dump($employee);

assert($employee->yearOfEmployment === 2014);
?>
```
4 changes: 3 additions & 1 deletion examples/Classification/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ function classify(string $data) : SinglePrediction {
<?php
// Test single-label classification
$prediction = classify("Hello there I'm a Nigerian prince and I want to give you money");
assert($prediction->classLabel == Label::SPAM);

dump($prediction);

assert($prediction->classLabel == Label::SPAM);
?>
```
3 changes: 2 additions & 1 deletion examples/ClassificationMulticlass/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ function multi_classify(string $data) : TicketLabels {
$ticket = "My account is locked and I can't access my billing info.";
$prediction = multi_classify($ticket);

dump($prediction);

assert(in_array(Label::TECH_ISSUE, $prediction->labels));
assert(in_array(Label::BILLING, $prediction->labels));
dump($prediction);
?>
```
4 changes: 2 additions & 2 deletions examples/CustomValidator/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public function validateName(ExecutionContextInterface $context, mixed $payload)
maxRetries: 2
);

assert($user->name === "JASON");

dump($user);

assert($user->name === "JASON");
?>
```

2 changes: 2 additions & 0 deletions examples/EntityRelationships/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,7 @@ class UserRelationships
);

dump($relationships);

assert(!empty($relationships->users));
?>
```
14 changes: 9 additions & 5 deletions examples/HandlingErrors/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@

class UserDetail
{
public int $age;
public string $name;
public ?string $role = null;
public int $age;
}

class MaybeUser
{
public ?UserDetail $result = null;
public ?UserDetail $user = null;
public bool $noUserData = false;
/** If no user data, provide reason */
public ?string $errorMessage = '';
public bool $error = false;

public function get(): ?UserDetail
{
return $this->error ? null : $this->result;
return $this->noUserData ? null : $this->user;
}
}

Expand All @@ -36,6 +36,10 @@ public function get(): ?UserDetail
);

dump($user);

assert($user->noUserData);
assert(!empty($user->errorMessage));
assert($user->get() === null);
?>
```

25 changes: 11 additions & 14 deletions examples/LimitingLengthOfLists/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function validate() : array
return [];
}
return [[
'message' => "Number of properties must not more than 2.",
'message' => "Number of properties must be not more than 2.",
'path' => 'properties',
'value' => $this->name
]];
Expand All @@ -50,19 +50,16 @@ public function validate() : array
a small house in Alamo. He likes to play guitar.
TEXT;

try {
$user = (new Instructor)->respond(
messages: [['role' => 'user', 'content' => $text]],
responseModel: UserDetail::class,
maxRetries: 0 // change to >0 to reattempt generation in case of validation error
);
$user = (new Instructor)->respond(
messages: [['role' => 'user', 'content' => $text]],
responseModel: UserDetail::class,
maxRetries: 1 // change to 0 to see validation error
);

assert($user->age === 25);
assert($user->name === "Jason");
assert(count($user->properties) < 3);
dump($user);
} catch (\Exception $e) {
dump("Max retries exceeded\nMessage: {$e->getMessage()}");
}
dump($user);

assert($user->age === 25);
assert($user->name === "Jason");
assert(count($user->properties) < 3);
?>
```
7 changes: 4 additions & 3 deletions examples/OptionalFields/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,23 @@

class UserRole
{
public ?string $title = 'unknown';
public string $title;
}

class UserDetail
{
public int $age;
public string $name;
public UserRole $role;
public ?UserRole $role;
}

$user = (new Instructor)->respond(
messages: [["role" => "user", "content" => "Jason is 25 years old."]],
responseModel: UserDetail::class,
);

assert($user->role == null);
dump($user);

assert(!isset($user->role));
?>
```
7 changes: 2 additions & 5 deletions examples/PartialUpdates/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,9 @@ function partialUpdate($partial) {
echo '$user = '."\n";
dump($user);


assert($user->roles[0]->title == 'engineer');
assert($user->roles[1]->title == 'tech lead');
assert(!empty($user->roles));
assert(!empty($user->hobbies));
assert($user->location == 'San Francisco');
assert($user->hobbies[0] == 'soccer');
assert($user->hobbies[1] == 'climb mountains');
assert($user->age == 25);
assert($user->name == 'Jason');
?>
Expand Down
7 changes: 6 additions & 1 deletion examples/RestatingInstructions/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class UserRole
public string $instructions;
/** Role description */
public string $description;
/* Most likely job title */
/* Guess job title */
public string $title;
}

Expand All @@ -47,6 +47,11 @@ class UserDetail
messages: [["role" => "user", "content" => $text]],
responseModel: UserDetail::class,
);

dump($user);

assert($user->name === "Jason");
assert($user->age === 28);
assert(!empty($user->role->title));
?>
```
5 changes: 5 additions & 0 deletions examples/RewritingInstructions/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ class UserDetail
messages: [["role" => "user", "content" => $text]],
responseModel: UserDetail::class,
);

dump($user);

assert($user->name === "Jason");
assert($user->age === 28);
assert(!empty($user->role->title));
?>
```

6 changes: 4 additions & 2 deletions examples/SearchCriteria/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,14 @@ function segment(string $data) : Search {
);
}

foreach (segment("Search for a picture of a cat and a video of a dog")->queries as $query) {
$search = segment("Find a picture of a cat and a video of a dog");
foreach ($search->queries as $query) {
$query->execute();
// dump($query);
}
// Results:
// Searching with query `picture of a cat` using `image`
// Searching with query `video of a dog` using `video`

assert(count($search->queries) === 2);
?>
```
3 changes: 2 additions & 1 deletion examples/SelfCorrection/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ class UserDetails
maxRetries: 2
);

assert($user->email === "jason@gmail.com");
dump($user);

assert($user->email === "jason@gmail.com");
?>
```
7 changes: 3 additions & 4 deletions examples/Sequences/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,13 @@ class Person
->request(
messages: [['role' => 'user', 'content' => $text]],
responseModel: Sequence::of(Person::class),
maxRetries: 2,
options: ['stream' => true]
)
->onEvent(SequenceUpdated::class, fn($event) => dump($event->items->last()))
//->onEvent(PartialResponseGenerated::class, fn($event) => dump($event->partialResponse))
->onSequenceUpdate(fn($sequence) => dump($sequence->last()))
->get();

dump(count($list));
//dump($list);

assert(count($list) === 4);
?>
```
7 changes: 4 additions & 3 deletions examples/TimeRange/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class TimeRange {

class UserDetail
{
public int $name;
public string $name;
/** Time range during which the user is working. */
public TimeRange $workTime;
/** Time range reserved for leisure activities. */
Expand All @@ -32,11 +32,12 @@ class UserDetail
maxRetries: 2
);

assert($user->name === "Jason");
dump($user);

assert($user->name == "Jason");
assert($user->workTime->startTime === 9);
assert($user->workTime->endTime === 14);
assert($user->leisureTime->startTime === 17);
assert($user->leisureTime->endTime === 19);
dump($user);
?>
```
5 changes: 3 additions & 2 deletions examples/TimeRangeWithCoT/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ class TimeRange
maxRetries: 2
);

assert($timeRange->startTime === 9);
assert($timeRange->endTime === 17);
dump($timeRange);

assert($timeRange->startTime === 9);
assert($timeRange->endTime === 15);
?>
```
4 changes: 2 additions & 2 deletions examples/ValidationMixin/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public function validate() : array {
maxRetries: 2
);

assert($user->name === "JASON");

dump($user);

assert($user->name === "JASON");
?>
```
15 changes: 7 additions & 8 deletions examples/Wiretap/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
$loader->add('Cognesy\\Instructor\\', __DIR__ . '../../src/');

use Cognesy\Instructor\Instructor;
use Symfony\Component\Validator\Constraints as Assert;

enum Role : string {
case CEO = 'ceo';
Expand All @@ -36,20 +35,20 @@ class UserDetail
{
public string $name;
public Role $role;
#[Assert\Positive]
public int $age;
}

$user = (new Instructor)
->wiretap(fn($event) => $event->print())
->respond(
messages: [["role" => "user", "content" => "Contact our CTO, Jason is -28 years old -- Tom"]],
->request(
messages: [["role" => "user", "content" => "Contact our CTO, Jason is 28 years old -- Tom"]],
responseModel: UserDetail::class,
maxRetries: 2,
options: ['stream' => true]
)
;
->wiretap(fn($event) => $event->print())
->get();

assert($user->role == null);
assert($user->name == "Jason");
assert($user->role == Role::CTO);
assert($user->age == 28);
?>
```
4 changes: 2 additions & 2 deletions src/Events/RequestHandler/SequenceUpdated.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
class SequenceUpdated extends Event
{
public function __construct(
public Sequenceable $items
public Sequenceable $sequence
) {
parent::__construct();
}

public function __toString() : string {
return json_encode($this->items);
return json_encode($this->sequence);
}
}
Loading

0 comments on commit 8fd02ac

Please sign in to comment.