Skip to content

Commit a2f4709

Browse files
committed
Fixing broken build
1 parent 5fea1e0 commit a2f4709

File tree

234 files changed

+14076
-33
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

234 files changed

+14076
-33
lines changed

.gitattributes

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
/docs/ export-ignore
2222
#/evals/ export-ignore # include this one with distribution
2323
#/examples/ export-ignore # include this one with distribution
24-
/experimental/ export-ignore
24+
/src-auxiliary/ export-ignore
25+
/src-experimental/ export-ignore
2526
/notes/ export-ignore
2627
/scratchpad/ export-ignore
2728
/tests/ export-ignore
@@ -52,6 +53,7 @@
5253

5354
# Linguist overrides (optional for GitHub)
5455
*.md linguist-documentation
56+
*.mdx linguist-documentation
5557
*.json linguist-vendored
5658
/vendor/ linguist-vendored
5759

.github/workflows/php.yml

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,20 @@ jobs:
2727
- name: Validate composer.json and composer.lock
2828
run: composer validate --strict
2929

30-
- name: Cache Composer packages
31-
id: composer-cache
32-
uses: actions/cache@v3
33-
with:
34-
path: vendor
35-
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
36-
restore-keys: |
37-
${{ runner.os }}-php-
30+
# - name: Cache Composer packages
31+
# id: composer-cache
32+
# uses: actions/cache@v3
33+
# with:
34+
# path: vendor
35+
# key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
36+
# restore-keys: |
37+
# ${{ runner.os }}-php-
3838

3939
- name: Install dependencies
4040
run: composer update --no-interaction --no-suggest ${{ matrix.composer }}
4141

42+
- name: Regenerate Autoloader
43+
run: composer dump-autoload
44+
4245
- name: Run test suite
4346
run: composer test

bin/sync-ver.sh

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,18 @@ VERSION=${VERSION#v}
1313

1414
# Define packages and their sections based on composer.json
1515
declare -A REQUIRE_PACKAGES=(
16-
["src-utils"]="cognesy/utils"
17-
["src-addons"]="cognesy/instructor-php-addons"
18-
["src-polyglot"]="cognesy/polyglot-php"
16+
["src-utils"]="cognesy/instructor-utils"
17+
["src-addons"]="cognesy/instructor-addons"
18+
["src-polyglot"]="cognesy/instructor-polyglot"
19+
["src-instructor"]="cognesy/instructor-core"
1920
)
2021

2122
declare -A REQUIRE_DEV_PACKAGES=(
22-
["src-aux"]="cognesy/aux"
23-
["src-experimental"]="cognesy/experimental"
24-
["src-hub"]="cognesy/instructor-php-hub"
25-
["src-setup"]="cognesy/instructor-php-setup"
26-
["src/Tell"]="cognesy/tell"
23+
["src-aux"]="cognesy/instructor-aux"
24+
["src-experimental"]="cognesy/instructor-experimental"
25+
["src-hub"]="cognesy/instructor-hub"
26+
["src-setup"]="cognesy/instructor-setup"
27+
["src-tell"]="cognesy/tell-cli"
2728
)
2829

2930
# Update version in each package's composer.json

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
],
2525
"autoload": {
2626
"psr-4": {
27+
"Cognesy\\Utils\\": "src-utils/",
2728
"Cognesy\\Addons\\": "src-addons/",
2829
"Cognesy\\Instructor\\": "src-instructor/",
2930
"Cognesy\\Polyglot\\": "src-polyglot/",
3031
"Cognesy\\Setup\\": "src-setup/",
31-
"Cognesy\\Utils\\": "src-utils/"
3232
},
3333
"files": []
3434
},

docs/release-notes/v0.12.6.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- Continued project files reorganization
2+
- Created README.md, LICENSE.md, composer.json, .gitattributes for subprojects to be ready for future split
3+
- Corrected composer.json, .gitignore and .gitattributes
4+
- Docs directory structure cleanup to work with Mintlify

