-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcall_function.py
More file actions
61 lines (54 loc) · 2.23 KB
/
call_function.py
File metadata and controls
61 lines (54 loc) · 2.23 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
from functions.get_files_info import get_files_info, schema_get_files_info
from functions.get_file_content import get_file_content, schema_get_file_content
from functions.run_python import run_python_file, schema_run_python_file
from functions.write_file import write_file, schema_write_file
from google.genai import types
working_directory = "./calculator"
available_functions = types.Tool(
function_declarations=[
schema_get_files_info,
schema_get_file_content,
schema_run_python_file,
schema_write_file,
]
)
def call_function(function_call_part, verbose=False):
"""
Calls the appropriate function based on the function_call_part.name.
Args:
function_call_part: The function call part from the Gemini response.
verbose: Whether to print verbose output.
Returns:
A types.Content object containing the function response.
"""
func_dict = {
"get_files_info": lambda: get_files_info(working_directory=working_directory, **function_call_part.args),
"get_file_content": lambda: get_file_content(working_directory=working_directory, **function_call_part.args),
"run_python_file": lambda: run_python_file(working_directory=working_directory, **function_call_part.args),
"write_file": lambda: write_file(working_directory=working_directory, **function_call_part.args)
}
if verbose:
print(f"Calling function: {function_call_part.name}({function_call_part.args})")
else:
print(f" - Calling function: {function_call_part.name}")
if function_call_part.name in func_dict:
function_result = func_dict[function_call_part.name]()
return types.Content(
role="tool",
parts=[
types.Part.from_function_response(
name=function_call_part.name,
response={"result": function_result},
)
],
)
else:
return types.Content(
role="tool",
parts=[
types.Part.from_function_response(
name=function_call_part.name,
response={"error": f"Unknown function: {function_call_part.name}"},
)
],
)