Skip to content

Commit

Permalink
Add AI support to ExtendedRuleMakeCommand
Browse files Browse the repository at this point in the history
This commit adds support for generating rule content using AI in the ExtendedRuleMakeCommand. The user is prompted to describe the validation rule they want to generate, and the AI generates the content of the rule file. The generated content is then stored in the correct file and location. Exceptions are thrown if there are errors fetching or storing the AI-generated content.
  • Loading branch information
salehhashemi1992 committed May 5, 2023
1 parent f4da330 commit 2b6f4e7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
44 changes: 30 additions & 14 deletions src/Console/ExtendedRuleMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Salehhashemi\LaravelIntelliDb\Console;

use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Foundation\Console\RuleMakeCommand;
use Illuminate\Http\Client\RequestException;
use Salehhashemi\LaravelIntelliDb\OpenAi;
use Symfony\Component\Console\Input\InputOption;

Expand All @@ -15,33 +17,47 @@ protected function configure()
$this->addOption('ai', 'a', InputOption::VALUE_NONE, 'Use AI to generate rule content');
}

/**
* {@inheritdoc}
*/
public function handle(): bool|null
{
if ($this->option('ai')) {
// Ask the user for the desired rule content
$ruleDescription = $this->ask('Please describe the validation rule you want to generate (e.g., "validate unique email")');
try {
if ($this->option('ai')) {
// Ask the user for the desired rule content
$ruleDescription = $this->ask('Please describe the validation rule you want to generate (e.g., "validate unique email")');

// Create a prompt to generate the content of the rule file
$prompt = 'Create a validation rule in PHP for '.$this->argument('name').' that does the following: '.$ruleDescription;
// Create a prompt to generate the content of the rule file
$prompt = 'Create a validation rule in PHP for '.$this->argument('name').' that does the following: '.$ruleDescription;

$generatedContent = (new OpenAi())->execute($prompt, 1000);
$generatedContent = (new OpenAi())->execute($prompt, 1000);

// Get generated content and store it
$this->storeGeneratedContent($generatedContent);
// Get generated content and store it
$this->storeGeneratedContent($generatedContent);

$this->info('AI-generated content stored at: '.$this->getPath($this->qualifyClass($this->getNameInput())));
}
$this->info('AI-generated content stored at: '.$this->getPath($this->qualifyClass($this->getNameInput())));
}

return parent::handle();
} catch (RequestException $e) {
$this->error('Error fetching AI-generated content: '.$e->getMessage());

return parent::handle();
return false;
} catch (FileNotFoundException $e) {
$this->error('Error storing AI-generated content: '.$e->getMessage());

return false;
}
}

protected function storeGeneratedContent(string $generatedContent)
/**
* Store the generated content in the correct file and location
*/
protected function storeGeneratedContent(string $generatedContent): void
{
// Store the generated content in the correct file and location
$path = $this->getPath($this->qualifyClass($this->getNameInput()));
$this->makeDirectory($path);

// Write the content to the file
$this->files->put($path, $generatedContent);

$this->info('Generated content stored at: '.$path);
Expand Down
2 changes: 2 additions & 0 deletions src/OpenAi.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class OpenAi
{
/**
* Execute the OpenAI API call with a given prompt.
*
* @throws RequestException
*/
public static function execute(string $prompt, int $maxTokens = 300): string
{
Expand Down

0 comments on commit 2b6f4e7

Please sign in to comment.