Build an NL2SQL flow with LangStudio and RDS

更新时间:
复制 MD 格式

Use LangStudio to build an "Intelligent SQL Generation Assistant" application flow. The flow retrieves a database schema, sends it with a user's question to a large language model (LLM) to generate an SQL query, executes the query, and returns the result. The template supports ApsaraDB RDS for MySQL and can be customized for your specific needs.

Background

The "Intelligent SQL Generation Assistant" application flow integrates natural language processing with ApsaraDB RDS for MySQL. It extracts table information from a user's question, uses an LLM to generate the corresponding SQL query, and executes the query in a Python node to return the result. This lets users interact with their database using natural language, improving accuracy for complex queries and streamlining data retrieval.

Prerequisites

The application flow supports ApsaraDB RDS for MySQL. Create a database before you begin. For instructions, see the ApsaraDB RDS User Guide.

1. (Optional) Deploy an LLM

The application flow requires an LLM service. If you already have a compatible model service that supports the OpenAI API, skip this step. This tutorial uses the DeepSeek-R1 model as an example. Go to QuickStart > Model Gallery to deploy it. For more deployment details, see One-click deployment of DeepSeek-V3 and DeepSeek-R1 and Model deployment and training.

image

2. Create connections

2.1 Create an LLM service connection

Create a connection to an LLM service deployed from QuickStart > Model Gallery, which runs on PAI-EAS. For information about other connection types, see Connection configuration.

Go to LangStudio, select a workspace, and click Enter LangStudio. On the Connection > Model Service tab, click New Connection to create a generic LLM service connection.

image

Key parameters:

Parameter

Description

Model Name

For a model deployed from the Model Gallery, find its name on the details page. To access this page, click the model card on the Model Gallery page. For more information, see Create a model service connection.

Service Provider

  • PAI-EAS Model Service: This tutorial uses a PAI-EAS model service. For EAS Service, select the LLM service that you deployed in 1. (Optional) Deploy an LLM. After you make a selection, the base_url field is populated with the LLM's VPC endpoint, and the api_key field is populated with its token.

  • Third-party Model Service: Use a third-party model service. For example, if you use the official DeepSeek service, set the base_url to https://api.deepseek.com and obtain your api_key from the DeepSeek official website.

2.2 Create a database connection

On the Connection > Database tab, click New Connection to create a connection for your RDS for MySQL database.

image

Key parameters:

Parameter

Description

host/port

The internal endpoint (host) and port. For instructions on how to obtain these values, see View and manage instance endpoints and ports.

database

The database name that you specified when you created a database.

username/password

The username and password that you configured when you created a database account.

3. Create and run the application flow

  1. Go to LangStudio, select your workspace, and click Enter LangStudio. On the Application Flow tab, click New Application Flow, and then select the "Intelligent SQL Generation Assistant" template.

    image

  2. Start the runtime: Click Select Runtime in the top-right corner and select a created runtime from the drop-down list. If no runtimes are available, you can create one on the Runtime tab of the LangStudio page. Note: For VPC Configuration, you must select the VPC that you used when you created the RDS for MySQL instance in the Prerequisites section, or ensure that the selected VPC can communicate with the VPC where the RDS for MySQL instance is located.

    Important

    The runtime must be running before you can parse Python nodes or access other tools.

  3. Develop the application flow.

    image

    Keep the default settings, or change them as needed. The key node configurations are as follows:

    • Get Table Creation DDL: Retrieves the DDL statement for the database tables.

      • connection_name: Enter the name of the database connection that you created in 2.2 Create a database connection. Make sure the name exactly matches the existing connection name.

    • LLM node: Sends the user's question and the table information from the DDL node to the LLM to generate an SQL statement.

      • Model Settings: Select the connection that you created in 2.1 Create an LLM service connection.

      • Chat History: Specifies whether to enable chat history, which uses historical conversation data as an input variable.

    • Execute SQL: Executes the SQL statement returned by the LLM node and returns the query result.

      • connection_name: Enter the name of the database connection that you created in 2.2 Create a database connection. Make sure the name exactly matches the existing connection name.

    For more information about each node component, see Built-in components.

  4. Debug and run the flow. Click Run in the upper-right corner to start the application flow. For information about common runtime issues, see FAQ.

    image

  5. View the trace. Click View Traces below the generated answer to view trace details or the topology view.

    image

4. Deploy the application flow

On the application flow development page, click Deploy in the upper-right corner to deploy the flow as a PAI-EAS service. Configure the key parameters as follows and keep the defaults for other parameters.

  • Resource Deployment > Instances: Configure the number of service instances. For this test deployment, set the number of instances to 1. In a production environment, we recommend that you configure multiple service instances to avoid a single point of failure.

  • VPC > VPC: Select the VPC where the RDS for MySQL instance is located, or ensure that the selected VPC can communicate with the VPC of the RDS for MySQL instance.

For more deployment details, see Deploy an application flow.

5. Call the service

After a successful deployment, you are redirected to the PAI-EAS console. On the Debug tab, configure and send a request. The key in the request body must match the Chat Input field from the application flow's Start node. This tutorial uses the default key, question.

image

For other ways to call the service, such as by API, see Call the service.

Related documentation