【阿里云百炼大模型】通过api上传知识库文档梳理(java)

操作步骤

官方文档地址:通过API上传文档_大模型服务平台百炼(Model Studio)-阿里云帮助中心

API地址:ListFile_大模型服务平台百炼_API调试-阿里云OpenAPI开发者门户

0.初始化账号

    public static com.aliyun.bailian20231229.Client createClient() throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                .setAccessKeyId(apiKey)//apiKey为RAM账户的key
                .setAccessKeySecret(apiSecret);//apiSecret为RAM账户的Secret
        config.endpoint = "bailian.cn-beijing.aliyuncs.com";
        return new com.aliyun.bailian20231229.Client(config);
    }

key与secret在阿里云控制台获取;

需要给RAM账户授权:

自定义权限策略给新建的RAM授权

对新建用户授权刚刚自定义的策略

1.申请文档上传租约

public static String getFileUploadLease() throws Exception {
        com.aliyun.bailian20231229.Client client = createClient();
        com.aliyun.bailian20231229.models.ApplyFileUploadLeaseRequest applyFileUploadLeaseRequest = new com.aliyun.bailian20231229.models.ApplyFileUploadLeaseRequest()
                .setFileName(fileName)
                .setMd5(fileMd5)
                .setSizeInBytes(fileSize);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        java.util.Map<String, String> headers = new java.util.HashMap<>();

        try {
            // 发送 API 请求并获取响应
            com.aliyun.bailian20231229.models.ApplyFileUploadLeaseResponse response = client.applyFileUploadLeaseWithOptions(CategoryId, WorkspaceId, applyFileUploadLeaseRequest, headers, runtime);

            // 创建一个 Map 来存储响应数据
            java.util.Map<String, Object> responseMap = new java.util.HashMap<>();
            responseMap.put("status", response.body.status);
            responseMap.put("headers", response.body.data.param.headers);
            responseMap.put("url", response.body.data.param.url);
            responseMap.put("fileUploadLeaseId", response.body.data.fileUploadLeaseId);

            // 转换 Map 为 JSON 格式字符串
            JSONObject jsonResponse = new JSONObject(responseMap);

            // 返回 JSON 格式的字符串
            return jsonResponse.toString();
        } catch (TeaException error) {
            // 错误处理
            return "Error message: " + error.getMessage() + "\n" +
                    "Recommend: " + error.getData().get("Recommend");
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 错误处理
            return "Error message: " + error.getMessage() + "\n" +
                    "Recommend: " + error.getData().get("Recommend");
        }
    }

responseMap.put("status", response.body.status);

responseMap.put("headers", response.body.data.param.headers);

responseMap.put("url", response.body.data.param.url);

responseMap.put("fileUploadLeaseId", response.body.data.fileUploadLeaseId);

这四个值是请求后后续需要用到的,返回后需进行存储操作。

2.上传至百炼的临时存储

传参(租约返回的url,文件地址,租约返回的header的X-bailian-extra,租约返回的header的ontent-Type)

    public static void uploadFile(String preSignedUrl, String filePath,String extra,String contentType) {
        HttpURLConnection connection = null;
        try {
            // 创建URL对象
            URL url = new URL(preSignedUrl);
            connection = (HttpURLConnection) url.openConnection();

            // 设置请求方法用于文档上传,需与您在上一步中调用ApplyFileUploadLease接口实际返回的Data.Param中Method字段的值一致
            connection.setRequestMethod("PUT");

            // 允许向connection输出,因为这个连接是用于上传文档的
            connection.setDoOutput(true);

            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[4096];
                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. ResponseCode: " + responseCode);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
        }
    }

3.将文档添加至百炼的数据管理

传参(租约返回的leaseId,parser,分类id,空间id)

parser目前只有默认值"DASHSCOPE_DOCMIND"

    public static String ToAddFile(String leaseId,String parser,String categoryId,String workspaceId) throws Exception{
        com.aliyun.bailian20231229.Client client = createClient();
        com.aliyun.bailian20231229.models.AddFileRequest addFileRequest = new com.aliyun.bailian20231229.models.AddFileRequest()
                .setLeaseId(leaseId)
                .setParser(parser)
                .setCategoryId(categoryId);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        java.util.Map<String, String> headers = new java.util.HashMap<>();
        try {
            // 复制代码运行请自行打印 API 的返回值

            com.aliyun.bailian20231229.models.AddFileResponse response=client.addFileWithOptions(workspaceId, addFileRequest, headers, runtime);
            // 创建一个 Map 来存储响应数据
            java.util.Map<String, Object> responseMap = new java.util.HashMap<>();
            responseMap.put("status", response.body.status);
            responseMap.put("fileId", response.body.data.fileId);

            // 转换 Map 为 JSON 格式字符串
            JSONObject jsonResponse = new JSONObject(responseMap);

            // 返回 JSON 格式的字符串
            return jsonResponse.toString();
        } catch (TeaException error) {

            return "Error message: " + error.getMessage() + "\n" +
                    "Recommend: " + error.getData().get("Recommend");
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 错误处理
            return "Error message: " + error.getMessage() + "\n" +
                    "Recommend: " + error.getData().get("Recommend");
        }
    }

responseMap.put("fileId", response.body.data.fileId);

fileId在调用提问接口的时候可以传入,让AI思考这个文件

4.查看文档解析状态

传参(空间id,步骤三返回的fileId)

    public static String DescribeFile(String workspaceId,String fileId) throws Exception{
        com.aliyun.bailian20231229.Client client = createClient();
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        java.util.Map<String, String> headers = new java.util.HashMap<>();
        try {
            // 复制代码运行请自行打印 API 的返回值
            com.aliyun.bailian20231229.models.DescribeFileResponse response=client.describeFileWithOptions("llm-7szhsx4j9umw12tn", "file_fec2e0f5abac4c9dad578e8af8f741af_10715285", headers, runtime);
//            System.out.println(response.body.data.status);
            return response.body.toString();
        } catch (TeaException error) {
            // 错误处理
            return "Error message: " + error.getMessage() + "\n" +
                    "Recommend: " + error.getData().get("Recommend");
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 错误处理
            return "Error message: " + error.getMessage() + "\n" +
                    "Recommend: " + error.getData().get("Recommend");
        }
    }

最后,可以在百炼控制台【数据管理】对应的分类里面看到上传的文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值