From e65ce2e83c5f6d550efbc18c44b37ad8441955f5 Mon Sep 17 00:00:00 2001 From: KrishCode Date: Mon, 14 Oct 2024 21:10:09 +0530 Subject: [PATCH] feat: external_function_tool.py Add validation and error handling for ExternalFunctionTool definition --- app/core/tools/external_function_tool.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/app/core/tools/external_function_tool.py b/app/core/tools/external_function_tool.py index 9dce4fb..d661696 100644 --- a/app/core/tools/external_function_tool.py +++ b/app/core/tools/external_function_tool.py @@ -35,3 +35,24 @@ def __init__(self, definition: dict) -> None: # 其它参数未使用到,暂时不做处理 self.openai_function = definition self.name = definition["function"]["name"] + + +def _validate_definition(self, definition: dict) -> bool: + """ + Validate the structure of the function definition. + Returns True if valid, False otherwise. + """ + if "type" not in definition or definition["type"] != "function": + return False + + if "function" not in definition: + return False + + if not isinstance(definition["function"], dict): + return False + + # Check if "name" exists + if "name" not in definition["function"]: + return False + + return True