From 8f45a5d432df2d1cf072b94468513e891c8ea290 Mon Sep 17 00:00:00 2001 From: Chengmo Date: Fri, 27 Dec 2024 19:27:07 -0800 Subject: [PATCH] add base code (#691) Co-authored-by: chengmo --- python/core/component.py | 7 +++++ python/tests/test_component_is_async.py | 37 +++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 python/tests/test_component_is_async.py diff --git a/python/core/component.py b/python/core/component.py index 5628ed9f..b62d9262 100644 --- a/python/core/component.py +++ b/python/core/component.py @@ -236,6 +236,13 @@ def set_secret_key_and_gateway(self, secret_key: Optional[str] = None, gateway: """ self.secret_key = secret_key self.gateway = gateway + # 因为子类可能重载init方法,导致没有is_async属性,所以此处需要判断一下 + try: + self.is_async + except AttributeError: + # 目前async仅在AppBuilderClient中使用,所以没有async属性的组件都可以设置为False + self.is_async = False + if self.is_async: self._http_client = AsyncHTTPClient(self.secret_key, self.gateway) else: diff --git a/python/tests/test_component_is_async.py b/python/tests/test_component_is_async.py new file mode 100644 index 00000000..c7bfb8fa --- /dev/null +++ b/python/tests/test_component_is_async.py @@ -0,0 +1,37 @@ +# Copyright (c) 2024 Baidu, Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import json +import os +import unittest +from appbuilder.core.component import Component + +class MockComponent(Component): + + def __init__(self, *args, **kwargs): + pass + + def tool_eval(self, *args, **kwargs): + print("Success") + + +class TestComponentInit(unittest.TestCase): + def test_component_init(self): + component = MockComponent() + os.environ["APPBUILDER_TOKEN"] = "abc" + component.set_secret_key_and_gateway() + component.tool_eval() + +if __name__ == '__main__': + unittest.main() +