examples/A01_Basics/Basic/run.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
---
2+
title: 'Basic use'
3+
docname: 'basic_use'
4+
path: ''
5+
---
6+
7+
## Overview
8+
9+
Instructor allows you to use large language models to extract information
10+
from the text (or content of chat messages), while following the structure
11+
you define.
12+
13+
LLM does not 'parse' the text to find and retrieve the information.
14+
Extraction leverages LLM ability to comprehend provided text and infer
15+
the meaning of the information it contains to fill fields of the
16+
response object with values that match the types and semantics of the
17+
class fields.
18+
19+
The simplest way to use the Instructor is to call the `respond` method
20+
on the Instructor instance. This method takes a string (or an array of
21+
strings in the format of OpenAI chat messages) as input and returns a
22+
data extracted from provided text (or chat) using the LLM inference.
23+
24+
Returned object will contain the values of fields extracted from the text.
25+
26+
The format of the extracted data is defined by the response model, which
27+
in this case is a simple PHP class with some public properties.
28+
29+
## Example
30+
31+
```php
32+
<?php
33+
require 'examples/boot.php';
34+
35+
use Cognesy\Instructor\Instructor;
36+
37+
// Step 1: Define a class that represents the structure and semantics
38+
// of the data you want to extract
39+
class User {
40+
public int $age;
41+
public string $name;
42+
}
43+
44+
// Step 2: Get the text (or chat messages) you want to use as context
45+
$text = "Jason is 25 years old and works as an engineer.";
46+
print("Input text:\n");
47+
print($text . "\n\n");
48+
49+
// Step 3: Extract structured data using default language model API (OpenAI)
50+
print("Extracting structured data using LLM...\n\n");
51+
$user = (new Instructor)->respond(
52+
messages: $text,
53+
responseModel: User::class,
54+
);
55+
56+
// Step 4: Now you can use the extracted data in your application
57+
print("Extracted data:\n");
58+
59+
dump($user);
60+
61+
assert(isset($user->name));
62+
assert(isset($user->age));
63+
assert($user->name === 'Jason');
64+
assert($user->age === 25);
65+
?>
66+
```
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
title: 'Basic use via mixin'
3+
docname: 'basic_use_mixin'
4+
---
5+
6+
## Overview
7+
8+
Instructor provides `HandlesSelfInference` trait that you can use to enable
9+
extraction capabilities directly on class via static `infer()` method.
10+
11+
`infer()` method returns an instance of the class with the data extracted
12+
using the Instructor.
13+
14+
`infer()` method has following signature (you can also find it in the
15+
`CanSelfInfer` interface):
16+
17+
```php
18+
static public function infer(
19+
string|array $messages, // (required) The message(s) to infer data from
20+
string $prompt = '', // (optional) The prompt to use for inference
21+
array $examples = [], // (optional) Examples to include in the prompt
22+
string $model = '', // (optional) The model to use for inference (otherwise - use default)
23+
int $maxRetries = 2, // (optional) The number of retries in case of validation failure
24+
array $options = [], // (optional) Additional data to pass to the Instructor or LLM API
25+
Mode $mode = Mode::Tools, // (optional) The mode to use for inference
26+
LLM $llm = null // (optional) LLM instance to use for inference
27+
) : static;
28+
```
29+
30+
## Example
31+
32+
```php
33+
<?php
34+
require 'examples/boot.php';
35+
36+
use Cognesy\Instructor\Extras\Mixin\HandlesSelfInference;
37+
38+
class User {
39+
use HandlesSelfInference;
40+
41+
public int $age;
42+
public string $name;
43+
}
44+
45+
$user = User::infer("Jason is 25 years old and works as an engineer.");
46+
47+
dump($user);
48+
49+
assert(isset($user->name));
50+
assert(isset($user->age));
51+
assert($user->name === 'Jason');
52+
assert($user->age === 25);
53+
?>
54+
```

