|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | + |
| 4 | +from typing import TYPE_CHECKING, List, Mapping, cast |
| 5 | +from typing_extensions import Literal |
| 6 | + |
| 7 | +from ...core import BaseAPI, maybe_transform |
| 8 | +from ...core import NOT_GIVEN, Body, Headers, NotGiven, FileTypes |
| 9 | + |
| 10 | +import httpx |
| 11 | + |
| 12 | +from ...core import ( |
| 13 | + make_request_options, |
| 14 | +) |
| 15 | +from ...core import deepcopy_minimal, extract_files |
| 16 | +from ...types.file_parser.file_parser_create_params import FileParserCreateParams |
| 17 | +from ...types.file_parser.file_parser_resp import FileParserTaskCreateResp |
| 18 | +from ...core import _legacy_binary_response |
| 19 | +from ...core import _legacy_response |
| 20 | + |
| 21 | +if TYPE_CHECKING: |
| 22 | + from ..._client import ZhipuAI |
| 23 | + |
| 24 | +__all__ = ["FileParser"] |
| 25 | + |
| 26 | + |
| 27 | +class FileParser(BaseAPI): |
| 28 | + |
| 29 | + def __init__(self, client: "ZhipuAI") -> None: |
| 30 | + super().__init__(client) |
| 31 | + |
| 32 | + def create( |
| 33 | + self, |
| 34 | + *, |
| 35 | + file: FileTypes = None, |
| 36 | + file_type: str = None, |
| 37 | + tool_type: Literal["simple", "doc2x", "tencent", "zhipu-pro"], |
| 38 | + extra_headers: Headers | None = None, |
| 39 | + extra_body: Body | None = None, |
| 40 | + timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN, |
| 41 | + ) -> FileParserTaskCreateResp: |
| 42 | + |
| 43 | + if not file: |
| 44 | + raise ValueError("At least one `file` must be provided.") |
| 45 | + body = deepcopy_minimal( |
| 46 | + { |
| 47 | + "file": file, |
| 48 | + "file_type": file_type, |
| 49 | + "tool_type": tool_type, |
| 50 | + } |
| 51 | + ) |
| 52 | + |
| 53 | + files = extract_files(cast(Mapping[str, object], body), paths=[["file"]]) |
| 54 | + if files: |
| 55 | + # It should be noted that the actual Content-Type header that will be |
| 56 | + # sent to the server will contain a `boundary` parameter, e.g. |
| 57 | + # multipart/form-data; boundary=---abc-- |
| 58 | + extra_headers = {"Content-Type": "multipart/form-data", **(extra_headers or {})} |
| 59 | + return self._post( |
| 60 | + "/fileParse/create", |
| 61 | + body=maybe_transform(body, FileParserCreateParams), |
| 62 | + files=files, |
| 63 | + options=make_request_options( |
| 64 | + extra_headers=extra_headers, extra_body=extra_body, timeout=timeout |
| 65 | + ), |
| 66 | + cast_type=FileParserTaskCreateResp, |
| 67 | + ) |
| 68 | + |
| 69 | + def content( |
| 70 | + self, |
| 71 | + task_id: str, |
| 72 | + *, |
| 73 | + format_type: Literal["text", "download_link"], |
| 74 | + extra_headers: Headers | None = None, |
| 75 | + extra_body: Body | None = None, |
| 76 | + timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN, |
| 77 | + ) -> _legacy_response.HttpxBinaryResponseContent: |
| 78 | + """ |
| 79 | + Returns the contents of the specified file. |
| 80 | +
|
| 81 | + Args: |
| 82 | + extra_headers: Send extra headers |
| 83 | +
|
| 84 | + extra_body: Add additional JSON properties to the request |
| 85 | +
|
| 86 | + timeout: Override the client-level default timeout for this request, in seconds |
| 87 | + """ |
| 88 | + if not task_id: |
| 89 | + raise ValueError(f"Expected a non-empty value for `task_id` but received {task_id!r}") |
| 90 | + extra_headers = {"Accept": "application/binary", **(extra_headers or {})} |
| 91 | + return self._get( |
| 92 | + f"/fileParse/getResult/{task_id}/{format_type}", |
| 93 | + options=make_request_options( |
| 94 | + extra_headers=extra_headers, extra_body=extra_body, timeout=timeout |
| 95 | + ), |
| 96 | + cast_type=_legacy_binary_response.HttpxBinaryResponseContent, |
| 97 | + ) |
0 commit comments