From 1313bf66fbe21fdbef065d1c8176ea7bfc09b60f Mon Sep 17 00:00:00 2001 From: jitendriya14 Date: Tue, 10 Sep 2024 00:11:06 +0530 Subject: [PATCH] Minor Fixes --- README.md | 15 ++-- src/Commands/JitoneAiCommand.php | 3 + src/Forms/Actions/GenerateContentAction.php | 87 ++++++++++++--------- 3 files changed, 59 insertions(+), 46 deletions(-) diff --git a/README.md b/README.md index d93fbdd..a7159ac 100644 --- a/README.md +++ b/README.md @@ -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.** @@ -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: @@ -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: diff --git a/src/Commands/JitoneAiCommand.php b/src/Commands/JitoneAiCommand.php index aeb2bcc..faae80b 100644 --- a/src/Commands/JitoneAiCommand.php +++ b/src/Commands/JitoneAiCommand.php @@ -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'); diff --git a/src/Forms/Actions/GenerateContentAction.php b/src/Forms/Actions/GenerateContentAction.php index 848ecad..c0fa167 100644 --- a/src/Forms/Actions/GenerateContentAction.php +++ b/src/Forms/Actions/GenerateContentAction.php @@ -8,6 +8,7 @@ use Filament\Forms\Components\RichEditor; use Filament\Forms\Components\Toggle; use Jiten14\JitoneAi\JitoneAi; +use Filament\Notifications\Notification; class GenerateContentAction { @@ -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');