Skip to main content

Xiyan GBI best practices

This topic describes how to use the Xiyan GBI API to implement an intelligent data Q&A feature and helps you quickly integrate the API into your projects.

Prerequisites

Procedure

  • Java
  • Python
  • Go

Step 1: Install the Xiyan GBI SDK for Java

  1. Obtain the latest version number of the Xiyan GBI Java SDK.
  2. Open the pom.xml file of your Maven project.
  3. Add the following dependency to the <dependencies> tag. Replace the version number in the <version></version> tag with the latest version.
 <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>alibabacloud-dataanalysisgbi20240823</artifactId>
            <version>1.0.0</version>
        </dependency>
  1. Save the pom.xml file.
  2. Update the project dependencies to add the SDK to your project.

Step 2: Implement the intelligent data Q&A feature

This example uses the Xiyan GBI RunDataAnalysis - Chat API and the RunDataAnalysisRequest class to demonstrate how to implement an intelligent data Q&A scenario.Replace accessKeyId, accessKeySecret, and workspaceId in the code example with your actual values to ensure that the code runs correctly and returns the expected results.
package com.alibaba.iic.llmsolution.gbi.util;

import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.gateway.pop.Configuration;
import com.aliyun.sdk.gateway.pop.auth.SignatureVersion;
import com.aliyun.sdk.service.dataanalysisgbi20240823.AsyncClient;
import com.aliyun.sdk.service.dataanalysisgbi20240823.models.RunDataAnalysisRequest;
import com.aliyun.sdk.service.dataanalysisgbi20240823.models.RunDataAnalysisResponseBody;
import com.google.gson.Gson;
import darabonba.core.ResponseIterable;
import darabonba.core.ResponseIterator;
import darabonba.core.client.ClientOverrideConfiguration;

public class CommonExample {
    public static void main(String[] args) throws Exception {

        StaticCredentialProvider provider = StaticCredentialProvider.create(
                Credential.builder()
                        .accessKeyId("accessKeyId")
                        .accessKeySecret("accessKeySecret")
                        .build()
        );

        AsyncClient client = AsyncClient.builder()
                .region("cn-beijing")
                .credentialsProvider(provider)
                .serviceConfiguration(Configuration.create()
                        .setSignatureVersion(SignatureVersion.V3)
                )
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setProtocol("HTTPS")
                                .setEndpointOverride("dataanalysisgbi.cn-beijing.aliyuncs.com")
                )
                .build();
        // Controls the overall parameters for conversational analysis.
        RunDataAnalysisRequest request = RunDataAnalysisRequest.builder()
                // The ID of the workspace for the current conversation request.
                .workspaceId("workspaceId")
                // The user query for the current conversation request.
                .query("Query the top five products by sales")
                // The version for the current conversation request. If the specified version is not purchased, the default workspace version is used.
                .specificationType("STANDARD_MIX")
                // Specifies whether to generate only SQL and skip the visualization module.
                .generateSqlOnly(true)
                .build();

        // Gets and parses the streaming content.
        ResponseIterable<RunDataAnalysisResponseBody> x = client.runDataAnalysisWithResponseIterable(request);
        ResponseIterator<RunDataAnalysisResponseBody> iterator = x.iterator();
        while (iterator.hasNext()) {
            RunDataAnalysisResponseBody event = iterator.next();
            System.out.println(new Gson().toJson(event.getData()));
            // sql_part is the event for streaming SQL output. It streams the generated SQL content.
            if (event.getData().getEvent().equals("sql_part")) {
                System.out.println(event.getData().getSql());
            }
            // sql is the final SQL generation event. It is not streamed and directly provides the final SQL.
            if (event.getData().getEvent().equals("sql")) {
                System.out.println(event.getData().getSql());
            }
            // The request result contains the execution result of the visualization model, which can be rendered.
            if (event.getData().getVisualization() != null) {
                System.out.println("The current conversation analysis contains visualization module content that can be rendered");
            }
        }
        System.out.println("ALL***********************");
        System.out.println("Request header values for a successful request:");
        System.out.println(x.getStatusCode());
        System.out.println(x.getHeaders());
    }

}

Sample response

