-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
79 lines (63 loc) · 2.53 KB
/
main.py
File metadata and controls
79 lines (63 loc) · 2.53 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
import os
from dotenv import load_dotenv
from google import genai
from google.genai import types
from argparse import ArgumentParser
from prompts import system_prompt
from call_functions import available_functions, call_function
from config import MAX_ITERS
parser = ArgumentParser()
parser.add_argument("user_prompt", type=str, help="User prompt")
parser.add_argument("--verbose", action="store_true", help="Enable verbose output")
args = parser.parse_args()
load_dotenv()
api_key = os.environ.get("GEMINI_API_KEY")
client = genai.Client(api_key=api_key)
model = "gemini-2.5-flash"
contents = args.user_prompt
messages = [types.Content(role="user", parts=[types.Part(text=args.user_prompt)])]
response = None
for _ in range(MAX_ITERS):
response = client.models.generate_content(
model=model,
contents=messages,
config=types.GenerateContentConfig(
system_instruction=system_prompt,
temperature=0,
tools=[available_functions],
),
)
if response.candidates and response.candidates[0].content:
messages.append(response.candidates[0].content)
function_calls = response.function_calls
function_results = []
if function_calls is not None:
for call in function_calls:
function_call_result = call_function(call)
if not function_call_result.parts:
raise Exception("Can't call a function.")
if not function_call_result.parts[0].function_response:
raise Exception("There are no function response.")
function_results.append(function_call_result.parts[0])
if args.verbose:
print("User prompt:", contents)
if response.usage_metadata:
prompt_token = response.usage_metadata.prompt_token_count
response_token = response.usage_metadata.candidates_token_count
print(f"Prompt tokens: {prompt_token}")
print(f"Response tokens: {response_token}")
print(
f"-> {function_call_result.parts[0].function_response.response}"
)
else:
raise RuntimeError("Usage metadata not found")
messages.append(types.Content(role="user", parts=function_results))
else:
print("Response:")
print(response.text)
break
if not response:
print(
f"Error: reached {MAX_ITERS} iterations, but the model never produced a final response."
)
raise SystemExit(1)