Cortex Analyst REST API

使用此 API 通过自然语言查询来回答有关数据的问题。

发送消息

POST /api/v2/cortex/analyst/message

Generates a SQL query for the given question using a semantic model or semantic view provided in the request. One or more models can be specified; when multiple models are specified, Cortex Analyst chooses the most appropriate one. 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; 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.

响应可以在处理完成后立即发送,也可以在生成响应时逐步发送。

请求标头

HeaderDescription
Authorization(Required) Authorization token. For more information, see Authenticating to the server.
Content-Type(Required) application/json
X-Snowflake-Authorization-Token-Type(Optional) Authorization token type. Defaults to OAuth. For more information, see Authenticating to the server.

请求正文

在请求正文中:

  • Set the last messages[].role field to the role of the speaker, which must be user.

  • Include the user’s question in the content object. In this object:

    • Set type to text.
    • Set text to the user’s question.
  • 包含以下项之一:

    • The YAML specification for a semantic view.
    • 包含语义视图规范的 YAML 文件路径。此文件必须处于暂存区中。
    • 语义视图的名称。

下表描述了可在请求正文中设置的字段:

FieldDescription
messages[].role

(Required) The role of the entity that is creating the message. Currently only supports user.

类型:string:enum

Example: user

messages[].content[]

(必需)作为消息一部分的内容对象。

类型:对象

示例:

{
  "type": "text",
  "text":  "Which company had the most revenue?"
}
messages[].content[].type

(Required) The content type. Currently only text is supported.

类型:string:enum

Example: text

messages[].content[].text

(必填)用户的问题。

类型:字符串

Example: Which company had the most revenue?

semantic_model_file

语义模型 YAML 文件的路径。必须是一个包括数据库和架构的完全限定的暂存区 URL。

To specify multiple semantic models, use the semantic_models field.

If you want to provide the YAML specification directly in the request instead, set the semantic_model field to the YAML specification for the semantic model.

类型:字符串

Example: @my_db.my_schema.my_stage/my_semantic_model.yaml

semantic_model

包含整个语义模型 YAML 的字符串。

To specify multiple semantic models, use the semantic_models field instead.

If you want to point to a YAML specification in a file instead, upload the file to a stage, and set the semantic_model_file field to the path to the file.

类型:字符串

semantic_models

An array containing JSON objects, each of which contains a semantic_model_file or semantic_view field.

These fields have the same semantics as the top-level semantic_model_file and semantic_view fields:

  • semantic_model_file specifies a YAML file, stored in a stage, that contains a semantic model definition. (You cannot specify the YAML for the semantic model directly in the request with this form.)
  • semantic_view specifies the fully qualified name of a semantic view. For example:
{
  /* ... */
  "semantic_models": [
    {"semantic_view": "my_db.my_sch.my_sem_view_1" },
    {"semantic_view": "my_db.my_sch.my_sem_view_2" }
  ]
  /* ... */
}

For each query, Cortex Analyst chooses the most appropriate model or view from the list.

This capability simplifies user interactions with Cortex Analyst. You don’t need to choose a data source to query, and you don’t need to keep track of which semantic model or semantic view to use for each. Just specify all of your models or views with each query and let Cortex Analyst figure out which one to use.

类型:数组

Tip

Cortex Analyst does not require that you specify more than one model or view. If you specify a single model or view, the request is functionally equivalent to one containing a top-level semantic_model_file or semantic_view field.

The advantage of using semantic_models for single-model requests is that you can use the same client code, regardless of the number of models or views.

semantic_view

Fully qualified name of the semantic view. For example:

{
  /* ... */
  "semantic_view": "MY_DB.MY_SCHEMA.SEMANTIC_VIEW"
  /* ... */
}

If the name is case-sensitive or contains characters that are not allowed in an unquoted identifier, you must enclose the name in backslash-escaped double quotes. For example, if the database name, schema name, and view name include hyphens (my-database.my-schema.my-semantic-view):

{
  /* ... */
  "semantic_view": "\"my-database\".\"my-schema\".\"\"my-semantic-view\"\""
  /* ... */
}

To specify multiple semantic views, use the semantic_models field.

类型:字符串

stream

(Optional) If set to true, the response is streamed to the client using server-sent events (https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events) as it is generated (see 流式传输响应). Otherwise the complete response is returned after Cortex Analyst has fully processed the user’s question.

类型:布尔

Important

您必须在请求正文中指定以下字段之一:

  • semantic_model_file
  • semantic_model
  • semantic_models
  • semantic_view

在暂存区上的文件中指定语义模型的示例

{
    "messages": [
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": "which company had the most revenue?"
                }
            ]
        }
    ],
    "semantic_model_file": "@my_db.my_schema.my_stage/my_semantic_model.yaml"
}

指定语义视图的示例

{
  "messages": [
    {
      "role": "user",
      "content": [
        {
          "type": "text",
          "text": "which company had the most revenue?"
        }
      ]
    }
  ],
  "semantic_view": "MY_DB.MY_SCH.MY_SEMANTIC_VIEW"
}

