当前位置: 首页 > news >正文

阿里云百炼平台创建智能体-上传文档

整体思路是:

1创建ram用户,授权

2上传文件获取FileSession

3调用智能体对话,传入FileSession

接下来每个步骤的细节:
1官方不推荐使用超级管理员用户获得accessKeyId和accessKeySecret,所以登录超级管理员账号创建ram用户,给这个用户授权想用的权限即可
创建ram用户 可以使用官方概览这里有个快速开始,我这里只是想开发人员使用key,所以选择了“创建程序用户”
在这里插入图片描述
在这里插入图片描述
最重要的需要授权
点击上个页面上的授权,搜素AliyunBailianFullAccess或AliyunBailianControlFullAccess。然后第二步授予workspace权限授予工作空间权限!这个不要忘否则调用的时候会报“401”

在这里插入图片描述
在这里插入图片描述
2上传文件主要有三个步骤,官方文档快速开始里面有在线调试和sdk开始。看官网就行。
上传文档参数说明
在这里插入图片描述

api接口调用在线调试
在这里插入图片描述
注意这个上传文档可以使用oss,但是需要是公开的,于是我使用本地文档上传,通用写法。我使用的是java,传入ossId为文档id,也可以直接换成本地绝对路径,就不需要从ossId转localFilePath了,其中的params 放入按照前面获取到的数据放入即可,如

