keylazy commited on
Commit
e188063
·
verified ·
1 Parent(s): 889cfdb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -1
app.py CHANGED
@@ -12,11 +12,42 @@ from tools.catering_service_tool import SimpleTool as CateringServiceTool
12
  from tools.superhero_party_theme_generator import SuperheroPartyThemeTool as SuperheroPartyThemeGenerator
13
  from tools.final_answer import FinalAnswerTool as FinalAnswer
14
 
 
 
 
 
 
 
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
  model = CountedLiteLLMModel(
18
  model_id='claude-3-5-sonnet-latest',
19
- api_base=None,
20
  )
21
 
22
  web_search = WebSearch()
 
12
  from tools.superhero_party_theme_generator import SuperheroPartyThemeTool as SuperheroPartyThemeGenerator
13
  from tools.final_answer import FinalAnswerTool as FinalAnswer
14
 
15
+ class CountedLiteLLMModel(LiteLLMModel):
16
+ def __init__(self, *args, **kwargs):
17
+ super().__init__(*args, **kwargs)
18
+ # ensure attrs exist (instrumentor will read them)
19
+ self.last_input_token_count = 0
20
+ self.last_output_token_count = 0
21
 
22
+ def generate(self, *args, **kwargs):
23
+ # call as usual
24
+ out = super().generate(*args, **kwargs)
25
+
26
+ # try to read usage from the last response if LiteLLM provided it
27
+ usage = getattr(self, "last_response_usage", None)
28
+ # some smolagents versions store usage on the response object instead:
29
+ if usage is None:
30
+ usage = getattr(out, "usage", None)
31
+
32
+ if usage:
33
+ # common OpenAI-style keys; adjust if your provider uses different names
34
+ self.last_input_token_count = \
35
+ usage.get("prompt_tokens") or usage.get("input_tokens") or 0
36
+ self.last_output_token_count = \
37
+ usage.get("completion_tokens") or usage.get("output_tokens") or 0
38
+ else:
39
+ # fallback: estimate with LiteLLM's token_counter to avoid zeros
40
+ prompt = kwargs.get("prompt") or (args[0] if args else "")
41
+ try:
42
+ self.last_input_token_count = token_counter(model=self.model_id, text=prompt) or 0
43
+ except Exception:
44
+ pass # leave prior value if estimation fails
45
+
46
+ return out
47
 
48
  model = CountedLiteLLMModel(
49
  model_id='claude-3-5-sonnet-latest',
50
+ api_key=os.getenv('ANTHROPIC_API_KEY'),
51
  )
52
 
53
  web_search = WebSearch()