-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More examples of using LLM via Inference class
- Loading branch information
1 parent
2c2a88f
commit f6c845b
Showing
3 changed files
with
58 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
--- | ||
title: 'Generating JSON Schema from PHP classes' | ||
docname: 'schema' | ||
--- | ||
|
||
## Overview | ||
|
||
Instructor has a built-in support for generating JSON Schema from | ||
the classes or objects. This is useful as it helps you avoid writing | ||
the JSON Schema manually, which can be error-prone and time-consuming. | ||
|
||
## Example | ||
|
||
```php | ||
<?php | ||
$loader = require 'vendor/autoload.php'; | ||
$loader->add('Cognesy\\Instructor\\', __DIR__ . '../../src/'); | ||
|
||
use Cognesy\Instructor\Enums\Mode;use Cognesy\Instructor\Extras\LLM\Inference; | ||
use Cognesy\Instructor\Schema\Factories\SchemaFactory; | ||
|
||
class City { | ||
public string $name; | ||
public int $population; | ||
public int $founded; | ||
} | ||
|
||
$schema = (new SchemaFactory)->schema(City::class); | ||
|
||
// regular API, allows to customize inference options | ||
$data = (new Inference) | ||
->withConnection('openai') | ||
->create( | ||
messages: [['role' => 'user', 'content' => 'What is capital of France? \ | ||
Respond with JSON data.']], | ||
responseFormat: [ | ||
'type' => 'json_schema', | ||
'description' => 'City data', | ||
'json_schema' => [ | ||
'name' => 'city_data', | ||
'schema' => $schema->toJsonSchema(), | ||
'strict' => true, | ||
], | ||
], | ||
options: ['max_tokens' => 64], | ||
mode: Mode::JsonSchema, | ||
) | ||
->toJson(); | ||
|
||
echo "USER: What is capital of France\n"; | ||
echo "ASSISTANT:\n"; | ||
dump($data); | ||
|
||
?> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters