Skip to content

Commit

Permalink
修改Component.create_output为classmethod方法,增加组件单测init_args (#621)
Browse files Browse the repository at this point in the history
* create_output变更为classmethod方法,component_tool_eval_schemas移动到case中

* create_output变更为classmethod方法,component_tool_eval_schemas移动到case中

* create_output变更为classmethod方法,component_tool_eval_schemas移动到case中

* create_output变更为classmethod方法,component_tool_eval_schemas移动到case中

---------

Co-authored-by: yepeiwen01 <[email protected]>
  • Loading branch information
peiwenYe and yepeiwen01 authored Nov 27, 2024
1 parent 60d2cd1 commit f418a33
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 64 deletions.
5 changes: 3 additions & 2 deletions python/core/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,11 +486,12 @@ def _langchain_tool_eval_implement(self, **kwargs):
final_result += step.get("text", "")
return final_result

def create_output(self, type, text, role="tool", name="", visible_scope="all", raw_data={}, usage={}, metrics={}):
@classmethod
def create_output(cls, type, text, role="tool", name="", visible_scope="all", raw_data={}, usage={}, metrics={}):
"""create_text_output
Args:
type (str): 类型,包括"text", "code", "files", "urls", "oral_text", "references", "image", "chart", "audio"
type (str): 类型,包括"text", "code", "files", "urls", "oral_text", "references", "image", "chart", "audio", "plan", "function_call"
text (str|dict): text字段,可输入str或dict
role (str, optional): 当前消息来源. Defaults to "tool".
name (str, optional): 当前yield内容的step name. Defaults to "".
Expand Down
71 changes: 34 additions & 37 deletions python/tests/component_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
from appbuilder.utils.json_schema_to_model import json_schema_to_pydantic_model
from appbuilder.tests.component_schemas import type_to_json_schemas
from component_tool_eval_cases import component_tool_eval_cases
from component_tool_eval_schemas import components_tool_eval_output_json_maps


class CheckInfo(BaseModel):
Expand Down Expand Up @@ -288,7 +287,7 @@ def _check_jsonschema(self, outputs, output_schemas):
"""检查输出格式是否符合对应的json schema
"""
invalid_details = []
if len(self._check_pre_format(outputs)) > 0 :
if len(invalid_details:=self._check_pre_format(outputs)) > 0 :
return invalid_details

for content in outputs["content"]:
Expand Down Expand Up @@ -369,42 +368,40 @@ def _check_text_and_code(self, component_case, output_dict):
def check(self, component_cls) -> CheckInfo:
invalid_details = []
component_cls_name = component_cls.__name__
if component_cls_name not in components_tool_eval_output_json_maps:
invalid_details.append("{} 没有注册到 components_tool_eval_output_json_maps 中".format(component_cls_name))
if component_cls_name not in component_tool_eval_cases:
invalid_details.append("{} 没有添加测试case到 component_tool_eval_cases 中".format(component_cls_name))
else:
output_json_schemas = components_tool_eval_output_json_maps[component_cls_name]
if component_cls_name not in component_tool_eval_cases:
invalid_details.append("{} 没有添加测试case到 component_tool_eval_cases 中".format(component_cls_name))
else:
component_case = component_tool_eval_cases[component_cls_name]()
input_dict = component_case.inputs()
component_obj = component_cls()

try:
stream_output_dict = {"text": "", "oral_text":"", "code": ""}
stream_outputs = component_obj.tool_eval(**input_dict)
for stream_output in stream_outputs: #校验流式输出
iter_invalid_detail = self._check_jsonschema(stream_output.model_dump(), output_json_schemas)
invalid_details.extend(["流式" + error_message for error_message in iter_invalid_detail])
iter_output_dict = self._gather_iter_outputs(stream_output)
stream_output_dict["text"] += iter_output_dict["text"]
stream_output_dict["oral_text"] += iter_output_dict["oral_text"]
stream_output_dict["code"] += iter_output_dict["code"]
if len(invalid_details) == 0:
invalid_details.extend(self._check_text_and_code(component_case, stream_output_dict))
except Exception as e:
invalid_details.append("ToolEval执行失败: {}".format(e))

time.sleep(2)
try:
non_stream_outputs = component_obj.non_stream_tool_eval(**input_dict)
non_stream_invalid_details = self._check_jsonschema(non_stream_outputs.model_dump(), output_json_schemas) #校验非流式输出
invalid_details.extend(["非流式" + error_message for error_message in non_stream_invalid_details])
if len(invalid_details) == 0:
non_stream_output_dict = self._gather_iter_outputs(non_stream_outputs)
invalid_details.extend(self._check_text_and_code(component_case, non_stream_output_dict))
except Exception as e:
invalid_details.append(" NonStreamToolEval执行失败: {}".format(e))
component_case = component_tool_eval_cases[component_cls_name]()
input_dict = component_case.inputs()
output_json_schemas = component_case.schemas()
init_args = component_case.init_args()
component_obj = component_cls(**init_args)

try: #校验流式输出
stream_output_dict = {"text": "", "oral_text":"", "code": ""}
stream_outputs = component_obj.tool_eval(**input_dict)
for stream_output in stream_outputs:
iter_invalid_detail = self._check_jsonschema(stream_output.model_dump(), output_json_schemas)
invalid_details.extend(["流式" + error_message for error_message in iter_invalid_detail])
iter_output_dict = self._gather_iter_outputs(stream_output)
stream_output_dict["text"] += iter_output_dict["text"]
stream_output_dict["oral_text"] += iter_output_dict["oral_text"]
stream_output_dict["code"] += iter_output_dict["code"]
if len(invalid_details) == 0:
invalid_details.extend(self._check_text_and_code(component_case, stream_output_dict))
except Exception as e:
invalid_details.append("ToolEval执行失败: {}".format(e))

time.sleep(2)
try: #校验非流式输出
non_stream_outputs = component_obj.non_stream_tool_eval(**input_dict)
non_stream_invalid_details = self._check_jsonschema(non_stream_outputs.model_dump(), output_json_schemas)
invalid_details.extend(["非流式" + error_message for error_message in non_stream_invalid_details])
if len(invalid_details) == 0:
non_stream_output_dict = self._gather_iter_outputs(non_stream_outputs)
invalid_details.extend(self._check_text_and_code(component_case, non_stream_output_dict))
except Exception as e:
invalid_details.append(" NonStreamToolEval执行失败: {}".format(e))

if len(invalid_details) > 0:
return CheckInfo(
Expand Down
46 changes: 42 additions & 4 deletions python/tests/component_tool_eval_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,27 @@
# 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.
from appbuilder.core.component import Component
from appbuilder.tests.component_schemas import text_schema, url_schema, image_schema, code_schema, file_schema, oral_text_schema, references_schema, chart_schema, audio_schema, plan_schema, function_call_schema

class AnimalRecognitionCase:
class Case():
def init_args(self):
return {}

def inputs(self):
return NotImplementedError()

def outputs(self):
return {}

def schemas(self):
return NotImplementedError()

def envs(self):
return {}


class AnimalRecognitionCase(Case):
def inputs(self):
return {
"img_name": "",
Expand All @@ -24,7 +43,10 @@ def inputs(self):
def outputs(self):
return {"text": ["熊猫"]}

class ASRCase:
def schemas(self):
return [text_schema]

class ASRCase(Case):
def inputs(self):
return {
"file_url": "https://bj.bcebos.com/v1/appbuilder/asr_test.pcm?authorization=bce-auth-v1" \
Expand All @@ -34,11 +56,17 @@ def inputs(self):
def outputs(self):
return {"text": ["北京科技馆"]}

class TreeMindCase:
def schemas(self):
return [text_schema]

class TreeMindCase(Case):
def inputs(self):
return {"query": "生成一份年度总结的思维导图"}

def schemas(self):
return [text_schema, url_schema]

class ImageUnderstandCase:
class ImageUnderstandCase(Case):
def inputs(self):
return {
"img_url": "https://bj.bcebos.com/v1/appbuilder/animal_recognize_test.png?" \
Expand All @@ -50,6 +78,16 @@ def inputs(self):
def outputs(self):
return {"text": ["熊猫"]}

def schemas(self):
return [text_schema]

class Text2ImageCase(Case):
def inputs(self):
return {"query": "生成一张熊猫图片"}

def schemas(self):
return [url_schema]

component_tool_eval_cases = {
"AnimalRecognition": AnimalRecognitionCase,
"ImageUnderstand": ImageUnderstandCase,
Expand Down
21 changes: 0 additions & 21 deletions python/tests/component_tool_eval_schemas.py

This file was deleted.

0 comments on commit f418a33

Please sign in to comment.