newer_project / llm_modules /function_extract.py
YaTharThShaRma999's picture
Create function_extract.py
5949b60 verified
raw
history blame
No virus
2.14 kB
function_dict = {
"robot": "none",
"image_gen": "sdxl_model.infer",
"code": "execute_code",
"music_gen": "audio_gen.infer",
"search": "search_engine.search_text",
"search_img": "search_engine.search_image",
"save": "none",
"plot_graph": "plot_equation",
"math": "calculator",
"change_volume": "none",
"current_news": "search_engine.search_news",
"weather": "search_engine.search_text",
"none": "none",
"listen": "none",
"screen": "none",
"VisualQ": "moondream_model.infer"
}
import json
class FunctionExecutor:
def __init__(self):
pass
def preprocess(self, h, prefixes_to_remove):
for prefix in prefixes_to_remove:
h = h.replace(prefix, '')
for key, value in function_dict.items():
h = h.replace(key, value)
return h
def execute_string(self, func_string):
"""
Executes the function or method specified by the given string.
Args:
- func_string: A string representing the function or method to execute.
Returns:
- The result of the function or method execution.
"""
try:
# Split the string to extract function and arguments
func_name_with_args = func_string.rstrip(')')
func_name, args = func_name_with_args.split('(', 1)
args = args.strip("'\"")
# Get the function by name
func = eval(func_name)
# Execute the function
result = func(args)
return result
except Exception as e:
return f"Error executing function: {e}"
def get_function(self, action):
if action:
function_args = ["num_images=", "description=", "prompt=", "code=", "length=", "query=", "input=", "graph=", "equation=", "volume=", "place=", "percentage="]
action = self.preprocess(str(action), function_args)
code_str = action
#code_str = action
result = self.execute_string(code_str)
return result
else:
print("Function not found.")