-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcall_function.py
More file actions
114 lines (103 loc) · 3.85 KB
/
Copy pathcall_function.py
File metadata and controls
114 lines (103 loc) · 3.85 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
from typing import Type
from google.genai import types
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
from functions.get_file_content import get_file_content
from functions.get_files_info import schema_get_files_info
def call_function(function_call, verbose=False):
# The function_call argument will be a types.FunctionCall object that has:
# A name property (the name of the function, a string)
# An args property (a dict of named arguments to the function)
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,
"write_file": write_file,
"run_python_file": run_python_file,
}
fn_name = function_call.name or ""
if fn_name not in function_map:
return types.Content(
role="tool",
parts=[
types.Part.from_function_response(
name=fn_name,
response={"error": f"Unknown function: {fn_name}"},
)
],
)
args = dict(function_call.args) if function_call.args else {}
args["working_directory"] = "./calculator"
fn_result = function_map[fn_name](**args)
return types.Content(
role="tool",
parts=[
types.Part.from_function_response(
name=fn_name,
response={"result": fn_result},
)
],
)
schema_get_file_content = types.FunctionDeclaration(
name="get_file_content",
description="Get first 10000 characters of a file in a specified path relative to the working directory.",
parameters=types.Schema(
type=types.Type.OBJECT,
properties={
"file_path": types.Schema(
type=types.Type.STRING,
description="Path to file, relative to the working directory (default is the working directory itself)",
),
},
required=["file_path"],
),
)
schema_run_python_file = types.FunctionDeclaration(
name="run_python_file",
description="Run a .py file in a specified path relative to the working directory with optional arguments.",
parameters=types.Schema(
type=types.Type.OBJECT,
properties={
"file_path": types.Schema(
type=types.Type.STRING,
description="Path to file, relative to the working directory (default is the working directory itself)",
),
"args": types.Schema(
type=types.Type.ARRAY,
items=types.Schema(type=types.Type.STRING),
description="Optional list of strings containing the command-line arguments to pass to the .py file.",
),
},
required=["file_path"],
),
)
schema_write_file = types.FunctionDeclaration(
name="write_file",
description="Write content to a file in a specified path relative to the working directory. Overwrites previous contents.",
parameters=types.Schema(
type=types.Type.OBJECT,
properties={
"file_path": types.Schema(
type=types.Type.STRING,
description="Path to file, relative to the working directory (default is the working directory itself)",
),
"content": types.Schema(
type=types.Type.STRING,
description="Content that shall be written to the file.",
),
},
required=["file_path", "content"],
),
)
available_functions = types.Tool(
function_declarations=[
schema_get_files_info,
schema_get_file_content,
schema_write_file,
schema_run_python_file,
],
)