Plugin - Knowledge Base
Authentication
Field | Parameter passing method | Type | Required | Description | Example value |
Authorization | header | String | Yes | API key | Bearer d1**2a |
Routing configuration
Note: The software development kit (SDK) handles routing automatically. You do not need to configure API routing when you use the SDK to call an API.
Field | Parameter passing method | Type | Required | Description | Example value |
x-fag-appcode | header | String | Yes | Application identity. The value is fixed to | |
x-fag-servicename | header | String | Yes | Request routing identity. The value varies by API. For more information, see the mapping management. | aca-character-create |
Mapping between x-fag-servicename and APIs
API | Path | x-fag-appcode |
Create knowledge base | /v2/api/knowledge_base/create | aca-kb-create |
Modify knowledge base | /v2/api/knowledge_base/update | aca-kb-update |
Query knowledge base | /v2/api/knowledge_base/search | aca-kb-search |
Delete knowledge base | /v2/api/knowledge_base/delete | aca-kb-delete |
Upload knowledge base file | /v2/api/knowledge_base/detail/upload | aca-kb-detail-upload |
Modify knowledge base file | /v2/api/knowledge_base/detail/update | aca-kb-detail-update |
Delete knowledge base file | /v2/api/knowledge_base/detail/delete | aca-kb-detail-delete |
Query knowledge base file | /v2/api/knowledge_base/detail/search | aca-kb-detail-search |
Knowledge base management
Create a knowledge base
API
POST /v2/api/knowledge_base/create
Request parameters
Name | Type | Required | Description |
name | string | Yes | The name of the knowledge base. The name can be up to 20 characters long. |
description | string | No | The description of the knowledge base. The description can be up to 200 characters long. |
Response parameters
Name | Type | Description |
code | int | The return code. A value of 200 is returned on success. Null is returned on failure. |
success | bool | A value of true is returned on success. Null is returned on failure. |
requestId | string | The unique ID of the request. |
data | object | The content returned for the request. |
data.knowledgeBaseId | string | The ID of the knowledge base. |
data.name | string | The name of the knowledge base. |
data.description | string | The description of the knowledge base. |
Update a knowledge base
API
POST /v2/api/knowledge_base/update
Request parameters
Name | Type | Required | Description |
knowledgeBaseId | string | Yes | The ID of the knowledge base. |
name | string | No | The name of the knowledge base. The name can be up to 20 characters long. |
description | string | No | The description of the knowledge base. The description can be up to 200 characters long. |
Response parameters
Name | Type | Description |
code | int | The return code. A value of 200 is returned on success. Null is returned on failure. |
success | bool | A value of true is returned on success. Null is returned on failure. |
requestId | string | The unique ID of the request. |
data | bool | Indicates whether the modification was successful. |
Query knowledge bases
API
POST /v2/api/knowledge_base/search
Request parameters
Name | Type | Required | Description |
pageNum | int | No (Default: 1) | The page number. |
pageSize | ing | No (Default: 30) | The page size. |
Response parameters
Name | Type | Description |
code | int | The return code. A value of 200 is returned on success. Null is returned on failure. |
success | bool | A value of true is returned on success. Null is returned on failure. |
requestId | string | The unique ID of the request. |
data | object | The content returned for the request. |
data.page | int | The page number. |
data.pageSize | int | The page size. |
data.total | int | The total number of entries. |
data.list | array | The data. |
data.list[i].id | int | The primary key ID of the knowledge base. |
data.list[i].knowledgeBaseId | string | The ID of the knowledge base. |
data.list[i].name | string | The name of the knowledge base. |
data.list[i].gmtCreate | int | The time when the knowledge base was created. |
data.list[i].gmtModified | int | The time when the knowledge base was last modified. |
data.list[i].description | string | The description of the knowledge base. |
data.list[i].count | int | The number of file details in the knowledge base. |
Delete a knowledge base
API
POST /v2/api/knowledge_base/delete
Request parameters
Name | Type | Required | Description |
knowledgeBaseId | string | Yes | The ID of the knowledge base. |
Response parameters
Name | Type | Description |
code | int | The return code. A value of 200 is returned on success. Null is returned on failure. |
success | bool | A value of true is returned on success. Null is returned on failure. |
requestId | string | The unique ID of the request. |
data | bool | Indicates whether the modification was successful. |
Upload a knowledge base file
API
POST /v2/api/knowledge_base/detail/upload
Request parameters
Name | Type | Required | Description |
knowledgeBaseId | string | Yes | The ID of the knowledge base. |
type | string | Yes | The file type. Valid values: text, form, and document. |
fileInfos | array | Yes | A collection of files to upload. |
fileInfos[i].filename | string | Yes | The file name. The name must end with a file extension. |
fileInfos[i].fileUrl | string | Yes | A publicly accessible URL. |
Response parameters
Name | Type | Description |
code | int | The return code. A value of 200 is returned on success. Null is returned on failure. |
success | bool | A value of true is returned on success. Null is returned on failure. |
requestId | string | The unique ID of the request. |
data | bool | Indicates whether the upload was successful. |
Update a knowledge base file
API
POST /v2/api/knowledge_base/detail/update
Request parameters
Name | Type | Required | Description |
knowledgeBaseId | string | Yes | The ID of the knowledge base. |
name | string | Yes | The current name of the file. |
newName | string | Yes | The new name. |
Response parameters
Name | Type | Description |
code | int | The return code. A value of 200 is returned on success. Null is returned on failure. |
success | bool | A value of true is returned on success. Null is returned on failure. |
requestId | string | The unique ID of the request. |
data | bool | Indicates whether the modification was successful. |
Query knowledge base files
API
POST /v2/api/knowledge_base/detail/search
Request parameters
Name | Type | Required | Description |
knowledgeBaseId | string | Yes | The ID of the knowledge base. |
pageNum | int | No (Default: 1) | The page number. |
pageSize | ing | No (Default: 30) | The page size. |
Response parameters
Name | Type | Description |
code | int | The return code. A value of 200 is returned on success. Null is returned on failure. |
success | bool | A value of true is returned on success. Null is returned on failure. |
requestId | string | The unique ID of the request. |
data | object | The content returned for the request. |
data.page | int | The page number. |
data.pageSize | int | The page size. |
data.total | int | The total number of entries. |
data.list | array | The data. |
data.list[i].id | int | The primary key ID of the knowledge base detail. |
data.list[i].knowledgeBaseId | string | The ID of the knowledge base. |
data.list[i].name | string | The name of the knowledge base detail. |
data.list[i].gmtCreate | int | The time when the knowledge base detail was created. |
data.list[i].gmtModified | int | The time when the knowledge base detail was last modified. |
data.list[i].type | string | The type of the knowledge base detail. |
data.list[i].status | int | The knowledge base details are invalid. |
data.list[i].fileInfo | object | File details. |
data.list[i].fileInfo.filename | string | File name. |
data.list[i].fileInfo.fileSavePath | string | The OSS path. |
data.list[i].fileInfo.fileUrl | string | The URL after the file is stored. |
Delete a knowledge base file
API
POST /v2/api/knowledge_base/detail/delete
Request parameters
Name | Type | Required | Description |
knowledgeBaseId | string | Yes | The ID of the knowledge base. |
name | string | Yes | The name of the knowledge base detail. |
Response parameters
Name | Type | Description |
code | int | The return code. A value of 200 is returned on success. Null is returned on failure. |
success | bool | A value of true is returned on success. Null is returned on failure. |
requestId | string | The unique ID of the request. |
data | bool | Indicates whether the modification was successful. |
Examples
Create a knowledge base
Sample call
curl --location 'https://nlp.aliyuncs.com/v2/api/knowledge_base/create' \
--header 'Expect;' \
--header 'accept: */*' \
--header 'Content-Type: application/json' \
--header 'x-fag-servicename: aca-kb-create' \
--header 'x-fag-appcode: aca' \
--header 'Authorization: Bearer {YOUR_API_KEY}' \
--data '{
"name": "My Knowledge Base",
"description": "Description of my knowledge base."
}'import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
/**
* API name: Create a knowledge base
* Environment requirement: Java 17 or later
*/
public class Example {
private static final String URL = "https://nlp.aliyuncs.com/v2/api/knowledge_base/create";
private static final String API_KEY = "Bearer {YOUR_API_KEY}";
public static void main(String[] args) throws Exception {
// Create
create();
}
private static void create() throws Exception {
System.out.println("=== Create a knowledge base ===");
String jsonBody = """
{
"name": "My Knowledge Base",
"description": "Description of my knowledge base."
}
""";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(URL))
.headers(
"Authorization", API_KEY,
// Fixed servicename
"x-fag-servicename", "aca-kb-create",
"x-fag-appcode", "aca",
"Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(jsonBody))
.build();
// Synchronous call
HttpResponse<String> response = client.send(
request,
HttpResponse.BodyHandlers.ofString()
);
String responseBody = response.body();
System.out.println("Response data: " + responseBody);
}
}import requests
URL = "https://nlp.aliyuncs.com/v2/api/knowledge_base/create"
# API name: Create a knowledge base
# Environment requirement: Python 3.10 or later
# Create
def create(api_key):
headers = {
"Content-Type": "application/json",
"x-fag-servicename": "aca-kb-create",
"x-fag-appcode": "aca",
"Authorization": f"Bearer {api_key}"
}
payload = {
"name": "My Knowledge Base",
"description": "Description of my knowledge base."
}
response = requests.post(URL, json=payload, headers=headers)
# Print the raw response content line by line
for line in response.iter_lines():
if line:
print(line.decode('utf-8'))
# Check the response status and raise an exception if an error occurs
response.raise_for_status()
return response
if __name__ == "__main__":
api_key = "YOUR_API_KEY"
model_name = "xingchen-plus-latest"
try:
# Create a knowledge base
response = create(api_key)
except requests.HTTPError as e:
print(f"Request error: {e}")package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
// API name: Create a knowledge base
// Environment requirement: Go 1.20 or later
const URL = "https://nlp.aliyuncs.com/v2/api/knowledge_base/create"
// Create a knowledge base
func create(apiKey string) (*http.Response, error) {
payload := map[string]interface{}{
"name": "My Knowledge Base",
"description": "Description of my knowledge base.",
}
payloadBytes, err := json.Marshal(payload)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", URL, bytes.NewBuffer(payloadBytes))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("x-fag-servicename", "aca-kb-create")
req.Header.Set("x-fag-appcode", "aca")
req.Header.Set("Authorization", "Bearer "+apiKey)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
// Print the raw response content line by line
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
fmt.Println(string(body))
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Request error, status code: %d", resp.StatusCode)
}
return resp, nil
}
func main() {
apiKey := "YOUR_API_KEY"
// Create a knowledge base
_, err := create(apiKey)
if err != nil {
fmt.Printf("Request error: %v\n", err)
}
}Sample response
{
"requestId": "80153dfe-3a44-448e-9076-e65321aa3cce",
"code": 200,
"data": {
"knowledgeBaseId": "1358303662ed4b63b93e1ba3d2473cb0",
"name": "My Knowledge Base",
"description": "Description of my knowledge base."
},
"success": true
}Update a knowledge base
Sample call
curl --location 'https://nlp.aliyuncs.com/v2/api/knowledge_base/update' \
--header 'Expect;' \
--header 'accept: */*' \
--header 'Content-Type: application/json' \
--header 'x-fag-servicename: aca-kb-update' \
--header 'x-fag-appcode: aca' \
--header 'Authorization: Bearer {YOUR_API_KEY}' \
--data '{
"knowledgeBaseId": "The ID of the knowledge base",
"name": "The name of the knowledge base",
"description": "The description of the knowledge base"
}'import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
/**
* API name: Update a knowledge base
* Environment requirement: Java 17 or later
*/
public class Example {
private static final String URL = "https://nlp.aliyuncs.com/v2/api/knowledge_base/update";
private static final String API_KEY = "Bearer {YOUR_API_KEY}";
public static void main(String[] args) throws Exception {
// Update
update();
}
private static void update() throws Exception {
System.out.println("=== Update a knowledge base ===");
String jsonBody = """
{
"knowledgeBaseId": "{your_knowledge_base_id}",
"name": "My Updated Knowledge Base",
"description": "Updated description."
}
""";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(URL))
.headers(
"Authorization", API_KEY,
// Fixed servicename
"x-fag-servicename", "aca-kb-update",
"x-fag-appcode", "aca",
"Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(jsonBody))
.build();
// Synchronous call
HttpResponse<String> response = client.send(
request,
HttpResponse.BodyHandlers.ofString()
);
String responseBody = response.body();
System.out.println("Response data: " + responseBody);
}
}import requests
URL = "https://nlp.aliyuncs.com/v2/api/knowledge_base/update"
# API name: Update a knowledge base
# Environment requirement: Python 3.10 or later
# Update
def update(api_key):
headers = {
"Content-Type": "application/json",
"x-fag-servicename": "aca-kb-update",
"x-fag-appcode": "aca",
"Authorization": f"Bearer {api_key}"
}
payload = {
"knowledgeBaseId": "{your_knowledge_base_id}",
"name": "My Knowledge Base 3",
"description": "Description for knowledge base 3."
}
response = requests.post(URL, json=payload, headers=headers)
# Print the raw response content line by line
for line in response.iter_lines():
if line:
print(line.decode('utf-8'))
# Check the response status and raise an exception if an error occurs
response.raise_for_status()
return response
if __name__ == "__main__":
api_key = "YOUR_API_KEY"
model_name = "xingchen-plus-latest"
try:
# Update a knowledge base
response = update(api_key)
except requests.HTTPError as e:
print(f"Request error: {e}")package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
// API name: Update a knowledge base
// Environment requirement: Go 1.20 or later
const URL = "https://nlp.aliyuncs.com/v2/api/knowledge_base/update"
// Update a knowledge base
func update(apiKey string) (*http.Response, error) {
payload := map[string]interface{}{
"knowledgeBaseId": "{your_knowledge_base_id}",
"name": "My Updated Knowledge Base",
"description": "Updated description.",
}
payloadBytes, err := json.Marshal(payload)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", URL, bytes.NewBuffer(payloadBytes))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("x-fag-servicename", "aca-kb-update")
req.Header.Set("x-fag-appcode", "aca")
req.Header.Set("Authorization", "Bearer "+apiKey)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
// Print the raw response content line by line
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
fmt.Println(string(body))
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Request error, status code: %d", resp.StatusCode)
}
return resp, nil
}
func main() {
apiKey := "YOUR_API_KEY"
// Update a knowledge base
_, err := update(apiKey)
if err != nil {
fmt.Printf("Request error: %v\n", err)
}
}Sample response
{
"requestId": "0e18df96-9f56-4cd2-8175-5d0072c3f1d7",
"code": 200,
"data": true,
"success": true
}Query knowledge bases
Sample call
curl --location 'https://nlp.aliyuncs.com/v2/api/knowledge_base/search' \
--header 'Expect;' \
--header 'accept: */*' \
--header 'Content-Type: application/json' \
--header 'x-fag-servicename: aca-kb-search' \
--header 'x-fag-appcode: aca' \
--header 'Authorization: Bearer {YOUR_API_KEY}' \
--data '{
"pageNum": 1,
"pageSize": 10
}'import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
/**
* API name: Query knowledge bases
* Environment requirement: Java 17 or later
*/
public class Example {
private static final String URL = "https://nlp.aliyuncs.com/v2/api/knowledge_base/search";
private static final String API_KEY = "Bearer {YOUR_API_KEY}";
public static void main(String[] args) throws Exception {
// Query
search();
}
private static void search() throws Exception {
System.out.println("=== Query knowledge bases ===");
String jsonBody = """
{
"pageNum": 1,
"pageSize": 10
}
""";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(URL))
.headers(
"Authorization", API_KEY,
// Fixed servicename
"x-fag-servicename", "aca-kb-search",
"x-fag-appcode", "aca",
"Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(jsonBody))
.build();
// Synchronous call
HttpResponse<String> response = client.send(
request,
HttpResponse.BodyHandlers.ofString()
);
String responseBody = response.body();
System.out.println("Response data: " + responseBody);
}
}import requests
URL = "https://nlp.aliyuncs.com/v2/api/knowledge_base/search"
# API name: Query knowledge bases
# Environment requirement: Python 3.10 or later
# Query
def search(api_key):
headers = {
"Content-Type": "application/json",
"x-fag-servicename":"aca-kb-search",
"x-fag-appcode": "aca",
"Authorization": f"Bearer {api_key}"
}
payload = {
"pageNum": 1,
"pageSize": 10
}
response = requests.post(URL, json=payload, headers=headers)
# Print the raw response content line by line
for line in response.iter_lines():
if line:
print(line.decode('utf-8'))
# Check the response status and raise an exception if an error occurs
response.raise_for_status()
return response
if __name__ == "__main__":
api_key = "YOUR_API_KEY"
model_name = "xingchen-plus-latest"
try:
# Query knowledge bases
response = search(api_key)
except requests.HTTPError as e:
print(f"Request error: {e}")package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
// API name: Query knowledge bases
// Environment requirement: Go 1.20 or later
const URL = "https://nlp.aliyuncs.com/v2/api/knowledge_base/search"
// Query knowledge bases
func search(apiKey string) (*http.Response, error) {
payload := map[string]interface{}{
"pageNum": 1,
"pageSize": 10,
}
payloadBytes, err := json.Marshal(payload)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", URL, bytes.NewBuffer(payloadBytes))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("x-fag-servicename", "aca-kb-search")
req.Header.Set("x-fag-appcode", "aca")
req.Header.Set("Authorization", "Bearer "+apiKey)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
// Print the raw response content line by line
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
fmt.Println(string(body))
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Request error, status code: %d", resp.StatusCode)
}
return resp, nil
}
func main() {
apiKey := "YOUR_API_KEY"
// Query knowledge bases
_, err := search(apiKey)
if err != nil {
fmt.Printf("Request error: %v\n", err)
}
}Sample response
{
"requestId": "5bad2a93-9889-4a16-bccf-fd162dcc0f46",
"code": 200,
"data": {
"list": [
{
"id": 9922,
"gmtCreate": 1755828181000,
"gmtModified": 1755828229000,
"knowledgeBaseId": "538656e030d646e3bdb743bde55ee0e0",
"name": "My Knowledge Base",
"userId": "5ed33fdb8d9d4d1489e44efb5c41312b",
"bizUserId": "",
"description": "Description of my knowledge base.",
"isDeleted": 0,
"offlineConfig": {
"textVectorModel": "text-embedding-v2",
"language": "cn",
"textChunkStrategy": "clever"
},
"onlineConfig": {
"multiRoundQueryRewrite": true,
"textEmbedding": true,
"searchJudgment": true,
"rank": true
},
"knowledgeType": "text",
"count": 0
}
],
"page": 1,
"pageSize": 10,
"total": 1
},
"success": true
}Delete a knowledge base
Sample call
curl --location 'https://nlp.aliyuncs.com/v2/api/knowledge_base/delete' \
--header 'Expect;' \
--header 'accept: */*' \
--header 'Content-Type: application/json' \
--header 'x-fag-servicename: aca-kb-delete' \
--header 'x-fag-appcode: aca' \
--header 'Authorization: Bearer {YOUR_API_KEY}' \
--data '{
"knowledgeBaseId": "{your_knowledge_base_id}"
}'/**
* API name: Delete a knowledge base
* Environment requirement: Java 17 or later
*/
public class Example {
private static final String URL = "https://nlp.aliyuncs.com/v2/api/knowledge_base/delete";
private static final String API_KEY = "Bearer {YOUR_API_KEY}";
public static void main(String[] args) throws Exception {
// Delete
delete();
}
private static void delete() throws Exception {
System.out.println("=== Delete a knowledge base ===");
String jsonBody = """
{
"knowledgeBaseId": "{your_knowledge_base_id}"
}
""";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(URL))
.headers(
"Authorization", API_KEY,
// Fixed servicename
"x-fag-servicename", "aca-kb-delete",
"x-fag-appcode", "aca",
"Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(jsonBody))
.build();
// Synchronous call
HttpResponse<String> response = client.send(
request,
HttpResponse.BodyHandlers.ofString()
);
String responseBody = response.body();
System.out.println("Response data: " + responseBody);
}
}import requests
URL = "https://nlp.aliyuncs.com/v2/api/knowledge_base/delete"
# API name: Delete a knowledge base
# Environment requirement: Python 3.10 or later
# Delete
def delete(api_key):
headers = {
"Content-Type": "application/json",
"x-fag-servicename": "aca-kb-delete",
"x-fag-appcode": "aca",
"Authorization": f"Bearer {api_key}"
}
payload = {
"knowledgeBaseId": "{your_knowledge_base_id}"
}
response = requests.post(URL, json=payload, headers=headers)
# Print the raw response content line by line
for line in response.iter_lines():
if line:
print(line.decode('utf-8'))
# Check the response status and raise an exception if an error occurs
response.raise_for_status()
return response
if __name__ == "__main__":
api_key = "YOUR_API_KEY"
model_name = "xingchen-plus-latest"
try:
# Delete a knowledge base
response = delete(api_key)
except requests.HTTPError as e:
print(f"Request error: {e}")package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
// API name: Delete a knowledge base
// Environment requirement: Go 1.20 or later
const URL = "https://nlp.aliyuncs.com/v2/api/knowledge_base/delete"
// Delete
func delete(apiKey string) (*http.Response, error) {
payload := map[string]interface{}{
"knowledgeBaseId": "{your_knowledge_base_id}",
}
payloadBytes, err := json.Marshal(payload)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", URL, bytes.NewBuffer(payloadBytes))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("x-fag-servicename", "aca-kb-delete")
req.Header.Set("x-fag-appcode", "aca")
req.Header.Set("Authorization", "Bearer "+apiKey)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
// Print the raw response content line by line
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
fmt.Println(string(body))
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Request error, status code: %d", resp.StatusCode)
}
return resp, nil
}
func main() {
apiKey := "YOUR_API_KEY"
// Delete a knowledge base
_, err := delete(apiKey)
if err != nil {
fmt.Printf("Request error: %v\n", err)
}
}Sample response
{
"requestId": "a2ff0262-7c43-4537-92cd-dfa15120f7ee",
"code": 200,
"data": true,
"success": true
}Upload a knowledge base file
You can upload a maximum of 10 knowledge base files. The size of a single file cannot exceed 4 MB.
Sample call
curl --location 'https://nlp.aliyuncs.com/v2/api/knowledge_base/detail/upload' \
--header 'Expect;' \
--header 'accept: */*' \
--header 'Content-Type: application/json' \
--header 'x-fag-servicename: aca-kb-detail-upload' \
--header 'x-fag-appcode: aca' \
--header 'Authorization: Bearer {YOUR_API_KEY}' \
--data '{
"knowledgeBaseId": "{your_knowledge_base_id}",
"type": "text",
"fileInfos": [
{
"filename": "example.txt",
"fileUrl": "https://example.com/public_file.txt"
}
]
}'/**
* API name: Upload a knowledge base file
* Environment requirement: Java 17 or later
*/
public class Example {
private static final String URL = "https://nlp.aliyuncs.com/v2/api/knowledge_base/detail/upload";
private static final String API_KEY = "Bearer {YOUR_API_KEY}";
public static void main(String[] args) throws Exception {
// Upload
upload();
}
private static void upload() throws Exception {
System.out.println("=== Upload a knowledge base file ===");
String jsonBody = """
{
"knowledgeBaseId": "d1f5c2797e9e4e80ac7a6363c830d31a",
"type": "text",
"fileInfos": [
{
"fileUrl": "https://lang.alicdn.com/xingchen/guanyu.txt",
"filename": "guanyu.txt"
}
]
}
""";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(URL))
.headers(
"Authorization", API_KEY,
// Fixed servicename
"x-fag-servicename", "aca-kb-detail-upload",
"x-fag-appcode", "aca",
"Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(jsonBody))
.build();
// Synchronous call
HttpResponse<String> response = client.send(
request,
HttpResponse.BodyHandlers.ofString()
);
String responseBody = response.body();
System.out.println("Response data: " + responseBody);
}
}import requests
URL = "https://nlp.aliyuncs.com/v2/api/knowledge_base/detail/upload"
# API name: Upload a knowledge base file
# Environment requirement: Python 3.10 or later
# Upload
def upload(api_key):
headers = {
"Content-Type": "application/json",
"x-fag-servicename": "aca-kb-detail-upload",
"x-fag-appcode": "aca",
"Authorization": f"Bearer {api_key}"
}
payload = {
"knowledgeBaseId": "{your_knowledge_base_id}",
"type": "text",
"fileInfos": [
{
"fileUrl": "https://lang.alicdn.com/xingchen/guanyu.txt",
"filename": "guanyu3.txt"
}
]
}
response = requests.post(URL, json=payload, headers=headers)
# Print the raw response content line by line
for line in response.iter_lines():
if line:
print(line.decode('utf-8'))
# Check the response status and raise an exception if an error occurs
response.raise_for_status()
return response
if __name__ == "__main__":
api_key = "YOUR_API_KEY"
model_name = "xingchen-plus-latest"
try:
# Upload a knowledge base file
response = upload(api_key)
except requests.HTTPError as e:
print(f"Request error: {e}")package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
// API name: Upload a knowledge base file
// Environment requirement: Go 1.20 or later
const URL = "https://nlp.aliyuncs.com/v2/api/knowledge_base/detail/upload"
// Upload
func upload(apiKey string) (*http.Response, error) {
payload := map[string]interface{}{
"knowledgeBaseId": "{your_knowledge_base_id}",
"type": "text",
"fileInfos": []map[string]string{
{
"fileUrl": "https://lang.alicdn.com/xingchen/guanyu.txt",
"filename": "guanyu.txt",
},
},
}
payloadBytes, err := json.Marshal(payload)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", URL, bytes.NewBuffer(payloadBytes))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("x-fag-servicename", "aca-kb-detail-upload")
req.Header.Set("x-fag-appcode", "aca")
req.Header.Set("Authorization", "Bearer "+apiKey)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
// Print the raw response content line by line
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
fmt.Println(string(body))
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Request error, status code: %d", resp.StatusCode)
}
return resp, nil
}
func main() {
apiKey := "YOUR_API_KEY"
// Upload
_, err := upload(apiKey)
if err != nil {
fmt.Printf("Request error: %v\n", err)
}
}Sample response
{
"requestId": "3d81366f-0d6b-4ca5-9b85-f51f347c050f",
"code": 200,
"data": true,
"success": true
}Update a knowledge base file
Sample call
curl --location 'https://nlp.aliyuncs.com/v2/api/knowledge_base/detail/update' \
--header 'Expect;' \
--header 'accept: */*' \
--header 'Content-Type: application/json' \
--header 'x-fag-servicename: aca-kb-detail-update' \
--header 'x-fag-appcode: aca' \
--header 'Authorization: Bearer {YOUR_API_KEY}' \
--data '{
"knowledgeBaseId": "{your_knowledge_base_id}",
"name": "current_file.txt",
"newName": "new_file.txt"
}'/**
* API name: Update a knowledge base file
* Environment requirement: Java 17 or later
*/
public class Example {
private static final String URL = "https://nlp.aliyuncs.com/v2/api/knowledge_base/detail/update";
private static final String API_KEY = "Bearer {YOUR_API_KEY}";
public static void main(String[] args) throws Exception {
// Update
update();
}
private static void update() throws Exception {
System.out.println("=== Update a knowledge base file name ===");
// Set the name field to the preceding filename.
String jsonBody = """
{
"knowledgeBaseId": "{your_knowledge_base_id}",
"name": "guanyu.txt",
"newName": "newName.txt"
}
""";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(URL))
.headers(
"Authorization", API_KEY,
// Fixed servicename
"x-fag-servicename", "aca-kb-detail-update",
"x-fag-appcode", "aca",
"Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(jsonBody))
.build();
// Synchronous call
HttpResponse<String> response = client.send(
request,
HttpResponse.BodyHandlers.ofString()
);
String responseBody = response.body();
System.out.println("Response data: " + responseBody);
}
}import requests
URL = "https://nlp.aliyuncs.com/v2/api/knowledge_base/detail/update"
# API name: Update a knowledge base file
# Environment requirement: Python 3.10 or later
# Update
def update(api_key):
headers = {
"Content-Type": "application/json",
"x-fag-servicename": "aca-kb-detail-update",
"x-fag-appcode": "aca",
"Authorization": f"Bearer {api_key}"
}
# Set name to the preceding filename.
payload = {
"knowledgeBaseId": "{your_knowledge_base_id}",
"name": "guanyu.txt",
"newName": "newName.txt"
}
response = requests.post(URL, json=payload, headers=headers)
# Print the raw response content line by line
for line in response.iter_lines():
if line:
print(line.decode('utf-8'))
# Check the response status and raise an exception if an error occurs
response.raise_for_status()
return response
if __name__ == "__main__":
api_key = "YOUR_API_KEY"
model_name = "xingchen-plus-latest"
try:
# Update a knowledge base file
response = update(api_key)
except requests.HTTPError as e:
print(f"Request error: {e}")package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
// API name: Update a knowledge base file
// Environment requirement: Go 1.20 or later
const URL = "https://nlp.aliyuncs.com/v2/api/knowledge_base/detail/update"
// Update a knowledge base file
func update(apiKey string) (*http.Response, error) {
// Set name to the preceding filename.
payload := map[string]interface{}{
"knowledgeBaseId": "{your_knowledge_base_id}",
"name": "guanyu.txt",
"newName": "guanyu1.txt",
}
payloadBytes, err := json.Marshal(payload)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", URL, bytes.NewBuffer(payloadBytes))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("x-fag-servicename", "aca-kb-detail-update")
req.Header.Set("x-fag-appcode", "aca")
req.Header.Set("Authorization", "Bearer "+apiKey)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
// Print the raw response content line by line
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
fmt.Println(string(body))
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Request error, status code: %d", resp.StatusCode)
}
return resp, nil
}
func main() {
apiKey := "YOUR_API_KEY"
// Update a knowledge base file
_, err := update(apiKey)
if err != nil {
fmt.Printf("Request error: %v\n", err)
}
}Sample response
{
"requestId": "3d81366f-0d6b-4ca5-9b85-f51f347c050f",
"code": 200,
"data": true,
"success": true
}Query knowledge base files
Sample call
curl --location 'https://nlp.aliyuncs.com/v2/api/knowledge_base/detail/search' \
--header 'Expect;' \
--header 'accept: */*' \
--header 'Content-Type: application/json' \
--header 'x-fag-servicename: aca-kb-detail-search' \
--header 'x-fag-appcode: aca' \
--header 'Authorization: Bearer {YOUR_API_KEY}' \
--data '{
"knowledgeBaseId": "{your_knowledge_base_id}",
"pageNum": 1,
"pageSize": 10
}'/**
* API name: Query knowledge base files
* Environment requirement: Java 17 or later
*/
public class Example {
private static final String URL = "https://nlp.aliyuncs.com/v2/api/knowledge_base/detail/search";
private static final String API_KEY = "Bearer {YOUR_API_KEY}";
public static void main(String[] args) throws Exception {
// Query
search();
}
private static void search() throws Exception {
System.out.println("=== Query knowledge base files ===");
String jsonBody = """
{
"knowledgeBaseId": "{your_knowledge_base_id}",
"pageNum": 1,
"pageSize": 10
}
""";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(URL))
.headers(
"Authorization", API_KEY,
// Fixed servicename
"x-fag-servicename", "aca-kb-detail-search",
"x-fag-appcode", "aca",
"Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(jsonBody))
.build();
// Synchronous call
HttpResponse<String> response = client.send(
request,
HttpResponse.BodyHandlers.ofString()
);
String responseBody = response.body();
System.out.println("Response data: " + responseBody);
}
}import requests
URL = "https://nlp.aliyuncs.com/v2/api/knowledge_base/detail/search"
# API name: Query knowledge base files
# Environment requirement: Python 3.10 or later
# Query
def search(api_key):
headers = {
"Content-Type": "application/json",
"x-fag-servicename": "aca-kb-detail-search",
"x-fag-appcode": "aca",
"Authorization": f"Bearer {api_key}"
}
payload = {
"knowledgeBaseId": "{your_knowledge_base_id}",
"pageNum": 1,
"pageSize": 10
}
response = requests.post(URL, json=payload, headers=headers)
# Print the raw response content line by line
for line in response.iter_lines():
if line:
print(line.decode('utf-8'))
# Check the response status and raise an exception if an error occurs
response.raise_for_status()
return response
if __name__ == "__main__":
api_key = "YOUR_API_KEY"
model_name = "xingchen-plus-latest"
try:
# Query knowledge base files
response = search(api_key)
except requests.HTTPError as e:
print(f"Request error: {e}")package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
// API name: Query knowledge base files
// Environment requirement: Go 1.20 or later
const URL = "https://nlp.aliyuncs.com/v2/api/knowledge_base/detail/search"
// Query knowledge base files
func search(apiKey string) (*http.Response, error) {
payload := map[string]interface{}{
"knowledgeBaseId": "{your_knowledge_base_id}",
"pageNum": 1,
"pageSize": 10,
}
payloadBytes, err := json.Marshal(payload)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", URL, bytes.NewBuffer(payloadBytes))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("x-fag-servicename", "aca-kb-detail-search")
req.Header.Set("x-fag-appcode", "aca")
req.Header.Set("Authorization", "Bearer "+apiKey)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
// Print the raw response content line by line
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
fmt.Println(string(body))
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Request error, status code: %d", resp.StatusCode)
}
return resp, nil
}
func main() {
apiKey := "YOUR_API_KEY"
//Query knowledge base files
_, err := search(apiKey)
if err != nil {
fmt.Printf("Request error: %v\n", err)
}
}Sample response
{
"requestId": "d39b2708-527a-45b7-9343-69ee2178b6cb",
"code": 200,
"data": {
"list": [
{
"id": 34405,
"gmtCreate": 1755658488000,
"gmtModified": 1755658531000,
"knowledgeBaseId": "123b9e9ec4af4ec5bc2f9b8973417435",
"name": "guanyu3.txt",
"type": "text",
"status": "success",
"fileInfo": {
"fileSavePath": "kb/oss-5ed33fdb8d9d4d1489e44efb5c41312b-57e41178f147c2a44a48b3cf7547f259.txt",
"filename": "guanyu3.txt",
"fileUrl": "//character-ai.oss-cn-hangzhou.aliyuncs.com/kb/oss-5ed33fdb8d9d4d1489e44efb5c41312b-57e41178f147c2a44a48b3cf7547f259.txt?Expires=1787365690&OSSAccessKeyId=STS.NXoeuqCeWQmA4EtJBsw81aCHu&Signature=CS%2Bf3fg2iyVIlcH1vp4hmanhCxM%3D&security-token=CAISxgJ1q6Ft5B2yfSjIr5vaLs%2FFrrp25q%2BqNmPFrkIme7cdjobjlzz2IHhMe3lgA%2BgctPQ%2Bm29W7Pkflrp6SJtIXleCZtF94oxN9h2gb4fb4yhNH1Hh08%2FLI3OaLjKm9u2wCryLYbGwU%2FOpbE%2B%2B5U0X6LDmdDKkckW4OJmS8%2FBOZcgWWQ%2FKBlgvRq0hRG1YpdQdKGHaONu0LxfumRCwNkdzvRdmgm4NgsbWgO%2Fks0OO0AKqmrFE%2Btirecj1NPMBZskvD42Hu8VtbbfE3SJq7BxHybx7lqQs%2B02c5onCUwkLu0%2FcY7uLrYM1c1JjB6ExEutPqP%2Fhme2FZUhRDl0SofwkHZa2M0y3LOjIqKNPmNJwucXerM7kIYLL%2FNq5JKTbpcLYER3QzOuDsjfA%2B5Kd%2BNHtq8gDx2J%2BkruwAXhIM0Pui8k8N1kfkz2AlRqAAQ0r4%2FRXMmZ0RCrN8sq1Bwo3pekrHYfEZMO5ty9tNpcDP6fiznYiIoJ3vUE1FdZVjWaVapUhkscYVnrUGDILLF4tCwBXuH6Mnn5IQgf4TQXdWC9YfBvu8xwSimIehUePNDSVKIAc6GsRoziietNmQPWT4BD9Hu9WY48s5HsFmlD9IAA%3D"
}
}
],
"page": 1,
"pageSize": 10,
"total": 2
},
"success": true
}Delete a knowledge base file
Sample call
curl --location 'https://nlp.aliyuncs.com/v2/api/knowledge_base/detail/delete' \
--header 'Expect;' \
--header 'accept: */*' \
--header 'Content-Type: application/json' \
--header 'x-fag-servicename: aca-kb-detail-delete' \
--header 'x-fag-appcode: aca' \
--header 'Authorization: Bearer {YOUR_API_KEY}' \
--data '{
"knowledgeBaseId": "{your_knowledge_base_id}",
"name": "example_detail_name"
}'/**
* API name: Delete a knowledge base file
* Environment requirement: Java 17 or later
*/
public class Example {
private static final String URL = "https://nlp.aliyuncs.com/v2/api/knowledge_base/detail/delete";
private static final String API_KEY = "Bearer {YOUR_API_KEY}";
public static void main(String[] args) throws Exception {
// Delete
delete();
}
private static void delete() throws Exception {
System.out.println("=== Delete a knowledge base file ===");
String jsonBody = """
{
"knowledgeBaseId": "{your_knowledge_base_id}",
"name": "newName.txt"
}
""";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(URL))
.headers(
"Authorization", API_KEY,
// Fixed servicename
"x-fag-servicename", "aca-kb-detail-delete",
"x-fag-appcode", "aca",
"Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(jsonBody))
.build();
// Synchronous call
HttpResponse<String> response = client.send(
request,
HttpResponse.BodyHandlers.ofString()
);
String responseBody = response.body();
System.out.println("Response data: " + responseBody);
}
}import requests
URL = "https://nlp.aliyuncs.com/v2/api/knowledge_base/detail/delete"
# API name: Delete a knowledge base file
# Environment requirement: Python 3.10 or later
# Delete
def delete(api_key):
headers = {
"Content-Type": "application/json",
"x-fag-servicename": "aca-kb-detail-delete",
"x-fag-appcode": "aca",
"Authorization": f"Bearer {api_key}"
}
payload = {
"knowledgeBaseId": "{your_knowledge_base_id}",
"name": "guanyu.txt"
}
response = requests.post(URL, json=payload, headers=headers)
# Print the raw response content line by line
for line in response.iter_lines():
if line:
print(line.decode('utf-8'))
# Check the response status and raise an exception if an error occurs
response.raise_for_status()
return response
if __name__ == "__main__":
api_key = "YOUR_API_KEY"
model_name = "xingchen-plus-latest"
try:
# Delete
response = delete(api_key)
except requests.HTTPError as e:
print(f"Request error: {e}")package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
// API name: Delete a knowledge base file
// Environment requirement: Go 1.20 or later
const URL = "https://nlp.aliyuncs.com/v2/api/knowledge_base/detail/delete"
// Delete
func delete(apiKey string) (*http.Response, error) {
payload := map[string]interface{}{
"knowledgeBaseId": "{your_knowledge_base_id}",
"name": "guanyu.txt",
}
payloadBytes, err := json.Marshal(payload)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", URL, bytes.NewBuffer(payloadBytes))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("x-fag-servicename", "aca-kb-detail-delete")
req.Header.Set("x-fag-appcode", "aca")
req.Header.Set("Authorization", "Bearer "+apiKey)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
// Print the raw response content line by line
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
fmt.Println(string(body))
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Request error, status code: %d", resp.StatusCode)
}
return resp, nil
}
func main() {
apiKey := "YOUR_API_KEY"
// Delete
_, err := delete(apiKey)
if err != nil {
fmt.Printf("Request error: %v\n", err)
}
}Sample response
{
"requestId": "3d81366f-0d6b-4ca5-9b85-f51f347c050f",
"code": 200,
"data": true,
"success": true
}