1. Initialize clientTo upload files and create a knowledge base, first initialize a client. Use your AccessKey and AccessKey Secret to verify your identity and configure the endpoint. Public endpoints Your client must have internet access. VPC endpoints If your client is deployed on the public cloud in the Alibaba Cloud China (Beijing) region (cn-beijing) and is within a VPC, you can use the following VPC endpoint. Cross-region access is not supported.
Creating the client returns 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 an example of a public endpoint for the public cloud. You can change the endpoint as needed.
config.endpoint = 'bailian.cn-beijing.aliyuncs.com'
return bailian20231229Client(config)
Java/**
* Initialize a client.
*
* @return The configured client object.
*/
public 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 an example of a public endpoint for the public cloud. You can change the endpoint as needed.
config.endpoint = "bailian.cn-beijing.aliyuncs.com";
return new com.aliyun.bailian20231229.Client(config);
}
PHP/**
* Initialize a client.
*
* @return Bailian The configured client object.
*/
public function createClient(){
$config = new Config([
"accessKeyId" => getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
"accessKeySecret" => getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
]);
// The following endpoint is an example of a public endpoint for the public cloud. You can change the endpoint as needed.
$config->endpoint = 'bailian.cn-beijing.aliyuncs.com';
return new Bailian($config);
}
Node.js/**
* Create and configure a client.
* @return Client
* @throws Exception
*/
function 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 an example of a public endpoint for the public cloud. You can change the endpoint as needed.
config.endpoint = `bailian.cn-beijing.aliyuncs.com`;
return new bailian20231229.default(config);
}
C#/// <summary>
/// Initialize a client.
/// </summary>
/// <returns>The configured client object.</returns>
/// <exception cref="Exception">Thrown when an error occurs during initialization.</exception>
public 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 an example of a public endpoint for the public cloud. You can change the endpoint as needed.
config.Endpoint = "bailian.cn-beijing.aliyuncs.com";
return new AlibabaCloud.SDK.Bailian20231229.Client(config);
}
Go// CreateClient creates and configures a client.
//
// Returns:
// - *client.Bailian20231229Client: The configured client.
// - error: The 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 an example of a public endpoint for the public cloud. You can change the endpoint as needed.
config.Endpoint = tea.String("bailian.cn-beijing.aliyuncs.com")
_result = &bailian20231229.Client{}
_result, _err = bailian20231229.NewClient(config)
return _result, _err
}
|
2. Upload knowledge base files |
2.1. Request a file upload leaseBefore creating a knowledge base, upload its source files to the same workspace. To do this, call the ApplyFileUploadLease operation to request a file upload lease. A lease is a temporary authorization to upload a file and is valid for several minutes. workspace_id: See How to obtain a workspace ID. category_id: In this example, pass default. Model Studio uses categories to manage your uploaded files. The system automatically creates a default category. You can also call the AddCategory API to create a new category and obtain the corresponding category_id. file_name: Pass the name of the uploaded file, including its extension. The value must match the actual filename. For example, when you upload the file shown in the figure, pass Alibaba_Cloud_Model_Studio_Mobile_Phone_Series_Introduction.docx. 
file_md5: Pass the MD5 hash of the file to upload. Alibaba Cloud does not currently verify this value, which facilitates uploading files from a URL. In Python, you can get the MD5 hash by 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, /path/to/your/Alibaba Cloud Model Studio Product Overview.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 to get the MD5 hash of the target file. The following is an example value: The MD5 hash of the file is: 2ef7361ea907f3a1b91e3b9936f5643a
file_size: Pass the size of the file to upload in bytes. In Python, you can get this value by 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 a 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, /path/to/your/Alibaba Cloud Model Studio Product Overview.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 with the actual local path of the file and run the code to get the size of the target file in bytes. The following is an example value: The size of the file in bytes is: 14015
A successful request for a temporary upload lease returns the following: | 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.
//
// Parameters:
// - 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: The 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": "Alibaba Cloud Model Studio Product Overview.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 file to temporary storageWith the upload lease, use the temporary upload parameters and URL to upload files from your local storage or a publicly accessible URL to the Model Studio server. Each workspace supports up to 100,000 files. The supported formats include PDF, DOCX, DOC, TXT, Markdown, PPTX, PPT, XLSX, XLS, HTML, PNG, JPG, JPEG, BMP, and GIF. |
Important This example does not support online debugging or sample code generation. Local uploadPythonimport 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 X-bailian-extra field in Data.Param.Headers returned by the ApplyFileUploadLease operation in the previous step.",
"Content-Type": "Replace this with the value of the Content-Type field in Data.Param.Headers returned by the ApplyFileUploadLease operation in the previous step. If a null value is returned, pass a null value."
}
# Read and upload the file.
with open(file_path, 'rb') as file:
# The request method for the file upload must be the same as the value of the Method field in Data.Param returned by the ApplyFileUploadLease operation in the previous step.
response = requests.put(pre_signed_url, data=file, headers=headers)
# Check the response status code.
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 Url field in Data.Param 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: /path/to/your/Alibaba Cloud Model Studio Product Overview.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 the file upload must be the same as the value of the Method field in Data.Param 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 X-bailian-extra field in Data.Param.Headers returned by the ApplyFileUploadLease operation in the previous step.");
connection.setRequestProperty("Content-Type", "Replace this with the value of the Content-Type field in Data.Param.Headers returned by the ApplyFileUploadLease operation in the previous step. If a null value is returned, pass a null 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 Url field in Data.Param 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: /path/to/your/Alibaba Cloud Model Studio Product Overview.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 response 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 Url field in Data.Param returned by the ApplyFileUploadLease operation in the previous step.
$preSignedUrl = "Replace this with the value of the Url field in Data.Param 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 X-bailian-extra field in Data.Param.Headers returned by the ApplyFileUploadLease operation in the previous step.",
"Content-Type" => "Replace this with the value of the Content-Type field in Data.Param.Headers returned by the ApplyFileUploadLease operation in the previous step. If a null value is returned, pass a null 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: /path/to/your/Alibaba Cloud Model Studio Product Overview.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 response status code.
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 Url field in Data.Param returned by the ApplyFileUploadLease operation in the previous step.";
const headers = {
"X-bailian-extra": "Replace this with the value of the X-bailian-extra field in Data.Param.Headers returned by the ApplyFileUploadLease operation in the previous step.",
"Content-Type": "Replace this with the value of the Content-Type field in Data.Param.Headers returned by the ApplyFileUploadLease operation in the previous step. If a null value is returned, pass a null 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: /path/to/your/Alibaba Cloud Model Studio Product Overview.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 the file upload must be the same as the value of the Method field in Data.Param 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 X-bailian-extra field in Data.Param.Headers returned by the ApplyFileUploadLease operation in the previous step.";
connection.ContentType = "Replace this with the value of the Content-Type field in Data.Param.Headers returned by the ApplyFileUploadLease operation in the previous step. If a null value is returned, pass a null 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 Url field in Data.Param 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: /path/to/your/Alibaba Cloud Model Studio Product Overview.docx";
UploadFile(preSignedUrlOrHttpUrl, filePath);
}
}
Gopackage main
import (
"fmt"
"io"
"os"
"github.com/go-resty/resty/v2"
)
// UploadFile uploads a local file to temporary storage.
//
// Parameters:
// - 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 Url field in Data.Param returned by the ApplyFileUploadLease operation in the previous step.
preSignedUrl := "Replace this with the value of the Url field in Data.Param 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 X-bailian-extra field in Data.Param.Headers returned by the ApplyFileUploadLease operation in the previous step.",
"Content-Type": "Replace this with the value of the Content-Type field in Data.Param.Headers returned by the ApplyFileUploadLease operation in the previous step. If a null value is returned, pass a null 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: /path/to/your/Alibaba Cloud Model Studio Product Overview.docx"
// Call the upload function.
err := UploadFile(preSignedUrl, headers, filePath)
if err != nil {
fmt.Printf("Upload failed: %v\n", err)
}
}
URL uploadThe URL must be publicly accessible and point 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 X-bailian-extra field in Data.Param.Headers returned by the ApplyFileUploadLease operation in the previous step.",
"Content-Type": "Replace this with the value of the Content-Type field in Data.Param.Headers returned by the ApplyFileUploadLease operation in the previous step. If a null value is returned, pass a null 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 the file upload must be the same as the value of the Method field in Data.Param returned by the ApplyFileUploadLease operation in the previous step.
response = requests.put(pre_signed_url, data=source_response.content, headers=headers)
# Check the response status code.
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 Url field in Data.Param returned by the ApplyFileUploadLease operation in the previous step."
# 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 the file upload must be the same as the value of the Method field in Data.Param 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 X-bailian-extra field in Data.Param.Headers returned by the ApplyFileUploadLease operation in the previous step.");
connection.setRequestProperty("Content-Type", "Replace this with the value of the Content-Type field in Data.Param.Headers returned by the ApplyFileUploadLease operation in the previous step. If a null value is returned, pass a null 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 Url field in Data.Param 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 response 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 Url field in Data.Param returned by the ApplyFileUploadLease operation in the previous step.
$preSignedUrl = "Replace this with the value of the Url field in Data.Param 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 X-bailian-extra field in Data.Param.Headers returned by the ApplyFileUploadLease operation in the previous step.",
"Content-Type" => "Replace this with the value of the Content-Type field in Data.Param.Headers returned by the ApplyFileUploadLease operation in the previous step. If a null value is returned, pass a null 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 response status code.
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 Url field in Data.Param returned by the ApplyFileUploadLease operation in the previous step.";
const headers = {
"X-bailian-extra": "Replace this with the value of the X-bailian-extra field in Data.Param.Headers returned by the ApplyFileUploadLease operation in the previous step.",
"Content-Type": "Replace this with the value of the Content-Type field in Data.Param.Headers returned by the ApplyFileUploadLease operation in the previous step. If a null value is returned, pass a null 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 X-bailian-extra field in Data.Param.Headers returned by the ApplyFileUploadLease operation in the previous step.";
connection.ContentType = "Replace this with the value of the Content-Type field in Data.Param.Headers returned by the ApplyFileUploadLease operation in the previous step. If a null value is returned, pass a null 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 Url field in Data.Param 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.
//
// Parameters:
// - 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 Url field in Data.Param returned by the ApplyFileUploadLease operation in the previous step.
preSignedUrl := "Replace this with the value of the Url field in Data.Param 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 X-bailian-extra field in Data.Param.Headers returned by the ApplyFileUploadLease operation in the previous step.",
"Content-Type": "Replace this with the value of the Content-Type field in Data.Param.Headers returned by the ApplyFileUploadLease operation in the previous step. If a null value is returned, pass a null 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 file to a categoryAfter uploading the file, add it to a category in the same workspace by calling the AddFile operation. parser: Specify DASHSCOPE_DOCMIND. lease_id: Set this parameter to the Data.FileUploadLeaseId that is returned when you request a file upload lease. category_id: In this example, pass default. If you use a custom category for uploads, you must pass the corresponding category_id.
Important The CategoryId passed here must match the CategoryId used in the Apply for a file upload lease step. Otherwise, you will receive a Category is mismatched error.
After you add a file, Model Studio returns a FileId for the file and automatically starts parsing it. The lease_id is immediately invalidated. Do not reuse the same lease ID for another submission. | 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.
//
// Parameters:
// - 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: The 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 file parsing statusA file cannot be used in a knowledge base until it is parsed. During peak hours, this process can take several hours. You can call the DescribeFile operation to query its parsing status. If the Data.Status field is PARSE_SUCCESS, the file has been successfully parsed and you can import it into the knowledge base. |
Important Before calling this operation, a RAM user must be granted the required API permissions (the AliyunBailianDataFullAccess or AliyunBailianDataReadOnlyAccess policy). This operation supports online debugging and sample code generation for multiple languages.
Pythondef describe_file(client, workspace_id, file_id):
"""
Get the basic information of 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 of 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 of 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 of 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 of a file.
//
// Parameters:
// - client (bailian20231229.Client): The client.
// - workspaceId (string): The workspace ID.
// - fileId (string): The file ID.
//
// Returns:
// - *bailian20231229.DescribeFileResponse: The response from Alibaba Cloud Model Studio.
// - error: The 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": "Alibaba Cloud Model Studio Product Overview.docx",
"FileId": "file_0b21e0a852cd40cd9741c54fefbb61cd_10xxxxxx",
"SizeInBytes": "14015",
"Parser": "DASHSCOPE_DOCMIND"
},
"Code": "Success",
"Success": "true"
}
|
3. Create a knowledge base |
3.1. Initialize knowledge baseOnce a file is parsed, you can create a knowledge base from it in the same workspace. To begin, call the CreateIndex operation to initialize (but not finalize) a document retrieval knowledge base. workspace_id: See How to obtain a workspace ID. file_id: Specify the FileId returned by the API when you add a file to a category. If source_type is set to DATA_CENTER_FILE, this parameter is required, and the API returns an error if it is not specified. structure_type: In this example, pass unstructured. source_type: In this example, pass DATA_CENTER_FILE. sink_type: In this example, specify BUILT_IN.
The value of the Data.Id field returned by this API is the knowledge base ID, which is used for subsequent index building. Keep the knowledge base ID secure, as it is required for all subsequent API operations related to this knowledge base. | 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 structure type of the knowledge base.
source_type (str): The data source type. 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 structure type of the knowledge base.
* @param sourceType The data source type. 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 structure type of the knowledge base.
* @param string $sourceType The data source type. 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 structure type of the knowledge base.
* @param {string} sourceType - The data source type. 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 structure type of the knowledge base.</param>
/// <param name="sourceType">The data source type. 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.
//
// Parameters:
// - 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 structure type of the knowledge base.
// - sourceType (string): The data source type. 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: The 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": "Alibaba Cloud 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 initializing the knowledge base, call the SubmitIndexJob operation to start the index building process. After the submission is complete, Model Studio immediately starts building the index as an asynchronous task. The Data.Id returned by this API call is the corresponding task ID. You will use this ID in the next step to query the latest status of the task. | 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.
//
// Parameters:
// - 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: The 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. Query index job statusThe index job takes some time to complete. During peak hours, this process can take several hours. Call the GetIndexJobStatus operation to query its execution status. When the Data.Status field is COMPLETED, the knowledge base has been created. | 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.
//
// Parameters:
// - 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: The 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": "Imported successfully.",
"DocName": "Alibaba Cloud Model Studio Product Overview",
"Code": "FINISH"
}
],
"JobId": "3cd6fb57aaf44cd0b4dd2ca584xxxxxx"
},
"Code": "Success",
"Success": "true"
}
|