For parameter descriptions, see the Return parameters section of the RunDataAnalysis - Chat API.
{"event":"rewrite","requestId":"8499A3E6-C1B2-5D2E-845F-2F63F8795B4D","rewrite":"Query the top five products by sales and their sales volumes","sessionId":"8db26af8-721c-49bd-95f5-cceb0053ecf6"}
{"event":"selector","requestId":"8499A3E6-C1B2-5D2E-845F-2F63F8795B4D","rewrite":"Query the top five products by sales and their sales volumes","selector":["products","orders"],"sessionId":"8db26af8-721c-49bd-95f5-cceb0053ecf6"}
{"event":"evidence","evidence":"For all metrics, if not all dimensions are restricted, you must perform a SUM aggregation before other aggregations or filtering","rewrite":"Query the top five products by sales and their sales volumes","selector":["products","orders"]}
{"event":"sql_part","evidence":"For all metrics, if not all dimensions are restricted, you must perform a SUM aggregation before other aggregations or filtering","requestId":"8499A3E6-C1B2-5D2E-845F-2F63F8795B4D","rewrite":"Query the top five products by sales and their sales volumes","selector":["products","orders"],"sessionId":"8db26af8-721c-49bd-95f5-cceb0053ecf6","sql":"sql"}
sql
{"event":"sql_part","evidence":"For all metrics, if not all dimensions are restricted, you must perform a SUM aggregation before other aggregations or filtering","requestId":"8499A3E6-C1B2-5D2E-845F-2F63F8795B4D","rewrite":"Query the top five products by sales and their sales volumes","selector":["products","orders"],"sessionId":"8db26af8-721c-49bd-95f5-cceb0053ecf6","sql":""}

