-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcall_function.py
More file actions
64 lines (52 loc) · 1.73 KB
/
call_function.py
File metadata and controls
64 lines (52 loc) · 1.73 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
57
58
59
60
61
62
63
64
# call_function.py
from google.genai import types
# Import your real implementations
from functions.get_files_info import get_files_info
from functions.get_file_content import get_file_content
from functions.run_python_file import run_python_file
from functions.write_file import write_file
# -----------------------------
# Function Map
# -----------------------------
function_map = {
"get_files_info": get_files_info,
"get_file_content": get_file_content,
"run_python_file": run_python_file,
"write_file": write_file,
}
# -----------------------------
# Function Caller
# -----------------------------
def call_function(function_call, verbose=False):
function_name = function_call.name or ""
if verbose:
print(f"Calling function: {function_name}({function_call.args})")
else:
print(f" - Calling function: {function_name}")
# Unknown function protection
if function_name not in function_map:
return types.Content(
role="tool",
parts=[
types.Part.from_function_response(
name=function_name,
response={"error": f"Unknown function: {function_name}"},
)
],
)
# Copy args safely
args = dict(function_call.args) if function_call.args else {}
# Inject working directory (security boundary)
args["working_directory"] = "./calculator"
# Call actual function
function_result = function_map[function_name](**args)
# Wrap result for LLM
return types.Content(
role="tool",
parts=[
types.Part.from_function_response(
name=function_name,
response={"result": function_result},
)
],
)