-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
94 lines (76 loc) · 3.01 KB
/
main.py
File metadata and controls
94 lines (76 loc) · 3.01 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
import os
from urllib import response
from dotenv import load_dotenv
from google import genai
from google.genai import types
import argparse
import config
from schemas import available_functions
from call_function import call_function
load_dotenv()
api_key = os.environ.get("GEMINI_API_KEY")
system_prompt = config.system_prompt
ai_model = config.ai_model
# If the environment variable wasn't found – i.e., if api_key is None – raise a RuntimeError with a helpful message.
if api_key is None:
raise RuntimeError("GEMINI_API_KEY environment variable not set.")
client = genai.Client(api_key=api_key)
# test the client by making a simple request to the Gemini API
def chat_with_gemini(chat_messages):
response = client.models.generate_content(
model=ai_model,
contents=chat_messages,
config=types.GenerateContentConfig(system_instruction=system_prompt,tools=[available_functions]),
)
return response
def usage_print(response):
if response.usage_metadata is None:
print("No usage metadata available.")
return
print("\nUSAGE:")
print("Prompt token count:", response.usage_metadata.prompt_token_count)
print("Candidates token count:", response.usage_metadata.candidates_token_count)
print("---------"*10)
def agent_loop(user_prompt, verbose=False, max_iterations=20):
messages = [
types.Content(
role="user",
parts=[types.Part(text=user_prompt)]
)
]
function_results = []
for iteration in range(max_iterations):
response = chat_with_gemini(messages)
if not response:
print("Error: No response from LLM")
break
if response.candidates:
for candidate in response.candidates:
messages.append(candidate.content)
if response.function_calls:
for function_call in response.function_calls:
if verbose:
print(f" - Calling function: {function_call.name}")
result_content = call_function(function_call, verbose=verbose)
function_results.append(result_content)
else:
print("\nFinal response:")
print(response.text)
return
# --- FIXED: correctly inject function results ---
if function_results:
parts_to_add = []
for result_content in function_results:
parts_to_add.extend(result_content.parts)
messages.append(types.Content(role="user", parts=parts_to_add))
function_results = []
def main():
parser = argparse.ArgumentParser(description="Chatbot Agent")
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()
MAX_ITERATIONS = 20 # safety stop
# Run the agent loop with the user's prompt
agent_loop(user_prompt=args.user_prompt, verbose=args.verbose, max_iterations=MAX_ITERATIONS)
if __name__ == "__main__":
main()