SQLAI.ai

Text to SQL AI Generator and RAG

Integrate the text-to-SQL generator and RAG in your application.

Please read the getting started guide first.

Table of contents

Example

You can integrate with our platform by using a simple fetch request:

            
const response = await fetch('/api/ai', { 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);

Options

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.
  • model: AI model to use. It can either be gpt-4 or gpt-3.5. gpt-4 is default and recommended for most cases.

Response

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 formatted 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; };

What is RAG?

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.

Questions

If you have any questions or suggestions, please reach out.