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
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 = "zhipuai"
version = "2.1.5.20250801"
version = "2.1.5.20250825"
description = "A SDK library for accessing big model apis from ZhipuAI"
authors = ["Zhipu AI"]
readme = "README.md"
Expand Down
40 changes: 40 additions & 0 deletions tests/integration_tests/test_file_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from __future__ import annotations

import logging
import logging.config
import os

import pytest

import zhipuai
from zhipuai import ZhipuAI


def test_file_parser_create(logging_conf):
logging.config.dictConfig(logging_conf) # type: ignore
client = ZhipuAI() # 填写您自己的APIKey
try:
response = client.file_parser.create(file=open('hitsuyoushorui-cn.pdf', 'rb'), file_type='pdf', tool_type='zhipu_pro')
print(response)

except zhipuai.core._errors.APIRequestFailedError as err:
print(err)
except zhipuai.core._errors.APIInternalError as err:
print(err)
except zhipuai.core._errors.APIStatusError as err:
print(err)

def test_file_parser_content(logging_conf):
logging.config.dictConfig(logging_conf) # type: ignore
client = ZhipuAI() # 填写您自己的APIKey
try:
response = client.file_parser.content(task_id="66e8f7ab884448c8b4190f251f6c2982-1", format_type="text")
print(response.content.decode('utf-8'))

except zhipuai.core._errors.APIRequestFailedError as err:
print(err)
except zhipuai.core._errors.APIInternalError as err:
print(err)
except zhipuai.core._errors.APIStatusError as err:
print(err)

1 change: 1 addition & 0 deletions zhipuai/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def __init__(
self.audio = api_resource.audio.Audio(self)
self.moderations = api_resource.moderation.Moderations(self)
self.agents = api_resource.agents.Agents(self)
self.file_parser = api_resource.file_parser.FileParser(self)

@property
@override
Expand Down
7 changes: 6 additions & 1 deletion zhipuai/api_resource/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@
Audio
)

from .file_parser import (
FileParser
)

__all__ = [
'Videos',
'AsyncCompletions',
Expand All @@ -68,5 +72,6 @@
'Tools',
'Assistant',
'Audio',
'Moderation'
'Moderation',
'FileParser'
]
3 changes: 3 additions & 0 deletions zhipuai/api_resource/file_parser/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .file_parser import FileParser

__all__ = ['FileParser']
95 changes: 95 additions & 0 deletions zhipuai/api_resource/file_parser/file_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Mapping, cast

import httpx
from typing_extensions import Literal

from ...core import BaseAPI, maybe_transform
from ...core import NOT_GIVEN, Body, Headers, NotGiven, FileTypes
from ...core import _legacy_binary_response
from ...core import _legacy_response
from ...core import deepcopy_minimal, extract_files
from ...core import (
make_request_options,
)
from ...types.file_parser.file_parser_create_params import FileParserCreateParams
from ...types.file_parser.file_parser_resp import FileParserTaskCreateResp

if TYPE_CHECKING:
from ..._client import ZhipuAI

__all__ = ["FileParser"]


class FileParser(BaseAPI):

def __init__(self, client: "ZhipuAI") -> None:
super().__init__(client)

def create(
self,
*,
file: FileTypes = None,
file_type: str = None,
tool_type: Literal["simple", "doc2x", "tencent", "zhipu-pro"],
extra_headers: Headers | None = None,
extra_body: Body | None = None,
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
) -> FileParserTaskCreateResp:

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/create",
body=maybe_transform(body, FileParserCreateParams),
files=files,
options=make_request_options(
extra_headers=extra_headers, extra_body=extra_body, timeout=timeout
),
cast_type=FileParserTaskCreateResp,
)

def content(
self,
task_id: str,
*,
format_type: Literal["text", "download_link"],
extra_headers: Headers | None = None,
extra_body: Body | None = None,
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
) -> _legacy_response.HttpxBinaryResponseContent:
"""
Returns the contents of the specified file.

Args:
extra_headers: Send extra headers

extra_body: Add additional JSON properties to the request

timeout: Override the client-level default timeout for this request, in seconds
"""
if not task_id:
raise ValueError(f"Expected a non-empty value for `task_id` but received {task_id!r}")
extra_headers = {"Accept": "application/binary", **(extra_headers or {})}
return self._get(
f"/files/parser/result/{task_id}/{format_type}",
options=make_request_options(
extra_headers=extra_headers, extra_body=extra_body, timeout=timeout
),
cast_type=_legacy_binary_response.HttpxBinaryResponseContent,
)
Empty file.
24 changes: 24 additions & 0 deletions zhipuai/types/file_parser/file_parser_create_params.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from __future__ import annotations

from typing_extensions import Literal, Required, TypedDict
from ...core import NOT_GIVEN, Body, Headers, NotGiven, FileTypes


__all__ = ["FileParserCreateParams", "FileParserDownloadParams"]


class FileParserCreateParams(TypedDict):
file: FileTypes
"""上传的文件"""
file_type: str
"""文件类型"""
tool_type: Literal["simple", "doc2x", "tencent", "zhipu-pro"]
"""工具类型"""


class FileParserDownloadParams(TypedDict):
task_id: str
"""解析任务id"""
format_type: Literal["text", "download_link"]
"""结果返回类型"""

18 changes: 18 additions & 0 deletions zhipuai/types/file_parser/file_parser_resp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from typing import List, Optional

from zhipuai.core import BaseModel

__all__ = [
"FileParserTaskCreateResp"
]


class FileParserTaskCreateResp(BaseModel):
task_id: str
# 任务id
message: str
# message
success: bool
# 是否成功