Skip to content

Commit

Permalink
Implemented Scripts for better control of controlling chat sequences …
Browse files Browse the repository at this point in the history
…generated by Instructor
  • Loading branch information
ddebowczyk committed Jun 7, 2024
1 parent 98741d8 commit b49c9df
Show file tree
Hide file tree
Showing 39 changed files with 1,327 additions and 349 deletions.
8 changes: 4 additions & 4 deletions examples/01_Basics/ValidationMixin/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
$loader = require 'vendor/autoload.php';
$loader->add('Cognesy\\Instructor\\', __DIR__.'../../src/');

use Cognesy\Instructor\Events\Request\RequestSentToLLM;
use Cognesy\Instructor\Instructor;
use Cognesy\Instructor\Validation\Traits\ValidationMixin;
use Cognesy\Instructor\Validation\ValidationResult;
Expand All @@ -41,14 +40,15 @@ public function validate() : ValidationResult {
}
}

$user = (new Instructor)->respond(
messages: [['role' => 'user', 'content' => 'Jason was born in 1990 and graduated in 1010.']],
$user = (new Instructor)->wiretap(fn($e)=>$e->printDump())->respond(
messages: [['role' => 'user', 'content' => 'Jason was born in 2000 and graduated in 1923.']],
responseModel: UserDetails::class,
model: 'gpt-3.5-turbo',
maxRetries: 2
);

dump($user);

assert($user->graduationYear === 2010);
assert($user->graduationYear === 2023);
?>
```
4 changes: 2 additions & 2 deletions examples/02_Advanced/CustomPrompts/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ class User {
->respond(
messages: "Our user Jason is 25 years old.",
responseModel: User::class,
prompt: "\nYour task is to respond correctly with JSON object. Response must follow JSONSchema:\n{json_schema}\n",
prompt: "\nYour task is to respond correctly with JSON object. Response must follow JSONSchema:\n<|json_schema|>\n",
mode: Mode::Json);

print("\n# Request for Mode::MdJson:\n\n");
$user = $instructor
->respond(
messages: "Our user Jason is 25 years old.",
responseModel: User::class,
prompt: "\nYour task is to respond correctly with strict JSON object containing extracted data within a ```json {} ``` codeblock. Object must validate against this JSONSchema:\n{json_schema}\n",
prompt: "\nYour task is to respond correctly with strict JSON object containing extracted data within a ```json {} ``` codeblock. Object must validate against this JSONSchema:\n<|json_schema|>\n",
mode: Mode::MdJson);

?>
Expand Down
2 changes: 1 addition & 1 deletion examples/02_Advanced/LanguagePrograms/run.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Language programs
# Language programs with Modules

Instructor provides an addon allowing to implement complex processing flows
using LLM in a modular way. This addon to Instructor has been inspired by DSPy
Expand Down
19 changes: 12 additions & 7 deletions examples/02_Advanced/LanguagePrograms2/run.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Language programs
# 'Structure to structure' LLM processing

Instructor provides an addon allowing to implement complex processing flows
using LLM in a modular way. This addon to Instructor has been inspired by DSPy
Expand All @@ -25,6 +25,8 @@
```php
<?php

use Cognesy\Instructor\Clients\Anthropic\AnthropicClient;
use Cognesy\Instructor\Clients\Groq\GroqClient;
use Cognesy\Instructor\Contracts\CanProvideSchema;
use Cognesy\Instructor\Extras\Module\Addons\Predict\Predict;
use Cognesy\Instructor\Extras\Module\CallData\Contracts\HasInputOutputData;
Expand All @@ -34,6 +36,7 @@
use Cognesy\Instructor\Extras\Module\Signature\Attributes\OutputField;
use Cognesy\Instructor\Extras\Module\CallData\SignatureData;
use Cognesy\Instructor\Instructor;
use Cognesy\Instructor\Utils\Env;

$loader = require 'vendor/autoload.php';
$loader->add('Cognesy\\Instructor\\', __DIR__ . '../../src/');
Expand All @@ -42,9 +45,10 @@

//#[Description('extract email details from text')]
class ParsedEmail extends SignatureData {
// INPUTS
#[InputField('text containing email')]
public string $text;

// OUTPUTS
#[OutputField('email address of sender')]
public string $senderEmail;
#[OutputField('subject of the email')]
Expand All @@ -54,28 +58,29 @@ class ParsedEmail extends SignatureData {
}

class FixedEmail extends SignatureData {
// INPUTS
#[InputField('subject of the email')]
public string $subject;
#[InputField('body of the email')]
public string $body;

// OUTPUTS
#[OutputField('subject of the email with fixed spelling mistakes')]
public string $fixedSubject;
#[OutputField('body of the email with fixed spelling mistakes')]
public string $fixedBody;
}

// Alternative way to define the module signature data without extending a class
// Alternative way to define the class signature data without extending a class
class EmailTranslation implements HasInputOutputData, CanProvideSchema {
use AutoSignature;

// INPUTS
#[InputField('subject of email')]
public string $subject;
#[InputField('body of email')]
public string $body;
#[InputField('language to translate to')]
public string $language;

// OUTPUTS
#[OutputField('translated subject of email')]
public string $translatedSubject;
#[OutputField('translated body of email')]
Expand Down Expand Up @@ -106,7 +111,7 @@ class ProcessEmail extends Module {
private Predict $translate;

public function __construct() {
$instructor = new Instructor();
$instructor = (new Instructor);//->withClient(new AnthropicClient(Env::get('ANTHROPIC_API_KEY')));//->wiretap(fn($e) => $e->printDump());

$this->parse = new Predict(signature: ParsedEmail::class, instructor: $instructor);
$this->fix = new Predict(signature: FixedEmail::class, instructor: $instructor);
Expand Down
8 changes: 6 additions & 2 deletions examples/05_APISupport/LLMSupportGroq/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ enum UserType : string {
}

class User {
public int $age;
public ?int $age;
public string $name;
public string $username;
public UserType $role;
Expand All @@ -45,8 +45,12 @@ class User {
messages: "Jason (@jxnlco) is 25 years old and is the admin of this project. He likes playing football and reading books.",
responseModel: User::class,
model: 'llama3-8b-8192',
options: ['stream' => false],
examples: [[
'input' => 'Ive got email Frank - their developer. He asked to come back to him [email protected]. Btw, he plays on drums!',
'output' => ['age' => null, 'name' => 'Frank', 'role' => 'developer', 'hobbies' => ['playing drums'],],
]],
mode: Mode::Json,
options: ['stream' => false]
);

print("Completed response model:\n\n");
Expand Down
31 changes: 28 additions & 3 deletions notes/NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,34 @@ Examples to demonstrate use cases.

Catch up with the latest additions.

## Research

## Brain dump

- MessageSequence to better process multi-stage chat content
- Extract APIClient to a separate package?
- example of integration with Laravel/Livewire
- DSPy next steps: eval, optimize, compile
- Finish logging support
- Documentation for logging
- Add more modules: XoT, RAG, ReAct, etc.
- RAG - how to handle multiple VDB providers?
- Better error messages
- Finish module observability via events - currently no access to this info & only 2 events supported
- Test validation in modules - provide an example
- Parallel execution of modules (e.g. a la Laravel jobs?)
- How to track API rate limits across multiple requests / parallel executions
- Moderation endpoint support
- Make using DocBlocks optional - it may not always to be desired to pass this info to LLM
- Rework Events so they have toArray() method, make __toString() use it
- Document how to inject custom HTTP client
- Git/GitHub integration module to allow easy automation
- Data mapping module(s) for easier data transformations
- Add super detailed tests of Module core functionality - esp. around input/output mappings
- How to handle dynamic module graph definition + visualization

## To research

- Schema.org ld+json // Spatie https://github.com/spatie/schema-org // https://developers.google.com/search/docs/appearance/structured-data?hl=pl
- nette/schema https://github.com/nette/schema
- Queue-based load leveling
- Throttling
- Circuit breaker
Expand All @@ -29,4 +55,3 @@ Catch up with the latest additions.
- Sparse task scheduler
- Marker and sweeper
- Actor model

Loading

0 comments on commit b49c9df

Please sign in to comment.