Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/simple_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ async def chat_completions_stream(request: ChatCompletionRequest):
if request.messages and len(request.messages) > 0:
last_message = request.messages[-1]
if hasattr(last_message, 'content') and last_message.content:
tokens = count_tokens(last_message.content, request.provider == "ollama")
tokens = count_tokens(last_message.content, embedder_type=request.provider)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Since request.provider is defined as a general str without strict casing validation in ChatCompletionRequest, a user could potentially pass provider names with mixed or upper casing (e.g., "Ollama", "Google"). Since count_tokens performs exact string matching (e.g., embedder_type == 'ollama'), passing the raw provider string directly might bypass the correct tokenizer selection. Normalizing the provider string to lowercase using .lower() ensures robust and case-insensitive matching.

Suggested change
tokens = count_tokens(last_message.content, embedder_type=request.provider)
tokens = count_tokens(last_message.content, embedder_type=request.provider.lower())

logger.info(f"Request size: {tokens} tokens")
if tokens > MAX_INPUT_TOKENS:
logger.warning(f"Request exceeds recommended token limit ({tokens} > {MAX_INPUT_TOKENS})")
Expand Down
2 changes: 1 addition & 1 deletion api/websocket_wiki.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ async def handle_websocket_chat(websocket: WebSocket):
if request.messages and len(request.messages) > 0:
last_message = request.messages[-1]
if hasattr(last_message, 'content') and last_message.content:
tokens = count_tokens(last_message.content, request.provider == "ollama")
tokens = count_tokens(last_message.content, embedder_type=request.provider)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Since request.provider is defined as a general str without strict casing validation in ChatCompletionRequest, a user could potentially pass provider names with mixed or upper casing (e.g., "Ollama", "Google"). Since count_tokens performs exact string matching (e.g., embedder_type == 'ollama'), passing the raw provider string directly might bypass the correct tokenizer selection. Normalizing the provider string to lowercase using .lower() ensures robust and case-insensitive matching.

Suggested change
tokens = count_tokens(last_message.content, embedder_type=request.provider)
tokens = count_tokens(last_message.content, embedder_type=request.provider.lower())

logger.info(f"Request size: {tokens} tokens")
if tokens > MAX_INPUT_TOKENS:
logger.warning(f"Request exceeds recommended token limit ({tokens} > {MAX_INPUT_TOKENS})")
Expand Down
Loading