examples/A01_Basics/Maybe/run.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
---
2+
title: 'Handling errors with `Maybe` helper class'
3+
docname: 'maybe'
4+
---
5+
6+
## Overview
7+
8+
You can create a wrapper class to hold either the result of an operation or an error message.
9+
This allows you to remain within a function call even if an error occurs, facilitating
10+
better error handling without breaking the code flow.
11+
12+
## Example
13+
14+
```php
15+
<?php
16+
require 'examples/boot.php';
17+
18+
use Cognesy\Instructor\Extras\Maybe\Maybe;
19+
use Cognesy\Instructor\Instructor;
20+
use Cognesy\Polyglot\LLM\Enums\Mode;
21+
22+
class User
23+
{
24+
public string $name;
25+
public int $age;
26+
}
27+
28+
29+
$text = 'We have no information about our new developer.';
30+
echo "\nINPUT:\n$text\n";
31+
32+
$maybeUser = (new Instructor)->respond(
33+
messages: [['role' => 'user', 'content' => $text]],
34+
responseModel: Maybe::is(User::class),
35+
model: 'gpt-4o-mini',
36+
mode: Mode::MdJson,
37+
);
38+
39+
echo "\nOUTPUT:\n";
40+
41+
dump($maybeUser->get());
42+
43+
assert($maybeUser->hasValue() === false);
44+
assert(!empty($maybeUser->error()));
45+
assert($maybeUser->get() === null);
46+
47+
$text = "Jason is our new developer, he is 25 years old.";
48+
echo "\nINPUT:\n$text\n";
49+
50+
$maybeUser = (new Instructor)->respond(
51+
messages: [['role' => 'user', 'content' => $text]],
52+
responseModel: Maybe::is(User::class)
53+
);
54+
55+
echo "\nOUTPUT:\n";
56+
57+
dump($maybeUser->get());
58+
59+
assert($maybeUser->hasValue() === true);
60+
assert(empty($maybeUser->error()));
61+
assert($maybeUser->get() != null);
62+
assert($maybeUser->get() instanceof User);
63+
?>
64+
```

examples/A01_Basics/Modes/run.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
---
2+
title: 'Modes'
3+
docname: 'modes'
4+
---
5+
6+
## Overview
7+
8+
Instructor supports several ways to extract data from the response:
9+
10+
- `Mode::Tools` - uses OpenAI-style tool calls to get the language
11+
model to generate JSON following the schema,
12+
- `Mode::JsonSchema` - guarantees output matching JSON Schema via
13+
Context Free Grammar, does not support optional properties,
14+
- `Mode::Json` - JSON mode, response follows provided JSON Schema,
15+
- `Mode::MdJson` - uses prompting to get the language model to
16+
generate JSON following the schema.
17+
18+
Note: not all modes are supported by all models or providers.
19+
20+
Mode can be set via parameter of `Instructor::response()` or `Instructor::request()`
21+
methods.
22+
23+
## Example
24+
25+
```php
26+
<?php
27+
require 'examples/boot.php';
28+
29+
use Cognesy\Instructor\Instructor;
30+
use Cognesy\Polyglot\LLM\Enums\Mode;
31+
32+
class User {
33+
public int $age;
34+
public string $name;
35+
}
36+
37+
$text = "Jason is 25 years old and works as an engineer.";
38+
39+
print("Input text:\n");
40+
print($text . "\n\n");
41+
42+
$instructor = new Instructor;
43+
44+
// CASE 1 - Mode::Tools
45+
print("\n1. Extracting structured data using LLM - Mode::Tools\n");
46+
$user = $instructor->respond(
47+
messages: $text,
48+
responseModel: User::class,
49+
mode: Mode::Tools,
50+
);
51+
check($user);
52+
dump($user);
53+
54+
// CASE 2 - Mode::JsonSchema
55+
print("\n2. Extracting structured data using LLM - Mode::JsonSchema\n");
56+
$user = $instructor->respond(
57+
messages: $text,
58+
responseModel: User::class,
59+
mode: Mode::JsonSchema,
60+
);
61+
check($user);
62+
dump($user);
63+
64+
// CASE 3 - Mode::Json
65+
print("\n3. Extracting structured data using LLM - Mode::Json\n");
66+
$user = $instructor->respond(
67+
messages: $text,
68+
responseModel: User::class,
69+
mode: Mode::Json,
70+
);
71+
check($user);
72+
dump($user);
73+
74+
// CASE 4 - Mode::MdJson
75+
print("\n4. Extracting structured data using LLM - Mode::MdJson\n");
76+
$user = $instructor->respond(
77+
messages: $text,
78+
responseModel: User::class,
79+
mode: Mode::MdJson,
80+
);
81+
check($user);
82+
dump($user);
83+
84+
function check(User $user) {
85+
assert(isset($user->name));
86+
assert(isset($user->age));
87+
assert($user->name === 'Jason');
88+
assert($user->age === 25);
89+
}
90+
?>
91+
```

0 commit comments

Comments
 (0)