Skip to content

Commit

Permalink
add sleep_per_last_token_model
Browse files Browse the repository at this point in the history
  • Loading branch information
Aki committed Mar 5, 2025
1 parent 84089bc commit 8cecb4e
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/smolagents/sleep_per_last_token_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import time
from typing import List, Optional, Dict

from .tools import Tool
from .models import ChatMessage, LiteLLMModel


class SleepPerLastTokenModelLiteLLM(LiteLLMModel):
def __init__(self, sleep_factor: float = 0.01, **kwargs):
super().__init__(**kwargs)
self.sleep_factor = sleep_factor

def __call__(
self,
messages: List[Dict[str, str]],
stop_sequences: Optional[List[str]] = None,
grammar: Optional[str] = None,
tools_to_call_from: Optional[List[Tool]] = None,
**kwargs,
) -> ChatMessage:
if self.last_input_token_count is not None:
sleep_time = (
self.last_input_token_count + self.last_output_token_count
) * self.sleep_factor
print(f"Sleeping for {sleep_time:.2f} seconds...")
time.sleep(sleep_time)

return super().__call__(
messages, stop_sequences, grammar, tools_to_call_from, **kwargs
)

0 comments on commit 8cecb4e

Please sign in to comment.