Skip to content

Commit 1bbd830

Browse files
committed
Refine AI moderator token calculation and config
Improved the token calculation logic in AIModerator to better estimate prompt and message overhead, ensuring more accurate content token allocation. Updated OPENAI_MAX_OUTPUT_TOKENS default in config.py to 500, reflecting the typical size of AI moderator responses.
1 parent 46329bd commit 1bbd830

2 files changed

Lines changed: 7 additions & 6 deletions

File tree

app/services/ai/ai_moderator.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@ def calculate_max_content_tokens(self, custom_prompt=None):
4343
Calculate the maximum tokens available for content based on prompt size.
4444
Dynamically adjusts for custom prompts to prevent exceeding context window.
4545
"""
46-
# Base system prompt size estimation
47-
base_system_tokens = 100 # System message overhead
48-
4946
# Calculate custom prompt tokens if provided
5047
prompt_tokens = 0
5148
if custom_prompt:
@@ -63,9 +60,12 @@ def calculate_max_content_tokens(self, custom_prompt=None):
6360
Does content violate this rule? JSON only:"""
6461
# Count tokens for the prompt parts (excluding content placeholder)
6562
prompt_tokens = self.count_tokens(system_message) + self.count_tokens(user_template)
63+
else:
64+
# For default moderation, estimate prompt overhead
65+
prompt_tokens = 150 # Typical system + user message without content
6666

67-
# Total overhead = base + prompt + output + safety margin
68-
total_overhead = base_system_tokens + prompt_tokens + self.max_output_tokens
67+
# Total overhead = prompt + output tokens + small buffer for message formatting
68+
total_overhead = prompt_tokens + self.max_output_tokens + 50 # 50 for message structure overhead
6969
safety_margin = 0.90 # Use 90% of available capacity
7070

7171
available_for_content = int(

config/config.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ class Config:
2121
OPENAI_CONTEXT_WINDOW = int(os.environ.get(
2222
'OPENAI_CONTEXT_WINDOW', '272000'))
2323
# Upper bound for output tokens; actual requests may use much less
24+
# AI moderator only returns small JSON responses (~100-200 tokens), so 500 is plenty
2425
OPENAI_MAX_OUTPUT_TOKENS = int(os.environ.get(
25-
'OPENAI_MAX_OUTPUT_TOKENS', '128000'))
26+
'OPENAI_MAX_OUTPUT_TOKENS', '500'))
2627
ADMIN_EMAIL = os.environ.get('ADMIN_EMAIL')
2728
ADMIN_PASSWORD = os.environ.get('ADMIN_PASSWORD')
2829

0 commit comments

Comments
 (0)