Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions examples/file_parsing_example.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from zai import ZaiClient
import time
import json
import traceback

client = ZaiClient(
Expand Down Expand Up @@ -76,6 +77,28 @@ def file_parser_complete_example():
print("File parser demo completed.")


def file_parser_sync_example():
"""
Full Example: Submit file for parsing and wait for the result to be returned.
"""
# Create parsing task
# Please modify the local file path
file_path = 'your file path'
with open(file_path, 'rb') as f:
print("Submitting file parsing task ...")
response = client.file_parser.create_sync(
file=f,
file_type="pdf",
tool_type="prime-sync",
)
print("Task created successfully. Response:")
print(response)

print("File parser demo completed.")

if __name__ == "__main__":
print("=== File Parsing Quick Demo ===\n")
file_parser_complete_example()

print("=== File synchronized parsing quick demo (Prime only) ===\n")
file_parser_sync_example()
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "zai-sdk"
version = "0.0.4.1"
version = "0.0.4.2"
description = "A SDK library for accessing big model apis from Z.ai"
authors = ["Z.ai"]
readme = "README.md"
Expand Down
2 changes: 1 addition & 1 deletion src/zai/_version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__title__ = 'Z.ai'
__version__ = '0.0.4.1'
__version__ = '0.0.4.2'
42 changes: 40 additions & 2 deletions src/zai/api_resource/file_parser/file_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
make_request_options
)

from zai.types.file_parser.file_parser_create_params import FileParserCreateParams
from zai.types.file_parser.file_parser_resp import FileParserTaskCreateResp
from zai.types.file_parser.file_parser_create_params import FileParserCreateParams,FileParserSyncParams
from zai.types.file_parser.file_parser_resp import FileParserTaskCreateResp,FileParsingDownloadResp

if TYPE_CHECKING:
from zai._client import ZaiClient
Expand Down Expand Up @@ -103,3 +103,41 @@ def content(
cast_type=_legacy_binary_response.HttpxBinaryResponseContent,
)
return httpxBinaryResponseContent.response


def create_sync(
self,
*,
file: FileTypes = None,
file_type: str = None,
tool_type: Literal["prime-sync"],
extra_headers: Headers | None = None,
extra_body: Body | None = None,
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
) -> FileParsingDownloadResp:

if not file:
raise ValueError("At least one `file` must be provided.")
body = deepcopy_minimal(
{
"file": file,
"file_type": file_type,
"tool_type": tool_type,
}
)

files = extract_files(cast(Mapping[str, object], body), paths=[["file"]])
if files:
# It should be noted that the actual Content-Type header that will be
# sent to the server will contain a `boundary` parameter, e.g.
# multipart/form-data; boundary=---abc--
extra_headers = {"Content-Type": "multipart/form-data", **(extra_headers or {})}
return self._post(
"/files/parser/sync",
body=maybe_transform(body, FileParserSyncParams),
files=files,
options=make_request_options(
extra_headers=extra_headers, extra_body=extra_body, timeout=timeout
),
cast_type=FileParsingDownloadResp,
)
8 changes: 8 additions & 0 deletions src/zai/types/file_parser/file_parser_create_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,11 @@ class FileParserDownloadParams(TypedDict):
"""Parsing task id"""
format_type: Literal["text", "download_link"]
"""Result return type"""

class FileParserSyncParams(TypedDict):
file: FileTypes
"""Uploaded file"""
file_type: str
"""File type"""
tool_type: Literal["prime-sync"]
"""Tool type"""
17 changes: 14 additions & 3 deletions src/zai/types/file_parser/file_parser_resp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

from zai.core import BaseModel

__all__ = [
"FileParserTaskCreateResp"
]
__all__ = ["FileParserTaskCreateResp", "FileParsingDownloadResp"]


class FileParserTaskCreateResp(BaseModel):
Expand All @@ -14,3 +12,16 @@ class FileParserTaskCreateResp(BaseModel):
# Message
success: bool
# Whether successful


class FileParsingDownloadResp(BaseModel):
task_id: str
# Task ID
message: str
# Message
status: bool
# Whether successful
content: str
# Parsed result text content
parsing_result_url: str
# Parsed result download link