非流式传输响应

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, and vice versa. 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.

When the request contains a semantic_models field, the response includes a semantic_model_selection field that indicates which semantic model was chosen for the request.

为确保向前兼容性,请确保实施考虑了内容类型并可处理类型。

CodeDescription
200

语句已成功执行。

响应正文包含一个消息对象,该对象包含以下字段:

  • message: Messages of the conversation between the user and analyst.
  • message (object): Represents a message within a chat.
  • message.role (string:enum): The entity that produced the message. One of user or analyst.
  • message.content[] (object): The content object that is part of a message.
  • message.content[].type (string:enum): The content type of the message. One of text, suggestion, or sql.
  • message.content[].text (string): The text of the content. Only returned for content type text.
  • message.content[].statement (string): A SQL statement. Only returned for content type sql.
  • message.content[].confidence (object): Contains confidence-related information. Only returned for the sql content type.
  • message.content[].confidence.verified_query_used (object): Represents the verified query from Verified Query Repository used in SQL response generation. If no verified query used, the field value is null.
  • message.content[].confidence.verified_query_used.name (string): The name of the verified query used, extracted from the Verified Query Repository.
  • message.content[].confidence.verified_query_used.question (string): The question that is answered by the verified query, extracted from the Verified Query Repository.
  • message.content[].confidence.verified_query_used.sql (string): The SQL statement of the verified query used, extracted from the Verified Query Repository.
  • message.content[].confidence.verified_query_used.verified_at (integer): The numeric representation of the timestamp when the query is verified, extracted from the Verified Query Repository.
  • message.content[].confidence.verified_query_used.verified_by (string): The person who verified the query, extracted from the Verified Query Repository.
  • message.content[].suggestions (string): If SQL cannot be generated, a list of questions the semantic model can generate SQL for. Only returned for content type suggestion.
  • warnings: List of warnings from the analyst about the user’s request.
  • warnings[].message (string): Contains a detailed description of one individual warning.
  • response_metadata (object): Metadata containing response generation details.
  • response_metadata.model_names: List of models used to generate response.
  • response_metadata.cortex_search_retrieval (object): Entities resolved with cortex search.
  • response_metadata.question_category (string): How the question in the request is categorized.

By default, the response is returned all at once after Cortex Analyst has fully processed the user’s question. See 流式传输响应 for the format of streaming mode responses.

{
    "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"
        }
    ],
    "response_metadata": {
        "model_names": [
            "claude-sonnet-4-5"
        ],
        "cortex_search_retrieval": [
            {
                "service": "my_db.my_schema.my_search_service",
                "response_body": {
                    "results": [
                        {
                            "CUST_NAME": "customer1"
                        }
                    ],
                    "request_id": "request1"
                },
                "query": "'customer1'"
            }
        ],
        "question_category": "CLEAR_SQL"
    }
}

流式传输响应

Streaming mode lets your client receive responses as they are generated by Cortex Analyst, rather than waiting for the entire response to be generated. This improves the perceived responsiveness of your application, especially for long-running queries, because users begin seeing output much sooner. Streaming responses also provide status information that can help you understand where Cortex Analyst is in the process of generating a response, and warnings that can help understand what went wrong when Cortex Analyst doesn’t work as you expected.

To receive a streaming response, set the stream field in the request body to true. Streaming responses use server-sent events (https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events).

Cortex Analyst sends five distinct types of events in a streaming response:

  • status: Conveys status updates about the SQL generation process.
  • message.content.delta: Contains a piece of the response. This event is sent multiple times.
  • error: Indicates that Cortex Analyst has encountered an error and cannot continue processing the request. No further message.content.delta events will be sent.
  • warnings: Contains any warnings encountered during processing. Warnings do not stop processing.
  • response_metadata: Sent at the end of a response to display data about request processing.
  • done: Sent to indicate that processing is complete and no further message.content.delta events will be sent.

Of these, the message.content.delta events are the most crucial to understand, because they contain the actual response content. Each delta contains tokens from some field in the complete response. It is possible for each delta event to contain anywhere between a single character to the full response, and they may be of different lengths. You receive these tokens as they are generated; it is up to you to assemble them into the final response.

Important

来自不同响应(甚至是极其相似的响应)的事件可能会有所不同。不能保证活动将以相同的顺序或相同的内容发送。

简单示例

以下是简单查询的非流式传输响应示例:

{
    "message": {
        "role": "analyst",
        "content": [
            {
                "type": "text",
                "text": "This is how we interpreted your question and this is how the sql is generated"
            },
            {
                "type": "sql",
                "statement": "SELECT * FROM table"
            }
        ]
    }
}

这是该响应可能发生的一系列流式传输事件(也可能一系列不同的事件):

event: status
data: { status: "interpreting_question" }

