Spaces:
Runtime error
Runtime error
File size: 3,362 Bytes
44504f7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
import inspect
import textwrap
KNW_INJECTION = {}
class knw:
def __init__(self):
self.name = 'knowledge_integration'
self.description = 'Integrate knowledge into the LLM.'
self.core_function = 'core_function'
self.test_case = 'test_case'
self.runnable_function = 'runnable_function'
self.mode = 'full'
self.method_code = {}
def get_core_function(self):
"""
Core function of the knowledge integration.
"""
function_name = self.core_function
core_function = getattr(self, function_name, None)
return textwrap.dedent(core_function())
# return self.method_code[self.core_function]
def get_runnable_function(self):
"""
Runnable function of the knowledge integration.
"""
function_name = self.runnable_function
runnable_function = getattr(self, function_name, None)
return textwrap.dedent(runnable_function())
#return self.method_code[self.runnable_function]
def get_all_code(self):
return self.get_core_function(), self.get_runnable_function()
#return "Core code:" + self.get_core_function() + "\nOther function code" + self.get_runnable_function()
def get_test_case(self):
"""
Test case for the knowledge integration.
"""
return self.method_code[self.test_case]
def get_internal_function(self):
"""
All other functions of the core function.
"""
internal_code = ""
for name, code in self.method_code.items():
if name not in [self.core_function, self.test_case]:
internal_code += f"{code}\n"
return internal_code
def get_function_code(self, function_name):
function = getattr(self, function_name, None)
if function is None:
logger.warning("Method not found.")
else:
inspect_function = inspect.getsource(function)
return inspect_function.replace('self,', '').replace('self.', '').replace('self','')
def get_all_function_code(self):
all_code = "```python"
for name, code in self.method_code.items():
all_code += f"\n{code}\n"
return all_code+"```"
def get_all_function(self):
methods = inspect.getmembers(self, predicate=inspect.ismethod)
self.method_code = {name: self.get_function_code(name) for name, _ in methods if name not in ['__init__', 'get_all_function', 'get_all_function_code', 'get_core_function', 'get_function_code', 'get_test_case', 'get_internal_function', 'get_fixed_function']}
return self.method_code
def get_fix_function(self):
return self.method_code
def remove_outer_indentation(code_str): # delete the outer indentation
lines = code_str.splitlines()
non_empty_lines = [line for line in lines if line.strip()]
if not non_empty_lines:
return code_str
min_indent = min(len(line) - len(line.lstrip()) for line in non_empty_lines)
aligned_lines = [line[min_indent:] for line in lines]
return '\n'.join(aligned_lines)
if __name__ == '__main__':
kwn = knw()
# print(kwn.get_entrance_function()) #todo : problem in indent
print(kwn.get_all_function_code())
# print(instantiate_subclasses(knw)) |