-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
193 lines (169 loc) · 6.11 KB
/
Copy pathmain.py
File metadata and controls
193 lines (169 loc) · 6.11 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import os
import sys
from dotenv import load_dotenv
from google.genai import types, Client
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
MAX_ITER = 20
schema_get_files_info = types.FunctionDeclaration(
name="get_files_info",
description="Lists files in the specified directory along with their sizes, constrained to the working directory.",
parameters=types.Schema(
type=types.Type.OBJECT,
properties={
"directory": types.Schema(
type=types.Type.STRING,
description="The directory to list files from, relative to the working directory. If not provided, lists files in the working directory itself.",
),
},
),
)
schema_get_file_content = types.FunctionDeclaration(
name="get_file_content",
description="Read the contents of a given file constrained to the working directory.",
parameters=types.Schema(
type=types.Type.OBJECT,
properties={
"file_path": types.Schema(
type=types.Type.STRING,
description="The name of the file within the given directory.",
),
},
),
)
schema_run_python_file = types.FunctionDeclaration(
name="run_python_file",
description="Runs a given Python script while optionally taking in any arguments",
parameters=types.Schema(
type=types.Type.OBJECT,
properties={
"file_path": types.Schema(
type=types.Type.STRING,
description="The name of the python file within the given directory.",
),
"args": types.Schema(
type=types.Type.ARRAY,
description="Additional arguments for the python file. (optional)",
items=types.Schema(
type=types.Type.STRING
)
),
},
),
)
schema_write_file = types.FunctionDeclaration(
name="write_file",
description="Writes or overwrites the content of the given file with the provided content.",
parameters=types.Schema(
type=types.Type.OBJECT,
properties={
"file_path": types.Schema(
type=types.Type.STRING,
description="The name of the file within the given directory. If absent, the file is created.",
),
"content": types.Schema(
type=types.Type.STRING,
description="The content to be written in the file."
),
},
),
)
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):
if verbose:
print(f"Calling function: {function_call_part.name}({function_call_part.args})")
else:
print(f" - Calling function: {function_call_part.name}")
# Build a dictionary that contains the functions and their names
func_dict = {
"get_file_content": get_file_content,
"get_files_info": get_files_info,
"write_file": write_file,
"run_python_file": run_python_file
}
if function_call_part.name not in func_dict:
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 = func_dict[function_call_part.name]
result = func("./calculator", **function_call_part.args)
return types.Content(
role="tool",
parts=[
types.Part.from_function_response(
name=function_call_part.name,
response={"result": result},
)
],
)
def generate_content(client, messages, verbose):
response = client.models.generate_content(
model='gemini-2.0-flash-001', contents=messages,
config=types.GenerateContentConfig(
tools=[available_functions], system_instruction=system_prompt
)
)
for candidate in response.candidates:
messages.append(candidate.content)
if not response.function_calls:
return response.text
function_responses = []
for function_call_part in response.function_calls:
result = call_function(function_call_part, verbose=verbose)
try:
if verbose:
print(f"-> {result.parts[0].function_response.response}")
function_responses.append(result.parts[0])
except:
raise Exception("Something's wrong...")
if not function_responses:
raise Exception("No function responses, exiting")
messages.append(types.Content(role="tool", parts=function_responses))
if len(sys.argv) == 1:
print("Add a prompt, please")
sys.exit(1)
load_dotenv()
api_key = os.environ.get("GEMINI_API_KEY")
client = Client(api_key=api_key)
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.
"""
messages = [
types.Content(role="user", parts=[types.Part(text=sys.argv[1])])
]
verbose = False
if len(sys.argv) == 3:
if sys.argv[2] == "--verbose":
verbose = True
for i in range(0,MAX_ITER):
try:
result = generate_content(client, messages, verbose)
if result:
print(f"Final Response: {result}")
break
except Exception as e:
print(f"Error in generate_content: {e}")
if i == MAX_ITER-1:
print("Max Iterations reached. Exiting...")
sys.exit(1)