直接用于拍照答題不大理想, 可能適用其他用途, 更好的方案:?阿里云百煉(1) : 阿里云百煉應用問答_回答圖片問題_方案2_提取題目再提問-CSDN博客
1.實現代碼
package cn.nordrassil.ly.test.拍照答題;import com.alibaba.dashscope.app.Application;
import com.alibaba.dashscope.app.ApplicationParam;
import com.alibaba.dashscope.app.ApplicationResult;
import com.alibaba.dashscope.app.RagOptions;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.bailian20231229.AsyncClient;
import com.aliyun.sdk.service.bailian20231229.models.*;
import com.google.gson.Gson;
import darabonba.core.client.ClientOverrideConfiguration;import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;public class 阿里云百煉應用問答_回答圖片問題_方案1_提問時上傳圖片文件 {private static Map<String, String> params = new HashMap<>();static {params.put("accessKeyId", "");params.put("accessKeySecret", "");params.put("workspaceId", "");params.put("apiKey", "");params.put("appId", "");params.put("filePath", "C:\\Users\\admin\\Pictures\\1.png");}public static void main(String[] args) throws Exception {init();/* 1.上傳文件 *//** 1.1.申請文檔上傳租約 **/StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()// Please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set..accessKeyId(params.get("accessKeyId")).accessKeySecret(params.get("accessKeySecret"))//.securityToken(System.getenv("ALIBABA_CLOUD_SECURITY_TOKEN")) // use STS token.build());// Configure the ClientAsyncClient client = AsyncClient.builder().region("cn-beijing") // Region ID//.httpClient(httpClient) // Use the configured HttpClient, otherwise use the default HttpClient (Apache HttpClient).credentialsProvider(provider)//.serviceConfiguration(Configuration.create()) // Service-level configuration// Client-level configuration rewrite, can set Endpoint, Http request parameters, etc..overrideConfiguration(ClientOverrideConfiguration.create()// Endpoint 請參考 https://api.aliyun.com/product/bailian.setEndpointOverride("bailian.cn-beijing.aliyuncs.com")//.setConnectTimeout(Duration.ofSeconds(30))).build();// Parameter settings for API requestApplyFileUploadLeaseRequest applyFileUploadLeaseRequest = ApplyFileUploadLeaseRequest.builder().categoryType("SESSION_FILE").categoryId("default").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 url = new URL(param.getUrl());connection = (HttpURLConnection) url.openConnection();// 設置請求方法用于文檔上傳,需與您在上一步中調用ApplyFileUploadLease接口實際返回的Data.Param中Method字段的值一致connection.setRequestMethod("PUT");// 允許向connection輸出,因為這個連接是用于上傳文檔的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));// Asynchronous processing of return values/*response.thenAccept(resp -> {System.out.println(new Gson().toJson(resp));}).exceptionally(throwable -> { // Handling exceptionsSystem.out.println(throwable.getMessage());return null;});*/// 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)// 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 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();/* 2.調用百煉應用 */ApplicationParam aiParam = ApplicationParam.builder()// 若沒有配置環境變量,可用百煉API Key將下行替換為:.apiKey("sk-xxx")。但不建議在生產環境中直接將API Key硬編碼到代碼中,以減少API Key泄露風險。.apiKey(params.get("apiKey")).appId(params.get("appId")) // 替換為實際的應用ID.prompt("請直接輸出圖片問題的答案, 無需多言").ragOptions(RagOptions.builder().sessionFileIds(Arrays.asList(fileId)) // 替換為實際指定的臨時文件 ID,逗號隔開多個.build()).build();Application application = new Application();ApplicationResult result = application.call(aiParam);System.out.printf("%s\n",result.getOutput().getText());// 處理只輸出文本textSystem.out.println();}private static void init() throws Exception {String filePath = params.get("filePath");File file = new File(filePath);if (!file.exists()) {System.err.println("文件[" + filePath + "]不存在");System.exit(1);}params.put("fileMd5", getMD5Checksum(file));params.put("fileLength", String.valueOf(file.length()));params.put("fileName", file.getName());}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();}
}
2.依賴
<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.83</version><scope>compile</scope></dependency><!-- https://mvnrepository.com/artifact/com.alibaba/dashscope-sdk-java --><dependency><groupId>com.alibaba</groupId><artifactId>dashscope-sdk-java</artifactId><version>2.20.1</version></dependency><dependency><groupId>com.aliyun</groupId><artifactId>alibabacloud-bailian20231229</artifactId><version>2.0.6</version></dependency>
3.百煉應用提示詞
# 角色
你是一位知識淵博的專家,能夠根據用戶的需求提供準確、詳細且有幫助的答案。你具備廣泛的知識背景,并能靈活運用這些知識來解答各種問題。## 技能
### 技能 1: 知識檢索與應用
- 根據用戶的問題,從知識庫中檢索相關信息。### 技能 2: 問題解答
- 理解用戶的問題意圖和需求。
- 提供詳細、準確的答案,解釋關鍵概念和邏輯。## 限制
- 回答問題時必須基于現有的知識庫
- 知識庫中沒有的回復"知識庫無相關信息"# 知識庫
請記住以下材料,他們可能對回答問題有幫助。
${documents}
4.測試
4.1.創建知識庫導入文檔
百煉控制臺
點擊[中國人民解放軍海軍福建艦_百度百科]保存為pdf導入知識庫