Skip to content

Commit 5ae9534

Browse files
author
mengqian
committed
add:file parsing
1 parent 372d99c commit 5ae9534

File tree

8 files changed

+444
-207
lines changed

8 files changed

+444
-207
lines changed

examples/file_parsing_example.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
from zai import ZaiClient
2+
import time
3+
import json
4+
5+
client = ZaiClient(
6+
base_url="",
7+
api_key="",
8+
disable_token_cache=False
9+
)
10+
11+
def file_parser_create_example(file_path,tool_type,file_type):
12+
"""
13+
Example: Create a file parsing task
14+
"""
15+
print("=== File Parser Create Example ===")
16+
with open(file_path, 'rb') as f:
17+
print("Submitting file parsing task ...")
18+
response = client.file_parser.create(
19+
file=f,
20+
file_type=file_type,
21+
tool_type=tool_type,
22+
)
23+
print("Task created successfully. Response:")
24+
print(response)
25+
# 一般可获取 task_id
26+
task_id = getattr(response, "task_id", None)
27+
return task_id
28+
29+
def file_parser_content_example(task_id, format_type="download_link"):
30+
"""
31+
Example: Get file parsing result
32+
"""
33+
print("=== File Parser Content Example ===")
34+
try:
35+
print(f"Querying parsing result for task_id: {task_id}")
36+
response = client.file_parser.content(
37+
task_id=task_id,
38+
format_type=format_type
39+
)
40+
return response
41+
except Exception as err:
42+
print("Failed to get parsing result:", err)
43+
return None
44+
45+
def file_parser_complete_example():
46+
"""
47+
Full Example: Submit file for parsing, then poll until result is ready
48+
"""
49+
# 1. 创建解析任务
50+
# 请修改本地文件路径
51+
file_path = 'your file path'
52+
task_id = file_parser_create_example(file_path=file_path,tool_type="lite",file_type="pdf")
53+
if not task_id:
54+
print("Could not submit file for parsing.")
55+
return
56+
''
57+
# 2. 轮询获取结果
58+
max_wait = 60 # 最多等待1分钟
59+
wait_time = 0
60+
while wait_time < max_wait:
61+
print(f"Waiting {wait_time}/{max_wait} seconds before querying result...")
62+
# format_type = text / download_link
63+
response = file_parser_content_example(task_id=task_id,format_type="download_link")
64+
result = response.json()
65+
if result.get("status") == "processing":
66+
print(result)
67+
68+
time.sleep(5)
69+
wait_time += 5
70+
else:
71+
print(result)
72+
break
73+
print("File parser demo completed.")
74+
75+
76+
77+
if __name__ == "__main__":
78+
print("=== File Parsing Quick Demo ===\n")
79+
file_parser_complete_example()

0 commit comments

Comments
 (0)