{"event":"sql_part","evidence":"For all metrics, if not all dimensions are restricted, you must perform a SUM aggregation before other aggregations or filtering","requestId":"8499A3E6-C1B2-5D2E-845F-2F63F8795B4D","rewrite":"Query the top five products by sales and their sales volumes","selector":["products","orders"],"sessionId":"8db26af8-721c-49bd-95f5-cceb0053ecf6","sql":"SELECT"}
SELECT
{"event":"sql_part","evidence":"For all metrics, if not all dimensions are restricted, you must perform a SUM aggregation before other aggregations or filtering","requestId":"8499A3E6-C1B2-5D2E-845F-2F63F8795B4D","rewrite":"Query the top five products by sales and their sales volumes","selector":["products","orders"],"sessionId":"8db26af8-721c-49bd-95f5-cceb0053ecf6","sql":"SELECT p.product_name, SUM(o.quantity)"}
SELECT p.product_name, SUM(o.quantity)
{"event":"sql_part","evidence":"For all metrics, if not all dimensions are restricted, you must perform a SUM aggregation before other aggregations or filtering","requestId":"8499A3E6-C1B2-5D2E-845F-2F63F8795B4D","rewrite":"Query the top five products by sales and their sales volumes","selector":["products","orders"],"sessionId":"8db26af8-721c-49bd-95f5-cceb0053ecf6","sql":"SELECT p.product_name, SUM(o.quantity) AS total_sales "}
SELECT p.product_name, SUM(o.quantity) AS total_sales
{"event":"sql_part","evidence":"For all metrics, if not all dimensions are restricted, you must perform a SUM aggregation before other aggregations or filtering","requestId":"8499A3E6-C1B2-5D2E-845F-2F63F8795B4D","rewrite":"Query the top five products by sales and their sales volumes","selector":["products","orders"],"sessionId":"8db26af8-721c-49bd-95f5-cceb0053ecf6","sql":"SELECT p.product_name, SUM(o.quantity) AS total_sales FROM products p "}
SELECT p.product_name, SUM(o.quantity) AS total_sales FROM products p
{"event":"sql_part","evidence":"For all metrics, if not all dimensions are restricted, you must perform a SUM aggregation before other aggregations or filtering","requestId":"8499A3E6-C1B2-5D2E-845F-2F63F8795B4D","rewrite":"Query the top five products by sales and their sales volumes","selector":["products","orders"],"sessionId":"8db26af8-721c-49bd-95f5-cceb0053ecf6","sql":"SELECT p.product_name, SUM(o.quantity) AS total_sales FROM products p JOIN orders o ON"}
SELECT p.product_name, SUM(o.quantity) AS total_sales FROM products p JOIN orders o ON
{"event":"sql_part","evidence":"For all metrics, if not all dimensions are restricted, you must perform a SUM aggregation before other aggregations or filtering","requestId":"8499A3E6-C1B2-5D2E-845F-2F63F8795B4D","rewrite":"Query the top five products by sales and their sales volumes","selector":["products","orders"],"sessionId":"8db26af8-721c-49bd-95f5-cceb0053ecf6","sql":"SELECT p.product_name, SUM(o.quantity) AS total_sales FROM products p JOIN orders o ON p.product_id \u003d"}
SELECT p.product_name, SUM(o.quantity) AS total_sales FROM products p JOIN orders o ON p.product_id =
{"event":"sql_part","evidence":"For all metrics, if not all dimensions are restricted, you must perform a SUM aggregation before other aggregations or filtering","requestId":"8499A3E6-C1B2-5D2E-845F-2F63F8795B4D","rewrite":"Query the top five products by sales and their sales volumes","selector":["products","orders"],"sessionId":"8db26af8-721c-49bd-95f5-cceb0053ecf6","sql":"SELECT p.product_name, SUM(o.quantity) AS total_sales FROM products p JOIN orders o ON p.product_id \u003d o.product_id "}
SELECT p.product_name, SUM(o.quantity) AS total_sales FROM products p JOIN orders o ON p.product_id = o.product_id
{"event":"sql_part","evidence":"For all metrics, if not all dimensions are restricted, you must perform a SUM aggregation before other aggregations or filtering","requestId":"8499A3E6-C1B2-5D2E-845F-2F63F8795B4D","rewrite":"Query the top five products by sales and their sales volumes","selector":["products","orders"],"sessionId":"8db26af8-721c-49bd-95f5-cceb0053ecf6","sql":"SELECT p.product_name, SUM(o.quantity) AS total_sales FROM products p JOIN orders o ON p.product_id \u003d o.product_id GROUP BY p.product"}
SELECT p.product_name, SUM(o.quantity) AS total_sales FROM products p JOIN orders o ON p.product_id = o.product_id GROUP BY p.product
{"event":"sql_part","evidence":"For all metrics, if not all dimensions are restricted, you must perform a SUM aggregation before other aggregations or filtering","requestId":"8499A3E6-C1B2-5D2E-845F-2F63F8795B4D","rewrite":"Query the top five products by sales and their sales volumes","selector":["products","orders"],"sessionId":"8db26af8-721c-49bd-95f5-cceb0053ecf6","sql":"SELECT p.product_name, SUM(o.quantity) AS total_sales FROM products p JOIN orders o ON p.product_id \u003d o.product_id GROUP BY p.product_name ORDER BY"}
SELECT p.product_name, SUM(o.quantity) AS total_sales FROM products p JOIN orders o ON p.product_id = o.product_id GROUP BY p.product_name ORDER BY
{"event":"sql_part","evidence":"For all metrics, if not all dimensions are restricted, you must perform a SUM aggregation before other aggregations or filtering","requestId":"8499A3E6-C1B2-5D2E-845F-2F63F8795B4D","rewrite":"Query the top five products by sales and their sales volumes","selector":["products","orders"],"sessionId":"8db26af8-721c-49bd-95f5-cceb0053ecf6","sql":"SELECT p.product_name, SUM(o.quantity) AS total_sales FROM products p JOIN orders o ON p.product_id \u003d o.product_id GROUP BY p.product_name ORDER BY total_sales DESC "}
SELECT p.product_name, SUM(o.quantity) AS total_sales FROM products p JOIN orders o ON p.product_id = o.product_id GROUP BY p.product_name ORDER BY total_sales DESC
{"event":"sql_part","evidence":"For all metrics, if not all dimensions are restricted, you must perform a SUM aggregation before other aggregations or filtering","requestId":"8499A3E6-C1B2-5D2E-845F-2F63F8795B4D","rewrite":"Query the top five products by sales and their sales volumes","selector":["products","orders"],"sessionId":"8db26af8-721c-49bd-95f5-cceb0053ecf6","sql":"SELECT p.product_name, SUM(o.quantity) AS total_sales FROM products p JOIN orders o ON p.product_id \u003d o.product_id GROUP BY p.product_name ORDER BY total_sales DESC LIMIT 5; "}
SELECT p.product_name, SUM(o.quantity) AS total_sales FROM products p JOIN orders o ON p.product_id = o.product_id GROUP BY p.product_name ORDER BY total_sales DESC LIMIT 5;
{"event":"sql_part","evidence":"For all metrics, if not all dimensions are restricted, you must perform a SUM aggregation before other aggregations or filtering","requestId":"8499A3E6-C1B2-5D2E-845F-2F63F8795B4D","rewrite":"Query the top five products by sales and their sales volumes","selector":["products","orders"],"sessionId":"8db26af8-721c-49bd-95f5-cceb0053ecf6","sql":"SELECT p.product_name, SUM(o.quantity) AS total_sales FROM products p JOIN orders o ON p.product_id \u003d o.product_id GROUP BY p.product_name ORDER BY total_sales DESC LIMIT 5; "}
SELECT p.product_name, SUM(o.quantity) AS total_sales FROM products p JOIN orders o ON p.product_id = o.product_id GROUP BY p.product_name ORDER BY total_sales DESC LIMIT 5;
{"event":"sql","evidence":"For all metrics, if not all dimensions are restricted, you must perform a SUM aggregation before other aggregations or filtering","requestId":"8499A3E6-C1B2-5D2E-845F-2F63F8795B4D","rewrite":"Query the top five products by sales and their sales volumes","selector":["products","orders"],"sessionId":"8db26af8-721c-49bd-95f5-cceb0053ecf6","sql":"SELECT p.product_name, SUM(o.quantity) AS total_sales\nFROM products p\nJOIN orders o ON p.product_id \u003d o.product_id\nGROUP BY p.product_name\nORDER BY total_sales DESC\nLIMIT 5;\n"}
SELECT p.product_name, SUM(o.quantity) AS total_sales
FROM products p
JOIN orders o ON p.product_id = o.product_id
GROUP BY p.product_name
ORDER BY total_sales DESC
LIMIT 5;

