Skip to content

Commit

Permalink
Moved hub.* to root
Browse files Browse the repository at this point in the history
  • Loading branch information
ddebowczyk committed Mar 16, 2024
1 parent c7e695d commit 017a902
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 7 deletions.
6 changes: 4 additions & 2 deletions docs/hub/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ composer install cognesy/instructor-php

We welcome contributions to the instructor hub, if you have a tutorial or example you'd like to add, please open a pull request in `docs/hub` and we'll review it.

1. The code must be in a single file
2. Make sure that the code is tested.
1. The code must be in a single .php file.
2. Please include documentation in the file - check existing examples for the format.
3. Make sure that the code is tested.



Expand All @@ -23,6 +24,7 @@ We welcome contributions to the instructor hub, if you have a tutorial or exampl
Instructor hub comes with a command line interface (CLI) that allows you to view and interact with the tutorials and examples and allows you to pull in the code you need to get started with the API.



### List Cookbooks

Run `./hub.sh list` you can see all the available tutorials and examples.
Expand Down
30 changes: 30 additions & 0 deletions examples/Collections/run.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Collections


```php
<?php
$loader = require 'vendor/autoload.php';
$loader->add('Cognesy\\Instructor\\', __DIR__.'../../src/');

use Cognesy\Instructor\Instructor;
use Cognesy\Instructor\Utils\Collection;

class Fact
{
public string $key;
public string $value;
}

$text = <<<TEXT
Jason is 25 years old. He is a programmer. He has a car. He lives
in a small house in Alamo. He likes to play guitar.
TEXT;

$list = (new Instructor)->respond(
messages: [['role' => 'user', 'content' => $text]],
responseModel: Collection::of(Fact::class)
);

dump($list);
?>
```
32 changes: 27 additions & 5 deletions examples/PartialUpdates/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,29 @@ class UserDetail
public array $hobbies;
}

// This function will be called every time a new token is received
function partialUpdate($partial) {
// Clear the screen and move the cursor to the top
echo chr(27).chr(91).'H'.chr(27).chr(91).'J';

// Print explanation
echo "Waiting 250ms on every token received to make changes easier to observe...\n";

// Display the partial object
dump($partial);

// Wait a bit before clearing the screen to make partial changes slower.
// Don't use this in your application :)
usleep(250000);
}
?>
```
Now we can use this data model to extract arbitrary properties from a text message. As the
tokens are streamed from LLM API, the `partialUpdate` function will be called with partially
updated object of type `UserDetail` that you can use, usually to update the UI.

```php
<?php
$text = <<<TEXT
Jason is 25 years old, he is an engineer and tech lead. He lives in
San Francisco. He likes to play soccer and climb mountains.
Expand All @@ -40,11 +63,10 @@ class UserDetail
responseModel: UserDetail::class,
)->onPartialUpdate(partialUpdate(...))->get();

function partialUpdate($partial) {
echo chr(27).chr(91).'H'.chr(27).chr(91).'J';
dump($partial);
usleep(100000);
}
echo "All tokens received, fully completed object available in `\$user` variable.\n";
echo '$user = '."\n";
dump($user);


assert($user->roles[0]->title == 'engineer');
assert($user->roles[1]->title == 'tech lead');
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 017a902

Please sign in to comment.