Closed
Conversation
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR integrates a new count_tokens helper to replace rough len(...split()) and division-based token estimates across all OpenAI provider implementations.
- Imported
count_tokensin each provider module - Replaced imprecise
len(...).split()or// 4token estimations withcount_tokens(...) - Ensured both prompt and completion token counts use the shared utility
Reviewed Changes
Copilot reviewed 18 out of 18 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| webscout/Provider/OPENAI/wisecat.py | Imported count_tokens and updated token logic |
| webscout/Provider/OPENAI/venice.py | Imported count_tokens and updated token logic |
| webscout/Provider/OPENAI/typegpt.py | Imported count_tokens and updated token logic |
| webscout/Provider/OPENAI/typefully.py | Imported count_tokens and updated token logic |
| webscout/Provider/OPENAI/textpollinations.py | Imported count_tokens and updated token logic |
| webscout/Provider/OPENAI/standardinput.py | Imported count_tokens and updated token logic |
| webscout/Provider/OPENAI/scirachat.py | Imported count_tokens and updated token logic |
| webscout/Provider/OPENAI/netwrck.py | Imported count_tokens and updated token logic |
| webscout/Provider/OPENAI/heckai.py | Imported count_tokens and updated token logic |
| webscout/Provider/OPENAI/exachat.py | Imported count_tokens and updated token logic |
| webscout/Provider/OPENAI/exaai.py | Imported count_tokens and updated token logic |
| webscout/Provider/OPENAI/chatgptclone.py | Imported count_tokens and updated token logic |
| webscout/Provider/OPENAI/chatgpt.py | Imported count_tokens and updated token logic |
| webscout/Provider/OPENAI/c4ai.py | Imported count_tokens and updated token logic |
| webscout/Provider/OPENAI/ai4chat.py | Imported count_tokens and updated token logic |
| webscout/Provider/OPENAI/FreeGemini.py | Imported count_tokens and updated token logic |
| webscout/Provider/OPENAI/Cloudflare.py | Imported count_tokens and updated token logic |
| webscout/Provider/OPENAI/BLACKBOXAI.py | Imported count_tokens and updated token logic |
Comments suppressed due to low confidence (2)
webscout/Provider/OPENAI/wisecat.py:12
- The token counting logic is now duplicated across every provider module. Consider moving the prompt/completion token accumulation into a shared method on the base provider class to avoid repetition and simplify future updates.
from .utils import ( ... ChatCompletionMessage, CompletionUsage, count_tokens
webscout/Provider/OPENAI/utils.py:1
- There are no existing unit tests for
count_tokens. Adding tests that validate its behavior on edge cases (empty strings, long texts, unicode) will help ensure accuracy.
def count_tokens(text: str) -> int:
Comment on lines
105
to
+107
| # Estimate prompt tokens based on message length | ||
| for msg in payload.get("messages", []): | ||
| prompt_tokens += len(msg.get("content", "").split()) | ||
| prompt_tokens += count_tokens(msg.get("content", "")) |
There was a problem hiding this comment.
[nitpick] Invoking count_tokens for each message in a streaming loop may introduce performance overhead. You might batch messages or cache intermediate values to reduce repeated computations.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
count_tokensfrom utils across providerscount_tokensto compute prompt and completion tokens for AI providersTesting
ruff check .pytest -q(fails: command not found)