Kinetica provides support for generative AI via SQL-GPT.
SQL-GPT context management features accessible via SQL include:
For SQL-GPT context permission management, see:
SQL-GPT query generation & execution features accessible via SQL include:
CREATE CONTEXT
Creates a new SQL-GPT context. The context can contain one or more base tables, a set of sample question/answer pairs, and a set of rules for the table to aid in generating appropriate SQL. While the tables, samples, and rules clauses are all optional, a context must contain at least one clause of any kind to be considered valid.
|
|
Parameters | Description |
---|---|
OR REPLACE | Any existing context reference with the same name will be dropped before creating this one |
<schema name> | Name of the schema containing the context |
<context name> | Name of the context |
<table definition clause> | Optional clause containing the definitions of all tables to be used within the context |
<sample definition clause> | Optional clause containing sample question/answer pairs that can help SQL-GPT understand the relationships between natural language and the tables within the context |
<rule definition clause> | Optional clause containing global rules SQL-GPT should take into account when generating SQL from the tables within the context |
Table Definition Clause
The table definition clause provides information about a single table to SQL-GPT. All parameters are optional except for the table name. The table definition clause itself is also optional.
Important
Kinetica will pass the all columns of the table to SQL-GPT regardless of whether they are named in the COMMENTS or RULES sections. To avoid consuming unnecessary space in the SQL-GPT context with the unused columns, create a view with descriptive column names containing only the columns needed for generating SQL.
|
|
Property | Description |
---|---|
TABLE | Specifier for the mandatory table definition table name |
<schema name> | Name of the schema containing the table to include in the context |
<table name> | Name of the table to include in the context |
COMMENT | Specifier for an optional table comment |
<table comment> | Note about the table that can give SLQ-GPT information about its use and the data it holds |
COMMENTS | Specifier for an optional comma-separated list of column name & comment pairs |
<column name> | Name of a column in the given table |
<column comment> | Note about the column that can give SQL-GPT information about its use and the specific data it holds |
RULES | Specifier for an optional comma-separated list of rules for SQL-GPT to use in employing this table to generate SQL |
<table rule> | Rule for SQL-GPT to use in employing this table to generate SQL; e.g., which columns should be considered (or not considered) when generating SQL using the table |
Sample Definition Clause
A sample definition contains a set of question & answer pairs used to train SQL-GPT for generating SQL. Few (if any) samples should be required to customize SQL-GPT for a use case. The sample definition clause is optional.
Tip
It is advised to use no more than 10 samples per context object as they take up space in the context.
Important
A sample definition should reference tables that are included in table definitions within either its own context or a separate context that is passed along with it into a GENERATE SQL or EXECUTE_AI_QUERY command. This ensures that SQL-GPT has a proper definition (and understanding) of the tables and columns referenced in each sample query.
|
|
Property | Description |
---|---|
SAMPLES | Sample definition specifier keyword |
<question> | Natural-language question that will be answered by the corresponding <SQL answer> query |
<SQL answer> | SQL query that answers <question> using the tables contained within the context Note Quoted constants within the query will need to be escaped as per standard SQL convention; e.g., 'lamp' in the following: 'How many lamps?' = 'SELECT COUNT(*) FROM product WHERE type = ''lamp'';' |
Rule Definition Clause
A rule definition contains a set of global rules that SQL-GPT should use in employing the various tables named in the context to generate SQL. By convention, rules that apply to a specific table belong in the RULES section of the table definition, while rules that apply across tables or independently from any tables belong here. The rule definition clause is optional.
Important
A rule definition should reference tables that are included in table definitions within either its own context or a separate context that is passed along with it into a GENERATE SQL or EXECUTE_AI_QUERY command. This ensures that SQL-GPT has a proper definition (and understanding) of any tables and columns referenced in each rule.
|
|
Property | Description |
---|---|
RULES | Global rule definition specifier keyword |
<global rule> | Global rule for SQL-GPT to use in employing multiple tables to generate SQL (joins, etc.) or when referencing database features that are independent of any table (functions, etc.) |
Example
The following example creates a SQL-GPT context with the following criteria:
- Contains one table, atc_current_position, with:
- a description
- 4 column comments
- 1 rule suggesting avoidance of one of the columns
- Contains one table, atc_points, with no extra definitions
- Contains one table, airport_positions, with:
- a description
- 2 column comments
- Contains 2 sample questions and their corresponding queries
- Contains 1 global rule suggesting how the atc_current_position & atc_points tables should be joined together
|
|
ALTER CONTEXT
Alters the configuration of a SQL-GPT context. Multiple context entries can be added & removed in one command. The general form is:
|
|
Add Context Table
A context table can be added to a SQL-GPT context.
|
|
See Table Definition Clause for syntax of the <table definition clause>.
For example, to add the example.atc_country table to the example.atc_ctx SQL-GPT context:
|
|
Drop Context Table
A context table can be removed from a SQL-GPT context.
|
|
For example, to drop the example.atc_country table from the example.atc_ctx SQL-GPT context:
|
|
Add Context Samples
Context samples can be added to a SQL-GPT context that has not been configured with any. To add samples to a context already configured with one or more, first drop all existing samples, and then add the new and any previous samples all at once.
|
|
See Sample Definition Clause for syntax of the <sample definition clause>.
For example, to add a sample query for finding JFK airport's location to the example.atc_ctx SQL-GPT context:
|
|
Drop Context Samples
Context samples can be removed from a SQL-GPT context that has been configured with one or more.
|
|
For example, to drop all context samples from the example.atc_ctx SQL-GPT context:
|
|
Add Context Rules
Context rules can be added to a SQL-GPT context that has not been configured with any. To add rules to a context already configured with one or more, first drop all existing rules, and then add the new and any previous rules all at once.
|
|
See Rule Definition Clause for syntax of the <rule definition clause>.
For example, to add a rule for joining the example.airport_positions table with the atc_current_position table to the example.atc_ctx SQL-GPT context:
|
|
Drop Context Rules
Context rules can be removed from a SQL-GPT context that has been configured with one or more.
|
|
For example, to drop all context rules from the example.atc_ctx SQL-GPT context:
|
|
DROP CONTEXT
Removes an existing SQL-GPT context.
|
|
For example, to remove the example.atc_ctx SQL-GPT context:
|
|
SHOW CONTEXT
Outputs the DDL statement required to reconstruct the given SQL-GPT context.
|
|
Note
The response to SHOW CONTEXT is a single-column result set with the DDL statement as the value in the CONTEXT_DEFINITION column.
Parameters | Description |
---|---|
<schema name> | Name of the schema containing the SQL-GPT context to show |
<context name> | Name of the existing SQL-GPT context for which the DDL will be output; use * instead of schema/context name to output the DDL of all contexts |
For example, to output the DDL for a SQL-GPT context, example.atc_ctx:
|
|
To output the DDL for all SQL-GPT contexts:
|
|
DESCRIBE CONTEXT
Outputs the configuration of an existing SQL-GPT context.
|
|
Note
The response to DESCRIBE CONTEXT is a seven-column result set:
- CONTEXT_NAME - name of the SQL-GPT context
- OBJECT_TYPE - type of the context entity being described within the context
- OBJECT_NAME - for a table entity type, the name of the table
- OBJECT_DESCRIPTION - for a table entity type, the table comment
- OBJECT_RULES - for a table entity type, rules associated with the table
- OBJECT_COMMENTS - for a table entity type, the table's column comments
- OBJECT_SAMPLES - for a samples entity type, the list of question/answer pairs
Parameters | Description |
---|---|
<schema name> | Name of the schema containing the SQL-GPT context to describe |
<context name> | Name of the existing SQL-GPT context for which the configuration will be output; use * instead of schema/context name to output the configuration of all contexts |
For example, to show the configuration for a SQL-GPT context, example.atc_ctx:
|
|
To show the configuration for all SQL-GPT contexts:
|
|
GENERATE SQL
Creates a SQL query equivalent of the given natural-language question using the specified SQL-GPT context.
|
|
Parameters | Description |
---|---|
<question> | Natural-language question for which an equivalent SQL query will be generated |
<schema name> | Name of the schema containing the context to use in generating the query |
WITH OPTIONS | Optional indicator that a comma-delimited list of SQL generation option/value assignments will follow. See SQL Generation Options for the complete list of options. |
SQL Generation Options
The following options can be specified to modify the way SQL queries are generated.
Option | Description |
---|---|
ai_api_provider | Name of an LLM service to use other than the default one; the default, sqlgpt, uses the Kinetica SQLGPT.io service. |
ai_api_url | URL of an LLM service to use other than the default one |
context_name | Name of the SQL-GPT context to use as a basis for generating SQL queries; should be of the form [<schema name>.]<context name>. |
context_names | Comma-separated list of names of SQL-GPT contexts to use as a basis for generating SQL queries; each should be of the form [<schema name>.]<context name>, and the entire list should be single-quoted. For example: context_names = 'example.atc_ctx,example.nyctaxi' |
SQL Generation Example
For example, to generate the SQL equivalent query to a given question using the example.atc_ctx SQL-GPT context:
|
|
EXECUTE_AI_QUERY
Executes a SQL query equivalent of the given natural-language question using the specified SQL-GPT context.
|
|
Parameters | Description |
---|---|
<question> | Natural-language question for which an equivalent SQL query will be executed |
<schema name> | Name of the schema containing the context to use in generating the query |
OPTIONS | SQL generation options, specified as a set of key/value pairs passed as a comma-delimited list of <key> = '<value>' assignments to the KV_PAIRS function; e.g.: KV_PAIRS(context_name = 'atc_ctx') |
SQL Generation Options
The following options can be specified to modify the way the executed SQL query is generated.
Option | Description |
---|---|
ai_api_provider | Name of an LLM service to use other than the default one; the default, sqlgpt, uses the Kinetica SQLGPT.io service. |
ai_api_url | URL of an LLM service to use other than the default one |
context_name | Name of the SQL-GPT context to use in generating the executed query; should be of the form [<schema name>.]<context name>. |
context_names | Comma-separated list of names of SQL-GPT contexts to use in generating the executed query; each should be of the form [<schema name>.]<context name>, and the entire list should be single-quoted. For example: context_names = 'example.atc_ctx,example.nyctaxi' |
SQL Execution Example
For example, to execute the SQL equivalent query to a given question using the example.atc_ctx SQL-GPT context:
|
|