` private static Map<String, String> params = new HashMap<>();static {params.put("accessKeyId", " ");params.put("accessKeySecret", " ");params.put("workspaceId", "  ");params.put("apiKey", "sk- ");params.put("appId", " ");`
    public String getFileSession(Long ossId) throws IOException {String localFilePath = "";if (null!=ossId) {Path downloadedPath = null;try {downloadedPath = this.getTempFullFilePath(ossId);if (!StrUtil.isBlankIfStr(downloadedPath)) {String strPath = downloadedPath.toAbsolutePath().toString();localFilePath = strPath;} else {return null;}//上传bailian文件init(localFilePath);/* 1.上传文件 *//** 1.1.申请文档上传租约 **/StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder().accessKeyId(params.get("accessKeyId")).accessKeySecret(params.get("accessKeySecret")).build());AsyncClient client = AsyncClient.builder().credentialsProvider(provider)configuration rewrite, can set Endpoint, Http request parameters, etc..overrideConfiguration(ClientOverrideConfiguration.create().setEndpointOverride("bailian.cn-beijing.aliyuncs.com")).build();ApplyFileUploadLeaseRequest applyFileUploadLeaseRequest = ApplyFileUploadLeaseRequest.builder().categoryType(params.get("CategoryType")).categoryId(params.get("CategoryId") ).fileName(params.get("fileName")).md5(params.get("fileMd5")).sizeInBytes(params.get("fileLength")).workspaceId(params.get("workspaceId"))// Request-level configuration rewrite, can set Http request parameters, etc.// .requestConfiguration(RequestConfiguration.create().setHttpHeaders(new HttpHeaders())).build();// Asynchronously get the return value of the API requestCompletableFuture<ApplyFileUploadLeaseResponse> response = client.applyFileUploadLease(applyFileUploadLeaseRequest);// Synchronously get the return value of the API requestApplyFileUploadLeaseResponse resp = response.get();System.out.println("- 申请文档上传租约结果: " + new Gson().toJson(resp));ApplyFileUploadLeaseResponseBody.Param param = resp.getBody().getData().getParam();/** 1.2.上传文档至阿里云百炼的临时存储 **/HttpURLConnection connection = null;try {URL url = new URL(param.getUrl());connection = (HttpURLConnection) url.openConnection();connection.setRequestMethod("PUT");connection.setDoOutput(true);JSONObject headers = JSONObject.parseObject(JSONObject.toJSONString(param.getHeaders()));connection.setRequestProperty("X-bailian-extra", headers.getString("X-bailian-extra"));connection.setRequestProperty("Content-Type", headers.getString("Content-Type"));try (DataOutputStream outStream = new DataOutputStream(connection.getOutputStream());FileInputStream fileInputStream = new FileInputStream(params.get("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("- 上传文件成功");} else {// 文档上传失败处理System.out.println("Failed to upload the file. ResponseCode: " + responseCode);}} catch (Exception e) {e.printStackTrace();} finally {if (connection != null) {connection.disconnect();}}/** 1.3.将文档添加至阿里云百炼的数据管理 **/AddFileRequest addFileRequest = AddFileRequest.builder().categoryType("SESSION_FILE").leaseId(resp.getBody().getData().getFileUploadLeaseId()).parser("DASHSCOPE_DOCMIND").categoryId("default").workspaceId(params.get("workspaceId"))// Request-level configuration rewrite, can set Http request parameters, etc.// .requestConfiguration(RequestConfiguration.create().setHttpHeaders(new HttpHeaders())).build();// Asynchronously get the return value of the API requestCompletableFuture<AddFileResponse> addFileresponse = client.addFile(addFileRequest);// Synchronously get the return value of the API requestAddFileResponse addFileresp = addFileresponse.get();System.out.println("- 将文档添加至阿里云百炼的数据管理结果: " + new Gson().toJson(addFileresp));// Finally, close the clientString fileId = addFileresp.getBody().getData().getFileId();System.out.println("- fileId: " + fileId);/** 1.4.查看文件是否解析 **/DescribeFileRequest describeFileRequest = DescribeFileRequest.builder().workspaceId(params.get("workspaceId")).fileId(fileId).build();// Asynchronously get the return value of the API requestString status = null;while (status == null || !status.equals("FILE_IS_READY")) {CompletableFuture<DescribeFileResponse> describeResponse = client.describeFile(describeFileRequest);// Synchronously get the return value of the API requestDescribeFileResponse describeResp = describeResponse.get();if (describeResp.getBody() == null) {continue;}if (describeResp.getBody().getData() == null) {continue;}status = describeResp.getBody().getData().getStatus();if (status == null) {continue;}System.out.println("- fileId状态: " + status);Thread.sleep(500);}// 关闭client.close();return fileId;} catch (Exception e) {log.error("处理ossId为 {} 的文件失败: {}",ossId, e.getMessage(), e);} finally {Path tempDir = null;if (null != downloadedPath) {tempDir = downloadedPath.getParent().toAbsolutePath();}if (null != tempDir) {// 递归删除目录(包括所有子目录和文件)Files.walkFileTree(tempDir, new SimpleFileVisitor<Path>() {@Overridepublic FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {// 删除文件Files.delete(file);return FileVisitResult.CONTINUE;}@Overridepublic FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {// 删除空目录Files.delete(dir);return FileVisitResult.CONTINUE;}});}log.info("临时目录已删除!");}}return localFilePath;}public static String getMD5Checksum(File file) throws IOException, NoSuchAlgorithmException {FileInputStream fis = new FileInputStream(file);MessageDigest digest = MessageDigest.getInstance("MD5");FileChannel fileChannel = fis.getChannel();MappedByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size());// 使用MappedByteBuffer可以更高效地读取大文件digest.update(buffer);byte[] hashBytes = digest.digest();// 将字节数组转换为十六进制字符串StringBuilder hexString = new StringBuilder();for (byte b : hashBytes) {String hex = Integer.toHexString(0xff & b);if (hex.length() == 1) hexString.append('0');hexString.append(hex);}fileChannel.close();fis.close();return hexString.toString();}

3调用智能体

        ApplicationParam aiParam = ApplicationParam.builder().apiKey("sk- ").appId(" ")  
//        增量输出,即后续输出内容不包含已输出的内容。实时地逐个读取这些片段以获得完整的结果。.incrementalOutput(true).prompt(chatModelDto.getMessage())//可传入历史记录
//            .messages().ragOptions(RagOptions.builder().sessionFileIds(fileSessionList).build()).build();Application application = new Application();ApplicationResult result = application.call(aiParam);String text = result.getOutput().getText();return text;
//流式输出 createResult可忽略,返回Flux<String>
//        Application application = new Application();
//        Flowable<ApplicationResult> result = application.streamCall(aiParam);
//        Flux<Result> resultFlux = Flux.from(result)
//            .map(applicationResult -> {
//                 String response = applicationResult.getOutput().getText();
//                return createResult(response);
//            });
//        return resultFlux;
http://www.xdnf.cn/news/1244809.html

相关文章:

  • Java学习第一百零六部分——Lucene
  • 2.4 组件通信
  • deepseek、GPT与claude在MATLAB编程上的准确性对比——以卡尔曼滤波调试为例
  • 大模型之后,机器人正在等待它的“GPT-1 时刻”
  • 本机部署K8S集群
  • 力扣:2246. 相邻字符不同的最长路径
  • ESP-idf框架下的HTTP服务器\HTML 485温湿度采集并长传
  • 14.Home-新鲜好物和人气推荐实现
  • 编程算法:技术创新与业务增长的核心引擎
  • Linux操作系统从入门到实战(十三)版本控制器Git基础概念讲解
  • 深入浅出 RabbitMQ-路由模式详解
  • 自由学习记录(77)
  • 24. 前端-js框架-Vue
  • vite面试题及详细答案120题(01-30)
  • 【工程化】tree-shaking 的作用以及配置
  • 研发团队看板协作中的自动化实践:集成CI/CD与任务流转
  • 【Linux系统】进程间通信:基于匿名管道实现进程池
  • linux_https,udp,tcp协议(更新中)
  • C语言基础_随机数、数组、函数、指针
  • 【机器学习深度学习】模型压缩简介
  • C++ - 基于多设计模式下的同步异步日志系统(11w字)
  • NLP——BERT模型全面解析:从基础架构到优化演进
  • AWS EKS节点扩容时NLB与Ingress的故障处理与优化方案
  • LSTM + 自注意力机制:精准预测天气变化的创新方案
  • 深入剖析 RAG 检索系统中的召回方式:BM25、向量召回、混合策略全解析
  • JS-第二十一天-尺寸位置
  • Android UI 组件系列(十一):RecyclerView 多类型布局与数据刷新实战
  • AI 对话高效输入指令攻略(四):AI+Apache ECharts:生成各种专业图表
  • 【学习笔记】Manipulate-Anything(基于视觉-语言模型的机器人自动化操控系统)
  • 【09】C++实战篇——C++ 生成静态库.lib 及 C++调用lib,及实际项目中的使用技巧