Skip to content

Commit d9a352f

Browse files
committed
Restored litellm token counter.
1 parent 2a389aa commit d9a352f

File tree

1 file changed

+4
-15
lines changed

1 file changed

+4
-15
lines changed

src/coverup/llm.py

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -82,23 +82,12 @@ def compute_cost(usage: dict, model_name: str) -> float | None:
8282
return None
8383

8484
_token_encoding_cache: dict[str, T.Any] = dict()
85+
8586
def count_tokens(model_name: str, completion: dict):
8687
"""Counts the number of tokens in a chat completion request."""
87-
import tiktoken
88-
89-
if "anthropic" in model_name:
90-
return 1
91-
92-
if not (encoding := _token_encoding_cache.get(model_name)):
93-
model = model_name
94-
if model_name.startswith('openai/'):
95-
model = model_name[7:]
96-
97-
encoding = _token_encoding_cache[model_name] = tiktoken.encoding_for_model(model)
88+
from litellm import token_counter
9889

99-
count = 0
100-
for m in completion['messages']:
101-
count += len(encoding.encode(m['content']))
90+
count = token_counter(model=model_name, messages=completion['messages'])
10291

10392
return count
10493

@@ -312,4 +301,4 @@ async def chat(self, messages: list, *, ctx: T.Optional[object] = None) -> dict
312301
})
313302

314303
self._log_msg(ctx, f"Too many function call requests, giving up")
315-
return None
304+
return None

0 commit comments

Comments
 (0)