-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcall_function.py
More file actions
127 lines (112 loc) · 4.03 KB
/
Copy pathcall_function.py
File metadata and controls
127 lines (112 loc) · 4.03 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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# call_function.py
from google.genai import types
from config import *
from Helper_Functions.file_handling import (
get_files_info,
schema_get_files_info,
get_file_content,
schema_get_file_content,
run_python_file,
schema_run_python_file,
write_file,
schema_write_file,
)
from Robot_Tools.Robot_Motion_Tools import (
get_dobot_device, schema_get_dobot_device,
move_to_home, schema_move_to_home,
move_to_specific_position, schema_move_to_specific_position,
get_current_pose, schema_get_current_pose,
suction_on, schema_suction_on,
suction_off, schema_suction_off,
set_affine_matrix, schema_set_affine_matrix,
move_robot_point_above, schema_move_robot_point_above,
move_robot_point_block, schema_move_robot_point_block,
update_scene_memory, schema_update_scene_memory,
)
from Robot_Tools.Pick_Place_Tool import pick_and_place_block,schema_pick_and_place_block
from Robot_Tools.Camera_Capture_Tools import capture_scene_with_detection, schema_capture_scene_with_detection
# This is what you pass to Gemini as tools:
available_functions = types.Tool(
function_declarations=[
# FILES
schema_get_files_info,
schema_get_file_content,
schema_run_python_file,
schema_write_file,
# ROBOT
schema_get_dobot_device,
schema_move_to_home,
schema_move_to_specific_position,
schema_get_current_pose,
schema_suction_on,
schema_suction_off,
schema_set_affine_matrix,
schema_move_robot_point_above,
schema_move_robot_point_block,
schema_update_scene_memory,
# CAMERA CAPTURE
schema_capture_scene_with_detection,
# PICK AND PLACE
schema_pick_and_place_block
]
)
# Names of tools that need working_directory injected
FILE_FUNCS = {"get_files_info", "get_file_content", "run_python_file", "write_file"}
def call_function(function_call_part, verbose=False):
if verbose:
print(
f" - Calling function: {function_call_part.name},({function_call_part.args})"
)
else:
print(f" - Calling function: {function_call_part.name}")
# Map tool names (from FunctionDeclaration.name) to real Python functions
function_map = {
# FILES
"get_files_info": get_files_info,
"get_file_content": get_file_content,
"run_python_file": run_python_file,
"write_file": write_file,
# ROBOT
"get_dobot_device": get_dobot_device,
"move_to_home": move_to_home,
"move_to_specific_position": move_to_specific_position,
"get_current_pose": get_current_pose,
"suction_on": suction_on,
"suction_off": suction_off,
"set_affine_matrix": set_affine_matrix,
"move_robot_point_above": move_robot_point_above,
"move_robot_point_block": move_robot_point_block,
"update_scene_memory": update_scene_memory,
# CAMERA CAPTURE
"capture_scene_with_detection": capture_scene_with_detection,
# PICK AND PLACE
"pick_and_place_block":pick_and_place_block
}
function_name = function_call_part.name
if function_name not in function_map:
# This is the error you were seeing
return types.Content(
role="tool",
parts=[
types.Part.from_function_response(
name=function_name,
response={"error": f"Unknown function: {function_name}"},
)
],
)
args = dict(function_call_part.args)
# Only add working_directory for file tools
if function_name in FILE_FUNCS:
args["working_directory"] = WORKING_DIR
# Call the actual Python function
function_result = function_map[function_name](**args)
# Wrap result back to the model as a tool response
return types.Content(
role="tool",
parts=[
types.Part.from_function_response(
name=function_name,
response={"result": function_result},
)
],
)