Laravel Ask DB: Natural Language Database Query Builder
Overview
Laravel Ask DB is an innovative package designed for PHP/Laravel frameworks, allowing developers to create natural language database queries effortlessly. This package leverages OpenAI's powerful GPT-3 technology to interpret and translate human-readable questions into actionable database queries. Though designed primarily as a learning tool for exploring prompt engineering and AI-driven query generation, it provides intriguing possibilities in database management.
Key Features
- Natural Language Processing: Users can simply pose questions in plain English, such as "How many users do we have on the 'pro' plan?", and Laravel Ask DB will use AI to process the query and interact with the database accordingly.
- Integration with GPT-3: By integrating with OpenAI’s GPT-3, the package intelligently deciphers and constructs database queries, making database interactions more intuitive and less code-heavy.
- Configurable: The package allows for configuration through a published config file, providing options such as defining the database connection, enabling or disabling strict mode, and setting the number of tables to check before requiring additional input from OpenAI.
Installation
Installing Laravel Ask DB is straightforward. It can be added to a Laravel project via Composer with the following command:
composer require beyondcode/laravel-ask-database
Following installation, users can publish the configuration file using:
php artisan vendor:publish --tag="ask-database-config"
Configuration
Users need to configure their OpenAI API key in the project's .env
file to start using the package. An example configuration looks like this:
OPENAI_API_KEY=sk-...
In order to tailor the package to specific requirements, the following settings are available in the configuration file:
- Database Connection: Can be set to any available database connection, e.g., 'mysql'.
- Strict Mode: When enabled, prevents write or alter operations on the database, ensuring read-only actions are performed which is ideal for security-sensitive applications.
- Table Lookup: Determines the maximum number of tables processed per operation before invoking OpenAI for additional table names, useful for projects with extensive databases.
Usage
Once configured, developers can use the DB::ask()
method to query the database using natural language:
$response = DB::ask('How many users are there?');
Testing
Testing the package is supported through Composer with:
composer test
Contribution and Development
Laravel Ask DB is an open-source project, encouraging collaboration and contribution. Contributors can refer to the contribution guidelines outlined in the CONTRIBUTING documentation.
Credits and Acknowledgment
The package is authored by Marcel Pociot and supported by contributions from developers across the globe. It is available under the MIT License, promoting open collaboration and innovation in the developer community.
For further details on updates and modifications over various iterations, users can refer to the CHANGELOG. To explore the licensing terms, consult the License File.