1. Initialize the clientBefore you upload files and create a knowledge base, you must initialize the client. Use your configured AccessKey and AccessKey secret to complete identity verification and configure the endpoint. Public endpoints: Ensure that your client can access the internet. VPC endpoints: If your client is deployed in an Alibaba Cloud VPC in the Singapore ap-southeast-1
After the client is created, you will obtain a Client object for subsequent API calls. | Pythondef create_client() -> bailian20231229Client:
"""
Create and configure a client.
Returns:
bailian20231229Client: The configured client.
"""
config = open_api_models.Config(
access_key_id=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID'),
access_key_secret=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET')
)
# The following endpoint is a public endpoint for the public cloud. Replace it as needed.
config.endpoint = 'bailian.ap-southeast-1.aliyuncs.com'
return bailian20231229Client(config)
Java/**
* Initialize the client.
*
* @return The configured client object.
*/
public static com.aliyun.bailian20231229.Client createClient() throws Exception {
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
// The following endpoint is a VPC endpoint for the public cloud. Replace it as needed.
config.endpoint = "bailian-vpc.ap-southeast-1.aliyuncs.com";
return new com.aliyun.bailian20231229.Client(config);
}
PHP/**
* Initialize the client.
*
* @return Bailian The configured client object.
*/
public static function createClient(){
$config = new Config([
"accessKeyId" => getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
"accessKeySecret" => getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
]);
// The following endpoint is a VPC endpoint for the public cloud. Replace it as needed.
$config->endpoint = 'bailian-vpc.ap-southeast-1.aliyuncs.com';
return new Bailian($config);
}
Node.js/**
* Create and configure a client.
* @return Client
* @throws Exception
*/
static createClient() {
const config = new OpenApi.Config({
accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET
});
// The following endpoint is a VPC endpoint for the public cloud. Replace it as needed.
config.endpoint = `bailian-vpc.ap-southeast-1.aliyuncs.com`;
return new bailian20231229.default(config);
}
C#/// <summary>
/// Initialize the client.
/// </summary>
/// <returns>The configured client object.</returns>
/// <exception cref="Exception">An exception is thrown if an error occurs during initialization.</exception>
public static AlibabaCloud.SDK.Bailian20231229.Client CreateClient()
{
var config = new AlibabaCloud.OpenApiClient.Models.Config
{
AccessKeyId = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"),
AccessKeySecret = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
};
// The following endpoint is a VPC endpoint for the public cloud. Replace it as needed.
config.Endpoint = "bailian-vpc.ap-southeast-1.aliyuncs.com";
return new AlibabaCloud.SDK.Bailian20231229.Client(config);
}
Go// CreateClient creates and configures a client.
//
// Returns:
// - *client.Bailian20231229Client: The configured client.
// - error: An error message.
func CreateClient() (_result *bailian20231229.Client, _err error) {
config := &openapi.Config{
AccessKeyId: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")),
AccessKeySecret: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")),
}
// The following endpoint is a VPC endpoint for the public cloud. Replace it as needed.
config.Endpoint = tea.String("bailian-vpc.ap-southeast-1.aliyuncs.com")
_result = &bailian20231229.Client{}
_result, _err = bailian20231229.NewClient(config)
return _result, _err
}
|
2. Upload files for the knowledge base |
2.1. Request a file upload leaseBefore you create a knowledge base, you must upload files to the same workspace to serve as the source of knowledge. Before you upload a file, call the ApplyFileUploadLease operation to request a file upload lease. The lease is a temporary authorization that lets you upload a file within a limited time. The lease is valid for several minutes. workspace_id: For more information, see How to obtain a workspace ID. category_id: In this example, enter default. Alibaba Cloud Model Studio uses categories to manage your uploaded files. The system automatically creates a default category. You can also call the AddCategory operation to create a new category and obtain the corresponding category_id. file_name: Enter the name of the file to upload, including the extension. The value must be the same as the actual filename. file_md5: Enter the MD5 hash of the file to upload. Alibaba Cloud does not currently verify this value. This lets you upload files from a URL. For example, in Python, you can obtain the MD5 hash using the hashlib module. For other languages, see the complete sample code. Code example import hashlib
def calculate_md5(file_path):
"""
Calculate the MD5 hash of a file.
Args:
file_path (str): The local path of the file.
Returns:
str: The MD5 hash of the file.
"""
md5_hash = hashlib.md5()
# Read the file in binary mode.
with open(file_path, "rb") as f:
# Read the file in chunks to avoid high memory usage for large files.
for chunk in iter(lambda: f.read(4096), b""):
md5_hash.update(chunk)
return md5_hash.hexdigest()
# Example usage
file_path = "Replace this with the actual local path of the file to upload, for example, /xxx/xxx/xxx/Bailian_Phones_Specifications.docx"
md5_value = calculate_md5(file_path)
print(f"The MD5 hash of the file is: {md5_value}")
Replace the file_path variable in the code with the actual local path of the file and run the code. You can then obtain the MD5 hash of the object file. The following value is an example. The MD5 hash of the file is: 2ef7361ea907f3a1b91e3b9936f5643a
file_size: Enter the size of the file to upload in bytes. For example, in Python, you can obtain this value using the os module. For other languages, see the complete sample code. Code example import os
def get_file_size(file_path: str) -> int:
"""
Get the size of the file in bytes.
Args:
file_path (str): The actual local path of the file.
Returns:
int: The file size in bytes.
"""
return os.path.getsize(file_path)
# Example usage
file_path = "Replace this with the actual local path of the file to upload, for example, /xxx/xxx/xxx/Bailian_Phones_Specifications.docx"
file_size = get_file_size(file_path)
print(f"The size of the file in bytes is: {file_size}")
Replace the file_path variable in the code with the actual local path of the file and run the code. You can then obtain the size of the object file in bytes. The following value is an example. The size of the file in bytes is: 14015
After you successfully request a temporary upload lease, you will obtain: You will use these in the next step. |
Important A RAM user must be granted the required API permissions (the AliyunBailianDataFullAccess policy) before calling this operation. This operation supports online debugging and the generation of sample code for multiple languages.
Pythondef apply_lease(client, category_id, file_name, file_md5, file_size, workspace_id):
"""
Request a file upload lease from Alibaba Cloud Model Studio.
Args:
client (bailian20231229Client): The client.
category_id (str): The category ID.
file_name (str): The file name.
file_md5 (str): The MD5 hash of the file.
file_size (int): The file size in bytes.
workspace_id (str): The workspace ID.
Returns:
The response from Alibaba Cloud Model Studio.
"""
headers = {}
request = bailian_20231229_models.ApplyFileUploadLeaseRequest(
file_name=file_name,
md_5=file_md5,
size_in_bytes=file_size,
)
runtime = util_models.RuntimeOptions()
return client.apply_file_upload_lease_with_options(category_id, workspace_id, request, headers, runtime)
Java/**
* Request a file upload lease.
*
* @param client The client object.
* @param categoryId The category ID.
* @param fileName The file name.
* @param fileMd5 The MD5 hash of the file.
* @param fileSize The file size in bytes.
* @param workspaceId The workspace ID.
* @return The response object from Alibaba Cloud Model Studio.
*/
public ApplyFileUploadLeaseResponse applyLease(com.aliyun.bailian20231229.Client client, String categoryId, String fileName, String fileMd5, String fileSize, String workspaceId) throws Exception {
Map<String, String> headers = new HashMap<>();
com.aliyun.bailian20231229.models.ApplyFileUploadLeaseRequest applyFileUploadLeaseRequest = new com.aliyun.bailian20231229.models.ApplyFileUploadLeaseRequest();
applyFileUploadLeaseRequest.setFileName(fileName);
applyFileUploadLeaseRequest.setMd5(fileMd5);
applyFileUploadLeaseRequest.setSizeInBytes(fileSize);
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
ApplyFileUploadLeaseResponse applyFileUploadLeaseResponse = null;
applyFileUploadLeaseResponse = client.applyFileUploadLeaseWithOptions(categoryId, workspaceId, applyFileUploadLeaseRequest, headers, runtime);
return applyFileUploadLeaseResponse;
}
PHP/**
* Request a file upload lease.
*
* @param Bailian $client The client.
* @param string $categoryId The category ID.
* @param string $fileName The file name.
* @param string $fileMd5 The MD5 hash of the file.
* @param int $fileSize The file size in bytes.
* @param string $workspaceId The workspace ID.
* @return ApplyFileUploadLeaseResponse The response from Alibaba Cloud Model Studio.
*/
public function applyLease($client, $categoryId, $fileName, $fileMd5, $fileSize, $workspaceId) {
$headers = [];
$applyFileUploadLeaseRequest = new ApplyFileUploadLeaseRequest([
"fileName" => $fileName,
"md5" => $fileMd5,
"sizeInBytes" => $fileSize
]);
$runtime = new RuntimeOptions([]);
return $client->applyFileUploadLeaseWithOptions($categoryId, $workspaceId, $applyFileUploadLeaseRequest, $headers, $runtime);
}
Node.js/**
* Request a file upload lease.
* @param {Bailian20231229Client} client - The client.
* @param {string} categoryId - The category ID.
* @param {string} fileName - The file name.
* @param {string} fileMd5 - The MD5 hash of the file.
* @param {string} fileSize - The file size in bytes.
* @param {string} workspaceId - The workspace ID.
* @returns {Promise<bailian20231229.ApplyFileUploadLeaseResponse>} - The response from Alibaba Cloud Model Studio.
*/
async function applyLease(client, categoryId, fileName, fileMd5, fileSize, workspaceId) {
const headers = {};
const req = new bailian20231229.ApplyFileUploadLeaseRequest({
md5: fileMd5,
fileName,
sizeInBytes: fileSize
});
const runtime = new Util.RuntimeOptions({});
return await client.applyFileUploadLeaseWithOptions(
categoryId,
workspaceId,
req,
headers,
runtime
);
}
C#/// <summary>
/// Request a file upload lease.
/// </summary>
/// <param name="client">The client object.</param>
/// <param name="categoryId">The category ID.</param>
/// <param name="fileName">The file name.</param>
/// <param name="fileMd5">The MD5 hash of the file.</param>
/// <param name="fileSize">The file size in bytes.</param>
/// <param name="workspaceId">The workspace ID.</param>
/// <returns>The response object from Alibaba Cloud Model Studio.</returns>
/// <exception cref="Exception">An exception is thrown if an error occurs during the call.</exception>
public AlibabaCloud.SDK.Bailian20231229.Models.ApplyFileUploadLeaseResponse ApplyLease(
AlibabaCloud.SDK.Bailian20231229.Client client,
string categoryId,
string fileName,
string fileMd5,
string fileSize,
string workspaceId)
{
var headers = new Dictionary<string, string>() { };
var applyFileUploadLeaseRequest = new AlibabaCloud.SDK.Bailian20231229.Models.ApplyFileUploadLeaseRequest
{
FileName = fileName,
Md5 = fileMd5,
SizeInBytes = fileSize
};
var runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
return client.ApplyFileUploadLeaseWithOptions(categoryId, workspaceId, applyFileUploadLeaseRequest, headers, runtime);
}
Go// ApplyLease requests a file upload lease from Alibaba Cloud Model Studio.
//
// Args:
// - client (bailian20231229.Client): The client.
// - categoryId (string): The category ID.
// - fileName (string): The file name.
// - fileMD5 (string): The MD5 hash of the file.
// - fileSize (string): The file size in bytes.
// - workspaceId (string): The workspace ID.
//
// Returns:
// - *bailian20231229.ApplyFileUploadLeaseResponse: The response from Alibaba Cloud Model Studio.
// - error: An error message.
func ApplyLease(client *bailian20231229.Client, categoryId, fileName, fileMD5 string, fileSize string, workspaceId string) (_result *bailian20231229.ApplyFileUploadLeaseResponse, _err error) {
headers := make(map[string]*string)
applyFileUploadLeaseRequest := &bailian20231229.ApplyFileUploadLeaseRequest{
FileName: tea.String(fileName),
Md5: tea.String(fileMD5),
SizeInBytes: tea.String(fileSize),
}
runtime := &util.RuntimeOptions{}
return client.ApplyFileUploadLeaseWithOptions(tea.String(categoryId), tea.String(workspaceId), applyFileUploadLeaseRequest, headers, runtime)
}
Request example {
"CategoryId": "default",
"FileName": "Bailian_Phones_Specifications.docx",
"Md5": "2ef7361ea907f3a1b91e3b9936f5643a",
"SizeInBytes": "14015",
"WorkspaceId": "llm-4u5xpd1xdjqpxxxx"
}
Response example {
"RequestId": "778C0B3B-59C2-5FC1-A947-36EDD1XXXXXX",
"Success": true,
"Message": "",
"Code": "success",
"Status": "200",
"Data": {
"FileUploadLeaseId": "1e6a159107384782be5e45ac4759b247.1719325231035",
"Type": "HTTP",
"Param": {
"Method": "PUT",
"Url": "https://bailian-datahub-data-origin-prod.oss-cn-hangzhou.aliyuncs.com/1005426495169178/10024405/68abd1dea7b6404d8f7d7b9f7fbd332d.1716698936847.pdf?Expires=1716699536&OSSAccessKeyId=TestID&Signature=HfwPUZo4pR6DatSDym0zFKVh9Wg%3D",
"Headers": " \"X-bailian-extra\": \"MTAwNTQyNjQ5NTE2OTE3OA==\",\n \"Content-Type\": \"application/pdf\""
}
}
}
|
2.2. Upload the file to temporary storageAfter you obtain the upload lease, use the temporary upload parameters and URL from the lease to upload a file to the Model Studio server. You can upload the file from local storage or a public URL. Each workspace supports up to 10,000 files. The supported file formats are PDF, DOCX, DOC, TXT, Markdown, PPTX, PPT, XLSX, XLS, HTML, PNG, JPG, JPEG, BMP, and GIF. |
Important This operation does not support online debugging or sample code generation. Upload from local storagePythonimport requests
from urllib.parse import urlparse
def upload_file(pre_signed_url, file_path):
"""
Upload a local file to temporary storage.
Args:
pre_signed_url (str): The URL from the upload lease.
file_path (str): The local path of the file.
Returns:
The response from Alibaba Cloud Model Studio.
"""
try:
# Set the request headers.
headers = {
"X-bailian-extra": "Replace this with the value of the Data.Param.Headers.X-bailian-extra field returned by the ApplyFileUploadLease operation in the previous step.",
"Content-Type": "Replace this with the value of the Data.Param.Headers.Content-Type field returned by the ApplyFileUploadLease operation in the previous step. If an empty value is returned, pass an empty value."
}
# Read the file and upload it.
with open(file_path, 'rb') as file:
# The request method for file upload must be the same as the value of the Data.Param.Method field returned by the ApplyFileUploadLease operation in the previous step.
response = requests.put(pre_signed_url, data=file, headers=headers)
# Check the status code of the response.
if response.status_code == 200:
print("File uploaded successfully.")
else:
print(f"Failed to upload the file. ResponseCode: {response.status_code}")
except Exception as e:
print(f"An error occurred: {str(e)}")
if __name__ == "__main__":
pre_signed_url_or_http_url = "Replace this with the value of the Data.Param.Url field returned by the ApplyFileUploadLease operation in the previous step."
# Upload a local file to temporary storage.
file_path = "Replace this with the actual local path of the file to upload (for example, on Linux: /xxx/xxx/Bailian_Phones_Specifications.docx)."
upload_file(pre_signed_url_or_http_url, file_path)
Javaimport java.io.DataOutputStream;
import java.io.FileInputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class UploadFile {
public static void uploadFile(String preSignedUrl, String filePath) {
HttpURLConnection connection = null;
try {
// Create a URL object.
URL url = new URL(preSignedUrl);
connection = (HttpURLConnection) url.openConnection();
// The request method for file upload must be the same as the value of the Data.Param.Method field returned by the ApplyFileUploadLease operation in the previous step.
connection.setRequestMethod("PUT");
// Allow output to the connection because this connection is used to upload the file.
connection.setDoOutput(true);
connection.setRequestProperty("X-bailian-extra", "Replace this with the value of the Data.Param.Headers.X-bailian-extra field returned by the ApplyFileUploadLease operation in the previous step.");
connection.setRequestProperty("Content-Type", "Replace this with the value of the Data.Param.Headers.Content-Type field returned by the ApplyFileUploadLease operation in the previous step. If an empty value is returned, pass an empty value.");
// Read the file and upload it through the connection.
try (DataOutputStream outStream = new DataOutputStream(connection.getOutputStream());
FileInputStream fileInputStream = new FileInputStream(filePath)) {
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = fileInputStream.read(buffer)) != -1) {
outStream.write(buffer, 0, bytesRead);
}
outStream.flush();
}
// Check the response.
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
// The file was uploaded successfully.
System.out.println("File uploaded successfully.");
} else {
// The file failed to be uploaded.
System.out.println("Failed to upload the file. ResponseCode: " + responseCode);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
public static void main(String[] args) {
String preSignedUrlOrHttpUrl = "Replace this with the value of the Data.Param.Url field returned by the ApplyFileUploadLease operation in the previous step.";
// Upload a local file to temporary storage.
String filePath = "Replace this with the actual local path of the file to upload (for example, on Linux: /xxx/xxx/Bailian_Phones_Specifications.docx).";
uploadFile(preSignedUrlOrHttpUrl, filePath);
}
}
PHP<?php
/**
* Upload a local file to temporary storage.
*
* @param string $preSignedUrl The pre-signed URL or HTTP address obtained from the ApplyFileUploadLease operation.
* @param array $headers An array of request headers containing "X-bailian-extra" and "Content-Type".
* @param string $filePath The local file path.
* @throws Exception If the upload fails.
*/
function uploadFile($preSignedUrl, $headers, $filePath) {
// Read the file content.
$fileContent = file_get_contents($filePath);
if ($fileContent === false) {
throw new Exception("Cannot read the file: " . $filePath);
}
// Initialize a cURL session.
$ch = curl_init();
// Set cURL options.
curl_setopt($ch, CURLOPT_URL, $preSignedUrl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); // Use the PUT method.
curl_setopt($ch, CURLOPT_POSTFIELDS, $fileContent); // Set the request body to the file content.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return the response instead of outputting it directly.
// Build the request headers.
$uploadHeaders = [
"X-bailian-extra: " . $headers["X-bailian-extra"],
"Content-Type: " . $headers["Content-Type"]
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $uploadHeaders);
// Execute the request.
$response = curl_exec($ch);
// Get the HTTP status code.
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// Close the cURL session.
curl_close($ch);
// Check the status code.
if ($httpCode != 200) {
throw new Exception("Upload failed. HTTP status code: " . $httpCode . ". Error message: " . $response);
}
// The upload is successful.
echo "File uploaded successfully.\n";
}
/**
* Main function: Upload a local file.
*/
function main() {
// Replace this with the value of the Data.Param.Url field returned by the ApplyFileUploadLease operation in the previous step.
$preSignedUrl = "Replace this with the value of the Data.Param.Url field returned by the ApplyFileUploadLease operation in the previous step.";
// Replace these with the values of X-bailian-extra and Content-Type in Data.Param.Headers returned by the ApplyFileUploadLease operation in the previous step.
$headers = [
"X-bailian-extra" => "Replace this with the value of the Data.Param.Headers.X-bailian-extra field returned by the ApplyFileUploadLease operation in the previous step.",
"Content-Type" => "Replace this with the value of the Data.Param.Headers.Content-Type field returned by the ApplyFileUploadLease operation in the previous step. If an empty value is returned, pass an empty value."
];
// Upload a local file to temporary storage.
$filePath = "Replace this with the actual local path of the file to upload (for example, on Linux: /xxx/xxx/Bailian_Phones_Specifications.docx).";
try {
uploadFile($preSignedUrl, $headers, $filePath);
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}
}
// Call the main function.
main();
?>
Node.jsconst fs = require('fs');
const axios = require('axios');
/**
* Upload a local file to temporary storage.
*
* @param {string} preSignedUrl - The URL from the upload lease.
* @param {Object} headers - The headers for the upload request.
* @param {string} filePath - The local path of the file.
* @throws {Error} If the upload fails.
*/
async function uploadFile(preSignedUrl, headers, filePath) {
// Build the request headers required for the upload.
const uploadHeaders = {
"X-bailian-extra": headers["X-bailian-extra"],
"Content-Type": headers["Content-Type"]
};
// Create a file read stream.
const fileStream = fs.createReadStream(filePath);
try {
// Use axios to send a PUT request.
const response = await axios.put(preSignedUrl, fileStream, {
headers: uploadHeaders
});
// Check the status code of the response.
if (response.status === 200) {
console.log("File uploaded successfully.");
} else {
console.error(`Failed to upload the file. ResponseCode: ${response.status}`);
throw new Error(`Upload failed with status code: ${response.status}`);
}
} catch (error) {
// Handle errors.
console.error("Error during upload:", error.message);
throw new Error(`Upload failed: ${error.message}`);
}
}
/**
* Main function: Upload a local file.
*/
function main() {
const preSignedUrl = "Replace this with the value of the Data.Param.Url field returned by the ApplyFileUploadLease operation in the previous step.";
const headers = {
"X-bailian-extra": "Replace this with the value of the Data.Param.Headers.X-bailian-extra field returned by the ApplyFileUploadLease operation in the previous step.",
"Content-Type": "Replace this with the value of the Data.Param.Headers.Content-Type field returned by the ApplyFileUploadLease operation in the previous step. If an empty value is returned, pass an empty value."
};
// Upload a local file to temporary storage.
const filePath = "Replace this with the actual local path of the file to upload (for example, on Linux: /xxx/xxx/Bailian_Phones_Specifications.docx).";
uploadFile(preSignedUrl, headers, filePath)
.then(() => {
console.log("Upload completed.");
})
.catch((err) => {
console.error("Upload failed:", err.message);
});
}
// Call the main function.
main();
C#using System;
using System.IO;
using System.Net;
public class UploadFilExample
{
public static void UploadFile(string preSignedUrl, string filePath)
{
HttpWebRequest connection = null;
try
{
// Create a URL object.
Uri url = new Uri(preSignedUrl);
connection = (HttpWebRequest)WebRequest.Create(url);
// The request method for file upload must be the same as the value of the Data.Param.Method field returned by the ApplyFileUploadLease operation in the previous step.
connection.Method = "PUT";
// Allow output to the connection because this connection is used to upload the file.
connection.AllowWriteStreamBuffering = false;
connection.SendChunked = false;
// Set the request headers to be the same as the field values in Data.Param.Headers returned by the ApplyFileUploadLease operation in the previous step.
connection.Headers["X-bailian-extra"] = "Replace this with the value of the Data.Param.Headers.X-bailian-extra field returned by the ApplyFileUploadLease operation in the previous step.";
connection.ContentType = "Replace this with the value of the Data.Param.Headers.Content-Type field returned by the ApplyFileUploadLease operation in the previous step. If an empty value is returned, pass an empty value.";
// Read the file and upload it through the connection.
using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
using (var requestStream = connection.GetRequestStream())
{
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
requestStream.Write(buffer, 0, bytesRead);
}
requestStream.Flush();
}
// Check the response.
using (HttpWebResponse response = (HttpWebResponse)connection.GetResponse())
{
if (response.StatusCode == HttpStatusCode.OK)
{
// The file was uploaded successfully.
Console.WriteLine("File uploaded successfully.");
}
else
{
// The file failed to be uploaded.
Console.WriteLine($"Failed to upload the file. ResponseCode: {response.StatusCode}");
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
e.StackTrace.ToString();
}
finally
{
if (connection != null)
{
connection.Abort();
}
}
}
public static void Main(string[] args)
{
string preSignedUrlOrHttpUrl = "Replace this with the value of the Data.Param.Url field returned by the ApplyFileUploadLease operation in the previous step.";
// Upload a local file to temporary storage.
string filePath = "Replace this with the actual local path of the file to upload (for example, on Linux: /xxx/xxx/Bailian_Phones_Specifications.docx).";
UploadFile(preSignedUrlOrHttpUrl, filePath);
}
}
Gopackage main
import (
"fmt"
"io"
"os"
"github.com/go-resty/resty/v2"
)
// UploadFile uploads a local file to temporary storage.
//
// Args:
// - preSignedUrl (string): The URL from the upload lease.
// - headers (map[string]string): The headers for the upload request.
// - filePath (string): The local path of the file.
//
// Returns:
// - error: An error message if the upload fails, otherwise nil.
func UploadFile(preSignedUrl string, headers map[string]string, filePath string) error {
// Open the local file.
file, err := os.Open(filePath)
if err != nil {
return fmt.Errorf("failed to open file: %w", err)
}
defer file.Close()
// Read the content.
body, err := io.ReadAll(file)
if err != nil {
return fmt.Errorf("failed to read file: %w", err)
}
// Create a REST client.
client := resty.New()
// Build the request headers required for the upload.
uploadHeaders := map[string]string{
"X-bailian-extra": headers["X-bailian-extra"],
"Content-Type": headers["Content-Type"],
}
// Send a PUT request.
resp, err := client.R().
SetHeaders(uploadHeaders).
SetBody(body).
Put(preSignedUrl)
if err != nil {
return fmt.Errorf("failed to send request: %w", err)
}
// Check the HTTP response status code.
if resp.IsError() {
return fmt.Errorf("HTTP error: %d", resp.StatusCode())
}
fmt.Println("File uploaded successfully.")
return nil
}
// main function
func main() {
// Replace this with the value of the Data.Param.Url field returned by the ApplyFileUploadLease operation in the previous step.
preSignedUrl := "Replace this with the value of the Data.Param.Url field returned by the ApplyFileUploadLease operation in the previous step."
// Replace these with the values of X-bailian-extra and Content-Type in Data.Param.Headers returned by the ApplyFileUploadLease operation in the previous step.
headers := map[string]string{
"X-bailian-extra": "Replace this with the value of the Data.Param.Headers.X-bailian-extra field returned by the ApplyFileUploadLease operation in the previous step.",
"Content-Type": "Replace this with the value of the Data.Param.Headers.Content-Type field returned by the ApplyFileUploadLease operation in the previous step. If an empty value is returned, pass an empty value.",
}
// Upload a local file to temporary storage.
filePath := "Replace this with the actual local path of the file to upload (for example, on Linux: /xxx/xxx/Bailian_Phones_Specifications.docx)."
// Call the upload function.
err := UploadFile(preSignedUrl, headers, filePath)
if err != nil {
fmt.Printf("Upload failed: %v\n", err)
}
}
Upload from a URLEnsure that the URL is publicly accessible and points to a valid file. Pythonimport requests
from urllib.parse import urlparse
def upload_file_link(pre_signed_url, source_url_string):
"""
Upload a file from a public URL to temporary storage.
Args:
pre_signed_url (str): The URL from the upload lease.
source_url_string (str): The URL of the file.
Returns:
The response from Alibaba Cloud Model Studio.
"""
try:
# Set the request headers.
headers = {
"X-bailian-extra": "Replace this with the value of the Data.Param.Headers.X-bailian-extra field returned by the ApplyFileUploadLease operation in the previous step.",
"Content-Type": "Replace this with the value of the Data.Param.Headers.Content-Type field returned by the ApplyFileUploadLease operation in the previous step. If an empty value is returned, pass an empty value."
}
# Set the request method to GET to access the file URL.
source_response = requests.get(source_url_string)
if source_response.status_code != 200:
raise RuntimeError("Failed to get source file.")
# The request method for file upload must be the same as the value of the Data.Param.Method field returned by the ApplyFileUploadLease operation in the previous step.
response = requests.put(pre_signed_url, data=source_response.content, headers=headers)
# Check the status code of the response.
if response.status_code == 200:
print("File uploaded successfully.")
else:
print(f"Failed to upload the file. ResponseCode: {response.status_code}")
except Exception as e:
print(f"An error occurred: {str(e)}")
if __name__ == "__main__":
pre_signed_url_or_http_url = "Replace this with the value of the Data.Param.Url field returned by the ApplyFileUploadLease operation in the previous step. If an empty value is returned, pass an empty value."
# The URL of the file.
source_url = "Replace this with the URL of the file to upload."
upload_file_link(pre_signed_url_or_http_url, source_url)
Javaimport java.io.BufferedInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class UploadFile {
public static void uploadFileLink(String preSignedUrl, String sourceUrlString) {
HttpURLConnection connection = null;
try {
// Create a URL object.
URL url = new URL(preSignedUrl);
connection = (HttpURLConnection) url.openConnection();
// The request method for file upload must be the same as the value of the Data.Param.Method field returned by the ApplyFileUploadLease operation in the previous step.
connection.setRequestMethod("PUT");
// Allow output to the connection because this connection is used to upload the file.
connection.setDoOutput(true);
connection.setRequestProperty("X-bailian-extra", "Replace this with the value of the Data.Param.Headers.X-bailian-extra field returned by the ApplyFileUploadLease operation in the previous step.");
connection.setRequestProperty("Content-Type", "Replace this with the value of the Data.Param.Headers.Content-Type field returned by the ApplyFileUploadLease operation in the previous step. If an empty value is returned, pass an empty value.");
URL sourceUrl = new URL(sourceUrlString);
HttpURLConnection sourceConnection = (HttpURLConnection) sourceUrl.openConnection();
// Set the request method to GET to access the file URL.
sourceConnection.setRequestMethod("GET");
// Get the response code. 200 indicates that the request was successful.
int sourceFileResponseCode = sourceConnection.getResponseCode();
// Read the file from the URL and upload it through the connection.
if (sourceFileResponseCode != HttpURLConnection.HTTP_OK) {
throw new RuntimeException("Failed to get source file.");
}
try (DataOutputStream outStream = new DataOutputStream(connection.getOutputStream());
InputStream in = new BufferedInputStream(sourceConnection.getInputStream())) {
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
outStream.write(buffer, 0, bytesRead);
}
outStream.flush();
}
// Check the response.
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
// The file was uploaded successfully.
System.out.println("File uploaded successfully.");
} else {
// The file failed to be uploaded.
System.out.println("Failed to upload the file. ResponseCode: " + responseCode);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
public static void main(String[] args) {
String preSignedUrlOrHttpUrl = "Replace this with the value of the Data.Param.Url field returned by the ApplyFileUploadLease operation in the previous step.";
String sourceUrl = "Replace this with the URL of the file to upload.";
uploadFileLink(preSignedUrlOrHttpUrl, sourceUrl);
}
}
PHP<?php
/**
* Upload a file from a public URL to temporary storage.
*
* @param string $preSignedUrl The pre-signed URL or HTTP address obtained from the ApplyFileUploadLease operation.
* @param array $headers An array of request headers containing "X-bailian-extra" and "Content-Type".
* @param string $sourceUrl The URL of the file.
* @throws Exception If the upload fails.
*/
function uploadFile($preSignedUrl, $headers, $sourceUrl) {
$fileContent = file_get_contents($sourceUrl);
if ($fileContent === false) {
throw new Exception("Cannot download the file from the given URL: " . $sourceUrl);
}
// Initialize a cURL session.
$ch = curl_init();
// Set cURL options.
curl_setopt($ch, CURLOPT_URL, $preSignedUrl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); // Use the PUT method.
curl_setopt($ch, CURLOPT_POSTFIELDS, $fileContent); // Set the request body to the file content.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return the response instead of outputting it directly.
// Build the request headers.
$uploadHeaders = [
"X-bailian-extra: " . $headers["X-bailian-extra"],
"Content-Type: " . $headers["Content-Type"]
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $uploadHeaders);
// Execute the request.
$response = curl_exec($ch);
// Get the HTTP status code.
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// Close the cURL session.
curl_close($ch);
// Check the status code.
if ($httpCode != 200) {
throw new Exception("Upload failed. HTTP status code: " . $httpCode . ". Error message: " . $response);
}
// The upload is successful.
echo "File uploaded successfully.\n";
}
/**
* Main function: Upload a file from a public URL to temporary storage.
*/
function main() {
// Replace this with the value of the Data.Param.Url field returned by the ApplyFileUploadLease operation in the previous step.
$preSignedUrl = "Replace this with the value of the Data.Param.Url field returned by the ApplyFileUploadLease operation in the previous step.";
// Replace these with the values of X-bailian-extra and Content-Type in Data.Param.Headers returned by the ApplyFileUploadLease operation in the previous step.
$headers = [
"X-bailian-extra" => "Replace this with the value of the Data.Param.Headers.X-bailian-extra field returned by the ApplyFileUploadLease operation in the previous step.",
"Content-Type" => "Replace this with the value of the Data.Param.Headers.Content-Type field returned by the ApplyFileUploadLease operation in the previous step. If an empty value is returned, pass an empty value."
];
$sourceUrl = "Replace this with the URL of the file to upload.";
try {
uploadFile($preSignedUrl, $headers, $sourceUrl);
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}
}
// Call the main function.
main();
?>
Node.jsconst axios = require('axios');
/**
* Upload a file from a public URL to temporary storage.
*
* @param {string} preSignedUrl - The URL from the upload lease.
* @param {Object} headers - The headers for the upload request.
* @param {string} sourceUrl - The URL of the file.
* @throws {Error} If the upload fails.
*/
async function uploadFileFromUrl(preSignedUrl, headers, sourceUrl) {
// Build the request headers required for the upload.
const uploadHeaders = {
"X-bailian-extra": headers["X-bailian-extra"],
"Content-Type": headers["Content-Type"]
};
try {
// Download the file from the given URL.
const response = await axios.get(sourceUrl, {
responseType: 'stream'
});
// Use axios to send a PUT request.
const uploadResponse = await axios.put(preSignedUrl, response.data, {
headers: uploadHeaders
});
// Check the status code of the response.
if (uploadResponse.status === 200) {
console.log("File uploaded successfully from URL.");
} else {
console.error(`Failed to upload the file. ResponseCode: ${uploadResponse.status}`);
throw new Error(`Upload failed with status code: ${uploadResponse.status}`);
}
} catch (error) {
// Handle errors.
console.error("Error during upload:", error.message);
throw new Error(`Upload failed: ${error.message}`);
}
}
/**
* Main function: Upload a publicly downloadable file to temporary storage.
*/
function main() {
const preSignedUrl = "Replace this with the value of the Data.Param.Url field returned by the ApplyFileUploadLease operation in the previous step.";
const headers = {
"X-bailian-extra": "Replace this with the value of the Data.Param.Headers.X-bailian-extra field returned by the ApplyFileUploadLease operation in the previous step.",
"Content-Type": "Replace this with the value of the Data.Param.Headers.Content-Type field returned by the ApplyFileUploadLease operation in the previous step. If an empty value is returned, pass an empty value."
};
const sourceUrl = "Replace this with the URL of the file to upload.";
uploadFileFromUrl(preSignedUrl, headers, sourceUrl)
.then(() => {
console.log("Upload completed.");
})
.catch((err) => {
console.error("Upload failed:", err.message);
});
}
// Call the main function.
main();
C#using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
public class UploadFileExample
{
public static async Task UploadFileFromUrl(string preSignedUrl, string url)
{
try
{
// Create an HTTP client to download the file from the given URL.
using (HttpClient httpClient = new HttpClient())
{
HttpResponseMessage response = await httpClient.GetAsync(url, HttpCompletionOption.ResponseHeadersRead);
response.EnsureSuccessStatusCode();
// Get the file stream.
using (Stream fileStream = await response.Content.ReadAsStreamAsync())
{
// Create a URL object.
Uri urlObj = new Uri(preSignedUrl);
HttpWebRequest connection = (HttpWebRequest)WebRequest.Create(urlObj);
// Set the request method for file upload.
connection.Method = "PUT";
connection.AllowWriteStreamBuffering = false;
connection.SendChunked = false;
// Set the request headers. Replace with actual values.
connection.Headers["X-bailian-extra"] = "Replace this with the value of the Data.Param.Headers.X-bailian-extra field returned by the ApplyFileUploadLease operation in the previous step.";
connection.ContentType = "Replace this with the value of the Data.Param.Headers.Content-Type field returned by the ApplyFileUploadLease operation in the previous step. If an empty value is returned, pass an empty value.";
// Get the request stream and write the file stream to it.
using (Stream requestStream = connection.GetRequestStream())
{
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = await fileStream.ReadAsync(buffer, 0, buffer.Length)) > 0)
{
await requestStream.WriteAsync(buffer, 0, bytesRead);
}
await requestStream.FlushAsync();
}
// Check the response.
using (HttpWebResponse responseResult = (HttpWebResponse)connection.GetResponse())
{
if (responseResult.StatusCode == HttpStatusCode.OK)
{
Console.WriteLine("File uploaded successfully from URL.");
}
else
{
Console.WriteLine($"Failed to upload the file. ResponseCode: {responseResult.StatusCode}");
}
}
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
}
}
public static async Task Main(string[] args)
{
string preSignedUrlOrHttpUrl = "Replace this with the value of the Data.Param.Url field returned by the ApplyFileUploadLease operation in the previous step.";
string url = "Replace this with the URL of the file to upload.";
await UploadFileFromUrl(preSignedUrlOrHttpUrl, url);
}
}
Gopackage main
import (
"fmt"
"net/http"
"github.com/go-resty/resty/v2"
)
// UploadFileFromUrl uploads a file from a public URL to temporary storage.
//
// Args:
// - preSignedUrl (string): The URL from the upload lease.
// - headers (map[string]string): The headers for the upload request.
// - sourceUrl (string): The URL of the file.
//
// Returns:
// - error: An error message if the upload fails, otherwise nil.
func UploadFileFromUrl(preSignedUrl string, headers map[string]string, sourceUrl string) error {
// Download the file from the given URL.
resp, err := http.Get(sourceUrl)
if err != nil {
return fmt.Errorf("failed to get file: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("failed to get file, status code: %d", resp.StatusCode)
}
// Create a REST client.
client := resty.New()
// Build the request headers required for the upload.
uploadHeaders := map[string]string{
"X-bailian-extra": headers["X-bailian-extra"],
"Content-Type": headers["Content-Type"],
}
// Send a PUT request.
response, err := client.R().
SetHeaders(uploadHeaders).
SetBody(resp.Body).
Put(preSignedUrl)
if err != nil {
return fmt.Errorf("failed to send request: %w", err)
}
if err != nil {
return fmt.Errorf("failed to send request: %w", err)
}
// Check the HTTP response status code.
if response.IsError() {
return fmt.Errorf("HTTP error: %d", response.StatusCode())
}
fmt.Println("File uploaded successfully from URL.")
return nil
}
// main function
func main() {
// Replace this with the value of the Data.Param.Url field returned by the ApplyFileUploadLease operation in the previous step.
preSignedUrl := "Replace this with the value of the Data.Param.Url field returned by the ApplyFileUploadLease operation in the previous step."
// Replace these with the values of X-bailian-extra and Content-Type in Data.Param.Headers returned by the ApplyFileUploadLease operation in the previous step.
headers := map[string]string{
"X-bailian-extra": "Replace this with the value of the Data.Param.Headers.X-bailian-extra field returned by the ApplyFileUploadLease operation in the previous step.",
"Content-Type": "Replace this with the value of the Data.Param.Headers.Content-Type field returned by the ApplyFileUploadLease operation in the previous step. If an empty value is returned, pass an empty value.",
}
sourceUrl := "Replace this with the URL of the file to upload."
// Call the upload function.
err := UploadFileFromUrl(preSignedUrl, headers, sourceUrl)
if err != nil {
fmt.Printf("Upload failed: %v\n", err)
}
}
|
2.3. Add the file to a categoryAlibaba Cloud Model Studio uses categories to manage your uploaded files. Therefore, you must call the AddFile operation to add the uploaded file to a category in the same workspace. parser: Enter DASHSCOPE_DOCMIND. lease_id: Enter the Data.FileUploadLeaseId returned by the request for the file upload lease. category_id: In this example, enter default. If you uploaded the file to a custom category, enter the corresponding category_id.
After the file is added, Model Studio returns the FileId for the file and automatically starts to parse it. The lease_id becomes invalid. Do not use the same lease ID to submit the request again. |
Important A RAM user must be granted the required API permissions (the AliyunBailianDataFullAccess policy) before calling this operation. This operation supports online debugging and the generation of sample code for multiple languages.
Pythondef add_file(client: bailian20231229Client, lease_id: str, parser: str, category_id: str, workspace_id: str):
"""
Add a file to a specified category in Alibaba Cloud Model Studio.
Args:
client (bailian20231229Client): The client.
lease_id (str): The lease ID.
parser (str): The parser for the file.
category_id (str): The category ID.
workspace_id (str): The workspace ID.
Returns:
The response from Alibaba Cloud Model Studio.
"""
headers = {}
request = bailian_20231229_models.AddFileRequest(
lease_id=lease_id,
parser=parser,
category_id=category_id,
)
runtime = util_models.RuntimeOptions()
return client.add_file_with_options(workspace_id, request, headers, runtime)
Java/**
* Add a file to a category.
*
* @param client The client object.
* @param leaseId The lease ID.
* @param parser The parser for the file.
* @param categoryId The category ID.
* @param workspaceId The workspace ID.
* @return The response object from Alibaba Cloud Model Studio.
*/
public AddFileResponse addFile(com.aliyun.bailian20231229.Client client, String leaseId, String parser, String categoryId, String workspaceId) throws Exception {
Map<String, String> headers = new HashMap<>();
com.aliyun.bailian20231229.models.AddFileRequest addFileRequest = new com.aliyun.bailian20231229.models.AddFileRequest();
addFileRequest.setLeaseId(leaseId);
addFileRequest.setParser(parser);
addFileRequest.setCategoryId(categoryId);
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
return client.addFileWithOptions(workspaceId, addFileRequest, headers, runtime);
}
PHP/**
* Add a file to a category.
*
* @param Bailian $client The client.
* @param string $leaseId The lease ID.
* @param string $parser The parser for the file.
* @param string $categoryId The category ID.
* @param string $workspaceId The workspace ID.
* @return AddFileResponse The response from Alibaba Cloud Model Studio.
*/
public function addFile($client, $leaseId, $parser, $categoryId, $workspaceId) {
$headers = [];
$addFileRequest = new AddFileRequest([
"leaseId" => $leaseId,
"parser" => $parser,
"categoryId" => $categoryId
]);
$runtime = new RuntimeOptions([]);
return $client->addFileWithOptions($workspaceId, $addFileRequest, $headers, $runtime);
}
Node.js/**
* Add a file to a category.
* @param {Bailian20231229Client} client - The client.
* @param {string} leaseId - The lease ID.
* @param {string} parser - The parser for the file.
* @param {string} categoryId - The category ID.
* @param {string} workspaceId - The workspace ID.
* @returns {Promise<bailian20231229.AddFileResponse>} - The response from Alibaba Cloud Model Studio.
*/
async function addFile(client, leaseId, parser, categoryId, workspaceId) {
const headers = {};
const req = new bailian20231229.AddFileRequest({
leaseId,
parser,
categoryId
});
const runtime = new Util.RuntimeOptions({});
return await client.addFileWithOptions(workspaceId, req, headers, runtime);
}
C#/// <summary>
/// Add a file to a category.
/// </summary>
/// <param name="client">The client object.</param>
/// <param name="leaseId">The lease ID.</param>
/// <param name="parser">The parser for the file.</param>
/// <param name="categoryId">The category ID.</param>
/// <param name="workspaceId">The workspace ID.</param>
/// <returns>The response object from Alibaba Cloud Model Studio.</returns>
/// <exception cref="Exception">An exception is thrown if an error occurs during the call.</exception>
public AlibabaCloud.SDK.Bailian20231229.Models.AddFileResponse AddFile(
AlibabaCloud.SDK.Bailian20231229.Client client,
string leaseId,
string parser,
string categoryId,
string workspaceId)
{
var headers = new Dictionary<string, string>() { };
var addFileRequest = new AlibabaCloud.SDK.Bailian20231229.Models.AddFileRequest
{
LeaseId = leaseId,
Parser = parser,
CategoryId = categoryId
};
var runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
return client.AddFileWithOptions(workspaceId, addFileRequest, headers, runtime);
}
Go// AddFile adds a file to a specified category in Alibaba Cloud Model Studio.
//
// Args:
// - client (bailian20231229.Client): The client.
// - leaseId (string): The lease ID.
// - parser (string): The parser for the file.
// - categoryId (string): The category ID.
// - workspaceId (string): The workspace ID.
//
// Returns:
// - *bailian20231229.AddFileResponse: The response from Alibaba Cloud Model Studio.
// - error: An error message.
func AddFile(client *bailian20231229.Client, leaseId, parser, categoryId, workspaceId string) (_result *bailian20231229.AddFileResponse, _err error) {
headers := make(map[string]*string)
addFileRequest := &bailian20231229.AddFileRequest{
LeaseId: tea.String(leaseId),
Parser: tea.String(parser),
CategoryId: tea.String(categoryId),
}
runtime := &util.RuntimeOptions{}
return client.AddFileWithOptions(tea.String(workspaceId), addFileRequest, headers, runtime)
}
Request example {
"CategoryId": "default",
"LeaseId": "d92bd94fa9b54326a2547415e100c9e2.1742195250069",
"Parser": "DASHSCOPE_DOCMIND",
"WorkspaceId": "llm-4u5xpd1xdjqpxxxx"
}
Response example {
"Status": "200",
"Message": "",
"RequestId": "5832A1F4-AF91-5242-8B75-35BDC9XXXXXX",
"Data": {
"FileId": "file_0b21e0a852cd40cd9741c54fefbb61cd_10xxxxxx",
"Parser": "DASHSCOPE_DOCMIND"
},
"Code": "Success",
"Success": "true"
}
|
2.4. Query the file parsing statusFiles that are not parsed cannot be used in a knowledge base. During peak hours, this process can take several hours. You can call the DescribeFile operation to query the parsing status of a file. When the Data.Status field in the response is PARSE_SUCCESS, the file is parsed. You can then import it into a knowledge base. |
Important A RAM user must be granted the required API permissions (the AliyunBailianDataFullAccess or AliyunBailianDataReadOnlyAccess policy) before calling this operation. This operation supports online debugging and the generation of sample code for multiple languages.
Pythondef describe_file(client, workspace_id, file_id):
"""
Get the basic information about a file.
Args:
client (bailian20231229Client): The client.
workspace_id (str): The workspace ID.
file_id (str): The file ID.
Returns:
The response from Alibaba Cloud Model Studio.
"""
headers = {}
runtime = util_models.RuntimeOptions()
return client.describe_file_with_options(workspace_id, file_id, headers, runtime)
Java/**
* Query the basic information about a file.
*
* @param client The client object.
* @param workspaceId The workspace ID.
* @param fileId The file ID.
* @return The response object from Alibaba Cloud Model Studio.
*/
public DescribeFileResponse describeFile(com.aliyun.bailian20231229.Client client, String workspaceId, String fileId) throws Exception {
Map<String, String> headers = new HashMap<>();
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
return client.describeFileWithOptions(workspaceId, fileId, headers, runtime);
}
PHP/**
* Query the basic information about a file.
*
* @param Bailian $client The client.
* @param string $workspaceId The workspace ID.
* @param string $fileId The file ID.
* @return DescribeFileResponse The response from Alibaba Cloud Model Studio.
*/
public function describeFile($client, $workspaceId, $fileId) {
$headers = [];
$runtime = new RuntimeOptions([]);
return $client->describeFileWithOptions($workspaceId, $fileId, $headers, $runtime);
}
Node.js/**
* Query the parsing status of a file.
* @param {Bailian20231229Client} client - The client.
* @param {string} workspaceId - The workspace ID.
* @param {string} fileId - The file ID.
* @returns {Promise<bailian20231229.DescribeFileResponse>} - The response from Alibaba Cloud Model Studio.
*/
async function describeFile(client, workspaceId, fileId) {
const headers = {};
const runtime = new Util.RuntimeOptions({});
return await client.describeFileWithOptions(workspaceId, fileId, headers, runtime);
}
C#/// <summary>
/// Query the basic information about a file.
/// </summary>
/// <param name="client">The client object.</param>
/// <param name="workspaceId">The workspace ID.</param>
/// <param name="fileId">The file ID.</param>
/// <returns>The response object from Alibaba Cloud Model Studio.</returns>
/// <exception cref="Exception">An exception is thrown if an error occurs during the call.</exception>
public AlibabaCloud.SDK.Bailian20231229.Models.DescribeFileResponse DescribeFile(
AlibabaCloud.SDK.Bailian20231229.Client client,
string workspaceId,
string fileId)
{
var headers = new Dictionary<string, string>() { };
var runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
return client.DescribeFileWithOptions(workspaceId, fileId, headers, runtime);
}
Go// DescribeFile gets the basic information about a file.
//
// Args:
// - client (bailian20231229.Client): The client.
// - workspaceId (string): The workspace ID.
// - fileId (string): The file ID.
//
// Returns:
// - any: The response from Alibaba Cloud Model Studio.
// - error: An error message.
func DescribeFile(client *bailian20231229.Client, workspaceId, fileId string) (_result *bailian20231229.DescribeFileResponse, _err error) {
headers := make(map[string]*string)
runtime := &util.RuntimeOptions{}
return client.DescribeFileWithOptions(tea.String(workspaceId), tea.String(fileId), headers, runtime)
}
Request example {
"FileId": "file_0b21e0a852cd40cd9741c54fefbb61cd_10xxxxxx",
"WorkspaceId": "llm-4u5xpd1xdjqpxxxx"
}
Response example {
"Status": "200",
"Message": "",
"RequestId": "B9246251-987A-5628-8E1E-17BB39XXXXXX",
"Data": {
"CategoryId": "cate_206ea350f0014ea4a324adff1ca13011_10xxxxxx",
"Status": "PARSE_SUCCESS",
"FileType": "docx",
"CreateTime": "2025-03-17 15:47:13",
"FileName": "Bailian_Phones_Specifications.docx",
"FileId": "file_0b21e0a852cd40cd9741c54fefbb61cd_10xxxxxx",
"SizeInBytes": "14015",
"Parser": "DASHSCOPE_DOCMIND"
},
"Code": "Success",
"Success": "true"
}
|
3. Create the knowledge base |
3.1. Initialize the knowledge baseAfter the file is parsed, you can import it into a knowledge base in the same workspace. To initialize a document search knowledge base, call the CreateIndex operation. This does not finalize the submission. workspace_id: For more information, see How to obtain a workspace ID. file_id: Enter the FileId returned when you added the file to a category. If `source_type` is set to DATA_CENTER_FILE, this parameter is required. Otherwise, an error is reported. structure_type: In this example, enter unstructured. You cannot create data query or image Q&A pair knowledge bases using an API. You must use the Model Studio console to create them. source_type: In this example, enter DATA_CENTER_FILE. sink_type: In this example, enter BUILT_IN.
The value of the Data.Id field in the response is the knowledge base ID. This ID is used for subsequent index building. Keep the knowledge base ID secure. You will need it for all subsequent API operations that are related to this knowledge base. |
Important A RAM user must be granted the required API permissions (the AliyunBailianDataFullAccess policy) before calling this operation. This operation supports online debugging and the generation of sample code for multiple languages.
Pythondef create_index(client, workspace_id, file_id, name, structure_type, source_type, sink_type):
"""
Create (initialize) a knowledge base in Alibaba Cloud Model Studio.
Args:
client (bailian20231229Client): The client.
workspace_id (str): The workspace ID.
file_id (str): The file ID.
name (str): The name of the knowledge base.
structure_type (str): The data type of the knowledge base.
source_type (str): The data type of the application data. Category and file types are supported.
sink_type (str): The vector storage type of the knowledge base.
Returns:
The response from Alibaba Cloud Model Studio.
"""
headers = {}
request = bailian_20231229_models.CreateIndexRequest(
structure_type=structure_type,
name=name,
source_type=source_type,
sink_type=sink_type,
document_ids=[file_id]
)
runtime = util_models.RuntimeOptions()
return client.create_index_with_options(workspace_id, request, headers, runtime)
Java/**
* Create (initialize) a knowledge base in Alibaba Cloud Model Studio.
*
* @param client The client object.
* @param workspaceId The workspace ID.
* @param fileId The file ID.
* @param name The name of the knowledge base.
* @param structureType The data type of the knowledge base.
* @param sourceType The data type of the application data. Category and file types are supported.
* @param sinkType The vector storage type of the knowledge base.
* @return The response object from Alibaba Cloud Model Studio.
*/
public CreateIndexResponse createIndex(com.aliyun.bailian20231229.Client client, String workspaceId, String fileId, String name, String structureType, String sourceType, String sinkType) throws Exception {
Map<String, String> headers = new HashMap<>();
com.aliyun.bailian20231229.models.CreateIndexRequest createIndexRequest = new com.aliyun.bailian20231229.models.CreateIndexRequest();
createIndexRequest.setStructureType(structureType);
createIndexRequest.setName(name);
createIndexRequest.setSourceType(sourceType);
createIndexRequest.setSinkType(sinkType);
createIndexRequest.setDocumentIds(Collections.singletonList(fileId));
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
return client.createIndexWithOptions(workspaceId, createIndexRequest, headers, runtime);
}
PHP/**
* Create (initialize) a knowledge base in Alibaba Cloud Model Studio.
*
* @param Bailian $client The client.
* @param string $workspaceId The workspace ID.
* @param string $fileId The file ID.
* @param string $name The name of the knowledge base.
* @param string $structureType The data type of the knowledge base.
* @param string $sourceType The data type of the application data. Category and file types are supported.
* @param string $sinkType The vector storage type of the knowledge base.
* @return CreateIndexResponse The response from Alibaba Cloud Model Studio.
*/
public function createIndex($client, $workspaceId, $fileId, $name, $structureType, $sourceType, $sinkType) {
$headers = [];
$createIndexRequest = new CreateIndexRequest([
"structureType" => $structureType,
"name" => $name,
"sourceType" => $sourceType,
"documentIds" => [
$fileId
],
"sinkType" => $sinkType
]);
$runtime = new RuntimeOptions([]);
return $client->createIndexWithOptions($workspaceId, $createIndexRequest, $headers, $runtime);
}
Node.js/**
* Initialize a knowledge base (index).
* @param {Bailian20231229Client} client - The client.
* @param {string} workspaceId - The workspace ID.
* @param {string} fileId - The file ID.
* @param {string} name - The name of the knowledge base.
* @param {string} structureType - The data type of the knowledge base.
* @param {string} sourceType - The data type of the application data. Category and file types are supported.
* @param {string} sinkType - The vector storage type of the knowledge base.
* @returns {Promise<bailian20231229.CreateIndexResponse>} - The response from Alibaba Cloud Model Studio.
*/
async function createIndex(client, workspaceId, fileId, name, structureType, sourceType, sinkType) {
const headers = {};
const req = new bailian20231229.CreateIndexRequest({
name,
structureType,
documentIds: [fileId],
sourceType,
sinkType
});
const runtime = new Util.RuntimeOptions({});
return await client.createIndexWithOptions(workspaceId, req, headers, runtime);
}
C#/// <summary>
/// Create (initialize) a knowledge base in Alibaba Cloud Model Studio.
/// </summary>
/// <param name="client">The client object.</param>
/// <param name="workspaceId">The workspace ID.</param>
/// <param name="fileId">The file ID.</param>
/// <param name="name">The name of the knowledge base.</param>
/// <param name="structureType">The data type of the knowledge base.</param>
/// <param name="sourceType">The data type of the application data. Category and file types are supported.</param>
/// <param name="sinkType">The vector storage type of the knowledge base.</param>
/// <returns>The response object from Alibaba Cloud Model Studio.</returns>
/// <exception cref="Exception">An exception is thrown if an error occurs during the call.</exception>
public AlibabaCloud.SDK.Bailian20231229.Models.CreateIndexResponse CreateIndex(
AlibabaCloud.SDK.Bailian20231229.Client client,
string workspaceId,
string fileId,
string name,
string structureType,
string sourceType,
string sinkType)
{
var headers = new Dictionary<string, string>() { };
var createIndexRequest = new AlibabaCloud.SDK.Bailian20231229.Models.CreateIndexRequest
{
StructureType = structureType,
Name = name,
SourceType = sourceType,
SinkType = sinkType,
DocumentIds = new List<string> { fileId }
};
var runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
return client.CreateIndexWithOptions(workspaceId, createIndexRequest, headers, runtime);
}
Go// CreateIndex creates (initializes) a knowledge base in Alibaba Cloud Model Studio.
//
// Args:
// - client (bailian20231229.Client): The client.
// - workspaceId (string): The workspace ID.
// - fileId (string): The file ID.
// - name (string): The name of the knowledge base.
// - structureType (string): The data type of the knowledge base.
// - sourceType (string): The data type of the application data. Category and file types are supported.
// - sinkType (string): The vector storage type of the knowledge base.
//
// Returns:
// - *bailian20231229.CreateIndexResponse: The response from Alibaba Cloud Model Studio.
// - error: An error message.
func CreateIndex(client *bailian20231229.Client, workspaceId, fileId, name, structureType, sourceType, sinkType string) (_result *bailian20231229.CreateIndexResponse, _err error) {
headers := make(map[string]*string)
createIndexRequest := &bailian20231229.CreateIndexRequest{
StructureType: tea.String(structureType),
Name: tea.String(name),
SourceType: tea.String(sourceType),
SinkType: tea.String(sinkType),
DocumentIds: []*string{tea.String(fileId)},
}
runtime := &util.RuntimeOptions{}
return client.CreateIndexWithOptions(tea.String(workspaceId), createIndexRequest, headers, runtime)
}
Request example {
"Name": "Model Studio Phone Knowledge Base",
"SinkType": "BUILT_IN",
"SourceType": "DATA_CENTER_FILE",
"StructureType": "unstructured",
"WorkspaceId": "llm-4u5xpd1xdjqpxxxx",
"DocumentIds": [
"file_0b21e0a852cd40cd9741c54fefbb61cd_10xxxxxx"
]
}
Response example {
"Status": "200",
"Message": "success",
"RequestId": "87CB0999-F1BB-5290-8C79-A875B2XXXXXX",
"Data": {
"Id": "mymxbdxxxx"
},
"Code": "Success",
"Success": "true"
}
|
3.2. Submit an index jobAfter you initialize the knowledge base, you must call the SubmitIndexJob operation to submit an index job. This starts the index building for the knowledge base. After you submit the job, Alibaba Cloud Model Studio starts to build the index as an asynchronous task. The Data.Id returned by this operation is the corresponding job ID. In the next step, you will use this ID to query the latest status of the job. |
Important A RAM user must be granted the required API permissions (the AliyunBailianDataFullAccess policy) before calling this operation. This operation supports online debugging and the generation of sample code for multiple languages.
Pythondef submit_index(client, workspace_id, index_id):
"""
Submit an index job to Alibaba Cloud Model Studio.
Args:
client (bailian20231229Client): The client.
workspace_id (str): The workspace ID.
index_id (str): The knowledge base ID.
Returns:
The response from Alibaba Cloud Model Studio.
"""
headers = {}
submit_index_job_request = bailian_20231229_models.SubmitIndexJobRequest(
index_id=index_id
)
runtime = util_models.RuntimeOptions()
return client.submit_index_job_with_options(workspace_id, submit_index_job_request, headers, runtime)
Java/**
* Submit an index job to Alibaba Cloud Model Studio.
*
* @param client The client object.
* @param workspaceId The workspace ID.
* @param indexId The knowledge base ID.
* @return The response object from Alibaba Cloud Model Studio.
*/
public SubmitIndexJobResponse submitIndex(com.aliyun.bailian20231229.Client client, String workspaceId, String indexId) throws Exception {
Map<String, String> headers = new HashMap<>();
com.aliyun.bailian20231229.models.SubmitIndexJobRequest submitIndexJobRequest = new com.aliyun.bailian20231229.models.SubmitIndexJobRequest();
submitIndexJobRequest.setIndexId(indexId);
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
return client.submitIndexJobWithOptions(workspaceId, submitIndexJobRequest, headers, runtime);
}
PHP/**
* Submit an index job to Alibaba Cloud Model Studio.
*
* @param Bailian $client The client.
* @param string $workspaceId The workspace ID.
* @param string $indexId The knowledge base ID.
* @return SubmitIndexJobResponse The response from Alibaba Cloud Model Studio.
*/
public static function submitIndex($client, $workspaceId, $indexId) {
$headers = [];
$submitIndexJobRequest = new SubmitIndexJobRequest([
'indexId' => $indexId
]);
$runtime = new RuntimeOptions([]);
return $client->submitIndexJobWithOptions($workspaceId, $submitIndexJobRequest, $headers, $runtime);
}
Node.js/**
* Submit an index job.
* @param {Bailian20231229Client} client - The client.
* @param {string} workspaceId - The workspace ID.
* @param {string} indexId - The knowledge base ID.
* @returns {Promise<bailian20231229.SubmitIndexJobResponse>} - The response from Alibaba Cloud Model Studio.
*/
async function submitIndex(client, workspaceId, indexId) {
const headers = {};
const req = new bailian20231229.SubmitIndexJobRequest({ indexId });
const runtime = new Util.RuntimeOptions({});
return await client.submitIndexJobWithOptions(workspaceId, req, headers, runtime);
}
C#/// <summary>
/// Submit an index job to Alibaba Cloud Model Studio.
/// </summary>
/// <param name="client">The client object.</param>
/// <param name="workspaceId">The workspace ID.</param>
/// <param name="indexId">The knowledge base ID.</param>
/// <returns>The response object from Alibaba Cloud Model Studio.</returns>
/// <exception cref="Exception">An exception is thrown if an error occurs during the call.</exception>
public AlibabaCloud.SDK.Bailian20231229.Models.SubmitIndexJobResponse SubmitIndex(
AlibabaCloud.SDK.Bailian20231229.Client client,
string workspaceId,
string indexId)
{
var headers = new Dictionary<string, string>() { };
var submitIndexJobRequest = new AlibabaCloud.SDK.Bailian20231229.Models.SubmitIndexJobRequest
{
IndexId = indexId
};
var runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
return client.SubmitIndexJobWithOptions(workspaceId, submitIndexJobRequest, headers, runtime);
}
Go// SubmitIndex submits an index job.
//
// Args:
// - client (bailian20231229.Client): The client.
// - workspaceId (string): The workspace ID.
// - indexId (string): The knowledge base ID.
//
// Returns:
// - *bailian20231229.SubmitIndexJobResponse: The response from Alibaba Cloud Model Studio.
// - error: An error message.
func SubmitIndex(client *bailian20231229.Client, workspaceId, indexId string) (_result *bailian20231229.SubmitIndexJobResponse, _err error) {
headers := make(map[string]*string)
submitIndexJobRequest := &bailian20231229.SubmitIndexJobRequest{
IndexId: tea.String(indexId),
}
runtime := &util.RuntimeOptions{}
return client.SubmitIndexJobWithOptions(tea.String(workspaceId), submitIndexJobRequest, headers, runtime)
}
Request example {
"IndexId": "mymxbdxxxx",
"WorkspaceId": "llm-4u5xpd1xdjqpxxxx"
}
Response example {
"Status": "200",
"Message": "success",
"RequestId": "7774575F-571D-5854-82C2-634AB8XXXXXX",
"Data": {
"IndexId": "mymxbdxxxx",
"Id": "3cd6fb57aaf44cd0b4dd2ca584xxxxxx"
},
"Code": "Success",
"Success": "true"
}
|
3.3. Wait for the index job to completeThe index job takes some time to complete. During peak hours, this process can take several hours. To query its execution status, you can call the GetIndexJobStatus operation. When the Data.Status field in the response is COMPLETED, the knowledge base is created. |
Important A RAM user must be granted the required API permissions (the AliyunBailianDataFullAccess policy) before calling this operation. This operation supports online debugging and the generation of sample code for multiple languages.
Pythondef get_index_job_status(client, workspace_id, index_id, job_id):
"""
Query the status of an index job.
Args:
client (bailian20231229Client): The client.
workspace_id (str): The workspace ID.
index_id (str): The knowledge base ID.
job_id (str): The job ID.
Returns:
The response from Alibaba Cloud Model Studio.
"""
headers = {}
get_index_job_status_request = bailian_20231229_models.GetIndexJobStatusRequest(
index_id=index_id,
job_id=job_id
)
runtime = util_models.RuntimeOptions()
return client.get_index_job_status_with_options(workspace_id, get_index_job_status_request, headers, runtime)
Java/**
* Query the status of an index job.
*
* @param client The client object.
* @param workspaceId The workspace ID.
* @param jobId The job ID.
* @param indexId The knowledge base ID.
* @return The response object from Alibaba Cloud Model Studio.
*/
public GetIndexJobStatusResponse getIndexJobStatus(com.aliyun.bailian20231229.Client client, String workspaceId, String jobId, String indexId) throws Exception {
Map<String, String> headers = new HashMap<>();
com.aliyun.bailian20231229.models.GetIndexJobStatusRequest getIndexJobStatusRequest = new com.aliyun.bailian20231229.models.GetIndexJobStatusRequest();
getIndexJobStatusRequest.setIndexId(indexId);
getIndexJobStatusRequest.setJobId(jobId);
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
GetIndexJobStatusResponse getIndexJobStatusResponse = null;
getIndexJobStatusResponse = client.getIndexJobStatusWithOptions(workspaceId, getIndexJobStatusRequest, headers, runtime);
return getIndexJobStatusResponse;
}
PHP/**
* Query the status of an index job.
*
* @param Bailian $client The client.
* @param string $workspaceId The workspace ID.
* @param string $indexId The knowledge base ID.
* @param string $jobId The job ID.
* @return GetIndexJobStatusResponse The response from Alibaba Cloud Model Studio.
*/
public function getIndexJobStatus($client, $workspaceId, $jobId, $indexId) {
$headers = [];
$getIndexJobStatusRequest = new GetIndexJobStatusRequest([
'indexId' => $indexId,
'jobId' => $jobId
]);
$runtime = new RuntimeOptions([]);
return $client->getIndexJobStatusWithOptions($workspaceId, $getIndexJobStatusRequest, $headers, $runtime);
}
Node.js/**
* Query the status of an index job.
* @param {Bailian20231229Client} client - The client.
* @param {string} workspaceId - The workspace ID.
* @param {string} jobId - The job ID.
* @param {string} indexId - The knowledge base ID.
* @returns {Promise<bailian20231229.GetIndexJobStatusResponse>} - The response from Alibaba Cloud Model Studio.
*/
async function getIndexJobStatus(client, workspaceId, jobId, indexId) {
const headers = {};
const req = new bailian20231229.GetIndexJobStatusRequest({ jobId, indexId });
const runtime = new Util.RuntimeOptions({});
return await client.getIndexJobStatusWithOptions(workspaceId, req, headers, runtime);
}
C#/// <summary>
/// Query the status of an index job.
/// </summary>
/// <param name="client">The client object.</param>
/// <param name="workspaceId">The workspace ID.</param>
/// <param name="jobId">The job ID.</param>
/// <param name="indexId">The knowledge base ID.</param>
/// <returns>The response object from Alibaba Cloud Model Studio.</returns>
/// <exception cref="Exception">An exception is thrown if an error occurs during the call.</exception>
public AlibabaCloud.SDK.Bailian20231229.Models.GetIndexJobStatusResponse GetIndexJobStatus(
AlibabaCloud.SDK.Bailian20231229.Client client,
string workspaceId,
string jobId,
string indexId)
{
var headers = new Dictionary<string, string>() { };
var getIndexJobStatusRequest = new AlibabaCloud.SDK.Bailian20231229.Models.GetIndexJobStatusRequest
{
IndexId = indexId,
JobId = jobId
};
var runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
return client.GetIndexJobStatusWithOptions(workspaceId, getIndexJobStatusRequest, headers, runtime);
}
Go// GetIndexJobStatus queries the status of an index job.
//
// Args:
// - client (bailian20231229.Client): The client.
// - workspaceId (string): The workspace ID.
// - jobId (string): The job ID.
// - indexId (string): The knowledge base ID.
//
// Returns:
// - *bailian20231229.GetIndexJobStatusResponse: The response from Alibaba Cloud Model Studio.
// - error: An error message.
func GetIndexJobStatus(client *bailian20231229.Client, workspaceId, jobId, indexId string) (_result *bailian20231229.GetIndexJobStatusResponse, _err error) {
headers := make(map[string]*string)
getIndexJobStatusRequest := &bailian20231229.GetIndexJobStatusRequest{
JobId: tea.String(jobId),
IndexId: tea.String(indexId),
}
runtime := &util.RuntimeOptions{}
return client.GetIndexJobStatusWithOptions(tea.String(workspaceId), getIndexJobStatusRequest, headers, runtime)
}
Request example {
"IndexId": "mymxbdxxxx",
"JobId": "3cd6fb57aaf44cd0b4dd2ca584xxxxxx",
"WorkspaceId": "llm-4u5xpd1xdjqpxxxx"
}
Response example {
"Status": "200",
"Message": "success",
"RequestId": "E83423B9-7D6D-5283-836B-CF7EAEXXXXXX",
"Data": {
"Status": "COMPLETED",
"Documents": [
{
"Status": "FINISH",
"DocId": "file_0b21e0a852cd40cd9741c54fefbb61cd_10xxxxxx",
"Message": "Import successful",
"DocName": "Bailian Phones Specifications",
"Code": "FINISH"
}
],
"JobId": "3cd6fb57aaf44cd0b4dd2ca584xxxxxx"
},
"Code": "Success",
"Success": "true"
}
|