Repository navigation
Expand file tree
/
Copy pathcall_function.py
More file actions
56 lines (48 loc) · 1.83 KB
/
Copy pathcall_function.py
File metadata and controls
56 lines (48 loc) · 1.83 KB
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
from collections.abc import Callable
from functions.get_files_info import schema_get_files_info, get_files_info
from functions.get_file_content import schema_get_file_content, get_file_content
from functions.run_python_file import schema_run_python_file, run_python_file
from functions.write_file import schema_write_file, write_file
from config import WORKING_DIR
import json
available_functions = [
schema_get_files_info,
schema_get_file_content,
schema_run_python_file,
schema_write_file
]
function_map: dict[str, Callable[..., str]] = {
"get_file_content": get_file_content,
"get_files_info": get_files_info,
"run_python_file": run_python_file,
"write_file": write_file,
}
def call_function(tool_call, verbose: bool = False) -> dict:
function_name = tool_call.function.name
function_args = json.loads(tool_call.function.arguments or "{}")
if verbose:
print(f" - Calling function: {function_name}({function_args})")
else:
print(f" - Calling function: {function_name}")
if not function_name in function_map:
return {
"role": "tool",
"tool_call_id": tool_call.id,
"content": f"Error: Unknown function: {function_name}",
}
# "./calculator" == function_args["working_directory"]
function_args["working_directory"] = WORKING_DIR
# some_var = function_map[function_name]()
result: str = function_map[function_name](**function_args)
# evaluate dict lookup first then pass args into it.
return {
"role": "tool",
"tool_call_id": tool_call.id,
"content": result,
}
# except Exception as e:
# return {
# "role": "tool",
# "tool_call_id": tool_call.id,
# "content": f"Error: Unknown function: {function_name}",
# }