Skip to content

Commit

Permalink
Minor Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jiten14 committed Sep 9, 2024
1 parent 7e8d0ec commit 1313bf6
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 46 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# jitone-ai:- Build AI-powered Filament forms.
# Jitone AI:- Build AI-powered Filament forms.

**jitone-ai is a powerful FilamentPHP plugin that integrates AI-powered features directly into your Filament forms.**

Expand Down Expand Up @@ -34,15 +34,16 @@ php artisan jitone-ai:install
```

**This command will:**
- Publish the configuration file
- Publish the configuration file for `Jitone AI` Settings.
- Publish the configiration file for `openai-php/laravel` Settings.
- Create a symbolic link for storage
2. We use [openai-php/laravel](https://github.com/openai-php/laravel) package to connect with OpenAI API. Configure your OpenAI API key in your .env file:

2. We use [openai-php/laravel](https://github.com/openai-php/laravel) package to connect with OpenAI API. Blank environment variables for the OpenAI API key and organization id are already appended to your .env file, Add your API key here.

```env
OPENAI_API_KEY=your_api_key_here
OPENAI_API_KEY=sk-...
OPENAI_ORGANIZATION=org-...
```
# JitoneAI

## Configuration

The `config/jitone-ai.php` file allows you to set default values and templates:
Expand Down Expand Up @@ -110,7 +111,7 @@ The package provides a dedicated `AITextField` for enhanced content generation:
```php
use Jiten14\JitoneAi\Forms\Components\AITextField;

AITextField::make('content')
AITextField::make('content')->withAI()
```

##### With Options:
Expand Down
3 changes: 3 additions & 0 deletions src/Commands/JitoneAiCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public function handle(): int
'--tag' => "jitone-ai-config"
]);

//Create openai-php/laravel Config File
$this->call('openai:install');

// Create storage link
$this->call('storage:link');

Expand Down
87 changes: 48 additions & 39 deletions src/Forms/Actions/GenerateContentAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Filament\Forms\Components\RichEditor;
use Filament\Forms\Components\Toggle;
use Jiten14\JitoneAi\JitoneAi;
use Filament\Notifications\Notification;

class GenerateContentAction
{
Expand Down Expand Up @@ -55,50 +56,58 @@ public function execute($field, $record, $data, array $options = [])
->visible(fn (callable $get) => !$get('use_existing_content')),
])
->action(function (array $data) use ($field, $options) {
if ($data['use_existing_content']) {
$currentContent = $field->getState();
$action = $data['existing_content_action'];

switch ($action) {
case 'refine':
$prompt = "Refine the following text: $currentContent";
break;
case 'expand':
$prompt = "Expand on the following text: $currentContent";
break;
case 'shorten':
$prompt = "Shorten the following text while maintaining its key points: $currentContent";
break;
default:
throw new \Exception("Invalid action selected for existing content.");
if (!env('OPENAI_API_KEY')) {
Notification::make()
->warning()
->title('OpenAI API Key Missing')
->body('Please add your OpenAI API Key to the .env file before proceeding.')
->send();
}else {
if ($data['use_existing_content']) {
$currentContent = $field->getState();
$action = $data['existing_content_action'];

switch ($action) {
case 'refine':
$prompt = "Refine the following text: $currentContent";
break;
case 'expand':
$prompt = "Expand on the following text: $currentContent";
break;
case 'shorten':
$prompt = "Shorten the following text while maintaining its key points: $currentContent";
break;
default:
throw new \Exception("Invalid action selected for existing content.");
}
} else {
$prompt = $data['ai_prompt'] ?? null;

if (empty($prompt)) {
throw new \Exception("Prompt is empty or null. Form data: " . json_encode($data));
}
}
} else {
$prompt = $data['ai_prompt'] ?? null;

if (empty($prompt)) {
throw new \Exception("Prompt is empty or null. Form data: " . json_encode($data));
}
}

$generatedContent = app(JitoneAi::class)->generateContent($prompt, $options);

if ($data['use_existing_content']) {
// Replace the existing content
$newContent = $generatedContent;
} else {
// Append the new content to the existing content
$currentContent = $field->getState();
if ($field instanceof RichEditor) {
$newContent = $currentContent . "\n\n" . $generatedContent;
} elseif ($field instanceof Textarea) {
$newContent = $currentContent . "\n" . $generatedContent;
$generatedContent = app(JitoneAi::class)->generateContent($prompt, $options);

if ($data['use_existing_content']) {
// Replace the existing content
$newContent = $generatedContent;
} else {
$newContent = trim($currentContent . ' ' . $generatedContent);
// Append the new content to the existing content
$currentContent = $field->getState();
if ($field instanceof RichEditor) {
$newContent = $currentContent . "\n\n" . $generatedContent;
} elseif ($field instanceof Textarea) {
$newContent = $currentContent . "\n" . $generatedContent;
} else {
$newContent = trim($currentContent . ' ' . $generatedContent);
}
}

// Set the new content
$field->state($newContent);
}

// Set the new content
$field->state($newContent);
})
->modalHeading('Generate Content with AI')
->modalButton('Generate');
Expand Down

0 comments on commit 1313bf6

Please sign in to comment.