Merge pull request #1169 from garylin2099/di_fixes

rm unnecessary condition
This commit is contained in:
garylin2099 2024-04-09 17:27:46 +08:00 committed by GitHub
commit 6c2b238a3e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -32,15 +32,8 @@ def convert_code_to_tool_schema(obj, include: list[str] = None) -> dict:
def convert_code_to_tool_schema_ast(code: str) -> list[dict]:
"""Converts a code string to a list of tool schemas by parsing the code with AST"""
# Modify the AST nodes to include parent references, enabling to attach methods to their class
def add_parent_references(node, parent=None):
for child in ast.iter_child_nodes(node):
child.parent = parent
add_parent_references(child, parent=node)
visitor = CodeVisitor(code)
parsed_code = ast.parse(code)
add_parent_references(parsed_code)
visitor.visit(parsed_code)
return visitor.get_tool_schemas()
@ -108,7 +101,7 @@ class CodeVisitor(ast.NodeVisitor):
self._visit_function(node)
def _visit_function(self, node):
if isinstance(node.parent, ast.ClassDef) or node.name.startswith("_"):
if node.name.startswith("_"):
return
function_schemas = self._get_function_schemas(node)
function_schemas["code"] = ast.get_source_segment(self.source_code, node)