From dddc55a56b253753a80b68ac941bc8c1336ac5e1 Mon Sep 17 00:00:00 2001 From: yinjiaqi Date: Thu, 12 Dec 2024 15:30:47 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E6=96=B0=E5=A2=9EJson=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- python/core/component.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/python/core/component.py b/python/core/component.py index c316b826..b5fd9a05 100644 --- a/python/core/component.py +++ b/python/core/component.py @@ -126,6 +126,8 @@ class FunctionCall(BaseModel, extra='allow'): name: str = Field(default="", description="工具名") arguments: dict = Field(default={}, description="参数列表") +class Json(BaseModel, extra='allow'): + json_data: dict = Field(default={}, description="json数据") class Content(BaseModel): name: str = Field(default="", @@ -140,7 +142,7 @@ class Content(BaseModel): description="耗时、性能、内存等trace及debug所需信息") type: str = Field(default="text", description="代表event 类型,包括 text、code、files、urls、oral_text、references、image、chart、audio该字段的取值决定了下面text字段的内容结构") - text: Union[Text, Code, Files, Urls, OralText, References, Image, Chart, Audio, Plan, FunctionCall] = Field(default=Text, + text: Union[Text, Code, Files, Urls, OralText, References, Image, Chart, Audio, Plan, Json, FunctionCall] = Field(default=Text, description="代表当前 event 元素的内容,每一种 event 对应的 text 结构固定") @field_validator('text', mode='before') From e7587c78bdce9d04cfe7b8c688a0c7adf203e172 Mon Sep 17 00:00:00 2001 From: yinjiaqi Date: Thu, 12 Dec 2024 16:44:59 +0800 Subject: [PATCH 2/5] update --- python/core/component.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/python/core/component.py b/python/core/component.py index b5fd9a05..9a37c195 100644 --- a/python/core/component.py +++ b/python/core/component.py @@ -127,7 +127,7 @@ class FunctionCall(BaseModel, extra='allow'): arguments: dict = Field(default={}, description="参数列表") class Json(BaseModel, extra='allow'): - json_data: dict = Field(default={}, description="json数据") + data: dict = Field(default="", description="json数据") class Content(BaseModel): name: str = Field(default="", @@ -169,6 +169,8 @@ def set_text(cls, v, values, **kwargs): return Plan(**v) elif values.data['type'] == 'function_call': return FunctionCall(**v) + elif values.data['type'] == 'json': + return Json(**v) else: raise ValueError(f"Invalid value for 'type': {values['type']}") @@ -540,6 +542,8 @@ def create_output(cls, type, text, role="tool", name="", visible_scope="all", ra key_list = ["detail", "steps"] elif type == "function_call": key_list = ["thought", "name", "arguments"] + elif type == "json": + key_list = ["data"] else: raise ValueError("Unknown type: {}".format(type)) # assert all(key in text for key in key_list), "all keys:{} must be included in the text field".format(key_list) From 2287c414133be2c72847f01dffcfacae5926e406 Mon Sep 17 00:00:00 2001 From: yinjiaqi Date: Thu, 12 Dec 2024 17:28:36 +0800 Subject: [PATCH 3/5] update --- python/core/component.py | 6 ++--- python/tests/test_core_components.py | 38 ++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 python/tests/test_core_components.py diff --git a/python/core/component.py b/python/core/component.py index 9a37c195..4254bbef 100644 --- a/python/core/component.py +++ b/python/core/component.py @@ -127,7 +127,7 @@ class FunctionCall(BaseModel, extra='allow'): arguments: dict = Field(default={}, description="参数列表") class Json(BaseModel, extra='allow'): - data: dict = Field(default="", description="json数据") + data: str = Field(default="", description="json数据") class Content(BaseModel): name: str = Field(default="", @@ -517,6 +517,8 @@ def create_output(cls, type, text, role="tool", name="", visible_scope="all", ra text = {"url": text} elif type == "oral_text": text = {"info": text} + elif type == "json": + text = {"data": text} else: raise ValueError("Only when type=text/code/urls/oral_text, string text is allowed! Please give dict text") elif isinstance(text, dict): @@ -542,8 +544,6 @@ def create_output(cls, type, text, role="tool", name="", visible_scope="all", ra key_list = ["detail", "steps"] elif type == "function_call": key_list = ["thought", "name", "arguments"] - elif type == "json": - key_list = ["data"] else: raise ValueError("Unknown type: {}".format(type)) # assert all(key in text for key in key_list), "all keys:{} must be included in the text field".format(key_list) diff --git a/python/tests/test_core_components.py b/python/tests/test_core_components.py new file mode 100644 index 00000000..ebeb1544 --- /dev/null +++ b/python/tests/test_core_components.py @@ -0,0 +1,38 @@ +# 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 os +import json +import unittest + +from appbuilder.core.component import Component + + +@unittest.skipUnless(os.getenv("TEST_CASE", "UNKNOWN") == "CPU_PARALLEL", "") +class TestObjectRecognition(unittest.TestCase): + def setUp(self): + self.com = Component() + self.json = json.dumps({ + "type": "object_recognition", + "text": "https://baidu.com/1.jpg", + }) + + def test_create_output(self): + result = self.com.create_output( + type="json", + text=self.json, + ) + print(result) + +if __name__ == '__main__': + unittest.main() \ No newline at end of file From 4b5ef587a7bb1b522d803a59ebebb1c8e474f9e2 Mon Sep 17 00:00:00 2001 From: yinjiaqi Date: Thu, 12 Dec 2024 17:42:38 +0800 Subject: [PATCH 4/5] update --- python/tests/test_base_component.py | 11 ++------ python/tests/test_core_components.py | 38 ---------------------------- 2 files changed, 2 insertions(+), 47 deletions(-) delete mode 100644 python/tests/test_core_components.py diff --git a/python/tests/test_base_component.py b/python/tests/test_base_component.py index 7c60c5dd..12ad2f9d 100644 --- a/python/tests/test_base_component.py +++ b/python/tests/test_base_component.py @@ -14,6 +14,7 @@ def test_valid_output_with_str(self): out2 = self.component.create_output(type="code", text="import appbuilder") out3 = self.component.create_output(type="urls", text="http://www.baidu.com") out4 = self.component.create_output(type="oral_text", text="你是哪个") + out5 = self.component.create_output(type="json", text="{'key':'value'}") self.assertIsInstance(out1, ComponentOutput) self.assertIsInstance(out2, ComponentOutput) self.assertIsInstance(out3, ComponentOutput) @@ -55,15 +56,7 @@ def test_valid_output_type_with_same_key(self): def test_invalid_output_type_json(self): with self.assertRaises(ValueError): - output = self.component.create_output(type="json", text="") - # with self.assertRaises(AssertionError): - # output = self.component.create_output(type="files", text={}) - # with self.assertRaises(AssertionError): - # output = self.component.create_output(type="references", text={"info": "text"}) - # with self.assertRaises(AssertionError): - # output = self.component.create_output(type="image", text={"url": "https://example.com/img"}) - # with self.assertRaises(AssertionError): - # output = self.component.create_output(type="chart", text={"url": "https://example.com/chart"}) + output = self.component.create_output(type="test", text="") if __name__ == '__main__': diff --git a/python/tests/test_core_components.py b/python/tests/test_core_components.py deleted file mode 100644 index ebeb1544..00000000 --- a/python/tests/test_core_components.py +++ /dev/null @@ -1,38 +0,0 @@ -# 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 os -import json -import unittest - -from appbuilder.core.component import Component - - -@unittest.skipUnless(os.getenv("TEST_CASE", "UNKNOWN") == "CPU_PARALLEL", "") -class TestObjectRecognition(unittest.TestCase): - def setUp(self): - self.com = Component() - self.json = json.dumps({ - "type": "object_recognition", - "text": "https://baidu.com/1.jpg", - }) - - def test_create_output(self): - result = self.com.create_output( - type="json", - text=self.json, - ) - print(result) - -if __name__ == '__main__': - unittest.main() \ No newline at end of file From 9bbd96e072837a75a9899de6a951eb1b8ba09030 Mon Sep 17 00:00:00 2001 From: yinjiaqi Date: Thu, 12 Dec 2024 17:45:15 +0800 Subject: [PATCH 5/5] update --- python/tests/test_base_component.py | 1 + 1 file changed, 1 insertion(+) diff --git a/python/tests/test_base_component.py b/python/tests/test_base_component.py index 12ad2f9d..94d5ff4e 100644 --- a/python/tests/test_base_component.py +++ b/python/tests/test_base_component.py @@ -19,6 +19,7 @@ def test_valid_output_with_str(self): self.assertIsInstance(out2, ComponentOutput) self.assertIsInstance(out3, ComponentOutput) self.assertIsInstance(out4, ComponentOutput) + self.assertIsInstance(out5, ComponentOutput) def test_valid_output_with_dict(self): output1 = self.component.create_output(type="text", text={"info": "1"})