Integrate the text-to-SQL generator and RAG in your application.
You can integrate with our platform by using a simple fetch
request:
const response = await fetch('https://api.sqlai.ai/api/public/v1', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
authentication: `Bearer ${SECRET_TOKEN}`,
},
body: JSON.stringify({
prompt: 'Get user with id 13',
mode: 'textToSQL',
engine: 'postgres',
}),
});
const { query, error } = await res.json();
if (error) {
// Handle error
}
// Run generated SQL query on your database
const data = await client.query(query);
Send POST
request to:
https://api.sqlai.ai/api/public/v1
Options when requesting a SQL generation:
prompt
(required): Tell AI what you want to do using everyday language.mode
(required): The generator to use, textToSQL
.engine
: The database engine to use:
mysql
postgres
oracle
mssql
snowflake
mariadb
sqlite
bigquery
mongodb
sql
(native SQL is default)engineVersion
: Database engine version number/code as string, e.g. "16"
, "5.7"
, "21c"
.dataSourceId
: Use a data source when generating SQL, e.g. database schema hosted on SQLAI.ai.dataSourceRaw
: Use included data source to generate SQL. CSV format recommended for SQL databases with: table_name, table_column, table_type
(table type can be left out if needed, AI can usually infer it).useRAG
: Set to true
to use RAG when generating SQL together with a dataSourceId
. The data source can be RAG-trained in our app. Defaults to false
(deprecated).The above example will return this response:
{
"query": "SELECT * FROM users WHERE id = 13;",
"content": "To get the user with id 13, you can use the following SQL query:\n\n```sql\nSELECT *\nFROM users\nWHERE id = 13;\n```\n\nThis query selects all columns from the `users` table where the `id` is equal to 13.",
"queries": {
"markdown": ["```sql\nSELECT *\nFROM users\nWHERE id = 13;\n```"],
"clean": ["SELECT * FROM users WHERE id = 13;"]
},
"meta": {
"usage": {
"prompt_tokens": 56,
"completion_tokens": 55,
"total_tokens": 111
},
"finish_reason": "stop"
}
}
The response object type:
type ResponseBody = {
query: string; // Generated SQL query stripped of formatting
content: string; // Complete Markdown generation
queries: {
markdown: string[]; // All SQL queries with formatting
clean: string[]; // All SQL queries stripped of formatting
};
meta: {
usage: { completion_tokens: number; prompt_tokens: number; total_tokens: number };
finish_reason: 'stop' | 'length' | 'tool_calls' | 'content_filter' | 'function_call';
};
error: string | undefined;
};
RAG means retrieval-augmented generation and it can be used together with a database schema hosted on www.SQLAI.ai, e.g. referenced by the dataSourceId
argument when making requests. You can read more about it here.
If you have any questions or suggestions, please reach out.