-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
179 lines (143 loc) · 5.75 KB
/
main.py
File metadata and controls
179 lines (143 loc) · 5.75 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
import os
import sys
from dotenv import load_dotenv
from google import genai
from google.genai import types
from functions import get_files_info, get_file_content, run_python_file, write_file
from functions.get_files_info import (
schema_get_files_info,
schema_get_file_content,
schema_write_file,
schema_run_python_file,
)
from configs import WORKING_DIR, MAX_ITERATIONS
def call_function(function_call_part, verbose=False):
if verbose is True:
print(f"Calling function: {function_call_part.name}({function_call_part.args})")
else:
print(f" - Calling function: {function_call_part.name}")
function_names_dict = {
"get_files_info": get_files_info.get_files_info,
"write_file": write_file.write_file,
"get_file_content": get_file_content.get_file_content,
"run_python_file": run_python_file.run_python_file,
}
function_name = function_call_part.name
if function_name not in function_names_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}"},
)
],
)
args = dict(function_call_part.args)
args["working_directory"] = WORKING_DIR
result = function_names_dict[function_name](**args)
return types.Content(
role="tool",
parts=[
types.Part.from_function_response(
name=function_name,
response={"result": result},
)
],
)
def main():
# Load env variables
load_dotenv()
api_key = os.environ.get("GEMINI_API_KEY")
verbose = "--verbose" in sys.argv
print(sys.argv)
args = []
for arg in sys.argv[1:]:
if not arg.startswith("--"):
args.append(arg)
if not args:
print("AI Code Assistant")
print('\nUsage: python main.py "your prompt here" [--verbose]')
print('Example: python main.py "How do I fix the calculator?"')
sys.exit(1)
user_prompt = " ".join(args)
if verbose:
print(f"User prompt: {user_prompt}\n")
# Create a new list of types.Content, and set the user's prompt as the only message (for now):
# We will use it later to keep track of the converstion between the user and the LLM
messages = [
types.Content(role="user", parts=[types.Part(text=user_prompt)]),
]
# Create an instance of the Gemini Client
client = genai.Client(api_key=api_key)
# We now need to use a model's method to get response from the gemini-2.0-flash-001 model
# We need teo name parameters: model and contents
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.
"""
model_name = "gemini-2.0-flash-001"
available_functions = types.Tool(
function_declarations=[
schema_get_files_info,
schema_run_python_file,
schema_get_file_content,
schema_write_file,
]
)
iteration = 0
while True:
iteration += 1
if iteration > MAX_ITERATIONS:
print(f"Maximum iterations ({MAX_ITERATIONS}) reached.")
sys.exit(1)
try:
#
# generation starts here #
#
generator = client.models.generate_content(
model=model_name,
contents=messages,
config=types.GenerateContentConfig(
tools=[available_functions], system_instruction=system_prompt
),
)
metadata = generator.usage_metadata
if verbose:
print("Prompt tokens:", metadata.prompt_token_count if metadata else 0)
print(
"Response tokens:",
metadata.candidates_token_count if metadata else 0,
)
# if not generator.function_calls:
# return generator.text
responses = []
if generator.function_calls:
for function_call in generator.function_calls:
result = call_function(function_call_part=function_call)
if not result.parts or not result.parts[0].function_response:
raise Exception("empty function call result")
if verbose:
print(f"-> {result.parts[0].function_response.response}")
# keep the result for future use by the LLM
responses.append(result.parts[0])
if generator.text:
print("Final response:")
print(generator.text)
# Using the walrus operator to assign generator.candidates to candidates only if it exists and is truthy
if candidates := getattr(generator, "candidates", None):
for candidate in candidates:
# adding the candidate content to the messages list
messages.append(candidate.content)
if not responses:
raise Exception("no function responses generated, exiting.")
messages.append(types.Content(role="tool", parts=responses))
except Exception as e:
return f"Error: {e} "
if __name__ == "__main__":
main()