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. You can have multi-turn conversations where you can ask follow-up questions that build upon previous queries. For more information, see Multi-turn conversation in Cortex Analyst.
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. |
|
Authorization token type. Defaults to OAuth; required if using any other type of 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:
{
"request_id": "75d343ee-699c-483f-83a1-e314609fb563",
"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"
}
}
}
]
},
"warnings": [
{
"message": "Table table1 has (30) columns, which exceeds the recommended maximum of 10"
},
{
"message": "Table table2 has (40) columns, which exceeds the recommended maximum of 10"
}
]
}
|
Send feedback¶
POST /api/v2/cortex/analyst/feedback
Provides qualitative end user feedback. Within Snowsight, the feedback is shown in Semantic Model Admins under Monitoring.
Request Headers¶
Header |
Description |
---|---|
|
(Required) Authorization token. For more information, see Authenticating to the server. |
|
(Required) application/json |
Request Body¶
Field |
Description |
---|---|
|
(Required) The id of the request that you’ve made to send a message.
Returned in the Type: string Example: |
|
(Required) Whether the feedback is positive or negative.
Type: boolean Example:
|
|
(Optional) The feedback message from the user. Example: |
Response¶
Empty response body with status code 200.
Access control requirements¶
For information on the required privileges, see Access control requirements.
For details about authenticating to the API, see Authenticating Snowflake REST APIs with Snowflake.