diff --git a/docs/cookbook/examples/api_support/cohere.mdx b/docs/cookbook/examples/api_support/cohere.mdx index cc0ee879..48c6e4c8 100644 --- a/docs/cookbook/examples/api_support/cohere.mdx +++ b/docs/cookbook/examples/api_support/cohere.mdx @@ -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.", diff --git a/docs/cookbook/examples/api_support/togetherai.mdx b/docs/cookbook/examples/api_support/togetherai.mdx index 91b5660a..5e234ba6 100644 --- a/docs/cookbook/examples/api_support/togetherai.mdx +++ b/docs/cookbook/examples/api_support/togetherai.mdx @@ -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'; diff --git a/docs/cookbook/examples/extras/image_car_damage.mdx b/docs/cookbook/examples/extras/image_car_damage.mdx index 1d79c833..5f302506 100644 --- a/docs/cookbook/examples/extras/image_car_damage.mdx +++ b/docs/cookbook/examples/extras/image_car_damage.mdx @@ -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 @@ -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'; @@ -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)); diff --git a/docs/cookbook/examples/extras/image_to_data_gemini.mdx b/docs/cookbook/examples/extras/image_to_data_gemini.mdx index 939162b7..6fdd5ff4 100644 --- a/docs/cookbook/examples/extras/image_to_data_gemini.mdx +++ b/docs/cookbook/examples/extras/image_to_data_gemini.mdx @@ -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 = ''; diff --git a/docs/cookbook/examples/extras/prompt_text.mdx b/docs/cookbook/examples/extras/prompt_text.mdx new file mode 100644 index 00000000..a8d3db2e --- /dev/null +++ b/docs/cookbook/examples/extras/prompt_text.mdx @@ -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 +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/.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')); + +?> +``` diff --git a/docs/mint.json b/docs/mint.json index 075bfd52..41738890 100644 --- a/docs/mint.json +++ b/docs/mint.json @@ -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", diff --git a/examples/A05_Extras/PromptText/run.php b/examples/A05_Extras/PromptText/run.php index 2f6d475c..a8d3db2e 100644 --- a/examples/A05_Extras/PromptText/run.php +++ b/examples/A05_Extras/PromptText/run.php @@ -23,23 +23,25 @@ // EXAMPLE 1: Simplfied API // use default template language, prompt files are in /prompts/twig/.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'));