GenerateSqlFromNL

更新时间:
复制 MD 格式

This API converts a natural language description into an executable SQL statement by retrieving relevant database and business information.

Operation description

In addition to the generated SQL statement, the result provides other useful information, such as the tables used, related concepts, and similar SQL.

Try it now

Try this API in OpenAPI Explorer, no manual signing needed. Successful calls auto-generate SDK code matching your parameters. Download it with built-in credential security for local usage.

Test

RAM authorization

The table below describes the authorization required to call this API. You can define it in a Resource Access Management (RAM) policy. The table's columns are detailed below:

  • Action: The actions can be used in the Action element of RAM permission policy statements to grant permissions to perform the operation.

  • API: The API that you can call to perform the action.

  • Access level: The predefined level of access granted for each API. Valid values: create, list, get, update, and delete.

  • Resource type: The type of the resource that supports authorization to perform the action. It indicates if the action supports resource-level permission. The specified resource must be compatible with the action. Otherwise, the policy will be ineffective.

    • For APIs with resource-level permissions, required resource types are marked with an asterisk (*). Specify the corresponding Alibaba Cloud Resource Name (ARN) in the Resource element of the policy.

    • For APIs without resource-level permissions, it is shown as All Resources. Use an asterisk (*) in the Resource element of the policy.

  • Condition key: The condition keys defined by the service. The key allows for granular control, applying to either actions alone or actions associated with specific resources. In addition to service-specific condition keys, Alibaba Cloud provides a set of common condition keys applicable across all RAM-supported services.

  • Dependent action: The dependent actions required to run the action. To complete the action, the RAM user or the RAM role must have the permissions to perform all dependent actions.

Action

Access level

Resource type

Condition key

Dependent action

dms:GenerateSqlFromNL

none

*All Resource

*

None None

Request syntax

POST / HTTP/1.1

Request parameters

Parameter

Type

Required

Description

Example

DbId

string

Yes

The database ID. You can call the ListDatabases operation to obtain this ID.

The API operation uses the schema from this database to generate the SQL statement.

1***

Knowledge

string

No

Supplementary business information to help the model better understand the question.

state>0代表成功

Question

string

Yes

The natural language question to convert into an SQL statement.

查询今天的用户访问量

Dialect

string

No

The SQL dialect. If unspecified, the system defaults to MySQL and generates a standard SQL statement.

MySQL

Level

string

No

The processing level. The default value is base.

base

Model

string

No

The Qwen series model to use.

qwen-plus

TableNames

string

No

A comma-separated list of table names. If specified, the model uses only these tables to generate the SQL statement.

users,orders

RealLoginUserUid

string

No

The Alibaba Cloud account ID of the user who calls the operation.

21400447956867****

Response elements

Element

Type

Description

Example

object

The data returned in the response.

RequestId

string

The request ID. You can use this ID to locate logs and troubleshoot issues.

0C1CB646-1DE4-4AD0-B4A4-7D47DD52E931

ErrorCode

string

The error code.

UnknownError

ErrorMessage

string

The error message.

UnknownError

Success

boolean

Indicates whether the request was successful.

  • true: The request was successful.

  • false: The request failed.

true

Data

object

The returned data.

Question

string

The natural language question provided as input.

查询今天用户访问量

Tables

array<object>

A list of tables used to generate the SQL statement.

object

A table object. Each object represents a specific table and includes information such as the table name and table ID.

TableName

string

The name of the table.

ins_1.db1.table1

KnowledgeReferences

array<object>

A list of knowledge items referenced during SQL generation.

object

A knowledge item.

Name

string

The name of the knowledge item.

sample_tbl

Content

string

The content of the knowledge item.

{'Status': 'OK'}

Level

string

The status of the knowledge item. Valid values include to_complete, to_review, to_verify, and verified.

Valid values:

  • to_verify :

  • verified :

  • to_complete :

  • to_review :

verified

Sql

string

The generated SQL statement.

SELECT * FROM table WHERE condition;

SimilarSql

array<object>

A list of similar SQL statements.

object

Information about a similar SQL statement.

Question

string

The natural language question.

实例xxx的访问量有多少?

Sql

string

A similar SQL statement.

SELECT * WHERE ResourceType = 'ACS::ECS::Instance' AND ResourceGroupId != 'rg-xxx'

Thought

string

The thought process used to generate the SQL statement.

度量(指标): 用户询问了xx指标,根据上下文,需要计算xxx。 维度(筛选条件): 用户问题里提到了xx的条件,结合上下文,核心维度为xxx。 综上,需要使用xxx表,使用xxx列。其中xxx作为projection选择,xxx用于过滤条件,xxx用于分组条件。

Score

string

The confidence score for the generated SQL statement, ranging from 0 to 1. A higher score indicates greater confidence.

0.52

Thought

string

The thought process used to generate the SQL statement.

通过分析用户的问题和提供的知识,生成了相应的SQL语句。

Examples

Success response

JSON format

{
  "RequestId": "0C1CB646-1DE4-4AD0-B4A4-7D47DD52E931",
  "ErrorCode": "UnknownError",
  "ErrorMessage": "UnknownError",
  "Success": true,
  "Data": {
    "Question": "查询今天用户访问量",
    "Tables": [
      {
        "TableName": "ins_1.db1.table1"
      }
    ],
    "KnowledgeReferences": [
      {
        "Name": "sample_tbl",
        "Content": "{'Status': 'OK'}",
        "Level": "verified"
      }
    ],
    "Sql": "SELECT * FROM table WHERE condition;",
    "SimilarSql": [
      {
        "Question": "实例xxx的访问量有多少?",
        "Sql": "SELECT * WHERE ResourceType = 'ACS::ECS::Instance' AND ResourceGroupId != 'rg-xxx'",
        "Thought": "度量(指标): 用户询问了xx指标,根据上下文,需要计算xxx。\n维度(筛选条件): 用户问题里提到了xx的条件,结合上下文,核心维度为xxx。\n综上,需要使用xxx表,使用xxx列。其中xxx作为projection选择,xxx用于过滤条件,xxx用于分组条件。",
        "Score": "0.52"
      }
    ],
    "Thought": "通过分析用户的问题和提供的知识,生成了相应的SQL语句。"
  }
}

Error codes

See Error Codes for a complete list.

Release notes

See Release Notes for a complete list.