Skip to content

armgit / camel

Public Imported from GitHub
0

🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org

fix: `validate_openai_tool_schema` (#1085)

Guohao Lidf02bb0729dd
diff --git a/camel/toolkits/function_tool.py b/camel/toolkits/function_tool.py
index 804bc894e..b0ec66fcd 100644
--- a/camel/toolkits/function_tool.py
+++ b/camel/toolkits/function_tool.py
@@ -185,17 +185,18 @@ class FunctionTool:
Raises:
ValidationError: If the schema does not comply with the
specifications.
- ValueError: If the function description or parameter descriptions
- are missing in the schema.
SchemaError: If the parameters do not meet JSON Schema reference
specifications.
"""
# Check the type
if not openai_tool_schema["type"]:
- raise ValueError("miss type")
- # Check the function description
- if not openai_tool_schema["function"]["description"]:
- raise ValueError("miss function description")
+ raise ValueError("miss `type` in tool schema.")
+
+ # Check the function description, if no description then raise warming
+ if not openai_tool_schema["function"].get("description"):
+ warnings.warn(f"""Function description is missing for
+ {openai_tool_schema['function']['name']}. This may
+ affect the quality of tool calling.""")
# Validate whether parameters
# meet the JSON Schema reference specifications.
@@ -208,14 +209,15 @@ class FunctionTool:
JSONValidator.check_schema(parameters)
except SchemaError as e:
raise e
- # Check the parameter description
+
+ # Check the parameter description, if no description then raise warming
properties: Dict[str, Any] = parameters["properties"]
for param_name in properties.keys():
param_dict = properties[param_name]
if "description" not in param_dict:
- raise ValueError(
- f'miss description of parameter "{param_name}"'
- )
+ warnings.warn(f"""Parameter description is missing for
+ {param_dict}. This may affect the quality of tool
+ calling.""")
def get_openai_tool_schema(self) -> Dict[str, Any]:
r"""Gets the OpenAI tool schema for this function.
diff --git a/test/toolkits/test_openai_function.py b/test/toolkits/test_openai_function.py
index 256345001..f813c2c82 100644
--- a/test/toolkits/test_openai_function.py
+++ b/test/toolkits/test_openai_function.py
@@ -329,11 +329,56 @@ function_schema = {
},
}
+function_schema_without_docs = {
+ "description": '',
+ "name": "add",
+ "parameters": {
+ 'type': 'object',
+ 'properties': {
+ 'a': {
+ 'type': 'integer',
+ },
+ 'b': {
+ 'type': 'integer',
+ },
+ },
+ 'required': ['a', 'b'],
+ },
+}
+
+function_schema_with_wrong_docs = {
+ "name": "add",
+ "description": "Adds two numbers.",
+ "parameters": {
+ 'type': 'object',
+ 'properties': {
+ 'a': {
+ 'type': 'integer',
+ 'description': 'The first number to be added.',
+ },
+ 'b': {
+ 'type': 'integer',
+ },
+ },
+ 'required': ['a', 'b'],
+ },
+}
+
tool_schema = {
"type": "function",
"function": function_schema,
}
+tool_schema_without_docs = {
+ "type": "function",
+ "function": function_schema_without_docs,
+}
+
+tool_schema_with_wrong_docs = {
+ "type": "function",
+ "function": function_schema_with_wrong_docs,
+}
+
def test_correct_function():
add = FunctionTool(add_with_doc)
@@ -344,19 +389,17 @@ def test_correct_function():
def test_function_without_doc():
add = FunctionTool(add_without_doc)
add.set_function_name("add")
- with pytest.raises(Exception, match="miss function description"):
- _ = add.get_openai_function_schema()
- add.set_openai_function_schema(function_schema)
- assert add.get_openai_function_schema() == function_schema
+ with pytest.warns(UserWarning, match="Function description is missing"):
+ _ = FunctionTool(add_without_doc).get_openai_function_schema()
+ with pytest.warns(UserWarning, match="Parameter description is missing"):
+ _ = FunctionTool(add_without_doc).get_openai_function_schema()
+ assert add.get_openai_tool_schema() == tool_schema_without_docs
def test_function_with_wrong_doc():
add = FunctionTool(add_with_wrong_doc)
add.set_function_name("add")
- with pytest.raises(Exception, match="miss description of parameter \"b\""):
- _ = add.get_openai_function_schema()
- add.set_parameter("b", function_schema["parameters"]["properties"]["b"])
- assert add.get_openai_function_schema() == function_schema
+ assert add.get_openai_tool_schema() == tool_schema_with_wrong_docs
def test_validate_openai_tool_schema_valid():