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.
响应可以在处理完成后立即发送,也可以在生成响应时逐步发送。
请求标头
| Header | Description |
|---|---|
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[].rolefield to the role of the speaker, which must beuser. -
Include the user’s question in the
contentobject. In this object:- Set
typetotext. - Set
textto the user’s question.
- Set
-
包含以下项之一:
- The YAML specification for a semantic view.
- 包含语义视图规范的 YAML 文件路径。此文件必须处于暂存区中。
- 语义视图的名称。
下表描述了可在请求正文中设置的字段:
| Field | Description |
|---|---|
messages[].role | (Required) The role of the entity that is creating the message. Currently only supports 类型:string:enum Example: |
messages[].content[] | (必需)作为消息一部分的内容对象。 类型:对象
|
messages[].content[].type | (Required) The content type. Currently only 类型:string:enum Example: |
messages[].content[].text | (必填)用户的问题。 类型:字符串 Example: |
semantic_model_file | 语义模型 YAML 文件的路径。必须是一个包括数据库和架构的完全限定的暂存区 URL。 To specify multiple semantic models, use the If you want to provide the YAML specification directly in the request instead, set the 类型:字符串 Example: |
semantic_model | 包含整个语义模型 YAML 的字符串。 To specify multiple semantic models, use the If you want to point to a YAML specification in a file instead, upload the file to a stage, and set the
类型:字符串 |
semantic_models | An array containing JSON objects, each of which contains a These fields have the same semantics as the top-level
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 The advantage of using |
semantic_view | Fully qualified name of the semantic view. For example: 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
( To specify multiple semantic views, use the 类型:字符串 |
stream | (Optional) If set to 类型:布尔 |
Important
您必须在请求正文中指定以下字段之一:
semantic_model_filesemantic_modelsemantic_modelssemantic_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.
为确保向前兼容性,请确保实施考虑了内容类型并可处理类型。
| Code | Description |
|---|---|
| 200 | 语句已成功执行。 响应正文包含一个消息对象,该对象包含以下字段:
|
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.
流式传输响应
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 furthermessage.content.deltaevents 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 furthermessage.content.deltaevents 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
来自不同响应(甚至是极其相似的响应)的事件可能会有所不同。不能保证活动将以相同的顺序或相同的内容发送。
简单示例
以下是简单查询的非流式传输响应示例:
这是该响应可能发生的一系列流式传输事件(也可能一系列不同的事件):
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.
带建议的示例
此示例包含针对模糊问题的建议问题。以下是非流式传输响应:
以下是构成该响应的一系列可能的流式传输事件:
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
- warnings
- response_metadata
发送反馈
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.
请求标头
| Header | Description |
|---|---|
Authorization | (Required) Authorization token. For more information, see Authenticating to the server. |
Content-Type | (Required) application/json |
请求正文
| Field | Description |
|---|---|
request_id | (Required) The id of the request that you’ve made to send a message.
Returned in the 类型:字符串 Example: |
positive | (Required) Whether the feedback is positive or negative.
类型:布尔 示例:
|
feedback_message | (可选)用户的反馈消息。 Example: |
响应
空响应正文,状态代码 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.