-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
123 lines (95 loc) · 3.7 KB
/
main.py
File metadata and controls
123 lines (95 loc) · 3.7 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
import os
import sys
from dotenv import load_dotenv
from google import genai
from google.genai import types
from call_function import *
from functions.get_files_info import schema_get_files_info
from functions.get_file_content import schema_get_file_content
from functions.write_file import schema_write_file
from functions.run_python_file import schema_run_python_file
load_dotenv()
api_key = os.environ.get("GEMINI_API_KEY")
client = genai.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.
"""
available_functions = types.Tool(
function_declarations=[
schema_get_files_info,
schema_get_file_content,
schema_write_file,
schema_run_python_file
]
)
def main():
verbose = False
user_prompt = "Why is Boot.dev such a great place to learn backend development? Use one paragraph maximum."
args = []
print('Hello from ai-agent!\n')
args = []
if len(sys.argv) >= 2:
verbose = "--verbose" in sys.argv
for arg in sys.argv[1:]:
if not arg.startswith("--"):
args.append(arg)
if args:
user_prompt = " ".join(args)
if not args:
print('\nUsage: python main.py "[insert your prompt]" [--verbose]')
print('Example: python main.py "How do I fix the calculator?"')
api_key = os.environ.get('GEMINI_API_KEY')
client = genai.Client(api_key=api_key)
print(f'User prompt: {user_prompt}\n')
print("Declared tools:", [fd.name for fd in available_functions.function_declarations])
messages = [
types.Content(role='user', parts=[types.Part(text=user_prompt)])
]
max_iterations = 20
current_iteration = 0
while True:
current_iteration += 1
if current_iteration > max_iterations:
print(f"Maximum iterations ({max_iterations}) reached.")
sys.exit(1)
try:
response = generate_content(client, messages, verbose)
if response.usage_metadata is None:
raise RuntimeError("Failed API request.")
for cand in response.candidates:
messages.append(cand.content)
if response.function_calls:
function_responses = []
for part in response.function_calls:
fr = call_function(part, verbose)
function_responses.append(fr.parts[0])
messages.append(types.Content(role="user", parts=function_responses))
continue
if response.text:
print(response.text)
break
except Exception as e:
print(f"Error: {e}")
# Fallback
print("Prompt tokens: 0")
print("Response tokens: 0")
break
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
),
)
print("Prompt tokens: ", response.usage_metadata.prompt_token_count)
print("Response tokens: ", response.usage_metadata.candidates_token_count)
return response
if __name__ == "__main__":
main()