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
144
145
146
147
148
149
150
151
152
153import os
import sys
from dotenv import load_dotenv
from google import genai
from google.genai import types
from functions.get_files_info import schema_get_files_info, get_files_info
from functions.get_file_content import schema_get_file_content, get_file_content
from functions.write_file import schema_write_file, write_file
from functions.run_python_file import schema_run_python_file, run_python_file
load_dotenv()
available_functions = types.Tool(
function_declarations=[
schema_get_files_info,
schema_get_file_content,
schema_write_file,
schema_run_python_file
]
)
system_prompt = """
You are a helpful AI coding agent.
When a user asks a question or makes a request, make a function call plan. You can perform the following operations:
- List files and directories
- Read file contents
- Execute Python files with optional arguments
- Write or overwrite files
All paths you provide should be relative to the working directory. You do not need to specify the working directory in your function calls as it is automatically injected for security reasons.
"""
def call_function(function_call_part, verbose=False):
functions = {
"get_files_info": get_files_info,
"get_file_content": get_file_content,
"write_file": write_file,
"run_python_file": run_python_file
}
if verbose:
print(f"Calling function: {function_call_part.name}({function_call_part.args})")
else:
print(f" - Calling function: {function_call_part.name}")
working_directory = "./calculator"
func = functions.get(function_call_part.name)
if not func:
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}"},
)
],
)
func_result = func(working_directory=working_directory, **function_call_part.args)
return types.Content(
role="tool",
parts=[
types.Part.from_function_response(
name=function_call_part.name,
response={"result": func_result},
)
],
)
def generate_content(client, messages, verbose):
response = client.models.generate_content(
model="gemini-2.0-flash-001",
contents=messages,
config=types.GenerateContentConfig(
system_instruction=system_prompt,
tools=[available_functions]
)
)
if verbose:
print(f"Prompt tokens: {response.usage_metadata.prompt_token_count}")
print(f"Response tokens: {response.usage_metadata.candidates_token_count}")
print()
if response.candidates:
for candidate in response.candidates:
function_call_content = candidate.content
messages.append(function_call_content)
if not response.function_calls:
return response.text
function_responses = []
for function_call_part in response.function_calls:
function_call_result = call_function(function_call_part, verbose=verbose)
if not function_call_result.parts or not function_call_result.parts[0].function_response.response:
raise Exception("A fatal error happened")
if verbose:
print(f"-> {function_call_result.parts[0].function_response.response}")
function_responses.append(function_call_result.parts[0])
messages.append(types.Content(role="user", parts=function_responses))
def main():
api_key = os.environ.get("GEMINI_API_KEY")
client = genai.Client(api_key=api_key)
try:
user_prompt = sys.argv[1]
except IndexError:
print("The prompt was not provided.")
sys.exit(1)
try:
verbose = sys.argv[2] == "--verbose"
except IndexError:
verbose = False
messages = [
types.Content(role="user", parts=[types.Part(text=user_prompt)])
]
iters = 0
while True:
iters += 1
if iters > 20:
print(f"Maximum iterations ({20}) reached.")
sys.exit(1)
try:
final_response = generate_content(client, messages, verbose)
if final_response:
print("Final response:")
print(final_response)
break
except Exception as e:
print(f"Error in generate_content: {e}")
if __name__ == "__main__":
main()