Commit ·
72d31a2
1
Parent(s): d02a7bb
token_tracking_logic
Browse files
agent.py
CHANGED
|
@@ -21,6 +21,9 @@ from smolagents import (
|
|
| 21 |
FinalAnswerTool,
|
| 22 |
)
|
| 23 |
|
|
|
|
|
|
|
|
|
|
| 24 |
# Import your custom tools (to be used in app, not in local notebook)
|
| 25 |
from tools.gemini_native_tools import analyze_video, analyze_image, analyze_audio
|
| 26 |
from tools.download_file import download_file_from_url
|
|
@@ -183,12 +186,20 @@ class GeminiAgent:
|
|
| 183 |
model = self.model,
|
| 184 |
tools = self.tools,
|
| 185 |
add_base_tools = True, # probably redundant, but it does not hurt
|
| 186 |
-
max_steps =
|
| 187 |
additional_authorized_imports = AUTHORIZED_IMPORTS,
|
| 188 |
verbosity_level = 1,
|
| 189 |
-
max_print_outputs_length=
|
| 190 |
)
|
| 191 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
print(f"✅ Gemini agent initialized with model: {model_id}")
|
| 193 |
|
| 194 |
def __call__(self, question: str, file_path: Optional[str] = None) -> str:
|
|
|
|
| 21 |
FinalAnswerTool,
|
| 22 |
)
|
| 23 |
|
| 24 |
+
# LiteLLM
|
| 25 |
+
from litellm import token_counter
|
| 26 |
+
|
| 27 |
# Import your custom tools (to be used in app, not in local notebook)
|
| 28 |
from tools.gemini_native_tools import analyze_video, analyze_image, analyze_audio
|
| 29 |
from tools.download_file import download_file_from_url
|
|
|
|
| 186 |
model = self.model,
|
| 187 |
tools = self.tools,
|
| 188 |
add_base_tools = True, # probably redundant, but it does not hurt
|
| 189 |
+
max_steps = 4,
|
| 190 |
additional_authorized_imports = AUTHORIZED_IMPORTS,
|
| 191 |
verbosity_level = 1,
|
| 192 |
+
max_print_outputs_length=2_000
|
| 193 |
)
|
| 194 |
|
| 195 |
+
def check_token_safety(self, question):
|
| 196 |
+
messages = self.gemini_agent.memory.get_messages()
|
| 197 |
+
count = token_counter(model="gemma3", messages=messages)
|
| 198 |
+
|
| 199 |
+
print(f"📊 Current Context Size: {count} tokens")
|
| 200 |
+
return count
|
| 201 |
+
|
| 202 |
+
|
| 203 |
print(f"✅ Gemini agent initialized with model: {model_id}")
|
| 204 |
|
| 205 |
def __call__(self, question: str, file_path: Optional[str] = None) -> str:
|
app.py
CHANGED
|
@@ -110,6 +110,11 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 110 |
# 3. Run your Agent
|
| 111 |
results_log = []
|
| 112 |
answers_payload = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
print(f"Running agent on {len(questions_data)} questions...")
|
| 114 |
for item in questions_data:
|
| 115 |
# --- Check the "Kill Switch" at the start of every loop iteration ---
|
|
@@ -133,6 +138,14 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 133 |
continue
|
| 134 |
|
| 135 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
if agent_type == "GeminiAgent" and request_count >= MAX_TOTAL_REQUESTS:
|
| 137 |
raise RateLimitError("Global request cap reached")
|
| 138 |
|
|
|
|
| 110 |
# 3. Run your Agent
|
| 111 |
results_log = []
|
| 112 |
answers_payload = []
|
| 113 |
+
|
| 114 |
+
# track tokens used in the LAST 60 seconds
|
| 115 |
+
tokens_used_this_minute = 0
|
| 116 |
+
TPM_LIMIT = 14000 # Keep a 1k safety buffer
|
| 117 |
+
|
| 118 |
print(f"Running agent on {len(questions_data)} questions...")
|
| 119 |
for item in questions_data:
|
| 120 |
# --- Check the "Kill Switch" at the start of every loop iteration ---
|
|
|
|
| 138 |
continue
|
| 139 |
|
| 140 |
try:
|
| 141 |
+
current_tokens = agent.check_token_safety(question_text)
|
| 142 |
+
if (tokens_used_this_minute + current_tokens) > TPM_LIMIT:
|
| 143 |
+
print(f"⏳TPM limit imminent ({tokens_used_this_minute}/{TPM_LIMIT}) Cooling down...")
|
| 144 |
+
time.sleep(60)
|
| 145 |
+
tokens_used_this_minute = 0 # Reset
|
| 146 |
+
last_usage = agent.model.last_input_token_count + agent.model.last_output_token_count
|
| 147 |
+
tokens_used_this_minute += last_usage
|
| 148 |
+
|
| 149 |
if agent_type == "GeminiAgent" and request_count >= MAX_TOTAL_REQUESTS:
|
| 150 |
raise RateLimitError("Global request cap reached")
|
| 151 |
|