keylazy commited on
Commit
7ddcd74
·
verified ·
1 Parent(s): 97c9e32

Update app.py

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