-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathFileParsingSyncExample.java
More file actions
66 lines (54 loc) · 2.47 KB
/
FileParsingSyncExample.java
File metadata and controls
66 lines (54 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package ai.z.openapi.samples;
import ai.z.openapi.ZaiClient;
import ai.z.openapi.service.fileparsing.FileParsingDownloadResponse;
import ai.z.openapi.service.fileparsing.FileParsingUploadReq;
import ai.z.openapi.utils.StringUtils;
public class FileParsingSyncExample {
public static void main(String[] args) {
// It is recommended to set the API Key using an environment variable
// export ZAI_API_KEY=your.api_key
// ZaiClient client = ZaiClient.builder().build();
// Alternatively, the API Key can be specified directly in the code
ZaiClient client = ZaiClient.builder()
.apiKey("API Key")
.build();
try {
System.out.println("=== Example: Creating file parsing task ===");
String filePath = "your file path";
FileParsingDownloadResponse result = syncFileParsingTaskExample(client, filePath, "pdf", "prime-sync");
System.out.println("Parsing task created successfully, TaskId: " + result.getData().getTaskId());
System.out.println("File content: " + result.getData().getContent());
System.out.println("Download link: " + result.getData().getParsingResultUrl());
} catch (Exception e) {
System.err.println("Exception occurred: " + e.getMessage());
e.printStackTrace();
} finally {
client.close();
}
}
/**
* Example: Create parsing task (upload file and parse)
*
* @param client ZaiClient instance
* @return Parsing task's taskId
*/
private static FileParsingDownloadResponse syncFileParsingTaskExample(ZaiClient client, String filePath, String fileType, String toolType) {
if (StringUtils.isEmpty(filePath)) {
System.err.println("Invalid file path.");
return null;
}
try {
FileParsingUploadReq uploadReq = FileParsingUploadReq.builder()
.filePath(filePath)
.fileType(fileType) // Supported types: pdf, docx, etc.
.toolType(toolType) // Parsing tool type: lite, prime, expert
.build();
System.out.println("Uploading file and creating parsing task...");
return client.fileParsing().syncParse(uploadReq);
} catch (Exception e) {
System.err.println("File parsing task error: " + e.getMessage());
}
// Returning null means task creation failed
return null;
}
}