Spaces:
Sleeping
Sleeping
Switch model backend from Groq to Cerebras
Browse filesGroq's free-tier TPM (6000-12000) was too small for this agent's per-call cost (2400-9000+ tokens). Cerebras free tier offers 30,000 TPM / 1,000,000 TPD - several times more headroom. Uses CEREBRAS_API_KEY, model cerebras/gpt-oss-120b.
app.py
CHANGED
|
@@ -46,11 +46,11 @@ class TokenPacer:
|
|
| 46 |
Step_callback that tracks actual token usage per step in a trailing 60s
|
| 47 |
window and sleeps as needed to stay under a tokens-per-minute budget.
|
| 48 |
Necessary because a single call's fixed overhead (system prompt + tool
|
| 49 |
-
schemas + question, before any tool output) already runs 2,400
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
"""
|
| 55 |
def __init__(self, tokens_per_minute_budget: int = 5000):
|
| 56 |
self.tokens_per_minute_budget = tokens_per_minute_budget
|
|
@@ -99,25 +99,21 @@ class MemoryTrimmer:
|
|
| 99 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 100 |
class BasicAgent:
|
| 101 |
def __init__(self):
|
| 102 |
-
model_id = os.getenv("AGENT_MODEL_ID", "
|
| 103 |
-
api_key = os.getenv("
|
| 104 |
if not api_key:
|
| 105 |
-
print("Warning:
|
| 106 |
-
|
| 107 |
-
#
|
| 108 |
-
#
|
| 109 |
-
#
|
| 110 |
-
#
|
| 111 |
-
#
|
| 112 |
-
# questions need to succeed here, so a call that still gets rate
|
| 113 |
-
# limited (e.g. on tokens/minute, which this doesn't track) should
|
| 114 |
-
# just fail fast - the per-question try/except below skips it and
|
| 115 |
-
# moves on, which is much cheaper than retry-storming.
|
| 116 |
self.model = LiteLLMModel(
|
| 117 |
model_id=model_id,
|
| 118 |
api_key=api_key,
|
| 119 |
temperature=0,
|
| 120 |
-
requests_per_minute=float(os.getenv("RATE_LIMIT_RPM", "
|
| 121 |
retry=False,
|
| 122 |
)
|
| 123 |
self.agent = CodeAgent(
|
|
@@ -129,7 +125,7 @@ class BasicAgent:
|
|
| 129 |
],
|
| 130 |
# add_base_tools=True would add a duplicate DuckDuckGo search tool -
|
| 131 |
# every tool's schema is baked into the system prompt on every call,
|
| 132 |
-
#
|
| 133 |
additional_authorized_imports=[
|
| 134 |
"pandas", "numpy", "math", "re", "json", "itertools",
|
| 135 |
"collections", "statistics", "datetime", "io", "openpyxl", "PIL",
|
|
@@ -137,7 +133,7 @@ class BasicAgent:
|
|
| 137 |
max_steps=7, # keep runs short: growing history otherwise blows the TPM budget by step ~6
|
| 138 |
step_callbacks=[
|
| 139 |
MemoryTrimmer(),
|
| 140 |
-
TokenPacer(tokens_per_minute_budget=int(os.getenv("RATE_LIMIT_TOKENS_PER_MINUTE", "
|
| 141 |
],
|
| 142 |
)
|
| 143 |
print("BasicAgent initialized.")
|
|
@@ -311,7 +307,7 @@ with gr.Blocks() as demo:
|
|
| 311 |
Once clicking on the "submit button, it can take quite some time ( this is the time for the agent to go through all the questions).
|
| 312 |
This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution. For instance for the delay process of the submit button, a solution could be to cache the answers and submit in a seperate action or even to answer the questions in async.
|
| 313 |
|
| 314 |
-
**Setup:** This agent calls
|
| 315 |
"""
|
| 316 |
)
|
| 317 |
|
|
|
|
| 46 |
Step_callback that tracks actual token usage per step in a trailing 60s
|
| 47 |
window and sleeps as needed to stay under a tokens-per-minute budget.
|
| 48 |
Necessary because a single call's fixed overhead (system prompt + tool
|
| 49 |
+
schemas + question, before any tool output) already runs 2,400+ tokens
|
| 50 |
+
on this agent and grows with context - smolagents' native
|
| 51 |
+
requests_per_minute throttle can't account for that since it only paces
|
| 52 |
+
call count, not size. This is a secondary safeguard: Cerebras' free-tier
|
| 53 |
+
TPM budget (30,000) is generous enough that this should rarely trigger.
|
| 54 |
"""
|
| 55 |
def __init__(self, tokens_per_minute_budget: int = 5000):
|
| 56 |
self.tokens_per_minute_budget = tokens_per_minute_budget
|
|
|
|
| 99 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 100 |
class BasicAgent:
|
| 101 |
def __init__(self):
|
| 102 |
+
model_id = os.getenv("AGENT_MODEL_ID", "cerebras/gpt-oss-120b")
|
| 103 |
+
api_key = os.getenv("CEREBRAS_API_KEY")
|
| 104 |
if not api_key:
|
| 105 |
+
print("Warning: CEREBRAS_API_KEY is not set - the agent will fail to call the model.")
|
| 106 |
+
|
| 107 |
+
# Cerebras' free tier: 5 RPM, 30,000 TPM, 1,000,000 TPD - far more
|
| 108 |
+
# TPM headroom than Groq's free tier (6000-12000), which was the
|
| 109 |
+
# actual bottleneck there. requests_per_minute paces call frequency
|
| 110 |
+
# natively; retry=False avoids smolagents' internal retry-on-error
|
| 111 |
+
# firing extra invisible HTTP calls on a rate-limited response.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
self.model = LiteLLMModel(
|
| 113 |
model_id=model_id,
|
| 114 |
api_key=api_key,
|
| 115 |
temperature=0,
|
| 116 |
+
requests_per_minute=float(os.getenv("RATE_LIMIT_RPM", "4.5")),
|
| 117 |
retry=False,
|
| 118 |
)
|
| 119 |
self.agent = CodeAgent(
|
|
|
|
| 125 |
],
|
| 126 |
# add_base_tools=True would add a duplicate DuckDuckGo search tool -
|
| 127 |
# every tool's schema is baked into the system prompt on every call,
|
| 128 |
+
# keeping that overhead small helps stay well under the TPM budget.
|
| 129 |
additional_authorized_imports=[
|
| 130 |
"pandas", "numpy", "math", "re", "json", "itertools",
|
| 131 |
"collections", "statistics", "datetime", "io", "openpyxl", "PIL",
|
|
|
|
| 133 |
max_steps=7, # keep runs short: growing history otherwise blows the TPM budget by step ~6
|
| 134 |
step_callbacks=[
|
| 135 |
MemoryTrimmer(),
|
| 136 |
+
TokenPacer(tokens_per_minute_budget=int(os.getenv("RATE_LIMIT_TOKENS_PER_MINUTE", "25000"))),
|
| 137 |
],
|
| 138 |
)
|
| 139 |
print("BasicAgent initialized.")
|
|
|
|
| 307 |
Once clicking on the "submit button, it can take quite some time ( this is the time for the agent to go through all the questions).
|
| 308 |
This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution. For instance for the delay process of the submit button, a solution could be to cache the answers and submit in a seperate action or even to answer the questions in async.
|
| 309 |
|
| 310 |
+
**Setup:** This agent calls Cerebras (gpt-oss-120b) via `smolagents`. Get a free key at https://cloud.cerebras.ai and set it as the `CEREBRAS_API_KEY` secret in this Space's settings before running.
|
| 311 |
"""
|
| 312 |
)
|
| 313 |
|