-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcall_functions.py
More file actions
143 lines (128 loc) · 4.93 KB
/
call_functions.py
File metadata and controls
143 lines (128 loc) · 4.93 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
from token import STRING
from google.genai import types
from functions.get_file_content import get_file_content
from functions.get_files_info import get_files_info
from functions.write_file import write_file
from functions.run_python_file import run_python_file
schema_get_files_info = types.FunctionDeclaration(
name="get_files_info",
description="Lists files in a specified directory relative to the working directory, providing file size and directory status",
parameters=types.Schema(
type=types.Type.OBJECT,
properties={
"directory": types.Schema(
type=types.Type.STRING,
description="Directory path to list files from, relative to the working directory (default is the working directory itself)",
),
},
required=["directory"],
),
)
schema_get_file_content = types.FunctionDeclaration(
name="get_file_content",
description="Retrieves the file content in a specified directory relative to the working directory.",
parameters=types.Schema(
type=types.Type.OBJECT,
properties={
"directory": types.Schema(
type=types.Type.STRING,
description="Directory path to look file from, relative to the working directory (default is the working directory itself)",
),
"file_path": types.Schema(
type=types.Type.STRING,
description="File path of the file to read its contents, relative to the working directory.",
),
},
required=["file_path"],
),
)
schema_run_python_files = types.FunctionDeclaration(
name="run_python_file",
description="Get the python file in a specified directory relative to the working directory and executes it.",
parameters=types.Schema(
type=types.Type.OBJECT,
properties={
"directory": types.Schema(
type=types.Type.STRING,
description="Directory path to look file from, relative to the working directory (default is the working directory itself)",
),
"file_path": types.Schema(
type=types.Type.STRING,
description="File path of the Python script to run, relative to the working directory.",
),
"args": types.Schema(
type=types.Type.ARRAY,
description="Optional list of command-line arguments to be pass on the running Python script.",
items=types.Schema(
type=types.Type.STRING,
description="List of command-line arguments.",
),
),
},
required=["file_path"],
),
)
schema_write_file = types.FunctionDeclaration(
name="write_file",
description="Write a new file or overwrite existing file in a specified directory relative to the working directory.",
parameters=types.Schema(
type=types.Type.OBJECT,
properties={
"directory": types.Schema(
type=types.Type.STRING,
description="Directory path to look file from, relative to the working directory (default is the working directory itself)",
),
"file_path": types.Schema(
type=types.Type.STRING,
description="File path of the file to write on, relative to the working directory.",
),
"content": types.Schema(
type=types.Type.STRING,
description="Content of the file to be written on the file.",
),
},
required=["file_path", "content"],
),
)
available_functions = types.Tool(
function_declarations=[
schema_get_files_info,
schema_get_file_content,
schema_run_python_files,
schema_write_file,
],
)
def call_function(function_call, verbose=False):
if verbose:
print(f"Calling function: {function_call.name}({function_call.args})")
else:
print(f" - Calling function: {function_call.name}")
function_map = {
"get_file_content": get_file_content,
"get_files_info": get_files_info,
"run_python_file": run_python_file,
"write_file": write_file,
}
function_name = function_call.name or ""
if not function_name:
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.args) if function_call.args else {}
args["working_directory"] = "./calculator"
function_result = function_map[function_name](**args)
return types.Content(
role="tool",
parts=[
types.Part.from_function_response(
name=function_name,
response={"result": function_result},
)
],
)