04/06 remediate some issues
This commit is contained in:
+28
-4
@@ -42,22 +42,46 @@ def _call_groq(api_key, history, user_msg):
|
||||
The system prompt is prepended as a system message. Prior history and the
|
||||
new user message are appended in order.
|
||||
|
||||
History is capped at the most recent _MAX_HISTORY_TURNS turns and each
|
||||
message content is truncated to _MAX_MSG_CHARS characters before being
|
||||
forwarded. This prevents a malicious or runaway client from exhausting
|
||||
the model's context window or inflating token costs.
|
||||
|
||||
Raises requests.HTTPError or requests.exceptions.RequestException on failure.
|
||||
"""
|
||||
# ── History sanitisation ──────────────────────────────────────────────────
|
||||
# 1. Strip create_ticket action blocks — re-sending them causes the model
|
||||
# to re-trigger ticket creation on every subsequent turn.
|
||||
# 2. Cap to the most recent N turns so the client cannot inflate context.
|
||||
# 3. Truncate each message's content to avoid per-message token blowout.
|
||||
_MAX_HISTORY_TURNS = 20
|
||||
_MAX_MSG_CHARS = 2000
|
||||
|
||||
model = current_app.config.get('GROQ_MODEL', _GROQ_MODEL)
|
||||
|
||||
# Strip any assistant messages containing a create_ticket action block.
|
||||
# These should never reach the model — if they do, the model re-triggers
|
||||
# ticket creation on every subsequent turn.
|
||||
clean_history = [
|
||||
msg for msg in history
|
||||
if not (msg.get('role') == 'assistant' and '"action": "create_ticket"' in msg.get('content', ''))
|
||||
]
|
||||
|
||||
# Keep only the most recent turns after filtering
|
||||
if len(clean_history) > _MAX_HISTORY_TURNS:
|
||||
logger.warning(
|
||||
f'[CHATBOT] history truncated from {len(clean_history)} to '
|
||||
f'{_MAX_HISTORY_TURNS} turns for user_id={current_user.id}'
|
||||
)
|
||||
clean_history = clean_history[-_MAX_HISTORY_TURNS:]
|
||||
|
||||
# Truncate individual message content lengths
|
||||
clean_history = [
|
||||
{**msg, 'content': msg.get('content', '')[:_MAX_MSG_CHARS]}
|
||||
for msg in clean_history
|
||||
]
|
||||
|
||||
messages = (
|
||||
[{'role': 'system', 'content': _SYSTEM_PROMPT}]
|
||||
+ clean_history
|
||||
+ [{'role': 'user', 'content': user_msg}]
|
||||
+ [{'role': 'user', 'content': user_msg[:_MAX_MSG_CHARS]}]
|
||||
)
|
||||
resp = requests.post(
|
||||
_GROQ_API_URL,
|
||||
|
||||
Reference in New Issue
Block a user