event: message.content.delta
data: {
  index: 0,
  type: "text",
  text_delta: "This is how we interpreted your question"
}

event: status
data: { status: "generating_sql" }

event: status
data: { status: "validating_sql" }

event: message.content.delta
data: {
  index: 0,
  type: "text",
  text_delta: " and this is how the sql is generated"
}

event: message.content.delta
data: {
  index: 1,
  type: "sql",
  statement_delta: "SELECT * FROM table"
}

event: status
data: { status: "done" }

Use the index field in the message.content.delta respnoses to determine which field in the full response the event is part of. For example, here the first two delta events use index 0, which means they are part of the first field (element 0) in the content array of the non-streaming response. Similarly, the delta event that contains the SQL response uses index 1.

带建议的示例

此示例包含针对模糊问题的建议问题。以下是非流式传输响应:

{
    "message": {
        "role": "analyst",
        "content": [
            {
                "type": "text",
                "text": "Your question is ambigous, here are some alternatives:"
            },
            {
                "type": "suggestions",
                "suggestions": [
                    "which company had the most revenue?",
                    "which company placed the most orders?"
                ]
            }
        ]
    }
}

以下是构成该响应的一系列可能的流式传输事件:

event: status
data: { status: "interpreting_question" }

event: message.content.delta
data: {
  index: 0,
  type: "text",
  text_delta: "Your question is ambigous,"
}

event: status
data: { status: "generating_suggestions" }

event: message.content.delta
data: {
  index: 0,
  type: "text",
  text_delta: " here are some alternatives:"
}

event: message.content.delta
data: {
  index: 1,
  type: "suggestions",
  suggestions_delta: {
    index: 0,
    suggestion_delta: "which company had",
  }
}

event: message.content.delta
data: {
  index: 1,
  type: "suggestions",
  suggestions_delta: {
    index: 0,
    suggestion_delta: " the most revenue?",
  }
}

event: message.content.delta
data: {
  index: 1,
  type: "suggestions",
  suggestions_delta: {
    index: 1,
    suggestion_delta: "which company placed",
  }
}

event: message.content.delta
data: {
  index: 1,
  type: "suggestions",
  suggestions_delta: {
    index: 1,
    suggestion_delta: " the most orders?",
  }
}

event: status
data: { status: "done" }

In this example, the content field of the non-streaming response is an array. One of the elements of content is the suggestions array. So the meaning of index fields for text and suggestions delta events refer to the location of elements in these two different arrays. You will need to keep track of these indexes separately when assembling the full response.

Note

当前,生成的 SQL 语句总是在单个事件中发送。将来可能不是这样。您的客户端必须做好在多个事件中收到 SQL 语句的准备。

其他示例

You can find a Streamlit streaming client for Cortex Analyst in the Cortex Analyst GitHub repo (https://github.com/Snowflake-Labs/sfguide-getting-started-with-cortex-analyst/blob/main/cortex_analyst_streaming_demo.py). This demo must be run locally; SiS does not currently support streaming.

请参阅 AI/ML Studio 中(在 Snowsight 中)的 Cortex Analyst Playground,了解流式传输响应的交互式演示。

流式传输事件架构

The following are the OpenAPI/Swagger schemas of the events sent by Cortex Analyst in a streaming response.

status
message.content.delta
error
StreamingError:
type: object
properties:
  message:
    type: string
    description: A description of the error
  code:
    type: string
    description: The Snowflake error code categorizing the error
  request_id:
    type: string
    description: Unique request ID
warnings
Warnings:
type: object
description: Warnings found while processing the request
properties:
  warnings:
    type: array
    items:
      $ref: "#/components/schemas/Warning"
Warning:
type: object
title: The warning object
description: Represents a warning within a chat.
properties:
  message:
    type: string
    description: A human-readable message describing the warning
response_metadata
ResponseMetadata:
type: object
description: Details about request processing

发送反馈

POST /api/v2/cortex/analyst/feedback

Provides qualitative end-user feedback. Within Snowsight, the feedback is shown in Snowsight → AI & ML → Cortex Analyst → Select Semantic View → Monitoring tab.

请求标头

HeaderDescription
Authorization(Required) Authorization token. For more information, see Authenticating to the server.
Content-Type(Required) application/json

请求正文

FieldDescription
request_id

(Required) The id of the request that you’ve made to send a message. Returned in the request_id field of /api/v2/cortex/analyst/message. For more information, see 非流式传输响应.

类型:字符串

Example: 75d343ee-699c-483f-83a1-e314609fb563

positive

(Required) Whether the feedback is positive or negative. true for positive or “thumbs up”, false for negative or “thumbs down”.

类型:布尔

示例:

true

feedback_message

(可选)用户的反馈消息。

Example: This is the best answer I've ever seen!

响应

空响应正文,状态代码 200。

访问控制要求

For information on the required privileges, see Access control requirements.

For details about authenticating to the API, see Authenticating Snowflake REST APIs with Snowflake.