Skip to content

Commit 017a902

Browse files
committed
Moved hub.* to root
1 parent c7e695d commit 017a902

File tree

6 files changed

+61
-7
lines changed

6 files changed

+61
-7
lines changed

docs/hub/index.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ composer install cognesy/instructor-php
1212

1313
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.
1414

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

1819

1920

@@ -23,6 +24,7 @@ We welcome contributions to the instructor hub, if you have a tutorial or exampl
2324
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.
2425

2526

27+
2628
### List Cookbooks
2729

2830
Run `./hub.sh list` you can see all the available tutorials and examples.

examples/Collections/run.php

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Collections
2+
3+
4+
```php
5+
<?php
6+
$loader = require 'vendor/autoload.php';
7+
$loader->add('Cognesy\\Instructor\\', __DIR__.'../../src/');
8+
9+
use Cognesy\Instructor\Instructor;
10+
use Cognesy\Instructor\Utils\Collection;
11+
12+
class Fact
13+
{
14+
public string $key;
15+
public string $value;
16+
}
17+
18+
$text = <<<TEXT
19+
Jason is 25 years old. He is a programmer. He has a car. He lives
20+
in a small house in Alamo. He likes to play guitar.
21+
TEXT;
22+
23+
$list = (new Instructor)->respond(
24+
messages: [['role' => 'user', 'content' => $text]],
25+
responseModel: Collection::of(Fact::class)
26+
);
27+
28+
dump($list);
29+
?>
30+
```

examples/PartialUpdates/run.php

+27-5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,29 @@ class UserDetail
3030
public array $hobbies;
3131
}
3232

33+
// This function will be called every time a new token is received
34+
function partialUpdate($partial) {
35+
// Clear the screen and move the cursor to the top
36+
echo chr(27).chr(91).'H'.chr(27).chr(91).'J';
37+
38+
// Print explanation
39+
echo "Waiting 250ms on every token received to make changes easier to observe...\n";
40+
41+
// Display the partial object
42+
dump($partial);
43+
44+
// Wait a bit before clearing the screen to make partial changes slower.
45+
// Don't use this in your application :)
46+
usleep(250000);
47+
}
48+
?>
49+
```
50+
Now we can use this data model to extract arbitrary properties from a text message. As the
51+
tokens are streamed from LLM API, the `partialUpdate` function will be called with partially
52+
updated object of type `UserDetail` that you can use, usually to update the UI.
53+
54+
```php
55+
<?php
3356
$text = <<<TEXT
3457
Jason is 25 years old, he is an engineer and tech lead. He lives in
3558
San Francisco. He likes to play soccer and climb mountains.
@@ -40,11 +63,10 @@ class UserDetail
4063
responseModel: UserDetail::class,
4164
)->onPartialUpdate(partialUpdate(...))->get();
4265

43-
function partialUpdate($partial) {
44-
echo chr(27).chr(91).'H'.chr(27).chr(91).'J';
45-
dump($partial);
46-
usleep(100000);
47-
}
66+
echo "All tokens received, fully completed object available in `\$user` variable.\n";
67+
echo '$user = '."\n";
68+
dump($user);
69+
4870

4971
assert($user->roles[0]->title == 'engineer');
5072
assert($user->roles[1]->title == 'tech lead');

examples/hub.bat hub.bat

File renamed without changes.

examples/hub.php hub.php

File renamed without changes.

examples/hub.sh hub.sh

File renamed without changes.

0 commit comments

Comments
 (0)