Alibaba Cloud Model Studio supports building retrieval-augmented generation (RAG) applications with the LlamaIndex API for scenarios such as internal knowledge Q&A and customer support. This topic demonstrates how to build a sample RAG application using the LlamaIndex API.
In this solution, LlamaIndex provides the tools and framework to build RAG applications. Alibaba Cloud Model Studio provides data management capabilities and large model services. If you are familiar with the LlamaIndex API, you can follow this solution to build a RAG application that combines the capabilities of both platforms.
You can use this solution to achieve the following:
Download and decompress llamaindex_cloud_rag.zip. The folder structure is as follows:
The
Navigate to the directory that contains the
Navigate to the directory that contains the
Navigate to the Knowledge Base page to view the created cloud knowledge base.
Navigate to the directory that contains the
For more information about the LlamaIndex APIs related to Alibaba Cloud Model Studio, see the related topics.
This solution deploys the knowledge base in the cloud and uses the default settings for smart document chunking and vector models. Custom document chunking methods and custom embedding models are not supported.
If you want to deploy the knowledge base locally for more flexibility in document chunking and embedding model selection, see Build a RAG application based on a local knowledge base.
If you want to deploy the knowledge base in the cloud and create a RAG application with zero code, see Build a RAG application with zero code.
Demo
You can use this solution to achieve the following:

Solution overview
- Read local files and build a cloud knowledge base: Read and parse unstructured data files, such as
.txt,.docx, and.pdf. Then, upload the files to the cloud to build a knowledge base. - Build a retrieval engine and a RAG application: Build a retrieval engine based on the cloud knowledge base. The engine accepts questions from end users and retrieves relevant text segments from the knowledge base. It then merges the questions and retrieval results, sends them to a large model, and generates an answer. The RAG application provides an interactive interface for end users. If the engine cannot retrieve relevant text segments or generate an answer from the retrieved segments, it returns an appropriate error message.
Prerequisites
- Activate Alibaba Cloud Model Studio and obtain an API key.
- Configure the API key as an environment variable.
- Activate the Knowledge Base service on the Knowledge Base page. Activation is free. You are charged based on usage.
- Python 3.9 or later is installed.
Download the sample files and code
Download and decompress llamaindex_cloud_rag.zip. The folder structure is as follows:
docs/ folder contains sample files. You can replace them with your own files. The create_cloud_index.py script reads the files in the docs/ folder to build a cloud knowledge base. The rag.py script builds the retrieval engine and the RAG application. The requirements.txt file lists the required dependencies.
Install dependencies
Navigate to the directory that contains the requirements.txt file and run the following command:
Read local files and build a cloud knowledge base
Navigate to the directory that contains the create_cloud_index.py file and run the following command: python create_cloud_index.py. This command uploads the files from the docs/ folder to Application Data in Alibaba Cloud Model Studio and builds a cloud knowledge base.
Note: Ensure that your local machine has Internet access. The file upload may take some time to complete.After the code finishes running, navigate to the Files page in Application Data to view the uploaded files.

Code explanation
Code explanation
create_cloud_index.pyBuild the retrieval engine and RAG application
Navigate to the directory that contains the rag.py file and run the following command: python rag.py. This command reads the created cloud knowledge base, builds a retrieval engine, and starts the RAG application on your local machine.
Follow the prompts to interact with the RAG application. Enter a question and press Enter to obtain a result. To exit the application, enter q and press Enter.
Note: Ensure that your local machine has Internet access. Generating an answer may take some time.

Code explanation
Code explanation
rag.pySettings.llm = DashScope(model_name="qwen-max"), you can set themodel_nameparameter to a model name, such as "qwen-max", to specify the large model that the retrieval engine uses to generate answers. For a complete list of model names, see Text Generation - Qwen and Text Generation - Qwen (Open Source).