feat: support file parsing sync (#46)
Co-authored-by: mengqian <cherish_a_meng@163.com>
This commit is contained in:
parent
e32a2498f9
commit
25f84e8b86
6 changed files with 87 additions and 7 deletions
|
|
@ -1,5 +1,6 @@
|
|||
from zai import ZaiClient
|
||||
import time
|
||||
import json
|
||||
import traceback
|
||||
|
||||
client = ZaiClient(
|
||||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -1,2 +1,2 @@
|
|||
__title__ = 'Z.ai'
|
||||
__version__ = '0.0.4.1'
|
||||
__version__ = '0.0.4.2'
|
||||
|
|
|
|||
|
|
@ -22,8 +22,8 @@ from zai.core import (
|
|||
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
|
||||
|
|
@ -103,3 +103,41 @@ class FileParser(BaseAPI):
|
|||
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,
|
||||
)
|
||||
|
|
|
|||
|
|
@ -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"""
|
||||
|
|
|
|||
|
|
@ -2,9 +2,7 @@ from typing import List, Optional
|
|||
|
||||
from zai.core import BaseModel
|
||||
|
||||
__all__ = [
|
||||
"FileParserTaskCreateResp"
|
||||
]
|
||||
__all__ = ["FileParserTaskCreateResp", "FileParsingDownloadResp"]
|
||||
|
||||
|
||||
class FileParserTaskCreateResp(BaseModel):
|
||||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue