This topic describes the API operations for PAI-RAG service v0.4.x, including request examples and core concepts to help you integrate PAI-RAG into your applications.
Preparations: Obtain the service endpoint and token
Before you call the RAG service API, you must obtain the service endpoint and authentication token. All API requests must include the EAS_TOKEN in the HTTP Authorization header.
The $EAS_SERVICE_URL and $EAS_TOKEN used in this topic are environment variables. Set them before use. To obtain them, perform the following steps:
-
Log on to the PAI console. Select a region on the top of the page. Then, select the desired workspace and click Elastic Algorithm Service (EAS).
-
Click the name of the service. In the Basic Information section, click View Endpoint Information.
-
On the Invocation Method page, obtain the Internet Endpoint (EAS_SERVICE_URL) and Token (EAS_TOKEN).
Important-
Remove the trailing slash (/) from the EAS_SERVICE_URL.
-
Use the Internet Endpoint if the client can access the Internet.
-
Use the VPC endpoint if the client is in the same virtual private cloud (VPC) as the RAG service.

-
After you obtain the values, set them as environment variables to simplify subsequent API calls:
export EAS_SERVICE_URL="your_service_endpoint"
export EAS_TOKEN="your_token"
You can then use $EAS_SERVICE_URL and $EAS_TOKEN to reference these values in subsequent curl commands.
Chat API
The Chat API is a streaming chat interface that supports intelligent agents and retrieval-augmented generation (RAG). It extends the OpenAI-compatible chat protocol to support advanced features such as knowledge base retrieval, multi-step reasoning, and external tool calling. It is suitable for building intelligent conversational systems, Q&A chatbots, and other similar scenarios.
To call the Chat API, you must configure your own chat application. For example, you can name the application my_assistant and select the appropriate knowledge base and web search configurations.
POST $EAS_SERVICE_URL/v1/chat/completions
Description: Initiates a chat request. You can call a pre-configured chat application by specifying the model parameter. The application can be associated with a knowledge base, search tools, and other resources.
Request body (application/json)
|
Field |
Type |
Required |
Description |
|
|
String |
Yes |
The name of the chat application you created. |
|
|
Array |
Yes |
A list of conversation history messages in OpenAI format. |
|
|
Boolean |
No |
Specifies whether to return the response in streaming mode. The default value is |
Response body
The response format is compatible with the OpenAI chat.completions API.
OpenAI client call example:
from openai import OpenAI
import os
EAS_ENDPOINT = os.getenv("EAS_SERVICE_URL")
EAS_TOKEN = os.getenv("EAS_TOKEN")
client = OpenAI(
base_url=EAS_ENDPOINT,
api_key=EAS_TOKEN
)
response = client.chat.completions.create(
model="my_assistant", # Replace with the actual name of your chat application.
messages=[
{"role": "user", "content": "Hello"}
],
stream=True
)
for chunk in response:
print(chunk.choices[0].delta.content)
Knowledge base management
Create a knowledge base
Creates a new knowledge base. You can specify its configurations for data chunking, embedding, and retrieval.
POST $EAS_SERVICE_URL/v1/config/knowledgebases
Request |
|
Headers |
|
|
Content-Type The content type of the request. This parameter must be set to |
|
|
Authorization The authentication token for the request. Use your EAS_TOKEN. |
|
Request Body |
|
|
name The name of the knowledge base. |
|
|
description The description of the knowledge base. |
|
|
embedding_model The name or path of the embedding model. Local and remote models are supported. |
|
|
chunk_config The data chunking configuration. |
|
|
retrieval_config The retrieval policy configuration. |
Response example
Get a list of knowledge bases
Retrieves a list of knowledge bases for the current service. Paging is supported.
GET $EAS_SERVICE_URL/v1/config/knowledgebases?page=1&size=10
Request |
|
Headers |
|
|
Authorization The authentication token for the request. Use your EAS_TOKEN. |
|
URL query parameters |
|
|
page The page number. Default value: |
|
|
size The number of entries to return on each page. Default value: |
Response example
Get a specific knowledge base
Retrieves the details of a specified knowledge base.
GET $EAS_SERVICE_URL/v1/config/knowledgebases/{kb_id}
Request |
|
Headers |
|
|
Authorization The authentication token for the request. Use your EAS_TOKEN. |
|
URL path parameters |
|
|
kb_id The unique identifier of the knowledge base. |
Response example
Update a knowledge base
Updates an existing knowledge base.
PUT $EAS_SERVICE_URL/v1/config/knowledgebases/{kb_id}
Request |
|
Headers |
|
|
Content-Type The content type of the request. This parameter must be set to |
|
|
Authorization The authentication token for the request. Use your EAS_TOKEN. |
|
URL path parameters |
|
|
kb_id The unique identifier of the knowledge base. |
|
Request Body |
|
|
The request body parameters are the same as those for the create knowledge base operation, including name, description, embedding_model, chunk_config, and retrieval_config. |
Response example
Delete a specific knowledge base
Deletes a specified knowledge base and all its files and indexes. This operation is irreversible and should be used with caution.
DELETE $EAS_SERVICE_URL/v1/config/knowledgebases/{kb_id}
Request |
|
Headers |
|
|
Authorization The authentication token for the request. Use your EAS_TOKEN. |
|
URL path parameters |
|
|
kb_id The unique identifier of the knowledge base. |
Response example
File management
Upload files
Uploads one or more files to a specified knowledge base. This is an asynchronous operation that immediately returns file information while parsing and indexing are performed in the background. Supported file formats include PDF, DOCX, and TXT.
POST $EAS_SERVICE_URL/v1/config/knowledgebases/{kb_id}/files
Request |
|
Headers |
|
|
Content-Type The content type of the request. This parameter must be set to |
|
|
Authorization The authentication token for the request. Use your EAS_TOKEN. |
|
URL path parameters |
|
|
kb_id The unique identifier of the knowledge base. |
|
Request Body parameters |
|
|
files The file or files to upload. PDF, DOCX, and TXT are supported. You can upload multiple files at once. |
Response example
List files
Lists the files in a specified knowledge base. You can filter the files by name and status, and use paging.
GET $EAS_SERVICE_URL/v1/config/knowledgebases/{kb_id}/files
Request |
|
Headers |
|
|
Authorization The authentication token for the request. Use your EAS_TOKEN. |
|
URL path parameters |
|
|
kb_id The unique identifier of the knowledge base. |
|
URL query parameters |
|
|
query Performs a fuzzy match on the file name. |
|
|
status Filters files by processing status. If you do not specify this parameter, files in all statuses are returned. Valid values: |
|
|
page The page number. Default value: 1. |
|
|
size The number of entries to return on each page. Default value: 10. |
Response example
Get file details
Retrieves the details of a file. This operation is often used to poll the file processing status.
GET $EAS_SERVICE_URL/v1/config/knowledgebases/{kb_id}/files/{file_id}
Request |
|
Headers |
|
|
Authorization The authentication token for the request. Use your EAS_TOKEN. |
|
URL path parameters |
|
|
kb_id The unique identifier of the knowledge base. |
|
|
file_id The unique identifier of the file. |
Response example
Reprocess a file
Triggers reprocessing for a file, such as a file that failed to process or whose content has been updated. This is an asynchronous operation that creates a new processing task and resets the file status to pending.
PUT $EAS_SERVICE_URL/v1/config/knowledgebases/{kb_id}/files/{file_id}
Request |
|
Headers |
|
|
Content-Type The content type of the request. This parameter must be set to |
|
|
Authorization The authentication token for the request. Use your EAS_TOKEN. |
|
URL path parameters |
|
|
kb_id The unique identifier of the knowledge base. |
|
|
file_id The unique identifier of the file. |
Response example
Delete a file
Deletes a specified file and its associated data chunks and indexes from a knowledge base.
DELETE $EAS_SERVICE_URL/v1/config/knowledgebases/{kb_id}/files/{file_id}
Request |
|
Headers |
|
|
Authorization The authentication token for the request. Use your EAS_TOKEN. |
|
URL path parameters |
|
|
kb_id The unique identifier of the knowledge base. |
|
|
file_id The unique identifier of the file. |
Response example
Data chunk management
View file chunks
Retrieves the list of data chunks for a specified file after it has been processed. Paging is supported.
GET $EAS_SERVICE_URL/v1/config/knowledgebases/{kb_id}/files/{file_id}/chunks?page=1&size=10
Request |
|
Headers |
|
|
Authorization The authentication token for the request. Use your EAS_TOKEN. |
|
URL path parameters |
|
|
kb_id The unique identifier of the knowledge base. |
|
|
file_id The unique identifier of the file. |
|
URL query parameters |
|
|
page The page number. Default value: 1. |
|
|
size The number of entries to return on each page. Default value: 10. |
Response example
Update a single chunk
Updates the content or status of a single data chunk. For example, you can manually correct poorly chunked text or disable a data chunk to prevent it from being retrieved.
PUT $EAS_SERVICE_URL/v1/config/knowledgebases/{kb_id}/files/{file_id}/chunks/{chunk_id}
Request |
|
Headers |
|
|
Authorization The authentication token for the request. Use your EAS_TOKEN. |
|
|
Content-Type application/json |
|
URL path parameters |
|
|
kb_id The unique identifier of the knowledge base. |
|
|
file_id The unique identifier of the file. |
|
|
chunk_id The unique identifier of the data chunk. |
|
Request Body |
|
|
text The updated text content of the data chunk. |
|
|
active Specifies whether to enable the data chunk. If set to false, the data chunk will not be retrieved. |
Response example
Disable a chunk
Use the update chunk operation and set the active field to false.
Metadata management
Metadata adds structured information to your documents, which allows for more precise filtering during retrieval. For example, you can retrieve only documents published by the IT department after 2024.
Define metadata fields
Defines a metadata field schema for a knowledge base, including the field ID, name, and value type.
POST $EAS_SERVICE_URL/v1/config/knowledgebases/{kb_id}/metadata
Request |
|
Headers |
|
|
Authorization The authentication token for the request. Use your EAS_TOKEN. |
|
|
Content-Type application/json |
|
URL path parameters |
|
|
kb_id The unique identifier of the knowledge base. |
|
Request Body |
|
|
kb_id The unique identifier of the knowledge base. |
|
|
name The display name of the field. It must be 3 to 50 characters in length. |
|
|
value_type The value type of the field. Valid values: |
|
|
description The description of the field. |
Response example
List metadata
Lists all defined metadata fields for the knowledge base.
GET $EAS_SERVICE_URL/v1/config/knowledgebases/{kb_id}/metadata
Request |
|
Headers |
|
|
Authorization The authentication token for the request. Use your EAS_TOKEN. |
|
URL path parameters |
|
|
kb_id The unique identifier of the knowledge base. |
Response example
Delete metadata
Deletes a specified metadata field from the knowledge base.
DELETE $EAS_SERVICE_URL/v1/config/knowledgebases/{kb_id}/metadata/{metadata_id}
Request |
|
Headers |
|
|
Authorization The authentication token for the request. Use your EAS_TOKEN. |
|
URL path parameters |
|
|
kb_id The unique identifier of the knowledge base. |
|
|
metadata_id The metadata ID. |
Response example
Bind file-level metadata
Binds metadata values to a file. Note: This operation overwrites existing data. Each call completely replaces all metadata for the file. To avoid data loss, first use a GET request to retrieve the existing metadata, modify it locally, and then submit the complete list using this operation.
POST $EAS_SERVICE_URL/v1/config/knowledgebases/{kb_id}/files/{file_id}/metadata
Request |
|
Headers |
|
|
Authorization The authentication token for the request. Use your EAS_TOKEN. |
|
|
Content-Type application/json |
|
URL path parameters |
|
|
kb_id The unique identifier of the knowledge base. |
|
|
file_id The unique identifier of the file. |
|
Request Body |
|
|
entries A list of metadata entries. |
Response example
Retrieval API
Hybrid retrieval (text + vector + metadata filtering)
Performs a retrieval operation on a specified knowledge base. Supports hybrid retrieval with text, vectors, and metadata filtering.
POST $EAS_SERVICE_URL/v1/retrieval
Request |
|
Headers |
|
|
Authorization The authentication token for the request. Use your EAS_TOKEN. |
|
|
Content-Type application/json |
|
Request Body |
|
|
query The query text for retrieval. |
|
|
knowledge_id The ID of the target knowledge base to retrieve from. |
|
|
user_id The unique identifier for the user, used for personalization or log tracking. |
|
|
metadata_condition The metadata filter conditions. Includes |
|
|
retrieval_setting Per-request retrieval settings that override the knowledge base defaults. |
Response example
Configure a code sandbox
Create or update a sandbox
Request |
|
Headers |
|
|
Content-Type The content type of the request. This parameter must be set to |
|
|
Authorization The authentication token for the request. Use your EAS_TOKEN. |
|
Request Body |
|
|
type The sandbox type. Currently, only Alibaba Cloud FC sandboxes are supported. The value must be |
|
|
aliyun_id Your Alibaba Cloud account ID. |
|
|
interpreter_id The code interpreter ID. |
|
|
enabled Specifies whether to enable the sandbox feature. |
Query the current sandbox configuration
curl -X GET "$EAS_SERVICE_URL/api/config/code_sandbox"
Configure FAQs
Base paths: /v1/config/apps (application configuration), /v1/faq-retrieval (FAQ retrieval)
All requests must include authentication, such as Authorization: Bearer YOUR_BEARER_TOKEN. You must also replace {EAS_SERVICE_URL} with the actual service endpoint.
Enable and configure FAQs
You can enable FAQs and write configurations by updating the application.
PUT {EAS_SERVICE_URL}/v1/config/apps/{id}
Request |
|
Headers |
|
|
Content-Type The content type of the request. This parameter must be set to |
|
|
Authorization The authentication token for the request. Use your EAS_TOKEN. |
|
URL path parameters |
|
|
id The primary key ID of the application. You can obtain this ID using the Query Application API. |
|
Request Body |
|
|
app_id The app ID of the application. |
|
|
description The application description. |
|
|
model_id The base model. |
|
|
kb_ids The knowledge bases used by the application. You can select multiple knowledge bases. |
|
|
enable_faq Specifies whether to enable FAQs. |
|
|
faq_config The FAQ configuration. |
Query an application
curl -X GET "$EAS_SERVICE_URL/v1/config/apps?app_id=your_app_id" \
-H "Authorization: Bearer $EAS_TOKEN"
You can obtain the app_id from the following figure.
Create a single FAQ
curl -X POST "$EAS_SERVICE_URL/v1/config/apps/{app_id}/faqs" \
-H "Authorization: Bearer $EAS_TOKEN" \
-H 'Content-Type: application/json' \
-d '{
"question": "How do I reset my password?",
"answer": "Click 'Forgot Password' on the login page and follow the prompts.",
"active": true
}'
Query a list of FAQs (with paging)
curl -X GET "$EAS_SERVICE_URL/v1/config/apps/{app_id}/faqs?page=1&size=100" \
-H "Authorization: Bearer $EAS_TOKEN"
Update a single FAQ
curl -X PUT "$EAS_SERVICE_URL/v1/config/apps/{app_id}/faqs/{faq_item_id}" \
-H "Authorization: Bearer $EAS_TOKEN" \
-H 'Content-Type: application/json' \
-d '{
"question": "How do I change my password?",
"answer": "After you log in, go to Account Settings > Security to change it.",
"active": true
}'
Delete a single FAQ
curl -X DELETE "$EAS_SERVICE_URL/v1/config/apps/{app_id}/faqs/{faq_item_id}" \
-H "Authorization: Bearer $EAS_TOKEN"
Batch upload an FAQ file (Excel)
This operation uses multipart/form-data. The upload field name is files, and multiple files are supported. The table_config parameter, which is a JSON string for table header and column mapping, is optional. .xlsx and .xls file formats are supported.
curl -X POST "$EAS_SERVICE_URL/v1/config/apps/{app_id}/faq-files" \
-H "Authorization: Bearer $EAS_TOKEN" \
-F 'files=@/path/to/faq.xlsx' \
-F 'table_config={"header_index_max":0,"question_column_index":0,"answer_column_index":1}'
table_config description:
-
header_index_max: The number of header rows. A value of0indicates that the first row is the header. -
question_column_index: The index of the question column, starting from 0. -
answer_column_index: The index of the answer column.
FAQ retrieval (standalone call)
Retrieves FAQ results by question without initiating a chat conversation.
curl -X POST "$EAS_SERVICE_URL/v1/faq-retrieval" \
-H "Authorization: Bearer $EAS_TOKEN" \
-H 'Content-Type: application/json' \
-d '{
"chatapp_id": "your_app_id",
"query": "The user's input question"
}'
The user_id and retrieval_setting (such as top_k and similarity_threshold) fields are optional. If you do not pass the retrieval_setting parameter, the default values from the application's FAQ configuration are used.