{"event":"sql_data","evidence":"For all metrics, if not all dimensions are restricted, you must perform a SUM aggregation before other aggregations or filtering","requestId":"8499A3E6-C1B2-5D2E-845F-2F63F8795B4D","rewrite":"Query the top five products by sales and their sales volumes","selector":["products","orders"],"sessionId":"8db26af8-721c-49bd-95f5-cceb0053ecf6","sql":"SELECT p.product_name, SUM(o.quantity) AS total_sales\nFROM products p\nJOIN orders o ON p.product_id \u003d o.product_id\nGROUP BY p.product_name\nORDER BY total_sales DESC\nLIMIT 5;\n","sqlData":{"column":["product_name","total_sales"],"data":[{"total_sales":"265","product_name":"Silk Scarf"},{"total_sales":"197","product_name":"Men's Electric Makeup Brush Set"},{"total_sales":"158","product_name":"Women's Electric Makeup Brush Set"},{"total_sales":"157","product_name":"Ring"},{"total_sales":"136","product_name":"Leather Bag"}]}}
{"event":"result","evidence":"For all metrics, if not all dimensions are restricted, you must perform a SUM aggregation before other aggregations or filtering","requestId":"8499A3E6-C1B2-5D2E-845F-2F63F8795B4D","rewrite":"Query the top five products by sales and their sales volumes","selector":["products","orders"],"sessionId":"8db26af8-721c-49bd-95f5-cceb0053ecf6","sql":"SELECT p.product_name, SUM(o.quantity) AS total_sales\nFROM products p\nJOIN orders o ON p.product_id \u003d o.product_id\nGROUP BY p.product_name\nORDER BY total_sales DESC\nLIMIT 5;\n","sqlData":{"column":["product_name","total_sales"],"data":[{"total_sales":"265","product_name":"Silk Scarf"},{"total_sales":"197","product_name":"Men's Electric Makeup Brush Set"},{"total_sales":"158","product_name":"Women's Electric Makeup Brush Set"},{"total_sales":"157","product_name":"Ring"},{"total_sales":"136","product_name":"Leather Bag"}]},"visualization":{"data":{"plotType":"bar","xAxis":["product_name"],"yAxis":["total_sales"]},"text":"The top five products by sales and their sales volumes are: Silk Scarf, with total sales of 265 units; Men's Electric Makeup Brush Set, with sales of 197 units; Women's Electric Makeup Brush Set, with sales of 158 units; Ring, with sales of 157 units; and Leather Bag, with sales of 136 units."}}
ALL***********************
Request header values for a successful request:
200
{Transfer-Encoding=chunked, Keep-Alive=timeout=25, Access-Control-Expose-Headers=*, Access-Control-Allow-Origin=*, x-acs-request-id=8499A3E6-C1B2-5D2E-845F-2F63F8795B4D, Connection=keep-alive, Date=Thu, 14 Nov 2024 08:16:49 GMT, Content-Type=text/event-stream;charset=utf-8, x-acs-trace-id=26e2559443d87082911aa44f656072df}
If you do not authorize a database connection, the following result is returned:
{"errorMessage":"data source is empty","event":"error"}
ALL***********************
Request header values for a successful request:
200
{Transfer-Encoding=chunked, Keep-Alive=timeout=25, Access-Control-Expose-Headers=*, Access-Control-Allow-Origin=*, x-acs-request-id=CB1F6A3B-43DC-5D9D-8FDC-57A9474B0DF3, Connection=keep-alive, Date=Thu, 14 Nov 2024 06:20:33 GMT, Content-Type=text/event-stream;charset=utf-8, x-acs-trace-id=dfa7db76cf65dcc933292999274fa687}
Agent Development
Agent Context
Agent Governance & Evolution
Store
Application Marketplace