百炼平台Java 集成API上传文档到数据中心并添加索引

简介: 本文主要演示阿里云百炼产品,如何通过API实现数据中心文档的上传和索引的添加。

一、主要操作步骤

图片.png

二、参数获取

  • categoryId:数据中心类目id

图片.png

  • workspaceId:业务空间id

图片.png

  • indexId:索引id

图片.png

  • ak、sk

ACCESS_KEY_ID、ACCESS_KEY_SECRET参数获取地址

hiwspwhvrvbmc_2efd51003f384b0c8d043538cf44f1a7.png

三、完整的JAVA Code Sample

  • pom.xml
 <dependencies>

        <!-- https://mvnrepository.com/artifact/com.alibaba/dashscope-sdk-java -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dashscope-sdk-java</artifactId>
            <version>2.15.1</version>
        </dependency>

        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>bailian20231229</artifactId>
            <version>1.4.1</version>
        </dependency>
    </dependencies>
  • Code

import com.google.gson.internal.LinkedTreeMap;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Formatter;
import java.util.HashMap;

public class apply {

    public static void main(String[] args_) throws Exception {

        // 上传文件的本地路径地址
        String filePath = "C:\\Users\\*****\\1.0.01.04.36569.docx";
        String categoryId = "cate_d78d9993**********";//数据中心类目id
        String workspaceId = "llm-w*******"; //业务空间id
        String indexId = "eev*******";// 索引库索引id
        String ak = "LT*********";
        String sk = "8R**********";

        // 初始化参数
        String md5 = "";
        String byteLength = "";
        String fileName = "";

        try {
            Path path = Paths.get(filePath);
            fileName = path.getFileName().toString();
            // 读取文件内容到字节数组
            byte[] fileBytes = Files.readAllBytes(path);

            // 获取文件字节内容长度
            byteLength = fileBytes.length + "";
            // 计算MD5值
            md5 = calculateMD5(fileBytes);

        } catch (IOException | NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        // 初始化Client对象
        com.aliyun.teaopenapi.Client client = apply.createClient(ak, sk);

        //1、申请文件上传租约
        HashMap<String,Object> response = apply.applyFileUploadLease(client, fileName,md5,byteLength,categoryId,workspaceId);

        // 从response中提取data,获取上传文件必要的url和
        if(response.containsKey("body")){
            LinkedTreeMap<String, Object> body1 = (LinkedTreeMap<String, Object>) response.get("body");
            if (body1.containsKey("Data")) {
                LinkedTreeMap<String, Object> dataObject = (LinkedTreeMap<String, Object>) body1.get("Data");
                // 这里是你需要的Data对象
                String fileUploadLeaseId = (String) dataObject.get("FileUploadLeaseId");
                String type = (String) dataObject.get("Type");
                LinkedTreeMap<String, Object> param = (LinkedTreeMap<String, Object>) dataObject.get("Param");
                String preSignedUrl = (String) param.get("Url");

                LinkedTreeMap<String, Object> headers = (LinkedTreeMap<String, Object>) param.get("Headers");
                String contentType = (String) headers.get("Content-Type");
                String extra = (String) headers.get("X-bailian-extra");

                //2、使用租约上传文档
                uploadFile(preSignedUrl,filePath,extra,contentType,Integer.parseInt(byteLength));

                //3、addfile,添加上传的文档到百炼系统,系统自动解析文档
                String fileId = addFile(client, fileUploadLeaseId, workspaceId, categoryId);

                //4、将解析的文档添加到索引
                AddDocumentsJob(client,indexId,workspaceId,fileId,categoryId);

            }else {
                System.out.println("body not found in response.");
            }
        }
    }



    /**
     * <b>description</b> :
     * <p>使用AK & SK初始化账号Client</p>
     * @return Client
     *
     * @throws Exception
     */
    public static com.aliyun.teaopenapi.Client createClient(String ak, String sk) throws Exception {
        // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
        // 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378657.html。
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
                .setAccessKeyId(ak)
                // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
                .setAccessKeySecret(sk);
        // Endpoint 请参考 https://api.aliyun.com/product/bailian
        config.endpoint = "bailian.cn-beijing.aliyuncs.com";
        return new com.aliyun.teaopenapi.Client(config);
    }

    /**
     * <b>申请文档上传租约</b> :
     * <p>API 相关</p>
     * @return OpenApi.Params
     */
    public static com.aliyun.teaopenapi.models.Params createApplyFileUploadLeaseApiInfo(String CategoryId, String WorkspaceId) throws Exception {
        com.aliyun.teaopenapi.models.Params params = new com.aliyun.teaopenapi.models.Params()
                // 接口名称
                .setAction("ApplyFileUploadLease")
                // 接口版本
                .setVersion("2023-12-29")
                // 接口协议
                .setProtocol("HTTPS")
                // 接口 HTTP 方法
                .setMethod("POST")
                .setAuthType("AK")
                .setStyle("ROA")
                // 接口 PATH
                .setPathname("/" + WorkspaceId + "/datacenter/category/" + CategoryId + "")
                // 接口请求体内容格式
                .setReqBodyType("formData")
                // 接口响应体内容格式
                .setBodyType("json");
        return params;
    }

    public static HashMap<String,Object> applyFileUploadLease(com.aliyun.teaopenapi.Client client, String fileName, String md5, String byteLength, String categoryId, String workspaceId ) throws Exception{
        com.aliyun.teaopenapi.models.Params params = apply.createApplyFileUploadLeaseApiInfo(categoryId, workspaceId);
        // body params
        java.util.Map<String, Object> body = new java.util.HashMap<>();
        body.put("FileName", fileName);
        body.put("Md5", md5);
        body.put("SizeInBytes", byteLength);
        // runtime options
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        com.aliyun.teaopenapi.models.OpenApiRequest request = new com.aliyun.teaopenapi.models.OpenApiRequest()
                .setBody(body);
        // 返回值为 Map 类型,可从 Map 中获得三类数据:响应体 body、响应头 headers、HTTP 返回的状态码 statusCode。
        HashMap<String,Object> response = (HashMap<String, java.lang.Object>)  client.callApi(params, request, runtime);
        return response;
    }

    /**
     * 计算md5值
     * @param bytes
     * @return
     * @throws NoSuchAlgorithmException
     */
    private static String calculateMD5(byte[] bytes) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] digest = md.digest(bytes);
        return byteArrayToHexString(digest);
    }

    private static String byteArrayToHexString(byte[] bytes) {
        Formatter formatter = new Formatter();
        for (byte b : bytes) {
            formatter.format("%02x", b);
        }
        String result = formatter.toString();
        formatter.close();
        return result;
    }

    /**
     * 使用获取到的租约上传实际文件到数据中心,注意这一步上传后在数据中心还看不到文档,AddFile之后才可以在数据中心看到添加的文档
     * @param preSignedUrl
     * @param filePath
     * @param extra
     * @param contentType
     * @param length
     */
    public static void uploadFile(String preSignedUrl, String filePath,String extra,String contentType,int length ) {
        HttpURLConnection connection = null;
        try {
            // 创建URL对象

            URL url = new URL(preSignedUrl);
            connection = (HttpURLConnection) url.openConnection();
            // 设置请求⽅法为PUT,预签名URL默认⽤于PUT操作进⾏⽂件上传

            connection.setRequestMethod("PUT");
            // 允许向connection输出,因为这个连接是⽤于上传⽂件的

            connection.setDoOutput(true);
            // 设置请求头,这⾥可以根据需要设置⽐如Content-Type

            connection.setRequestProperty("X-bailian-extra", extra);
            connection.setRequestProperty("Content-Type", contentType);
            // 读取⽂件并通过连接上传

            try (DataOutputStream outStream = new DataOutputStream(connection.getOutputStream());
                 FileInputStream fileInputStream = new FileInputStream(filePath)) {
                byte[] buffer = new byte[length];
                int bytesRead;
                while ((bytesRead = fileInputStream.read(buffer)) != -1) {
                    outStream.write(buffer, 0, bytesRead);
                }
                outStream.flush();
            }
            // 检查响应代码
            int responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                // ⽂件上传成功处理

                System.out.println("File uploaded successfully.");
            } else {
                // ⽂件上传失败处理

                System.out.println("Failed to upload the file. ResponseCod e: " + responseCode);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
        }
    }

    /**
     * 构建添加文档API
     * @param WorkspaceId
     * @return
     * @throws Exception
     */
    public static com.aliyun.teaopenapi.models.Params createAddFileApiInfo(String WorkspaceId) throws Exception {
        com.aliyun.teaopenapi.models.Params params = new com.aliyun.teaopenapi.models.Params()
                // 接⼝名称
                .setAction("AddFile")
                // 接⼝版本
                .setVersion("2023-12-29")
                // 接⼝协议
                .setProtocol("HTTPS")
                // 接⼝ HTTP ⽅法
                .setMethod("PUT")
                .setAuthType("AK")
                .setStyle("ROA")
                // 接⼝ PATH
                .setPathname("/" + WorkspaceId + "/datacenter/file")
                // 接⼝请求体内容格式
                .setReqBodyType("formData")
                // 接⼝响应体内容格式
                .setBodyType("json");
        return params;
    }

    /**
     * AddFile
     * 该接口用于将已经成功上传的文档添加到百炼系统数据中心,添加成功之后,系统会自动启动文件的解析,在数据中心可以看到文档
     * @param client
     * @param leaseId
     * @param workSpaceId
     * @param categoryId
     * @throws Exception
     */
    public static String addFile(com.aliyun.teaopenapi.Client client,String leaseId,String workSpaceId,String categoryId) throws Exception {
        com.aliyun.teaopenapi.models.Params params = apply.createAddFileApiInfo(workSpaceId);
        // body params
        java.util.Map<String, Object> body = new java.util.HashMap<>();
        body.put("LeaseId", leaseId);
        body.put("Parser", "DASHSCOPE_DOCMIND");
        body.put("CategoryId", categoryId);
        // runtime options
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        com.aliyun.teaopenapi.models.OpenApiRequest request = new com.aliyun.teaopenapi.models.OpenApiRequest()
                .setBody(body);
        // 复制代码运行请自行打印 API 的返回值
        // 返回值为 Map 类型,可从 Map 中获得三类数据:响应体 body、响应头 headers、HTTP 返回的状态码 statusCode。
        HashMap<String,Object> response = (HashMap<String, Object>) client.callApi(params, request, runtime);

        LinkedTreeMap<String, Object> file_body = (LinkedTreeMap<String, Object>) response.get("body");
        LinkedTreeMap<String, Object> data = (LinkedTreeMap<String, Object>) file_body.get("Data");
        String fileId = (String) data.get("FileId");
        System.out.println("addFile:" + response.get("statusCode"));
        return fileId;
    }

    public static com.aliyun.teaopenapi.models.Params createSubmitIndexAddDocumentsJobApiInfo(String WorkspaceId) throws Exception {
        com.aliyun.teaopenapi.models.Params params = new com.aliyun.teaopenapi.models.Params()
                // 接口名称
                .setAction("SubmitIndexAddDocumentsJob")
                // 接口版本
                .setVersion("2023-12-29")
                // 接口协议
                .setProtocol("HTTPS")
                // 接口 HTTP 方法
                .setMethod("POST")
                .setAuthType("AK")
                .setStyle("ROA")
                // 接口 PATH
                .setPathname("/" + WorkspaceId + "/index/add_documents_to_index")
                // 接口请求体内容格式
                .setReqBodyType("json")
                // 接口响应体内容格式
                .setBodyType("json");
        return params;
    }

    /**
     * 追加以及添加到数据中心的文档到索引
     * @param client
     * @param indexId
     * @param workSpaceId
     * @param documentIds
     * @param categoryIds
     * @throws Exception
     */
    public static void AddDocumentsJob(com.aliyun.teaopenapi.Client client,String indexId,String workSpaceId,String documentIds, String categoryIds) throws Exception {
        com.aliyun.teaopenapi.models.Params params = apply.createSubmitIndexAddDocumentsJobApiInfo(workSpaceId);
        // query params
        java.util.Map<String, Object> queries = new java.util.HashMap<>();
        queries.put("IndexId", indexId);
        queries.put("SourceType", "DATA_CENTER_FILE");
        queries.put("DocumentIds", com.aliyun.openapiutil.Client.arrayToStringWithSpecifiedStyle(java.util.Arrays.asList(
                documentIds
        ), "DocumentIds", "json"));
        queries.put("CategoryIds", com.aliyun.openapiutil.Client.arrayToStringWithSpecifiedStyle(java.util.Arrays.asList(
                categoryIds
        ), "CategoryIds", "json"));
        // runtime options
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        com.aliyun.teaopenapi.models.OpenApiRequest request = new com.aliyun.teaopenapi.models.OpenApiRequest()
                .setQuery(com.aliyun.openapiutil.Client.query(queries));
        // 复制代码运行请自行打印 API 的返回值
        // 返回值为 Map 类型,可从 Map 中获得三类数据:响应体 body、响应头 headers、HTTP 返回的状态码 statusCode。
        HashMap<String,Object> response = (HashMap<String, Object>) client.callApi(params, request, runtime);
        System.out.println("AddDocumentsJob: " + response.get("statusCode"));
    }

}
  • 上传效果查看

