Skip to content

Commit

Permalink
组件规范化更新, trace_id参数更新,部分组件AnyOf参数未添加默认值 (#713)
Browse files Browse the repository at this point in the history
* 组件规范化更新, trace_id参数更新,部分组件AnyOf参数未添加默认值

* update

---------

Co-authored-by: yinjiaqi <[email protected]>
  • Loading branch information
C9luster and yinjiaqi authored Jan 10, 2025
1 parent a4e7014 commit e70a371
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 23 deletions.
12 changes: 8 additions & 4 deletions python/core/components/v2/animal_recognize/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]:
"""
Expand All @@ -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): 图片文件名
Expand Down
7 changes: 4 additions & 3 deletions python/core/components/v2/asr/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
"""
执行语音识别操作,并返回识别结果
Expand Down
10 changes: 6 additions & 4 deletions python/core/components/v2/general_ocr/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
):
"""
Expand All @@ -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
Expand Down
8 changes: 5 additions & 3 deletions python/core/components/v2/image_understand/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]:
"""
Expand All @@ -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')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion python/core/components/v2/llms/style_rewrite/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion python/core/components/v2/llms/style_writing/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions python/core/components/v2/object_recognize/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
"""
对给定的图片进行物体识别,并返回识别结果。
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion python/core/components/v2/translate/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 7 additions & 1 deletion python/tests/test_v2_animal_recognize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
2 changes: 1 addition & 1 deletion python/tests/test_v2_image_understand.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit e70a371

Please sign in to comment.