YaTharThShaRma999
commited on
Commit
•
5949b60
1
Parent(s):
fa5afd1
Create function_extract.py
Browse files
llm_modules/function_extract.py
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
function_dict = {
|
2 |
+
"robot": "none",
|
3 |
+
"image_gen": "sdxl_model.infer",
|
4 |
+
"code": "execute_code",
|
5 |
+
"music_gen": "audio_gen.infer",
|
6 |
+
"search": "search_engine.search_text",
|
7 |
+
"search_img": "search_engine.search_image",
|
8 |
+
"save": "none",
|
9 |
+
"plot_graph": "plot_equation",
|
10 |
+
"math": "calculator",
|
11 |
+
"change_volume": "none",
|
12 |
+
"current_news": "search_engine.search_news",
|
13 |
+
"weather": "search_engine.search_text",
|
14 |
+
"none": "none",
|
15 |
+
"listen": "none",
|
16 |
+
"screen": "none",
|
17 |
+
"VisualQ": "moondream_model.infer"
|
18 |
+
}
|
19 |
+
import json
|
20 |
+
class FunctionExecutor:
|
21 |
+
def __init__(self):
|
22 |
+
pass
|
23 |
+
def preprocess(self, h, prefixes_to_remove):
|
24 |
+
for prefix in prefixes_to_remove:
|
25 |
+
h = h.replace(prefix, '')
|
26 |
+
for key, value in function_dict.items():
|
27 |
+
h = h.replace(key, value)
|
28 |
+
return h
|
29 |
+
def execute_string(self, func_string):
|
30 |
+
"""
|
31 |
+
Executes the function or method specified by the given string.
|
32 |
+
|
33 |
+
Args:
|
34 |
+
- func_string: A string representing the function or method to execute.
|
35 |
+
|
36 |
+
Returns:
|
37 |
+
- The result of the function or method execution.
|
38 |
+
"""
|
39 |
+
try:
|
40 |
+
# Split the string to extract function and arguments
|
41 |
+
func_name_with_args = func_string.rstrip(')')
|
42 |
+
func_name, args = func_name_with_args.split('(', 1)
|
43 |
+
args = args.strip("'\"")
|
44 |
+
# Get the function by name
|
45 |
+
func = eval(func_name)
|
46 |
+
# Execute the function
|
47 |
+
result = func(args)
|
48 |
+
return result
|
49 |
+
except Exception as e:
|
50 |
+
return f"Error executing function: {e}"
|
51 |
+
|
52 |
+
def get_function(self, action):
|
53 |
+
if action:
|
54 |
+
function_args = ["num_images=", "description=", "prompt=", "code=", "length=", "query=", "input=", "graph=", "equation=", "volume=", "place=", "percentage="]
|
55 |
+
action = self.preprocess(str(action), function_args)
|
56 |
+
code_str = action
|
57 |
+
#code_str = action
|
58 |
+
result = self.execute_string(code_str)
|
59 |
+
return result
|
60 |
+
else:
|
61 |
+
print("Function not found.")
|