How to use OpenAI GPT-4o to query your database?

88 / 100

Today, OpenAI unveiled its latest language model, GPT-4o. Enthusiasts are buzzing about the innovative applications developed with this cutting-edge model. GPT-4o enhances data retrieval, offering users quicker responses and greater cost-efficiency, outperforming even GPT-4 Turbo in terms of affordability.

How to use OpenAI GPT-4o to query your database?
Model comparison from OpenAI website

Within 12 hours, our team at WrenAI released our latest version, to support the GPT-4o LLM model for querying your own database. In this post, we will guide you through setting up using gpt-4o to query your own PostgreSQL database!

How to use OpenAI GPT-4o to query your database?

WrenAI is an open-source text-to-SQL solution for data teams to get results and insights faster by asking business questions without writing SQL.

Let’s get started!

How to use OpenAI GPT-4o to query your database?
Using WrenAI with GPT-4o to query PostgreSQL

Setup your PostgreSQL database

Start your PostgreSQL server, and enter your PostgreSQL database using the psql client.

How to use OpenAI GPT-4o to query your database?

Create a database in PostgreSQL here, we called ecom-wrenai

CREATE DATABASE ecom_wrenai;

Connect to the created database ecom_wrenai using \c <database_name>

lijungchi=# \c ecom_wrenai
psql (13.1, server 13.0)
You are now connected to database "ecom_wrenai" as user "lijungchi".
ecom_wrenai=#

Now, you want to create tables of this dataset and import the data. Please check the SQL script here in the gist.

You can either copy the script to your psqlor Import SQL via psql

ecom_wrenai=# \i <path to your file>/import.sql

If you successfully import the data, you will see the information below.

How to use OpenAI GPT-4o to query your database?
Import data successfully

Installing WrenAI

Next, let’s start installing WrenAI; before we start, you need to install Docker.

1. Install Docker Desktop on your local computer.

Please ensure the version of Docker Desktop is at least >= 4.17.

2. Prepare an OpenAI API key

Please ensure that your Open API key has Full Permission(All).

Visit the OpenAI developer platform.

How to use OpenAI GPT-4o to query your database?
Enter the OpenAI API key page

Generate a new API key for WrenAI with full permission

How to use OpenAI GPT-4o to query your database?
Generate your OpenAI API key with full permission

3. Install WrenAI Launcher

If you are on Mac(using Windows or Linux check here) enter the below command to install the latest WrenAI Launcher.

curl -L https://github.com/Canner/WrenAI/releases/latest/download/wren-launcher-darwin.tar.gz | tar -xz && ./wren-launcher-darwin

The launcher will then ask for your OpenAI API key as below, paste your key into the command and hit enter.

Now you can select gpt-4o , gpt-4-turbo , gpt-3.5-turbo of OpenAI’s generation model in WrenAI.

How to use OpenAI GPT-4o to query your database?
Install WrenAI with CLI

Now you’ll see we are running docker-compose on your computer; after the installation, the tool will automatically open up your browser to access WrenAI.

How to use OpenAI GPT-4o to query your database?
Running Docker Compose on your computer

Connect your Data Source

While the terminal is successfully installed, it will launch the browser

How to use OpenAI GPT-4o to query your database?
First-time launching WrenAI

Select PostgreSQL database connector

How to use OpenAI GPT-4o to query your database?
Fill out the connection information

If you use the Mac local PostgreSQL database, enter docker.for.mac.localhost to access the database.

How to use OpenAI GPT-4o to query your database?
Select data models in your database.

Next step, define relationships to the semantic models; this could help LLMs to understand more about your semantic data structure.

How to use OpenAI GPT-4o to query your database?
Add semantic relationships

Now, you are all set!

You can view all your data models in the WrenAI UI.

How to use OpenAI GPT-4o to query your database?
Complete the setup

Playing around with WrenAI

WrenAI UI semantic modeling

With WrenAI UI, you can model your data models in a semantic context, such as adding descriptions, relationships, calculations, etc. The context could help LLMs learn your business terminologies and KPI definitions and reduce hallucinations when joining multiple tables; LLMs know the data structure hierarchy by learning through relationships such as whether it is a many-to-one , one-to-many , or many-to-many relationships between tables.

How to use OpenAI GPT-4o to query your database?
Semantic modeling through WrenAI UI

Define your business KPIs and formulas via calculations in WrenAI.

How to use OpenAI GPT-4o to query your database?
Add calculations in the data model

Adding semantic relationships between tables.

How to use OpenAI GPT-4o to query your database?
Add relationships in the data model.

Ask business questions and Follow-up insights with WrenAI

Now you can switch to the Home page in WrenAI UI, by clicking New Thread you can start asking any of your business questions, WrenAI search for relevant semantics through the vector store and provides recommendations.

How to use OpenAI GPT-4o to query your database?
Ask any business questions to WrenAI

WrenAI will show up to 3 options for your business questions; you can select the most relevant question to generate the final result.

How to use OpenAI GPT-4o to query your database?
WrenAI searches for the most relevant result

As you select an option, WrenAI will display a step-by-step breakdown of the summary, including SQL and data, allowing you to verify if the result is what you were looking for.

How to use OpenAI GPT-4o to query your database?
Step-by-step breakdown of the SQL summary

You can ask follow-up questions to gain deeper insights into the result.

How to use OpenAI GPT-4o to query your database?
Ask follow-up questions to gain more insights.

Let’s about it! Hope you are having fun with gpt-4o!

1 thought on “How to use OpenAI GPT-4o to query your database?”

Leave a Comment