What about this:
- you first ask GPT to translate the request to a query for your database by providing the necessary information such as schema and format (of course in a way that doesn’t entail SQL injection or involuntary disclosure, e.g. you only ask for the parameters not the full query)
- you query the database to retrieve the context
- you ask GPT the question by also providing the context from the query result
EXAMPLE
I have a database with Hotels and I want to ask GPT the following “Which hotels are located in Rome? Provide name and address.”
Request to OpenAI
I have the following database:
create table hotel
(
id INT not null,
name TEXT not null,
address TEXT not null,
city TEXT not null,
country TEXT not null,
latitude DOUBLE PRECISION,
longitude DOUBLE PRECISION
);
Generate the query to answer the following question:
Which hotels are located in Rome? Provide name and address.
Only provide the code.
Answer from OpenAI
SELECT name, address
FROM hotel
WHERE city = 'Rome';
I submit the query above to my database and retrieve the response, then:
Request to OpenAI
The query returned the following in JSON format:
[
{
"name": "Acropoli",
"address": "Via Principe Amedeo, 63-65"
},
{
"name": "Hotel The Brand",
"address": "Via Degli Aldobrandeschi 295"
},
{
"name": "Alius Hotel",
"address": "via Nazionale, 243"
},
{
"name": "All Time Relais & Sport Hotel",
"address": "Via Don Pasquino Borghi 100"
},
{
"name": "Amalia Vaticano Hotel",
"address": "Via Germanico 66"
},
{
"name": "Hotel American Palace Eur",
"address": "Via Laurentina, 554"
},
{
"name": "Antico Palazzo Rospigliosi",
"address": "Via Liberiana 21"
},
{
"name": "Raeli Hotel Archimede",
"address": "Via Dei Mille, 19"
},
{
"name": "Best Western Ars Hotel",
"address": "Via Monte Altissimo, 20-24"
},
{
"name": "Hotel Astoria Garden",
"address": "Via Vittorio Bachelet, 8"
}
]
Use this data to answer the following question:
Which hotels are located in Rome? Provide name and address.
Response from OpenAI
Acropoli - Via Principe Amedeo, 63-65
Hotel The Brand - Via Degli Aldobrandeschi 295
Alius Hotel - via Nazionale, 243
All Time Relais & Sport Hotel - Via Don Pasquino Borghi 100
Amalia Vaticano Hotel - Via Germanico 66
Hotel American Palace Eur - Via Laurentina, 554
Antico Palazzo Rospigliosi - Via Liberiana 21
Raeli Hotel Archimede - Via Dei Mille, 19
Best Western Ars Hotel - Via Monte Altissimo, 20-24
Hotel Astoria Garden - Via Vittorio Bachelet, 8