Cortex Analyst REST API¶
Use this API to answer questions about your data with natural language queries.
Send message¶
POST /api/v2/cortex/analyst/message
Generates a SQL query for the given question using the semantic model provided in the request. Note that the API is stateless, so only single-turn conversations are supported.
The request includes a user question and the response includes the user question and the analyst response. Each message in a response
can have multiple content blocks of different types. Three values that are currently supported for the type
field of the content
object are: text
, suggestions
, and sql
.
Request Headers¶
Header |
Description |
---|---|
|
(Required) Authorization token. For more information, see Authenticating to the server. |
|
(Required) application/json |
Request Body¶
The request body contains the role of the speaker which must be user
, the user’s question and a path to the semantic model YAML file.
The user question is contained in a content
object which has two fields, type
and text
. text
is also the only allowed
value of the field type
.
Field |
Description |
---|---|
|
(Required) The role of the entity that is creating the message. Currently only supports Type: string:enum Example: |
|
(Required) The content object that is part of a message. Type: object
|
|
(Required) The content type. Currently only Type: string:enum Example: |
|
(Required) The user’s question. Type: string Example: |
|
Path to the semantic model YAML file. Must be a fully qualified stage URL including the database and schema.
You can instead provide the complete semantic model YAML in the Type: string Example: |
|
A string containing the entire semantic model YAML. You can instead pass the semantic model YAML as a staged file
by providing its URL in the Type: string |
Important
You must provide either semantic_model_file
or semantic_model
in the request body.
Example¶
{
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "which company had the most revenue?"
}
]
},
],
"semantic_model_file": "@my_stage/my_semantic_model.yaml"
}
Response¶
This operation can return the response codes listed below.
The response always has the following structure. Currently, three content types are supported for the
response, text
, suggestion
, and sql
. The content types suggestion
and sql
are mutually exclusive so that if the
response contains a sql
content type, it won’t contain a suggestion
content type. The suggestion
content type is only included
in a response if the user question was ambiguous and Cortex Analyst could not return a SQL statement for that query.
To ensure forward compatibility, make sure your implementation takes the content type into account and handles types.
Code |
Description |
---|---|
200 |
The statement was executed successfully. The body of the response contains a message object that contains the following fields:
{
"message":
{
"role": "analyst",
"content":
[
{
"type": "text",
"text": "We interpreted your question as ..."
},
{
"type": "sql",
"statement": "SELECT * FROM table",
"confidence":
{
"verified_query_used":
{
"name": "My verified query",
"question": "What was the total revenue?",
"sql": "SELECT * FROM table2",
"verified_at": 1714497970,
"verified_by": "Jane Doe"
}
}
}
]
}
}
|
Access control requirements¶
For information on the required privileges, see Access control requirements.
For details on how to authenticate to the API, see Authenticating Snowflake REST APIs with Snowflake.