> ## Documentation Index
> Fetch the complete documentation index at: https://docs.kinetica.com/llms.txt
> Use this file to discover all available pages before exploring further.

# EXECUTE_AI_QUERY

<a id="sql-gpt-execute" />

Executes a SQL query equivalent of the given natural-language question using the
specified [SQL-GPT context](/content/sql/sqlgpt/create-context#sql-gpt-create-context).

```sql title="EXECUTE_AI_QUERY Table Function Syntax" theme={null}
SELECT *
FROM TABLE
(
    EXECUTE_AI_QUERY
    (
        QUESTION => '<question>',
        OPTIONS => KV_PAIRS (<option name> = '<option value>'[,...])
    )
)
```

## Parameters

<AccordionGroup>
  <Accordion title="<question>" id="<question>" defaultOpen>
    Natural-language question for which an equivalent SQL query will be executed
  </Accordion>

  <Accordion title="<schema name>" id="<schema-name>" defaultOpen>
    Name of the [schema](/content/sql/ddl/create-schema#sql-create-schema) containing the *context* to use in generating
    the query
  </Accordion>

  <Accordion title="OPTIONS" id="options" defaultOpen>
    [SQL generation options](/content/sql/sqlgpt/execute-ai-query#sql-gpt-execute-opts), 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')
    ```
  </Accordion>
</AccordionGroup>

<a id="sql-gpt-execute-opts" />

## SQL Generation Options

The following options can be specified to modify the way the executed SQL query
is generated.

<AccordionGroup>
  <Accordion title="ai_api_provider" id="ai_api_provider" defaultOpen>
    Name of an LLM service to use other than the default one; the default, `sqlgpt`, uses the
    Kinetica SQLGPT.io service.
  </Accordion>

  <Accordion title="ai_api_url" id="ai_api_url" defaultOpen>
    URL of an LLM service to use other than the default one.
  </Accordion>

  <Accordion title="context_name" id="context_name" defaultOpen>
    Name of the *SQL-GPT context* to use in generating the executed query; should be of the form
    `[<schema name>.]<context name>`.
  </Accordion>

  <Accordion title="context_names" id="context_names" defaultOpen>
    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'
    ```
  </Accordion>

  <Accordion title="use_rag" id="use_rag" defaultOpen>
    Whether to use [retrieval-augmented generation (RAG)](/content/sql-gpt/concepts#sql-gpt-overview-rag) facilities
    with the given *SQL-GPT contexts* when processing this request.
  </Accordion>
</AccordionGroup>

## SQL Execution Examples

To execute the SQL equivalent query to a given question using the
`example.atc_ctx` *SQL-GPT context*:

```sql EXECUTE_AI_QUERY Example theme={null}
SELECT *
FROM TABLE
(
	EXECUTE_AI_QUERY
	(
		QUESTION => 'What is the callsign and position of the aircraft with most recent timestamp?',
		OPTIONS => KV_PAIRS (context_name = 'example.atc_ctx')
	)
)
```

To execute the SQL equivalent query to a given question using the
`nyctaxi_ctx` & `flights_ctx` *SQL-GPT contexts* with SQL RAG:

```sql EXECUTE_AI_QUERY with RAG Example theme={null}
SELECT *
FROM TABLE
(
	EXECUTE_AI_QUERY
	(
		QUESTION => 'What was the busiest airport, including taxis and flights, during January and February of 2023?',
		OPTIONS => KV_PAIRS (context_names = 'nyctaxi_ctx, flights_ctx', use_rag = 'true')
	)
)
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.