All Products
Search
Document Center

Alibaba Cloud Model Studio:Quickly build RAG applications with low code

Last Updated:Feb 17, 2025

This topic provides a best practice on how to build a Retrieval-Augmented Generation (RAG) application for unstructured data in Model Studio and how to call the application through API or SDK with low code.

Prepare the application

Step 1: Import data

  1. Log on to the Model Studio console.

  2. In the left-side navigation pane, choose Data Center > Data Management.

  3. On the Unstructured Data tab, select a category and click Import Data.

  4. On the Import Data page, upload your local files. Then, click Confirm.

    When the Status becomes Imported, the files are imported.

image

Step 2: Create a knowledge index

  1. In the left-side navigation bar, choose Data Application > Knowledge Index.

  2. Click Create Knowledge Base.

  3. Specify a name for the knowledge base. For the other parameters, you can use the default settings. Then, click Next Step.

    image

  4. Select the required files and click Next Step.

    image

  5. Use the default settings and click Import. The system will automatically parse the documents.

    When the Status becomes Parsed, the files are imported.

    image

  6. Return to the Knowledge Base Index homepage to retrieve the knowledge index ID.

    image

Step 3: Create an application

  1. In the left-side navigation bar, choose My Applications.

  2. Click Create Application > Agent Application > Create Directly.

  3. Click image to choose a model. You can also adjust parameters that controls content generation, such as temperature.

  4. Enable Knowledge Base Retrieval Augmentation and click Configure Knowledge Base.

  5. Choose the knowledge base you created in Step 2.

  6. Click Publish on the upper-right corner to publish the application. You can also test the application in the right side.

  7. Go back to the My Applications page. View the application ID of the application you just created.

    image

Call the application

Prerequisites

Use API or SDK

Replace YOUR_APP_ID with your application ID to ensure the code functions correctly.

Python

import os
from http import HTTPStatus
import dashscope
from dashscope import Application

dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'

def call_agent_app():
    response = Application.call(app_id='YOUR_APP_ID',
                                prompt='What is the workspace of Model Studio? How to use the workspace?',
                                api_key=os.getenv("DASHSCOPE_API_KEY"),  # If the environment variable is not configured, replace this line with Bailian API Key: api_key="sk-xxx",
                                )

    if response.status_code != HTTPStatus.OK:
        print('request_id=%s, code=%s, message=%s\n' % (response.request_id, response.status_code, response.message))
    else:
        print('request_id=%s\n output=%s\n usage=%s\n' % (response.request_id, response.output, response.usage))


if __name__ == '__main__':
    call_agent_app()

Java

import com.alibaba.dashscope.app.*;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.Constants;

public class Main{
 static {
        Constants.baseHttpApiUrl="https://dashscope-intl.aliyuncs.com/api/v1";
    }
      public static void callAgentApp() throws ApiException, NoApiKeyException, InputRequiredException {
        ApplicationParam param = ApplicationParam.builder()
            // If the environment variable is not configured, replace the following line with Bailian API Key: .apiKey("sk-xxx")
            .apiKey(System.getenv("DASHSCOPE_API_KEY"))
            .appId("YOUR_APP_ID")
            .prompt("What is the workspace of Model Studio? How to use the workspace?")
            .build();

        Application application = new Application();
        ApplicationResult result = application.call(param);

        System.out.printf("requestId: %s, text: %s, finishReason: %s\n",
                result.getRequestId(), result.getOutput().getText(), result.getOutput().getFinishReason());
    }

    public static void main(String[] args) {
        try {
            callAgentApp();
        } catch (ApiException | NoApiKeyException | InputRequiredException e) {
            System.out.printf("Exception: %s", e.getMessage());
        }
        System.exit(0);
    }  
}

curl

curl -X POST https://dashscope-intl.aliyuncs.com/api/v1/apps/YOUR_APP_ID/completion \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header 'Content-Type: application/json' \
--data '{
    "input": {
        "prompt": "What is the workspace of Model Studio? How to use the workspace?"
    },
    "parameters":  {},
    "debug": {}
}' --verbose