agent loop is ready

This commit is contained in:
2026-03-30 20:52:19 +02:00
parent b3d068edf9
commit 4cda5f0e75
2 changed files with 88 additions and 26 deletions
+45 -21
View File
@@ -1,11 +1,12 @@
import argparse
import os
import sys
from dotenv import load_dotenv
from google import genai
from google.genai import types
from prompts import system_prompt
from functions.call_function import available_functions
from functions.call_function import available_functions, call_function
load_dotenv()
api_key = os.environ.get("GEMINI_API_KEY")
@@ -19,25 +20,48 @@ parser.add_argument("--verbose", action="store_true", help="Enable verbose outpu
args = parser.parse_args()
messages = [types.Content(role="user", parts=[types.Part(text=args.user_prompt)])]
response = client.models.generate_content(
model="gemini-2.5-flash",
contents=messages,
config=types.GenerateContentConfig(
system_instruction=system_prompt,
tools=[available_functions],
),
)
if not response.usage_metadata:
raise RuntimeError("Cannot get usage metadata")
for _ in range(10):
response = client.models.generate_content(
model="gemini-2.5-flash",
contents=messages,
config=types.GenerateContentConfig(
system_instruction=system_prompt,
tools=[available_functions],
),
)
if response.candidates:
for candidate in response.candidates:
if candidate.content:
messages.append(candidate.content)
if args.verbose:
print(f"User prompt: {args.user_prompt}")
print(f"Prompt tokens: {response.usage_metadata.prompt_token_count}")
print(f"Response tokens: {response.usage_metadata.candidates_token_count}")
function_calls = response.function_calls
if function_calls:
for function_call in function_calls:
print(f"Calling function: {function_call.name}({function_call.args})")
if not response.usage_metadata:
raise RuntimeError("Cannot get usage metadata")
if args.verbose:
print(f"User prompt: {args.user_prompt}")
print(f"Prompt tokens: {response.usage_metadata.prompt_token_count}")
print(f"Response tokens: {response.usage_metadata.candidates_token_count}")
function_calls = response.function_calls
function_responses = []
if function_calls:
for function_call in function_calls:
function_call_result = call_function(function_call, args.verbose)
if not function_call_result.parts:
raise Exception("function_call_result.parts list is empty")
if function_call_result.parts[0].function_response is None:
raise Exception("FunctionResponse object is equal to None")
if function_call_result.parts[0].function_response.response is None:
raise Exception("FunctionResponse object response is equal to None")
function_responses.append(function_call_result.parts[0])
if args.verbose:
print(f"-> {function_call_result.parts[0].function_response.response}")
messages.append(types.Content(role="user", parts=function_responses))
else:
break
else:
print(response.text)
print("Maximum iterations reached without a final response")
sys.exit(1)