Getting started

Updated at:

This topic describes how to use a software development kit (SDK) to call a legacy Alibaba Cloud Model Studio retrieval-augmented generation (RAG) application.

ImportantOn May 3, 2024, Alibaba Cloud Model Studio was fully upgraded. For more information about the upgrade, see Announcement on the Upgrade and Adjustment of Official Pre-built Applications. After this upgrade, legacy RAG applications can no longer be created. If your application was created before this upgrade, you can continue to use this topic to call your application.

  • To learn how to migrate your enterprise knowledge base to the new knowledge base and build a new RAG application, see Migration plan below.
  • To learn how to call a new RAG application in your business code over an HTTP interface or by using an SDK, see Call an application.

Migration plan

The enterprise knowledge base is no longer maintained. We recommend that you migrate your private knowledge to the new knowledge base as soon as possible. (Alibaba Cloud does not provide official migration tools or services at this time.) The new knowledge base provides more features and supports more document formats.

Enterprise knowledge type

Migration plan

Documents

We recommend that you create a non-structured knowledge base to manage document-based knowledge. You can import documents by uploading them from a local device or from Object Storage Service (OSS).

If you upload documents from a local device, you must manually update the knowledge base later. If you import documents from Object Storage Service (OSS), you can integrate OSS, Function Compute (FC), and the knowledge base API operations of Model Studio to implement automatic updates for the knowledge base.

You can upload documents from a local device by using the console or by calling API operations. Importing documents from Object Storage Service (OSS) is currently supported only in the console.

For more information, see Knowledge base: Step 1. Import data.

FAQ

We recommend that you create a structured knowledge base to manage FAQ-based knowledge. You can create a structured knowledge base by uploading local documents or based on ApsaraDB RDS.

If you upload local documents, you must manually update the knowledge base later. If you build the knowledge base based on ApsaraDB RDS, data updates in the RDS data tables are automatically synchronized to the knowledge base.

Currently, both methods can be performed only in the console.

For more information, see Knowledge base: Step 1. Import data.

After you create a new knowledge base, you can associate it with your agent application or workflow application in My Applications. For more information, see Knowledge base: Step 4. Reference a knowledge base.

Prerequisites

Request example

If you have installed the DashScope SDK, you can use the following sample code to send a request to your RAG application.

import os
import dashscope
from dashscope.v2 import DashScopeError

client = dashscope.Client(api_key=os.getenv('DASHSCOPE_API_KEY'))

try:
    response = client.apps.completions.create(
        app_id='YOUR_APP_ID',
        prompt='API接口说明中, TopP参数改如何传递?',
    )
    print('request_id=%s\n text=%s\n usage=%s\n' % (response.request_id, response.text, response.usage))
except DashScopeError as e:
    print(f"HTTP返回码:{e.status_code}")
    print(f"错误码:{e.code}")
    print(f"错误信息:{e.message}")
    print("请参考文档:https://help.aliyun.com/zh/model-studio/developer-reference/error-code")
import com.alibaba.dashscope.app.*;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import java.util.List;

public class Main{
      public static void ragCall()
            throws ApiException, NoApiKeyException, InputRequiredException {
        RagApplicationParam param = RagApplicationParam.builder()
                .appId("YOUR_APP_ID")
                .prompt("API接口说明中, TopP参数改如何传递?")
                .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());

        if (result.getUsage() != null && result.getUsage().getModels() != null) {
            for (ApplicationUsage.ModelUsage usage : result.getUsage().getModels()) {
                System.out.printf("modelId: %s, inputTokens: %d, outputTokens: %d\n",
                        usage.getModelId(), usage.getInputTokens(), usage.getOutputTokens());
            }
        }
    }

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

Response example

request_id=131c75de-b53d-9787-a301-1a841dc73eda
 text=在API接口说明中,TopP参数是一个可选的请求参数,用于控制生成过程中 nucleus 采样方法的概率阈值。当调用API时,您可以按照JSON格式将此参数放在请求体(Request Body)中进行传递,其具体格式如下:

```json
{
  "TopP": <float_value>
}
```

其中 `<float_value>` 应替换为实际要设置的浮点数数值,取值范围通常在0到1之间。这个参数的作用是,在模型生成文本时,只保留概率加起来大于等于TopP阈值的最可能token集合作为候选集,从而影响生成结果的随机性和多样性。取值越低,生成的文本确定性越高;取值越大,生成的随机性则会增加。

 usage={"models": [{"model_id": "qwen-max", "input_tokens": 1814, "output_tokens": 165}]}

Next steps

For more information about how to call the API operations of legacy RAG applications, see API reference.