Skip to content

Commit

Permalink
More examples of using LLM via Inference class
Browse files Browse the repository at this point in the history
  • Loading branch information
ddebowczyk committed Oct 3, 2024
1 parent 2c2a88f commit f6c845b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
55 changes: 55 additions & 0 deletions docs/cookbook/examples/extras/schema.mdx
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);

?>
```
2 changes: 1 addition & 1 deletion docs/mint.json
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@
"cookbook/examples/extras/llm_json_schema",
"cookbook/examples/extras/llm_md_json",
"cookbook/examples/extras/llm_tools",
"cookbook/examples/extras/llm_json_schema",
"cookbook/examples/extras/schema",
"cookbook/examples/extras/transcription_to_tasks",
"cookbook/examples/extras/translate_ui_fields",
"cookbook/examples/extras/web_to_objects"
Expand Down
4 changes: 2 additions & 2 deletions examples/A05_Extras/Schema/run.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: 'Working directly with LLMs and JSON - JSON Schema mode'
docname: 'llm_json_schema'
title: 'Generating JSON Schema from PHP classes'
docname: 'schema'
---

## Overview
Expand Down

0 comments on commit f6c845b

Please sign in to comment.