图片.png

图片.png


四、更多参考

通过API上传文件

API参考地址

相关文章
|
8月前
|
云安全 人工智能 安全
Dify平台集成阿里云AI安全护栏,构建AI Runtime安全防线
阿里云 AI 安全护栏加入Dify平台,打造可信赖的 AI
3991 166
|
9月前
|
人工智能 安全 架构师
告别旅行规划的"需求文档地狱"!这个AI提示词库,让你像调API一样定制完美旅程
作为开发者,旅行规划如同“需求地狱”:信息碎片、需求多变、缺乏测试。本文提出一套“企业级”AI提示词库,将模糊需求转化为结构化“API请求”,实现标准化输入输出,让AI成为你的专属旅行架构师,30分钟生成专业定制方案,提升决策质量,降低90%时间成本。
890 129
|
8月前
|
JSON API 数据格式
小红书API接口文档:笔记详情数据开发手册
小红书笔记详情API可获取指定笔记的标题、正文、互动数据及多媒体资源,支持字段筛选与评论加载。通过note_id和access_token发起GET/POST请求,配合签名验证,广泛用于内容分析与营销优化。
1793 3
|
9月前
|
人工智能 安全 API
Dify平台集成安全护栏最佳实践
Dify平台提供低代码构建AI大模型应用的解决方案,支持云服务与私有化部署。本文介绍了在工作流和Agent中集成安全护栏的最佳实践,包括插件和扩展API两种方案。插件方式适用于工作流,一键安装实现输入输出防控;扩展API方式适用于Agent和工作流私有化部署场景,通过本地服务适配安全护栏API。文中还详细说明了操作步骤、前提条件及常见问题处理方法,帮助用户快速实现内容安全控制。
|
11月前
|
人工智能 搜索推荐 API
AI-Compass DeepSearch深度搜索生态:集成阿里ZeroSearch、字节DeerFlow、MindSearch等前沿平台,实现超越传统关键词匹配的智能信息检索革命
AI-Compass DeepSearch深度搜索生态:集成阿里ZeroSearch、字节DeerFlow、MindSearch等前沿平台,实现超越传统关键词匹配的智能信息检索革命
AI-Compass DeepSearch深度搜索生态:集成阿里ZeroSearch、字节DeerFlow、MindSearch等前沿平台,实现超越传统关键词匹配的智能信息检索革命
|
8月前
|
机器学习/深度学习 人工智能 监控
Java与AI模型部署:构建企业级模型服务与生命周期管理平台
随着企业AI模型数量的快速增长,模型部署与生命周期管理成为确保AI应用稳定运行的关键。本文深入探讨如何使用Java生态构建一个企业级的模型服务平台,实现模型的版本控制、A/B测试、灰度发布、监控与回滚。通过集成Spring Boot、Kubernetes、MLflow和监控工具,我们将展示如何构建一个高可用、可扩展的模型服务架构,为大规模AI应用提供坚实的运维基础。
649 0
|
SQL 人工智能 Java
阿里云百炼开源面向 Java 开发者的 NL2SQL 智能体框架
Spring-ai-alibaba-nl2sql 是析言 GBI 产品在数据问答领域的一次重要开源尝试,专注于 NL2SQL 场景下的核心能力开放。
3234 48
|
10月前
|
供应链 监控 搜索推荐
35页PPT|零售行业自助数据分析方法论:指标体系构建平台集成、会员与商品精细化运营实践
在零售行业环境剧变的背景下,传统“人找货”模式正被“货找人”取代。消费者需求日益个性化,购买路径多元化,企业亟需构建统一的指标体系,借助BI平台实现数据驱动的精细化运营。本文从指标体系构建、平台集成到会员与商品运营实践,系统梳理零售经营分析的方法论,助力企业实现敏捷决策与业务闭环。
35页PPT|零售行业自助数据分析方法论:指标体系构建平台集成、会员与商品精细化运营实践
|
11月前
|
机器学习/深度学习 人工智能 监控
CI/CD与模型监控平台集成MLOps系统实现的全面路径
MLOps是机器学习模型在生产环境中持续优化、部署和维护的关键。通过CI/CD流水线和模型监控平台的结合,可以大大提高模型开发和运维的效率,实现高效、稳定的模型服务。随着AI技术的快速发展,MLOps将在企业级AI应用中发挥越来越重要的作用。
CI/CD与模型监控平台集成MLOps系统实现的全面路径

热门文章

最新文章

相关产品

  • 大模型服务平台百炼