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
Log on to the Model Studio console.
In the left-side navigation pane, choose
.On the Unstructured Data tab, select a category and click Import Data.
On the Import Data page, upload your local files. Then, click Confirm.
When the Status becomes Imported, the files are imported.
Step 2: Create a knowledge index
In the left-side navigation bar, choose .
Click Create Knowledge Base.
Specify a name for the knowledge base. For the other parameters, you can use the default settings. Then, click Next Step.
Select the required files and click Next Step.
Use the default settings and click Import. The system will automatically parse the documents.
When the Status becomes Parsed, the files are imported.
Return to the Knowledge Base Index homepage to retrieve the knowledge index ID.
Step 3: Create an application
In the left-side navigation bar, choose My Applications.
Click
.Click
to choose a model. You can also adjust parameters that controls content generation, such as
temperature
.Enable Knowledge Base Retrieval Augmentation and click Configure Knowledge Base.
Choose the knowledge base you created in Step 2.
Click Publish on the upper-right corner to publish the application. You can also test the application in the right side.
Go back to the My Applications page. View the application ID of the application you just created.
Call the application
Prerequisites
You have obtained an API Key and set the API key as an environment variable.
you have obtained the application ID.
You have installed the SDK of the latest version.
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