Skip to content

Commit

Permalink
(feat): adds exclusion support and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
ashleyhindle committed Sep 7, 2024
1 parent 77cef63 commit 5ede277
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ This package listens to the `saved` model event, then adds a queued job to autof
Example:
```php
class Article extends Model {
use AshleyHindle\AiAutofill;
use AshleyHindle\AiAutofill\AiAutofill;

protected $autofill = ['tagline' => 'a super click-baity obnoxious tagline'];
}
Expand All @@ -28,15 +28,20 @@ composer require ashleyhindle/laravel-ai-autofill
### Model Trait Usage

Simply use the trait in your model, and add the `$autofill` array.
The keys are the properties you want to autofill, and the values are the prompts you want to use to fill them.

The whole model, minus `$autofillExclude` properties, is provided to the LLM for context, so the prompts in `$autofill` can be very simple.

```php
class MeetingNotes extends Model {
use AshleyHindle\AiAutofill;
use AshleyHindle\AiAutofill\AiAutofill;

protected $autofill = [
'summary' => 'executive summary',
'action_items' => 'Flat JSON array of action items (e.g. ["Item 1", "Item 2", "Item 3"])'
];

protected $autofillExclude = ['zoom_password'];
}
```

Expand Down
2 changes: 1 addition & 1 deletion src/AiAutofill.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static function bootAiAutofill()
return;
}

AiAutofillJob::dispatch($model, $model->autofill);
AiAutofillJob::dispatch($model, $model->autofill, $model->autofillExclude ?? []);
});
}
}
9 changes: 7 additions & 2 deletions src/Jobs/AiAutofillJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class AiAutofillJob implements ShouldQueue
{
use Queueable;

public function __construct(protected Model $model, protected array $autofill) {}
public function __construct(protected Model $model, protected array $autofill, protected array $autofillExclude = []) {}

public function handle()
{
Expand All @@ -23,6 +23,11 @@ public function handle()
$count = count($this->autofill);
$jsonAutofill = json_encode($this->autofill);
$modelName = class_basename($this->model);
$modelProperties = $this->model->toArray();
foreach ($this->autofillExclude as $property) {
unset($modelProperties[$property]);
}
$modelContext = json_encode($modelProperties);

$systemPrompt = <<<AUTOFILL_PROMPT
Return JSON matching the JSON schema provided, returning {$count} values for the property & prompt values provided by the user for this model: {$modelName}.
Expand All @@ -31,7 +36,7 @@ public function handle()
The values returned must be the value for the property and nothing else. The values should be strings, unless the prompt specifically requests JSON.
### CONTEXT ###
{$this->model->toJson()}
{$modelContext}
### PROPERTIES & PROMPTS ###
{$jsonAutofill}
Expand Down

0 comments on commit 5ede277

Please sign in to comment.