by Juan David Meza Meza, Staff Software Engineer at Growth Acceleration Partners.
Natural language processing models, such as GPT (Generative Pre-trained Transformer), have advanced understanding and generate human-like text. Apart from that, we can now use these models to solve almost any task we need, especially from a programming perspective.
This article will explore integrating Laravel, a popular PHP framework, with GPT to build database queries using natural language. Imagine writing something like, “How many users do I have?” and then getting an immediate response with an accurate number of users.
For doing this, we have plenty of options. Fortunately, the Laravel community is very active and we can leverage a community package to integrate Laravel with GPT to ask natural language questions directly to our database. This package is called “Laravel Ask Database”
Understanding Laravel-Ask-Database
The laravel-ask-database package acts as a bridge between Laravel and GPT. It enables developers to send natural language queries to GPT, which converts them into SQL queries based on the database context provided. The generated SQL queries are executed against the database, and the results are sent back to GPT for interpretation.
Let’s see how to integrate this:
First, create a fresh Laravel project by executing composer create-project laravel/laravel Laravel-AI-sample.
Also, make sure to register on the OpenAI website and get an API key here.
Now, let’s integrate an ask database:
1. Require laravel-ask-database by executing composer require beyondcode/laravel-ask-database
2. Configure your database context; define your database’s tables and columns for the project. For this article, we can stick with the default migrations from Laravel.
a. Make sure to execute php artisan migrate to get all of your tables
b. Also, ensure you have your .env file with the DB credentials
i. I’d recommend using SQLite for a quick demo
3. Set up GPT and the Ask Database environment variables:
a. Add OPENAI_API_KEY in your .env file
i. Set the value of OPENAI_API_KEY with the API key you generated before.
b. Publish the ask database configuration file
i. Execute php artisan vendor:publish — tag=”ask-database-config”
c. Configure the database connection
i. Set the value for ASK_DATABASE_DB_CONNECTION in your .env file
ii. For a quick demo, use sqlite
4. Now, you are set! Let’s test this quickly with tinker
a. Execute php artisan tinker
b. Within tinker, issue DB::ask(‘How many users we have?’);
c. This should say 0, so let’s insert some users
i. Within tinker, execute App\Models\User::factory(500)->create():
d. After that, if you ask the same question to the database, the answer should now be 500.
Benefits and Use Cases
Integrating Laravel with GPT using the laravel-ask-database package offers several advantages:
- User-Friendly: Users can query the database using natural language, eliminating the need to learn complex SQL syntax.
- Real Time Information: Your database can provide real time information, even building complex queries in little time.
- Rapid Development: Developers can quickly build database-driven applications without spending extensive time on query creation and optimization.
- Increased Accessibility: Non-technical stakeholders can interact with the database directly, reducing the dependency on developers for generating custom reports or extracting insights.
Considerations
Even when it is quite powerful, we need to be careful about some details
- It is important to make sure you can share the database structure with GPT without issues. So, confirm first.
- You are sending actual data to GPT to get the final answer, but you may want to avoid this. You can just execute the raw query instead of sending the results back to GPT.
- Make sure to avoid no-read actions on the Database.
For this, you can set the strict mode on the package to true.
- It would be good to only get the query that GPT builds for you to execute it or even confirm before executing it
To do that, you better use DB::askForQuery(‘question here’) instead of DB::ask(…)
Conclusion
Integrating Laravel with GPT using the laravel-ask-database package empowers developers and users to interact with databases using natural language. This approach reduces the entry barrier for non-technical individuals and facilitates the rapid development of database-driven applications.
Leveraging GPT’s language understanding and generation capabilities enhances the user experience and opens up new possibilities for database query generation. By combining the power of Laravel and GPT, developers can unlock the potential of natural language querying and streamline the development process. But of course, it is important to keep security and privacy in mind, so make sure to double check what is happening under the hood.
Note: Please follow the official documentation of laravel-ask-database and OpenAI’s GPT-3 for detailed instructions on setup, usage and security best practices.
REF: https://beyondco.de/blog/query-your-laravel-database-using-natural-language