Skip to content

Commit 25f84e8

Browse files
code-c-lightmengqian
andauthored
feat: support file parsing sync (#46)
Co-authored-by: mengqian <cherish_a_meng@163.com>
1 parent e32a249 commit 25f84e8

File tree

6 files changed

+87
-7
lines changed

6 files changed

+87
-7
lines changed

examples/file_parsing_example.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from zai import ZaiClient
22
import time
3+
import json
34
import traceback
45

56
client = ZaiClient(
@@ -76,6 +77,28 @@ def file_parser_complete_example():
7677
print("File parser demo completed.")
7778

7879

80+
def file_parser_sync_example():
81+
"""
82+
Full Example: Submit file for parsing and wait for the result to be returned.
83+
"""
84+
# Create parsing task
85+
# Please modify the local file path
86+
file_path = 'your file path'
87+
with open(file_path, 'rb') as f:
88+
print("Submitting file parsing task ...")
89+
response = client.file_parser.create_sync(
90+
file=f,
91+
file_type="pdf",
92+
tool_type="prime-sync",
93+
)
94+
print("Task created successfully. Response:")
95+
print(response)
96+
97+
print("File parser demo completed.")
98+
7999
if __name__ == "__main__":
80100
print("=== File Parsing Quick Demo ===\n")
81101
file_parser_complete_example()
102+
103+
print("=== File synchronized parsing quick demo (Prime only) ===\n")
104+
file_parser_sync_example()

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "zai-sdk"
3-
version = "0.0.4.1"
3+
version = "0.0.4.2"
44
description = "A SDK library for accessing big model apis from Z.ai"
55
authors = ["Z.ai"]
66
readme = "README.md"

src/zai/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
__title__ = 'Z.ai'
2-
__version__ = '0.0.4.1'
2+
__version__ = '0.0.4.2'

src/zai/api_resource/file_parser/file_parser.py

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
make_request_options
2323
)
2424

25-
from zai.types.file_parser.file_parser_create_params import FileParserCreateParams
26-
from zai.types.file_parser.file_parser_resp import FileParserTaskCreateResp
25+
from zai.types.file_parser.file_parser_create_params import FileParserCreateParams,FileParserSyncParams
26+
from zai.types.file_parser.file_parser_resp import FileParserTaskCreateResp,FileParsingDownloadResp
2727

2828
if TYPE_CHECKING:
2929
from zai._client import ZaiClient
@@ -103,3 +103,41 @@ def content(
103103
cast_type=_legacy_binary_response.HttpxBinaryResponseContent,
104104
)
105105
return httpxBinaryResponseContent.response
106+
107+
108+
def create_sync(
109+
self,
110+
*,
111+
file: FileTypes = None,
112+
file_type: str = None,
113+
tool_type: Literal["prime-sync"],
114+
extra_headers: Headers | None = None,
115+
extra_body: Body | None = None,
116+
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
117+
) -> FileParsingDownloadResp:
118+
119+
if not file:
120+
raise ValueError("At least one `file` must be provided.")
121+
body = deepcopy_minimal(
122+
{
123+
"file": file,
124+
"file_type": file_type,
125+
"tool_type": tool_type,
126+
}
127+
)
128+
129+
files = extract_files(cast(Mapping[str, object], body), paths=[["file"]])
130+
if files:
131+
# It should be noted that the actual Content-Type header that will be
132+
# sent to the server will contain a `boundary` parameter, e.g.
133+
# multipart/form-data; boundary=---abc--
134+
extra_headers = {"Content-Type": "multipart/form-data", **(extra_headers or {})}
135+
return self._post(
136+
"/files/parser/sync",
137+
body=maybe_transform(body, FileParserSyncParams),
138+
files=files,
139+
options=make_request_options(
140+
extra_headers=extra_headers, extra_body=extra_body, timeout=timeout
141+
),
142+
cast_type=FileParsingDownloadResp,
143+
)

src/zai/types/file_parser/file_parser_create_params.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,11 @@ class FileParserDownloadParams(TypedDict):
2020
"""Parsing task id"""
2121
format_type: Literal["text", "download_link"]
2222
"""Result return type"""
23+
24+
class FileParserSyncParams(TypedDict):
25+
file: FileTypes
26+
"""Uploaded file"""
27+
file_type: str
28+
"""File type"""
29+
tool_type: Literal["prime-sync"]
30+
"""Tool type"""

src/zai/types/file_parser/file_parser_resp.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
from zai.core import BaseModel
44

5-
__all__ = [
6-
"FileParserTaskCreateResp"
7-
]
5+
__all__ = ["FileParserTaskCreateResp", "FileParsingDownloadResp"]
86

97

108
class FileParserTaskCreateResp(BaseModel):
@@ -14,3 +12,16 @@ class FileParserTaskCreateResp(BaseModel):
1412
# Message
1513
success: bool
1614
# Whether successful
15+
16+
17+
class FileParsingDownloadResp(BaseModel):
18+
task_id: str
19+
# Task ID
20+
message: str
21+
# Message
22+
status: bool
23+
# Whether successful
24+
content: str
25+
# Parsed result text content
26+
parsing_result_url: str
27+
# Parsed result download link

0 commit comments

Comments
 (0)