-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcall_function.py
More file actions
56 lines (44 loc) · 1.37 KB
/
call_function.py
File metadata and controls
56 lines (44 loc) · 1.37 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
import os
import sys
from dotenv import load_dotenv
from google import genai
from google.genai import types
from functions.get_files_info import get_files_info
from functions.get_file_content import get_file_content
from functions.write_file import write_file
from functions.run_python_file import run_python_file
FUNCTIONS = {
"get_files_info": get_files_info,
"get_file_content": get_file_content,
"write_file": write_file,
"run_python_file": run_python_file
}
def call_function(function_call_part, verbose=False):
fn_name = function_call_part.name
kwargs = dict(function_call_part.args)
kwargs["working_directory"] = "./calculator"
if verbose:
print(f"Calling function: {fn_name}({function_call_part.args})")
else:
print(f" - Calling function: {fn_name}")
fn = FUNCTIONS.get(fn_name)
if fn is None:
return types.Content(
role="tool",
parts=[
types.Part.from_function_response(
name=fn_name,
response={"error": f"Unknown function: {fn_name}"},
)
],
)
result = fn(**kwargs)
return types.Content(
role="tool",
parts=[
types.Part.from_function_response(
name=fn_name,
response={"result": result},
)
],
)