From 8384f22c4196fe6f6f846a530931d021d982f3f1 Mon Sep 17 00:00:00 2001 From: glide-the <2533736852@qq.com> Date: Mon, 12 Aug 2024 15:56:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=A7=86=E9=A2=91=E6=8F=90?= =?UTF-8?q?=E7=A4=BA=E8=AF=8D=E5=8F=AF=E9=80=89=20(#50)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加文件删除 --- tests/integration_tests/test_file.py | 32 +++++++++++---- zhipuai/api_resource/files.py | 56 +++++++++++++-------------- zhipuai/api_resource/videos/videos.py | 2 +- 3 files changed, 53 insertions(+), 37 deletions(-) diff --git a/tests/integration_tests/test_file.py b/tests/integration_tests/test_file.py index a0d26dc..b035b47 100644 --- a/tests/integration_tests/test_file.py +++ b/tests/integration_tests/test_file.py @@ -1,4 +1,3 @@ - from __future__ import annotations import unittest @@ -16,6 +15,7 @@ from zhipuai import ZhipuAI from zhipuai.api_resource import FilesWithRawResponse + @pytest.fixture(scope='class') def test_server(): class SharedData: @@ -31,11 +31,11 @@ class TestZhipuAIFileServer: def test_logs(self, logging_conf): logging.config.dictConfig(logging_conf) # type: ignore - def test_files(self,test_server, test_file_path): + def test_files(self, test_server, test_file_path): try: result = test_server.client.files.create( - file=open(os.path.join(test_file_path,"demo.jsonl"), "rb"), + file=open(os.path.join(test_file_path, "demo.jsonl"), "rb"), purpose="fine-tune" ) print(result) @@ -49,11 +49,10 @@ def test_files(self,test_server, test_file_path): except zhipuai.core._errors.APIStatusError as err: print(err) - - def test_files_validation(self,test_server, test_file_path): + def test_files_validation(self, test_server, test_file_path): try: result = test_server.client.files.create( - file=open(os.path.join(test_file_path,"demo.jsonl"), "rb"), + file=open(os.path.join(test_file_path, "demo.jsonl"), "rb"), purpose="fine-tune" ) print(result) @@ -69,8 +68,7 @@ def test_files_validation(self,test_server, test_file_path): except zhipuai.core._errors.APIStatusError as err: print(err) - - def test_files_list(self,test_server): + def test_files_list(self, test_server): try: list = test_server.client.files.list() print(list) @@ -84,5 +82,23 @@ def test_files_list(self,test_server): except zhipuai.core._errors.APIStatusError as err: print(err) + def test_delete_files(self, test_server): + try: + delete1 = test_server.client.files.delete( + file_id=test_server.file_id1 + ) + print(delete1) + + delete2 = test_server.client.files.delete( + file_id=test_server.file_id2 + ) + print(delete2) + + 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) diff --git a/zhipuai/api_resource/files.py b/zhipuai/api_resource/files.py index 1960220..cb3a14c 100644 --- a/zhipuai/api_resource/files.py +++ b/zhipuai/api_resource/files.py @@ -127,34 +127,34 @@ def list( ), ) - # def delete( - # self, - # file_id: str, - # *, - # extra_headers: Headers | None = None, - # extra_body: Body | None = None, - # timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN, - # ) -> FileDeleted: - # """ - # Delete a file. - # - # Args: - # file_id: The ID of the file to delete - # 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 file_id: - # raise ValueError(f"Expected a non-empty value for `file_id` but received {file_id!r}") - # return self._delete( - # f"/files/{file_id}", - # options=make_request_options( - # extra_headers=extra_headers, extra_body=extra_body, timeout=timeout - # ), - # cast_type=FileDeleted, - # ) + def delete( + self, + file_id: str, + *, + extra_headers: Headers | None = None, + extra_body: Body | None = None, + timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN, + ) -> FileDeleted: + """ + Delete a file. + + Args: + file_id: The ID of the file to delete + 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 file_id: + raise ValueError(f"Expected a non-empty value for `file_id` but received {file_id!r}") + return self._delete( + f"/files/{file_id}", + options=make_request_options( + extra_headers=extra_headers, extra_body=extra_body, timeout=timeout + ), + cast_type=FileDeleted, + ) def content( self, diff --git a/zhipuai/api_resource/videos/videos.py b/zhipuai/api_resource/videos/videos.py index 3e6ab2a..41e991d 100644 --- a/zhipuai/api_resource/videos/videos.py +++ b/zhipuai/api_resource/videos/videos.py @@ -30,8 +30,8 @@ def __init__(self, client: "ZhipuAI") -> None: def generations( self, model: str, - prompt: str, *, + prompt: str = None, image_url: str = None, request_id: str = None, user_id: str = None,