fix: early return tool_schema_list in convert_tool_into_openai_schema function

pull/662/head
snakelu 1 month ago
parent 82a2d8954b
commit 9a193344a9

@ -387,39 +387,38 @@ class BaseTool(BaseModel):
"Converting tools into OpenAI function calling schema" "Converting tools into OpenAI function calling schema"
) )
tool_schema_list = [] # Store all tool schemas
for tool in self.tools: for tool in self.tools:
# Transform the tool into a openai function calling schema # Check if the tool has both documentation and type hints
if self.check_func_if_have_docs( if self.check_func_if_have_docs(tool) and self.check_func_if_have_type_hints(tool):
tool
) and self.check_func_if_have_type_hints(tool):
name = tool.__name__ name = tool.__name__
description = tool.__doc__ description = tool.__doc__
logger.info( logger.info(
f"Converting tool: {name} into a OpenAI certified function calling schema. Add documentation and type hints." f"Converting tool: {name} into OpenAI schema."
) )
tool_schema_list = ( schema = get_openai_function_schema_from_func(
get_openai_function_schema_from_func( tool, name=name, description=description
tool, name=name, description=description
)
) )
logger.info( logger.info(
f"Tool {name} converted successfully into OpenAI schema" f"Tool {name} converted successfully into OpenAI schema"
) )
tool_schema_list.append(schema)
# Transform the dictionary to a string
tool_schema_list = json.dumps(
tool_schema_list, indent=4
)
return tool_schema_list
else: else:
logger.error( # Log specific errors for missing documentation or type hints
f"Tool {tool.__name__} does not have documentation or type hints, please add them to make the tool execution reliable." if not self.check_func_if_have_docs(tool):
) logger.error(
f"Tool {tool.__name__} is missing documentation."
)
if not self.check_func_if_have_type_hints(tool):
logger.error(
f"Tool {tool.__name__} is missing type hints."
)
return tool_schema_list # Return the list as a JSON string
return json.dumps(tool_schema_list, indent=4)
def check_func_if_have_docs(self, func: callable): def check_func_if_have_docs(self, func: callable):
if func.__doc__ is not None: if func.__doc__ is not None:

Loading…
Cancel
Save