Quick Guide: GPT-4 Assistant to generate SQL for reporting

Any and all non-support discussions

Moderators: gerski, enjay, williamconley, Op3r, Staydog, gardo, mflorell, MJCoate, mcargile, Kumba, Michael_N

Quick Guide: GPT-4 Assistant to generate SQL for reporting

Postby njr » Wed Jan 10, 2024 11:56 am

Hi all,

This post is aimed at those (like me) who are just starting to try to wrangle all of the vicidial tables and/or those that just want to save time writing queries for custom reports.

Overview: Using the OpenAI playground, it's possible to make an assistant in about 10 minutes that can automatically deduce the correlations between different tables in vicidial given a schema in csv format. It then provides a chatbot-like interface where you can ask for a query for whatever report you need, and it will output it.

Requirements:
-OpenAI account. I'm using the gpt-4 model which, as of this posting, requires putting in $10 of credit I believe. It would probably work reasonably well with one of the gpt-3.5 models which should be free to try as far as I know.
-A full schema of your vicidial DB. If you log in to whichever DB server you want to use (I'm using a replica for this) and run
Code: Select all
mariadb
, you should get to a MariaDB prompt. Then type
Code: Select all
use asterisk;
or whatever the name of your main vicidial database is. Finally, to generate the schema, type
Code: Select all
SELECT table_name, column_name, data_type, column_key INTO OUTFILE '/path/to/output_file.txt' FROM information_schema.columns WHERE table_schema = 'asterisk';
and change the path to wherever you will be able to find it on the server. Due to secure-file-priv being set, you likely need to put the file in /var/lib/mysql-files. Use your preferred means of getting that file to your local machine.

Setup: Head to https://platform.openai.com/playground and log in or create an account.
You should get to a screen that says Playground with a select next to it. Select Assistants if it isn't already selected, and then click where it says create new assistant. Give it any name you prefer. For the Model, I'm using gpt-4-1106-preview mostly because it allows for rather large inputs and more importantly outputs (large enough that it can actually generate not only the query, but also stick it in php and wrap it in html to give you a full web report page.)

Here are the instructions I used:
Code: Select all
Purpose: This Assistant is designed to facilitate interaction with the Vicidial mariaDB database, allowing users to generate reports and gain insights through natural language queries. It understands the structure and relationships within the Vicidial database, providing an intuitive interface for querying complex data without needing direct SQL knowledge.

Capabilities:

Database Query Interpretation: Converts natural language queries into SQL commands. It can handle queries related to inbound and outbound calls, campaigns, agent activities, and more.
Schema Analysis: Analyzes the database schema to infer relationships between tables. This is particularly useful for identifying potential joins and understanding the data flow.
Report Generation: Translates query results into easily understandable reports. Can handle requests for daily summaries, performance metrics, campaign statistics, etc.
Dynamic Query Support: Capable of handling dynamic and context-based queries, like "How many calls were there for a specific campaign today?" or "What was the activity of agents in a particular location yesterday?"

For any uploaded schema file, fully parse and ingest the database schema in order to be able to determine the relationships between tables.


I also enabled Code Interpreter and Retrieval, which allow for code generation and file upload, respectively.

Create the assistant, and that's pretty much it. In the first query, upload the schema file you generated and start with "This is the database schema. Use this information to answer any further questions." or something to that effect. It will save the file for future use. Note: If it says it can't parse the txt file, just change it to a csv.

If you're asking a question and feel like it's not correctly referring to the schema, just remind it to do so.

Hope this helps someone. I generated queries for 12 KPIs in one prompt with no issue, and have made 5 or 6 rather detailed reports so far. Has definitely saved me time as well as helped me learn where everything is in the DB.

Here's what it looks like.
Image
Vicibox 11 from .iso installed/set up by Vicidial | Vicidial 2.14-900a Build: 231115-1636 | Asterisk 16.30.0-vici | 10-server cluster (1 primary DB, 1 primary web, 8 asterisk) in Colo DC | OpenSIPS on web as LB | 10x Dell R740XD
njr
 
Posts: 16
Joined: Fri Dec 08, 2023 1:41 pm

Re: Quick Guide: GPT-4 Assistant to generate SQL for reporti

Postby carpenox » Wed Jan 10, 2024 5:37 pm

Awesome work bro, definitely gonna try this one out
Alma Linux 9.4 | SVN Version: 3889 | DB Schema Version: 1721 | Asterisk 18.21.1 | PHP8
www.dialer.one -:- 1-833-DIALER-1 -:- https://linktr.ee/CyburDial -:- WA: +19549477572
GC: https://join.skype.com/ujkQ7i5lV78O | DC: https://discord.gg/DVktk6smbh
carpenox
 
Posts: 2423
Joined: Wed Apr 08, 2020 2:02 am
Location: St Petersburg, FL


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 72 guests