Query local or remote files with natural language queries powered by
langchain
and gpt-3.5-turbo
and duckdb
π¦.
Works on local CSV files:
remote CSV files:
$ qabot \
-f https://www.stats.govt.nz/assets/Uploads/Environmental-economic-accounts/Environmental-economic-accounts-data-to-2020/renewable-energy-stock-account-2007-2020-csv.csv \
-q "How many Gigawatt hours of generation was there for Solar resources in 2015 through to 2020?"
Even on (public) data stored in S3:
You need to set the OPENAI_API_KEY
environment variable to your OpenAI API key,
which you can get from here.
Install the qabot
command line tool using pip/poetry:
$ pip install qabot
Then run the qabot
command with either local files (-f my-file.csv
) or a database connection string.
Note if you want to use a database, you will need to install the relevant drivers,
e.g. pip install psycopg2-binary
for postgres.
$ qabot -q "how many passengers survived by gender?" -f data/titanic.csv
π¦ Loading data from files...
Loading data/titanic.csv into table titanic...
Query: how many passengers survived by gender?
Result:
There were 233 female passengers and 109 male passengers who survived.
π any further questions? [y/n] (y): y
π Query: what was the largest family who did not survive?
Query: what was the largest family who did not survive?
Result:
The largest family who did not survive was the Sage family, with 8 members.
π any further questions? [y/n] (y): n
Use the -v
flag to see the intermediate steps and database queries.
Sometimes it takes a long route to get to the answer, but it's interesting to see how it gets there:
qabot -f data/titanic.csv -q "how many passengers survived by gender?" -v
π¦ Loading data from files...
Query: how many passengers survived by gender?
I need to check the columns in the 'titanic' table to see which ones contain gender and survival information.
Action: Describe Table
Action Input: titanic
Observation: titanic
βββββββββββββββ¬ββββββββββββββ¬ββββββββββ¬ββββββββββ¬ββββββββββ¬ββββββββ
β column_name β column_type β null β key β default β extra β
β varchar β varchar β varchar β varchar β varchar β int32 β
βββββββββββββββΌββββββββββββββΌββββββββββΌββββββββββΌββββββββββΌββββββββ€
β PassengerId β BIGINT β YES β NULL β NULL β NULL β
β Survived β BIGINT β YES β NULL β NULL β NULL β
β Pclass β BIGINT β YES β NULL β NULL β NULL β
β Name β VARCHAR β YES β NULL β NULL β NULL β
β Sex β VARCHAR β YES β NULL β NULL β NULL β
β Age β DOUBLE β YES β NULL β NULL β NULL β
β SibSp β BIGINT β YES β NULL β NULL β NULL β
β Parch β BIGINT β YES β NULL β NULL β NULL β
β Ticket β VARCHAR β YES β NULL β NULL β NULL β
β Fare β DOUBLE β YES β NULL β NULL β NULL β
β Cabin β VARCHAR β YES β NULL β NULL β NULL β
β Embarked β VARCHAR β YES β NULL β NULL β NULL β
βββββββββββββββ΄ββββββββββββββ΄ββββββββββ΄ββββββββββ΄ββββββββββ΄ββββββββ€
β 12 rows 6 columns β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
I need to create a view that only includes the columns I need for this question.
Action: Data Op
Action Input:
CREATE VIEW titanic_gender_survival AS
SELECT Sex, Survived
FROM titanic
Thought:
> Entering new AgentExecutor chain...
This is a valid SQL query creating a view. We can execute it directly.
Action: execute
Action Input:
CREATE VIEW titanic_gender_survival AS
SELECT Sex, Survived
FROM titanic
Observation: No output
Thought:The view has been created successfully. We can now query it.
Action: execute
Action Input: SELECT * FROM titanic_gender_survival LIMIT 5
Observation:
βββββββββββ¬βββββββββββ
β Sex β Survived β
β varchar β int64 β
βββββββββββΌβββββββββββ€
β male β 0 β
β female β 1 β
β female β 1 β
β female β 1 β
β male β 0 β
βββββββββββ΄βββββββββββ
Thought:The view has been created successfully and we can see the first 5 rows of the view. The final answer is the first 5 rows of the titanic_gender_survival view, showing the sex and survival status of passengers on the
Titanic.
Final Answer:
βββββββββββ¬βββββββββββ
β Sex β Survived β
β varchar β int64 β
βββββββββββΌβββββββββββ€
β male β 0 β
β female β 1 β
β female β 1 β
β female β 1 β
β male β 0 β
βββββββββββ΄βββββββββββ
> Finished chain.
Observation: βββββββββββ¬βββββββββββ
β Sex β Survived β
β varchar β int64 β
βββββββββββΌβββββββββββ€
β male β 0 β
β female β 1 β
β female β 1 β
β female β 1 β
β male β 0 β
βββββββββββ΄βββββββββββ
I need to group the data by gender and count the number of survivors for each group.
Action: Data Op
Action Input:
SELECT Sex, SUM(Survived) AS num_survived
FROM titanic_gender_survival
GROUP BY Sex
Thought:
> Entering new AgentExecutor chain...
We need to check if the table titanic_gender_survival exists and if it has the columns Sex and Survived.
Action: Describe Table
Action Input: titanic_gender_survival
Observation: titanic_gender_survival
βββββββββββββββ¬ββββββββββββββ¬ββββββββββ¬ββββββββββ¬ββββββββββ¬ββββββββ
β column_name β column_type β null β key β default β extra β
β varchar β varchar β varchar β varchar β varchar β int32 β
βββββββββββββββΌββββββββββββββΌββββββββββΌββββββββββΌββββββββββΌββββββββ€
β Sex β VARCHAR β YES β NULL β NULL β NULL β
β Survived β BIGINT β YES β NULL β NULL β NULL β
βββββββββββββββ΄ββββββββββββββ΄ββββββββββ΄ββββββββββ΄ββββββββββ΄ββββββββ
Thought:The table titanic_gender_survival exists and has the columns Sex and Survived. We can now run the query.
Action: execute
Action Input:
SELECT Sex, SUM(Survived) AS num_survived FROM titanic_gender_survival GROUP BY Sex LIMIT 5
Observation:
βββββββββββ¬βββββββββββββββ
β Sex β num_survived β
β varchar β int128 β
βββββββββββΌβββββββββββββββ€
β male β 109 β
β female β 233 β
βββββββββββ΄βββββββββββββββ
Thought:The query returned the number of survivors grouped by gender. The table titanic_gender_survival has been used.
Final Answer: The number of survivors grouped by gender are:
- 109 males survived
- 233 females survived.
> Finished chain.
Observation: The number of survivors grouped by gender are:
- 109 males survived
- 233 females survived.
Intermediate Steps:
Step 1
Describe Table
titanic
titanic
βββββββββββββββ¬ββββββββββββββ¬ββββββββββ¬ββββββββββ¬ββββββββββ¬ββββββββ
β column_name β column_type β null β key β default β extra β
β varchar β varchar β varchar β varchar β varchar β int32 β
βββββββββββββββΌββββββββββββββΌββββββββββΌββββββββββΌββββββββββΌββββββββ€
β PassengerId β BIGINT β YES β NULL β NULL β NULL β
β Survived β BIGINT β YES β NULL β NULL β NULL β
β Pclass β BIGINT β YES β NULL β NULL β NULL β
β Name β VARCHAR β YES β NULL β NULL β NULL β
β Sex β VARCHAR β YES β NULL β NULL β NULL β
β Age β DOUBLE β YES β NULL β NULL β NULL β
β SibSp β BIGINT β YES β NULL β NULL β NULL β
β Parch β BIGINT β YES β NULL β NULL β NULL β
β Ticket β VARCHAR β YES β NULL β NULL β NULL β
β Fare β DOUBLE β YES β NULL β NULL β NULL β
β Cabin β VARCHAR β YES β NULL β NULL β NULL β
β Embarked β VARCHAR β YES β NULL β NULL β NULL β
βββββββββββββββ΄ββββββββββββββ΄ββββββββββ΄ββββββββββ΄ββββββββββ΄ββββββββ€
β 12 rows 6 columns β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Step 2
Data Op
CREATE VIEW titanic_gender_survival AS
SELECT Sex, Survived
FROM titanic
βββββββββββ¬βββββββββββ
β Sex β Survived β
β varchar β int64 β
βββββββββββΌβββββββββββ€
β male β 0 β
β female β 1 β
β female β 1 β
β female β 1 β
β male β 0 β
βββββββββββ΄βββββββββββ
Step 3
Data Op
SELECT Sex, SUM(Survived) AS num_survived
FROM titanic_gender_survival
GROUP BY Sex
The number of survivors grouped by gender are:
- 109 males survived
- 233 females survived.
Thought:
Result:
109 males and 233 females survived.
Use the -f <url>
flag to load data from a url, e.g. a csv file on s3:
$ qabot -f s3://covid19-lake/enigma-jhu-timeseries/csv/jhu_csse_covid_19_timeseries_merged.csv -q "how many confirmed cases are there" -v
π¦ Loading data from files...
create table jhu_csse_covid_19_timeseries_merged as select * from 's3://covid19-lake/enigma-jhu-timeseries/csv/jhu_csse_covid_19_timeseries_merged.csv';
Result:
264308334 confirmed cases