> ## 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

Executes a SQL query equivalent of the given natural-language question using the
specified [SQL-GPT context](https://docs.kinetica.com/content/sql/sqlgpt/create-context.md#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

> [accordion] \<question>
>
> Natural-language question for which an equivalent SQL query will be executed

> [accordion] \<schema name>
>
> Name of the [schema](https://docs.kinetica.com/content/sql/ddl/create-schema.md#sql-create-schema) containing the *context* to use in generating
> the query

> [accordion] OPTIONS
>
> [SQL generation options](https://docs.kinetica.com/content/sql/sqlgpt/execute-ai-query.md#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')
> ```

## SQL Generation Options

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

> [accordion] ai\_api\_provider
>
> Name of an LLM service to use other than the default one; the default, `sqlgpt`, uses the
> Kinetica SQLGPT.io service.

> [accordion] ai\_api\_url
>
> URL of an LLM service to use other than the default one.

> [accordion] context\_name
>
> Name of the *SQL-GPT context* to use in generating the executed query; should be of the form
> `[<schema name>.]<context name>`.

> [accordion] 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'
> ```

> [accordion] use\_rag
>
> Whether to use [retrieval-augmented generation (RAG)](https://docs.kinetica.com/content/sql-gpt/concepts.md#sql-gpt-overview-rag) facilities
> with the given *SQL-GPT contexts* when processing this request.

## 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.