This topic describes how to use a software development kit (SDK) to call a legacy Alibaba Cloud Model Studio retrieval-augmented generation (RAG) application.
On May 3, 2024, Alibaba Cloud Model Studio was upgraded. For more information about the upgrade, see Announcement on the Upgrade and Adjustment of Official Pre-built Applications. After this upgrade, you can no longer create legacy RAG applications. 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 a new knowledge base and build a new RAG application, see the Migration plan section below.
-
To learn how to call a new RAG application in your business code using an HTTP interface or an SDK, see Call an application.
Migration plan
The enterprise knowledge base feature is no longer maintained. You must migrate your private knowledge to the new knowledge base as soon as possible. Alibaba Cloud does not provide official migration tools or services. The new knowledge base provides more features and supports more document formats.
|
Enterprise knowledge type |
Migration plan |
|
Documents |
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 data updates for the knowledge base. You can upload documents from a local device using the console or by calling an API operation. You can import documents from OSS only using the console. For more information, see Knowledge base: Step 1. Import data. |
|
FAQ |
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 create a 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
-
Obtain and configure an API key. For more information, see Obtain and configure an API key and Configure the API key as an environment variable (to be unpublished and merged into Configure an API key).
-
Install the latest version of the SDK. The DashScope SDK is available in Python and Java. For more information, see Install the latest version of the SDK.
-
A retrieval-augmented generation (RAG) application has been created (new ones are no longer supported), and enterprise knowledge has been uploaded as described in Upload enterprise knowledge.
-
Obtain the app ID. You can find the app ID on the target application's card on the My Applications page.
Request example
After you install the DashScope SDK, you can use the following sample code to send a request to your RAG application.
from http import HTTPStatus
from dashscope import Application
response = Application.call(app_id='YOUR_APP_ID',
prompt='How do I pass the TopP parameter as described in the API reference?',
)
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))
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("How do I pass the TopP parameter as described in the API reference?")
.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
output={"text": "In the API reference, the TopP parameter is an optional request parameter used to control the probability threshold for the nucleus sampling method during generation. When you call the API operation, you can pass this parameter in the request body in JSON format. The format is as follows:\n\n```json\n{\n \"TopP\": <float_value>\n}\n```\n\nReplace `<float_value>` with the actual floating-point number. The value is typically between 0 and 1. This parameter ensures that when the model generates text, only the set of most likely tokens whose probabilities sum up to a value greater than or equal to the TopP threshold is retained as the candidate set. This affects the randomness and diversity of the generated result. A smaller value leads to more deterministic text generation. A larger value increases randomness.", "finish_reason": "stop", "session_id": "25d6b00b574a4740b6e8a97500b556d5", "thoughts": null, "doc_references": null}
usage={"models": [{"model_id": "qwen-max", "input_tokens": 1814, "output_tokens": 165}]}
Next steps
For more information about how to call the API operations for legacy RAG applications, see API reference.