Skip to content

Commit b155301

Browse files
author
mengqian
committed
add:file parsing
1 parent 0ebf756 commit b155301

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

examples/file_parsing_example.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
from zai import ZaiClient
22
import time
33
import json
4+
import traceback
45

56
client = ZaiClient(
67
base_url="",
7-
api_key="",
8-
disable_token_cache=False
8+
api_key=""
99
)
1010

11-
def file_parser_create_example(file_path,tool_type,file_type):
11+
12+
def file_parser_create_example(file_path, tool_type, file_type):
1213
"""
1314
Example: Create a file parsing task
1415
"""
@@ -26,6 +27,7 @@ def file_parser_create_example(file_path,tool_type,file_type):
2627
task_id = getattr(response, "task_id", None)
2728
return task_id
2829

30+
2931
def file_parser_content_example(task_id, format_type="download_link"):
3032
"""
3133
Example: Get file parsing result
@@ -39,28 +41,31 @@ def file_parser_content_example(task_id, format_type="download_link"):
3941
)
4042
return response
4143
except Exception as err:
42-
print("Failed to get parsing result:", err)
44+
print("Failed to get parsing result:", traceback.format_exc())
4345
return None
4446

47+
4548
def file_parser_complete_example():
4649
"""
4750
Full Example: Submit file for parsing, then poll until result is ready
4851
"""
4952
# 1. 创建解析任务
5053
# 请修改本地文件路径
5154
file_path = 'your file path'
52-
task_id = file_parser_create_example(file_path=file_path,tool_type="lite",file_type="pdf")
55+
task_id = file_parser_create_example(file_path=file_path, tool_type="lite", file_type="pdf")
5356
if not task_id:
5457
print("Could not submit file for parsing.")
5558
return
59+
5660
''
5761
# 2. 轮询获取结果
5862
max_wait = 60 # 最多等待1分钟
5963
wait_time = 0
6064
while wait_time < max_wait:
6165
print(f"Waiting {wait_time}/{max_wait} seconds before querying result...")
6266
# format_type = text / download_link
63-
response = file_parser_content_example(task_id=task_id,format_type="download_link")
67+
response = file_parser_content_example(task_id=task_id, format_type="download_link")
68+
6469
result = response.json()
6570
if result.get("status") == "processing":
6671
print(result)
@@ -73,7 +78,6 @@ def file_parser_complete_example():
7378
print("File parser demo completed.")
7479

7580

76-
7781
if __name__ == "__main__":
7882
print("=== File Parsing Quick Demo ===\n")
79-
file_parser_complete_example()
83+
file_parser_complete_example()

src/zai/api_resource/file_parser/file_parser.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,11 @@ def content(
9595
if not task_id:
9696
raise ValueError(f"Expected a non-empty value for `task_id` but received {task_id!r}")
9797
extra_headers = {"Accept": "application/binary", **(extra_headers or {})}
98-
_legacy_binary_response.HttpxBinaryResponseContent = self._get(
98+
httpxBinaryResponseContent = self._get(
9999
f"/files/parser/result/{task_id}/{format_type}",
100100
options=make_request_options(
101101
extra_headers=extra_headers, extra_body=extra_body, timeout=timeout
102102
),
103103
cast_type=_legacy_binary_response.HttpxBinaryResponseContent,
104104
)
105-
return _legacy_binary_response.HttpxBinaryResponseContent.response
106-
107-
# def json(self):
108-
# return json.loads(self.content.decode("utf-8"))
105+
return httpxBinaryResponseContent.response

0 commit comments

Comments
 (0)