diff --git a/python/core/components/v2/animal_recognize/component.py b/python/core/components/v2/animal_recognize/component.py index e4e16ff9d..062e67b9b 100644 --- a/python/core/components/v2/animal_recognize/component.py +++ b/python/core/components/v2/animal_recognize/component.py @@ -17,6 +17,8 @@ import base64 import json +from typing import Optional + from appbuilder.core.component import Component, ComponentOutput from appbuilder.core.components.animal_recognize.model import * from appbuilder.core.message import Message @@ -145,8 +147,8 @@ def _recognize( @components_run_stream_trace def tool_eval( self, - img_name: str, - img_url: str, + img_name: Optional[str] = "", + img_url: Optional[str] = "", **kwargs, ) -> Union[Generator[str, None, None], str]: """ @@ -161,11 +163,13 @@ def tool_eval( Union[Generator[str, None, None], str]: 返回一个生成器,生成图像识别结果,或者返回图像识别结果的字符串。 """ - traceid = kwargs.get("_sys_traceid") + if not img_name and not img_url: + raise ValueError("img_name or img_url is required") + traceid = kwargs.get("_sys_traceid", "") file_urls = kwargs.get("_sys_file_urls", {}) yield from self._recognize_w_post_process(img_name, img_url, file_urls, request_id=traceid) - def _recognize_w_post_process(self, img_name, img_url, file_urls, request_id=None) -> str: + def _recognize_w_post_process(self, img_name, img_url, file_urls, request_id=None) -> str: # type: ignore r"""调底层接口对图片或图片url进行动物识别,并返回类别及其置信度 Args: img_name (str): 图片文件名 diff --git a/python/core/components/v2/asr/component.py b/python/core/components/v2/asr/component.py index c98f1a7fd..0cbf6b6b1 100644 --- a/python/core/components/v2/asr/component.py +++ b/python/core/components/v2/asr/component.py @@ -21,6 +21,7 @@ import requests import tempfile from urllib.parse import urlparse +from typing import Optional from appbuilder.core.component import Component from appbuilder.core.message import Message @@ -181,9 +182,9 @@ def _check_service_error(request_id: str, data: dict): @components_run_stream_trace def tool_eval(self, - file_url: str = '', - file_name: str = '', - file_type: str = '', + file_url: Optional[str] = '', + file_name: Optional[str] = '', + file_type: Optional[str] = '', **kwargs): """ 执行语音识别操作,并返回识别结果 diff --git a/python/core/components/v2/general_ocr/component.py b/python/core/components/v2/general_ocr/component.py index 9afd161ee..3a71d368b 100644 --- a/python/core/components/v2/general_ocr/component.py +++ b/python/core/components/v2/general_ocr/component.py @@ -15,6 +15,8 @@ import json import os.path +from typing import Optional + from appbuilder.core._client import HTTPClient from appbuilder.core._exception import AppBuilderServerException, InvalidRequestArgumentError @@ -169,9 +171,9 @@ def _check_service_error(request_id: str, data: dict): @components_run_stream_trace def tool_eval( self, - img_name: str = '', - img_url: str = '', - language_type: str = 'CHN_ENG', + img_name: Optional[str] = '', + img_url: Optional[str] = '', + language_type: Optional[str] = 'CHN_ENG', **kwargs ): """ @@ -195,7 +197,7 @@ def tool_eval( if not img_name and not img_url: raise ValueError( "request format error, one of image or url or must pdf_file or ofd_file be set") - traceid = kwargs.get("_sys_traceid") + traceid = kwargs.get("_sys_traceid", "") if not img_url: file_urls = kwargs.get("_sys_file_urls", {}) img_path = img_name diff --git a/python/core/components/v2/image_understand/component.py b/python/core/components/v2/image_understand/component.py index f07120891..c14046f9f 100644 --- a/python/core/components/v2/image_understand/component.py +++ b/python/core/components/v2/image_understand/component.py @@ -16,6 +16,8 @@ import base64 import time +from typing import Optional + from appbuilder.core.component import Component, ComponentOutput from appbuilder.core.message import Message from appbuilder.core._client import HTTPClient @@ -173,8 +175,8 @@ def __recognize( @components_run_stream_trace def tool_eval( self, - img_name: str, - img_url: str, + img_name: Optional[str] = '', + img_url: Optional[str] = '', **kwargs, ) -> Union[Generator[str, None, None], str]: """ @@ -188,7 +190,7 @@ def tool_eval( Returns: Union[Generator[str, None, None], str]: 图片内容理解结果 """ - traceid = kwargs.get("_sys_traceid") + traceid = kwargs.get("_sys_traceid", '') file_urls = kwargs.get("_sys_file_urls", {}) rec_res, raw_data = self._recognize_w_post_process(img_name, img_url, file_urls, request_id=traceid) llm_result = self.create_output(type="text", text=rec_res, name="text_1", raw_data=raw_data, visible_scope='llm') diff --git a/python/core/components/v2/llms/similar_question/component.py b/python/core/components/v2/llms/similar_question/component.py index 1f1d2fa1a..cfe55c6f8 100644 --- a/python/core/components/v2/llms/similar_question/component.py +++ b/python/core/components/v2/llms/similar_question/component.py @@ -122,7 +122,7 @@ def tool_eval(self, Generator[Output]: 返回一个生成器,生成类型为Output的对象。 """ - traceid = kwargs.get("_sys_traceid") + traceid = kwargs.get("_sys_traceid", "") msg = Message(query) model_configs = kwargs.get('model_configs', {}) temperature = model_configs.get("temperature", 1e-10) diff --git a/python/core/components/v2/llms/style_rewrite/component.py b/python/core/components/v2/llms/style_rewrite/component.py index bc6b590a6..a3a03674d 100644 --- a/python/core/components/v2/llms/style_rewrite/component.py +++ b/python/core/components/v2/llms/style_rewrite/component.py @@ -127,7 +127,7 @@ def tool_eval(self, query: str, style: str = "营销话术", **kwargs): Raises: ValueError: 如果缺少参数 'query'。 """ - traceid = kwargs.get("_sys_traceid") + traceid = kwargs.get("_sys_traceid", "") if not query: raise ValueError("param `query` is required") msg = Message(query) diff --git a/python/core/components/v2/llms/style_writing/component.py b/python/core/components/v2/llms/style_writing/component.py index b0566bbe7..32e864801 100644 --- a/python/core/components/v2/llms/style_writing/component.py +++ b/python/core/components/v2/llms/style_writing/component.py @@ -162,7 +162,7 @@ def tool_eval(self, Output: 生成的输出对象,包含文本类型和文本内容。 """ - traceid = kwargs.get("_sys_traceid") + traceid = kwargs.get("_sys_traceid", "") if not query: raise ValueError("param `query` is required") msg = Message(query) diff --git a/python/core/components/v2/object_recognize/component.py b/python/core/components/v2/object_recognize/component.py index a02afdfe2..6311cd719 100644 --- a/python/core/components/v2/object_recognize/component.py +++ b/python/core/components/v2/object_recognize/component.py @@ -16,6 +16,8 @@ import json import os +from typing import Optional + from appbuilder.core._client import HTTPClient from appbuilder.core.component import Component from appbuilder.core.message import Message @@ -150,8 +152,8 @@ def _check_service_error(request_id: str, data: dict): @components_run_stream_trace def tool_eval(self, - img_url: str = '', - img_name: str = '', + img_url: Optional[str] = '', + img_name: Optional[str] = '', **kwargs): """ 对给定的图片进行物体识别,并返回识别结果。 @@ -168,7 +170,7 @@ def tool_eval(self, InvalidRequestArgumentError: 如果请求格式错误,例如文件名未设置或文件URL不存在,则引发此异常。 """ - traceid = kwargs.get("_sys_traceid") + traceid = kwargs.get("_sys_traceid", "") if not img_url: file_urls = kwargs.get("_sys_file_urls", {}) img_path = img_name diff --git a/python/core/components/v2/translate/component.py b/python/core/components/v2/translate/component.py index 3ded44190..48f5e392e 100644 --- a/python/core/components/v2/translate/component.py +++ b/python/core/components/v2/translate/component.py @@ -168,7 +168,7 @@ def tool_eval(self, InvalidRequestArgumentError: 如果参数 `q` 未设置,则引发此异常。 """ - traceid = kwargs.get("_sys_traceid", None) + traceid = kwargs.get("_sys_traceid", "") text = q req = TranslateRequest() if not text: diff --git a/python/tests/test_v2_animal_recognize.py b/python/tests/test_v2_animal_recognize.py index b9b7c5e32..b7c9e054b 100644 --- a/python/tests/test_v2_animal_recognize.py +++ b/python/tests/test_v2_animal_recognize.py @@ -146,11 +146,17 @@ def test_tool_eval_valid(self): def test_tool_eval_invalid(self): """测试 tool 方法对无效请求的处理。""" - with self.assertRaises(TypeError): + with self.assertRaises(ValueError): result = self.animal_recognition.tool_eval(name="animal_recognition", streaming=True, origin_query="") next(result) + def test_tool_eval_raise_exception(self): + """测试 tool 方法对异常情况的处理。""" + with self.assertRaises(ValueError): + result = self.animal_recognition.tool_eval() + next(result) + if __name__ == '__main__': unittest.main() diff --git a/python/tests/test_v2_image_understand.py b/python/tests/test_v2_image_understand.py index a9f639519..009283937 100644 --- a/python/tests/test_v2_image_understand.py +++ b/python/tests/test_v2_image_understand.py @@ -93,7 +93,7 @@ def test_tool_eval_valid(self): def test_tool_eval_invalid(self): """测试 tool 方法对无效请求的处理。""" - with self.assertRaises(TypeError): + with self.assertRaises(ValueError): result = self.image_understand.tool_eval(name="image_understand", streaming=True, origin_query="") next(result)