Skip to content

Commit

Permalink
Docs update
Browse files Browse the repository at this point in the history
  • Loading branch information
ddebowczyk committed Oct 28, 2024
1 parent 0238af1 commit 51e4491
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 17 deletions.
2 changes: 1 addition & 1 deletion docs/cookbook/examples/api_support/cohere.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class User {

// Get Instructor with specified LLM client connection
// See: /config/llm.php to check or change LLM client connection configuration details
$instructor = (new Instructor)->withConnection('cohere1');
$instructor = (new Instructor)->withConnection('cohere2');

$user = $instructor->respond(
messages: "Jason (@jxnlco) is 25 years old and is the admin of this project. He likes playing football and reading books.",
Expand Down
1 change: 1 addition & 0 deletions docs/cookbook/examples/api_support/togetherai.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ $loader->add('Cognesy\\Instructor\\', __DIR__ . '../../src/');

use Cognesy\Instructor\Enums\Mode;
use Cognesy\Instructor\Instructor;
use Cognesy\Instructor\Utils\Debug\Debug;

enum UserType : string {
case Guest = 'guest';
Expand Down
21 changes: 12 additions & 9 deletions docs/cookbook/examples/extras/image_car_damage.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ location of the damage and the type of damage.

Here's the image we're going to extract data from.

![Receipt](/images/car-damage.jpg)
![Car Photo](/images/car-damage.jpg)


## Example
Expand All @@ -27,7 +27,9 @@ Here's the image we're going to extract data from.
$loader = require 'vendor/autoload.php';
$loader->add('Cognesy\\Instructor\\', __DIR__ . '../../src/');

use Cognesy\Instructor\Extras\Image\Image;use Cognesy\Instructor\Features\Schema\Attributes\Description;use Cognesy\Instructor\Instructor;use Cognesy\Instructor\Utils\Str;
use Cognesy\Instructor\Extras\Image\Image;
use Cognesy\Instructor\Features\Schema\Attributes\Description;
use Cognesy\Instructor\Utils\Str;

enum DamageSeverity : string {
case Minor = 'minor';
Expand Down Expand Up @@ -63,13 +65,14 @@ class DamageAssessment {
public string $summary;
}

$assessment = (new Instructor)->respond(
input: Image::fromFile(__DIR__ . '/car-damage.jpg'),
responseModel: DamageAssessment::class,
prompt: 'Identify and assess each car damage location and severity separately.',
model: 'gpt-4o',
options: ['max_tokens' => 4096]
);
$assessment = Image::fromFile(__DIR__ . '/car-damage.jpg')
->toData(
responseModel: DamageAssessment::class,
prompt: 'Identify and assess each car damage location and severity separately.',
connection: 'openai',
model: 'gpt-4o',
options: ['max_tokens' => 4096]
);

dump($assessment);
assert(Str::contains($assessment->make, 'Toyota', false));
Expand Down
4 changes: 3 additions & 1 deletion docs/cookbook/examples/extras/image_to_data_gemini.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ Here's the image we're going to extract data from.
$loader = require 'vendor/autoload.php';
$loader->add('Cognesy\\Instructor\\', __DIR__ . '../../src/');

use Cognesy\Instructor\Enums\Mode;use Cognesy\Instructor\Extras\Image\Image;use Cognesy\Instructor\Instructor;
use Cognesy\Instructor\Enums\Mode;
use Cognesy\Instructor\Extras\Image\Image;
use Cognesy\Instructor\Instructor;

class Vendor {
public ?string $name = '';
Expand Down
50 changes: 50 additions & 0 deletions docs/cookbook/examples/extras/prompt_text.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
title: 'Prompts'
docname: 'prompt_text'
---

## Overview

`Prompt` class in Instructor PHP provides a way to define and use
prompt templates using Twig or Blade template syntax.


## Example

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

use Cognesy\Instructor\Extras\Prompt\Prompt;
use Cognesy\Instructor\Features\LLM\Inference;
use Cognesy\Instructor\Utils\Str;

// EXAMPLE 1: Simplfied API

// use default template language, prompt files are in /prompts/twig/<prompt>.twig
$prompt = Prompt::text('capital', ['country' => 'Germany']);
$answer = (new Inference)->create(messages: $prompt)->toText();

echo "EXAMPLE 1: prompt = $prompt\n";
echo "ASSISTANT: $answer\n";
echo "\n";
assert(Str::contains($answer, 'Berlin'));

// EXAMPLE 2: Define prompt template inline

$prompt = Prompt::using('twig')
->withTemplateContent('What is capital of {{country}}')
->withValues(['country' => 'Germany'])
->toText();
$answer = (new Inference)->create(messages: $prompt)->toText();



echo "EXAMPLE 2: prompt = $prompt\n";
echo "ASSISTANT: $answer\n";
echo "\n";
assert(Str::contains($answer, 'Berlin'));

?>
```
1 change: 1 addition & 0 deletions docs/mint.json
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@
"cookbook/examples/extras/llm_json_schema",
"cookbook/examples/extras/llm_md_json",
"cookbook/examples/extras/llm_tools",
"cookbook/examples/extras/prompt_text",
"cookbook/examples/extras/schema",
"cookbook/examples/extras/schema_dynamic",
"cookbook/examples/extras/summary_with_llm",
Expand Down
14 changes: 8 additions & 6 deletions examples/A05_Extras/PromptText/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,25 @@
// EXAMPLE 1: Simplfied API

// use default template language, prompt files are in /prompts/twig/<prompt>.twig
$text = Prompt::text('capital', ['country' => 'Germany']);
$answer = (new Inference)->create(messages: $text)->toText();
$prompt = Prompt::text('capital', ['country' => 'Germany']);
$answer = (new Inference)->create(messages: $prompt)->toText();

echo "EXAMPLE 1: prompt = $text\n";
echo "EXAMPLE 1: prompt = $prompt\n";
echo "ASSISTANT: $answer\n";
echo "\n";
assert(Str::contains($answer, 'Berlin'));

// EXAMPLE 2: Define prompt template inline

$text = Prompt::using('twig')
$prompt = Prompt::using('twig')
->withTemplateContent('What is capital of {{country}}')
->withValues(['country' => 'Germany'])
->toText();
$answer = (new Inference)->create(messages: $text)->toText();
$answer = (new Inference)->create(messages: $prompt)->toText();

echo "EXAMPLE 2: prompt = $text\n";


echo "EXAMPLE 2: prompt = $prompt\n";
echo "ASSISTANT: $answer\n";
echo "\n";
assert(Str::contains($answer, 'Berlin'));
Expand Down

0 comments on commit 51e4491

Please sign in to comment.